From f04211b1722e27037b305b58479157b5a492f30a Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Fri, 27 Feb 2015 00:40:37 +0000 Subject: [PATCH] Improve test coverage --- app/helpers/geocoder_helper.rb | 1 + app/models/changeset.rb | 4 -- app/models/old_relation.rb | 5 -- app/models/old_way.rb | 5 -- app/models/relation.rb | 5 -- app/models/way.rb | 15 ------ lib/geo_record.rb | 6 --- test/controllers/changeset_controller_test.rb | 12 +++++ test/controllers/message_controller_test.rb | 4 +- test/helpers/browse_helper_test.rb | 6 +++ test/helpers/geocoder_helper_test.rb | 23 +++++++++ test/helpers/user_roles_helper_test.rb | 51 +++++++++++++++++++ test/lib/bounding_box_test.rb | 6 ++- test/lib/rich_text_test.rb | 30 +++++++++++ test/models/language_test.rb | 21 +++++++- test/models/user_test.rb | 24 +++++++++ 16 files changed, 174 insertions(+), 44 deletions(-) create mode 100644 test/helpers/geocoder_helper_test.rb create mode 100644 test/helpers/user_roles_helper_test.rb diff --git a/app/helpers/geocoder_helper.rb b/app/helpers/geocoder_helper.rb index d8dc8c558..e135917a9 100644 --- a/app/helpers/geocoder_helper.rb +++ b/app/helpers/geocoder_helper.rb @@ -18,6 +18,7 @@ module GeocoderHelper html << result[:prefix] if result[:prefix] html << " " if result[:prefix] && result[:name] html << link_to(result[:name], url, html_options) if result[:name] + html << " " if result[:suffix] && result[:name] html << result[:suffix] if result[:suffix] html.html_safe end diff --git a/app/models/changeset.rb b/app/models/changeset.rb index cede7daa1..bf939e425 100644 --- a/app/models/changeset.rb +++ b/app/models/changeset.rb @@ -120,10 +120,6 @@ class Changeset < ActiveRecord::Base self.num_changes += elements end - def tags_as_hash - tags - end - def tags unless @tags @tags = {} diff --git a/app/models/old_relation.rb b/app/models/old_relation.rb index af49ddefb..0e85122a4 100644 --- a/app/models/old_relation.rb +++ b/app/models/old_relation.rb @@ -93,11 +93,6 @@ class OldRelation < ActiveRecord::Base el end - # Temporary method to match interface to nodes - def tags_as_hash - tags - end - # Temporary method to match interface to relations def relation_members old_members diff --git a/app/models/old_way.rb b/app/models/old_way.rb index c86590ed9..63c4d8b65 100644 --- a/app/models/old_way.rb +++ b/app/models/old_way.rb @@ -116,11 +116,6 @@ class OldWay < ActiveRecord::Base points end - # Temporary method to match interface to nodes - def tags_as_hash - tags - end - # Temporary method to match interface to ways def way_nodes old_nodes diff --git a/app/models/relation.rb b/app/models/relation.rb index d39fd2796..9e9e7144f 100644 --- a/app/models/relation.rb +++ b/app/models/relation.rb @@ -262,11 +262,6 @@ class Relation < ActiveRecord::Base true end - # Temporary method to match interface to nodes - def tags_as_hash - tags - end - ## # if any members are referenced by placeholder IDs (i.e: negative) then # this calling this method will fix them using the map from placeholders diff --git a/app/models/way.rb b/app/models/way.rb index f0b35a88a..34e568e4a 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -83,16 +83,6 @@ class Way < ActiveRecord::Base way end - # Find a way given it's ID, and in a single SQL call also grab its nodes - # - - # You can't pull in all the tags too unless we put a sequence_id on the way_tags table and have a multipart key - def self.find_eager(id) - Way.find(id, :include => { :way_nodes => :node }) - # If waytag had a multipart key that was real, you could do this: - # Way.find(id, :include => [:way_tags, {:way_nodes => :node}]) - end - # Find a way given it's ID, and in a single SQL call also grab its nodes and tags def to_xml doc = OSM::API.new.get_xml_doc @@ -242,11 +232,6 @@ class Way < ActiveRecord::Base end end - # Temporary method to match interface to nodes - def tags_as_hash - tags - end - ## # if any referenced nodes are placeholder IDs (i.e: are negative) then # this calling this method will fix them using the map from placeholders diff --git a/lib/geo_record.rb b/lib/geo_record.rb index 61185a314..09ced9729 100644 --- a/lib/geo_record.rb +++ b/lib/geo_record.rb @@ -38,10 +38,4 @@ module GeoRecord def lon longitude.to_f / SCALE end - - private - - def lat2y(a) - 180 / Math::PI * Math.log(Math.tan(Math::PI / 4 + a * (Math::PI / 180) / 2)) - end end diff --git a/test/controllers/changeset_controller_test.rb b/test/controllers/changeset_controller_test.rb index 8eb933744..7d751a5c7 100644 --- a/test/controllers/changeset_controller_test.rb +++ b/test/controllers/changeset_controller_test.rb @@ -180,6 +180,7 @@ class ChangesetControllerTest < ActionController::TestCase # document structure. def test_read changeset_id = changesets(:normal_user_first_change).id + get :read, :id => changeset_id assert_response :success, "cannot get first changeset" @@ -193,6 +194,17 @@ class ChangesetControllerTest < ActionController::TestCase assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1 assert_select "osm>changeset[id='#{changeset_id}']", 1 assert_select "osm>changeset>discussion", 1 + assert_select "osm>changeset>discussion>comment", 0 + + changeset_id = changesets(:normal_user_closed_change).id + + get :read, :id => changeset_id, :include_discussion => true + assert_response :success, "cannot get closed changeset with comments" + + assert_select "osm[version='#{API_VERSION}'][generator='OpenStreetMap server']", 1 + assert_select "osm>changeset[id='#{changeset_id}']", 1 + assert_select "osm>changeset>discussion", 1 + assert_select "osm>changeset>discussion>comment", 3 end ## diff --git a/test/controllers/message_controller_test.rb b/test/controllers/message_controller_test.rb index eb4222bd5..427519c32 100644 --- a/test/controllers/message_controller_test.rb +++ b/test/controllers/message_controller_test.rb @@ -366,8 +366,8 @@ class MessageControllerTest < ActionController::TestCase assert_equal false, m.to_user_visible # Check that the deleting a sent message works - post :delete, :message_id => messages(:unread_message).id - assert_redirected_to inbox_path(:display_name => users(:normal_user).display_name) + post :delete, :message_id => messages(:unread_message).id, :referer => outbox_path(:display_name => users(:normal_user).display_name) + assert_redirected_to outbox_path(:display_name => users(:normal_user).display_name) assert_equal "Message deleted", flash[:notice] m = Message.find(messages(:unread_message).id) assert_equal false, m.from_user_visible diff --git a/test/helpers/browse_helper_test.rb b/test/helpers/browse_helper_test.rb index f89561781..e72f370d0 100644 --- a/test/helpers/browse_helper_test.rb +++ b/test/helpers/browse_helper_test.rb @@ -90,6 +90,12 @@ class BrowseHelperTest < ActionView::TestCase html = format_value("phone", "+1234567890") assert_dom_equal "+1234567890", html + + html = format_value("wikipedia", "Test") + assert_dom_equal "Test", html + + html = format_value("wikidata", "Q42") + assert_dom_equal "Q42", html end def test_icon_tags diff --git a/test/helpers/geocoder_helper_test.rb b/test/helpers/geocoder_helper_test.rb new file mode 100644 index 000000000..789916bb4 --- /dev/null +++ b/test/helpers/geocoder_helper_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class GeocoderHelperTest < ActionView::TestCase + def test_result_to_html + html = result_to_html(:lat => 1.23, :lon => 4.56, :zoom => 16, :name => "Name") + assert_dom_equal %q(Name), html + + html = result_to_html(:lat => 1.23, :lon => 4.56, :zoom => 16, :prefix => "Prefix", :name => "Name") + assert_dom_equal %q(Prefix Name), html + + html = result_to_html(:lat => 1.23, :lon => 4.56, :zoom => 16, :name => "Name", :suffix => "Suffix") + assert_dom_equal %q(Name Suffix), html + + html = result_to_html(:lat => 1.23, :lon => 4.56, :zoom => 16, :prefix => "Prefix", :name => "Name", :suffix => "Suffix") + assert_dom_equal %q(Prefix Name Suffix), html + + html = result_to_html(:type => "node", :id => 123456, :name => "Name") + assert_dom_equal %q(Name), html + + html = result_to_html(:min_lat => 1.23, :max_lat => 4.56, :min_lon => -1.23, :max_lon => 2.34, :name => "Name") + assert_dom_equal %q(Name), icon + end + + def test_role_icon_administrator + @user = users(:administrator_user) + + icon = role_icon(users(:normal_user), "moderator") + assert_dom_equal %q(Grant moderator access), icon + + icon = role_icon(users(:moderator_user), "moderator") + assert_dom_equal %q(Revoke moderator access), icon + end + + def test_role_icons_normal + @user = users(:normal_user) + + icons = role_icons(users(:normal_user)) + assert_dom_equal " ", icons + + icons = role_icons(users(:moderator_user)) + assert_dom_equal %q( This user is a moderator), icons + + icons = role_icons(users(:super_user)) + assert_dom_equal %q( This user is an administrator This user is a moderator), icons + end + + def test_role_icons_administrator + @user = users(:administrator_user) + + icons = role_icons(users(:normal_user)) + assert_dom_equal %q( Grant administrator access Grant moderator access), icons + + icons = role_icons(users(:moderator_user)) + assert_dom_equal %q( Grant administrator access Revoke moderator access), icons + + icons = role_icons(users(:super_user)) + assert_dom_equal %q( Revoke administrator access Revoke moderator access), icons + end +end diff --git a/test/lib/bounding_box_test.rb b/test/lib/bounding_box_test.rb index 4e962aedf..834d217cb 100644 --- a/test/lib/bounding_box_test.rb +++ b/test/lib/bounding_box_test.rb @@ -209,7 +209,7 @@ class BoundingBoxTest < ActiveSupport::TestCase end end - def test_bbox_area + def test_good_bbox_area @good_bbox.each do |string| bbox = BoundingBox.from_s(string) array = string.split(",") @@ -217,6 +217,10 @@ class BoundingBoxTest < ActiveSupport::TestCase end end + def test_nil_bbox_area + assert_equal 0, @bbox_from_nils.area + end + def test_complete assert !@bbox_from_nils.complete?, "should contain a nil" assert @bbox_from_string.complete?, "should not contain a nil" diff --git a/test/lib/rich_text_test.rb b/test/lib/rich_text_test.rb index 9ba0f7dcb..74d396b68 100644 --- a/test/lib/rich_text_test.rb +++ b/test/lib/rich_text_test.rb @@ -49,6 +49,16 @@ class RichTextTest < ActiveSupport::TestCase end end + def test_html_to_text + r = RichText.new("html", "foo bar baz") + assert_equal "foo bar baz", r.to_text + end + + def test_html_spam_score + r = RichText.new("html", "foo bar baz") + assert_equal 55, r.spam_score.round + end + def test_markdown_to_html r = RichText.new("markdown", "foo http://example.com/ bar") assert_html r do @@ -137,6 +147,16 @@ class RichTextTest < ActiveSupport::TestCase end end + def test_markdown_to_text + r = RichText.new("markdown", "foo [bar](http://example.com/) baz") + assert_equal "foo [bar](http://example.com/) baz", r.to_text + end + + def test_markdown_spam_score + r = RichText.new("markdown", "foo [bar](http://example.com/) baz") + assert_equal 50, r.spam_score.round + end + def test_text_to_html r = RichText.new("text", "foo http://example.com/ bar") assert_html r do @@ -156,6 +176,16 @@ class RichTextTest < ActiveSupport::TestCase end end + def test_text_to_text + r = RichText.new("text", "foo http://example.com/ bar") + assert_equal "foo http://example.com/ bar", r.to_text + end + + def test_text_spam_score + r = RichText.new("text", "foo http://example.com/ bar") + assert_equal 141, r.spam_score.round + end + private def assert_html(richtext, &block) diff --git a/test/models/language_test.rb b/test/models/language_test.rb index 39e3f017b..cb4c8c631 100644 --- a/test/models/language_test.rb +++ b/test/models/language_test.rb @@ -1,9 +1,28 @@ +# coding: utf-8 require "test_helper" class LanguageTest < ActiveSupport::TestCase fixtures :languages - test "language count" do + def test_language_count assert_equal 3, Language.count end + + def test_name + assert_equal "English (English)", languages(:en).name + assert_equal "German (Deutsch)", languages(:de).name + assert_equal "Slovenian (slovenščina)", languages(:sl).name + end + + def test_load + assert_equal 3, Language.count + assert_raise ActiveRecord::RecordNotFound do + Language.find("zh") + end + + Language.load("config/languages.yml") + + assert_equal 197, Language.count + assert_not_nil Language.find("zh") + end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 04aa8ace4..adae97a0e 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -2,6 +2,8 @@ require "test_helper" class UserTest < ActiveSupport::TestCase + include Rails::Dom::Testing::Assertions::SelectorAssertions + api_fixtures fixtures :friends, :languages, :user_roles @@ -121,6 +123,8 @@ class UserTest < ActiveSupport::TestCase assert_equal [], users(:inactive_user).nearby # north_pole_user has no user nearby, and doesn't throw exception assert_equal [], users(:north_pole_user).nearby + # confirmed_user has no home location + assert_equal [], users(:confirmed_user).nearby end def test_friends_with @@ -243,4 +247,24 @@ class UserTest < ActiveSupport::TestCase assert_equal false, user.visible? assert_equal false, user.active? end + + def test_to_xml + user = users(:normal_user) + xml = user.to_xml + assert_select Nokogiri::XML::Document.parse(xml.to_s), "user" do + assert_select "[display_name=?]", user.display_name + assert_select "[account_created=?]", user.creation_time.xmlschema + assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s + end + end + + def test_to_xml_node + user = users(:normal_user) + xml = user.to_xml_node + assert_select Nokogiri::XML::DocumentFragment.parse(xml.to_s), "user" do + assert_select "[display_name=?]", user.display_name + assert_select "[account_created=?]", user.creation_time.xmlschema + assert_select "home[lat=?][lon=?][zoom=?]", user.home_lat.to_s, user.home_lon.to_s, user.home_zoom.to_s + end + end end -- 2.43.2