From 97d63db36993434d7175f0f7445e5b0b250d847f Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Mon, 31 Oct 2016 11:49:51 +0100 Subject: [PATCH] Replace fixtures with factory for way_tags --- test/controllers/search_controller_test.rb | 5 +++ test/controllers/way_controller_test.rb | 12 ++--- test/factories/way_tags.rb | 9 ++++ test/fixtures/current_way_tags.yml | 29 ------------ test/models/way_tag_test.rb | 51 +++++++--------------- test/models/way_test.rb | 15 ++++--- test/test_helper.rb | 3 +- 7 files changed, 47 insertions(+), 77 deletions(-) create mode 100644 test/factories/way_tags.rb delete mode 100644 test/fixtures/current_way_tags.yml diff --git a/test/controllers/search_controller_test.rb b/test/controllers/search_controller_test.rb index d3300452b..a3b862419 100644 --- a/test/controllers/search_controller_test.rb +++ b/test/controllers/search_controller_test.rb @@ -43,6 +43,11 @@ class SearchControllerTest < ActionController::TestCase ## # test searching ways def test_search_ways + [:visible_way, :invisible_way, :used_way].each do |way| + create(:way_tag, :way => current_ways(way), :k => "test", :v => "yes") + end + create(:way_tag, :way => current_ways(:used_way), :k => "name", :v => "Test Way") + get :search_ways, :type => "test" assert_response :service_unavailable assert_equal "Searching for a key without value is currently unavailable", response.headers["Error"] diff --git a/test/controllers/way_controller_test.rb b/test/controllers/way_controller_test.rb index ccf299455..650977078 100644 --- a/test/controllers/way_controller_test.rb +++ b/test/controllers/way_controller_test.rb @@ -538,10 +538,12 @@ class WayControllerTest < ActionController::TestCase # setup auth basic_authorization(users(:normal_user).email, "test") + existing = create(:way_tag, :way => current_ways(:visible_way)) + # add an identical tag to the way tag_xml = XML::Node.new("tag") - tag_xml["k"] = current_way_tags(:t1).k - tag_xml["v"] = current_way_tags(:t1).v + tag_xml["k"] = existing.k + tag_xml["v"] = existing.v # add the tag into the existing xml way_xml = current_ways(:visible_way).to_xml @@ -559,8 +561,8 @@ class WayControllerTest < ActionController::TestCase # add an identical tag to the way tag_xml = XML::Node.new("tag") - tag_xml["k"] = current_way_tags(:t1).k - tag_xml["v"] = current_way_tags(:t1).v + tag_xml["k"] = existing.k + tag_xml["v"] = existing.v # add the tag into the existing xml way_xml = current_ways(:visible_way).to_xml @@ -571,7 +573,7 @@ class WayControllerTest < ActionController::TestCase put :update, :id => current_ways(:visible_way).id assert_response :bad_request, "adding a duplicate tag to a way should fail with 'bad request'" - assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{current_way_tags(:t1).k}", @response.body + assert_equal "Element way/#{current_ways(:visible_way).id} has duplicate tags with key #{existing.k}", @response.body end ## diff --git a/test/factories/way_tags.rb b/test/factories/way_tags.rb new file mode 100644 index 000000000..3bc3192f5 --- /dev/null +++ b/test/factories/way_tags.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :way_tag do + sequence(:k) { |n| "Key #{n}" } + sequence(:v) { |n| "Value #{n}" } + + # Fixme requires way factory + way_id 1 + end +end diff --git a/test/fixtures/current_way_tags.yml b/test/fixtures/current_way_tags.yml deleted file mode 100644 index 2566c5484..000000000 --- a/test/fixtures/current_way_tags.yml +++ /dev/null @@ -1,29 +0,0 @@ -t1: - way_id: 1 - k: 'test' - v: 'yes' - -t2: - way_id: 2 - k: 'test' - v: 'yes' - -t3: - way_id: 3 - k: 'test' - v: 'yes' - -t3_t2: - way_id: 3 - k: 'name' - v: 'Test Way' - -wv_t1: - way_id: 4 - k: 'testing' - v: 'added in way version 3' - -wv_t2: - way_id: 4 - k: 'testing two' - v: 'modified in way version 4' diff --git a/test/models/way_tag_test.rb b/test/models/way_tag_test.rb index 46be06c94..cde5bd415 100644 --- a/test/models/way_tag_test.rb +++ b/test/models/way_tag_test.rb @@ -1,54 +1,34 @@ require "test_helper" class WayTagTest < ActiveSupport::TestCase - api_fixtures - - def test_way_tag_count - assert_equal 6, WayTag.count - end - def test_length_key_valid - key = "k" + tag = create(:way_tag) (0..255).each do |i| - tag = WayTag.new - tag.way_id = current_way_tags(:t1).way_id - tag.k = key * i - tag.v = current_way_tags(:t1).v + tag.k = "k" * i assert tag.valid? end end def test_length_value_valid - val = "v" + tag = create(:way_tag) (0..255).each do |i| - tag = WayTag.new - tag.way_id = current_way_tags(:t1).way_id - tag.k = "k" - tag.v = val * i + tag.v = "v" * i assert tag.valid? end end def test_length_key_invalid - ["k" * 256].each do |i| - tag = WayTag.new - tag.way_id = current_way_tags(:t1).way_id - tag.k = i - tag.v = "v" - assert !tag.valid?, "Key #{i} should be too long" - assert tag.errors[:k].any? - end + tag = create(:way_tag) + tag.k = "k" * 256 + assert !tag.valid?, "Key should be too long" + assert tag.errors[:k].any? end def test_length_value_invalid - ["v" * 256].each do |i| - tag = WayTag.new - tag.way_id = current_way_tags(:t1).way_id - tag.k = "k" - tag.v = i - assert !tag.valid?, "Value #{i} should be too long" - assert tag.errors[:v].any? - end + tag = create(:way_tag) + tag.v = "v" * 256 + assert !tag.valid?, "Value should be too long" + assert tag.errors[:v].any? end def test_empty_tag_invalid @@ -58,10 +38,11 @@ class WayTagTest < ActiveSupport::TestCase end def test_uniqueness + existing = create(:way_tag) tag = WayTag.new - tag.way_id = current_way_tags(:t1).way_id - tag.k = current_way_tags(:t1).k - tag.v = current_way_tags(:t1).v + tag.way_id = existing.way_id + tag.k = existing.k + tag.v = existing.v assert tag.new_record? assert !tag.valid? assert_raise(ActiveRecord::RecordInvalid) { tag.save! } diff --git a/test/models/way_test.rb b/test/models/way_test.rb index 6c8b0f80b..654076eec 100644 --- a/test/models/way_test.rb +++ b/test/models/way_test.rb @@ -165,20 +165,23 @@ class WayTest < ActiveSupport::TestCase def test_way_tags way = current_ways(:way_with_versions) + taglist = create_list(:way_tag, 2, :way => way) tags = Way.find(way.id).way_tags.order(:k) assert_equal 2, tags.count - assert_equal "testing", tags[0].k - assert_equal "added in way version 3", tags[0].v - assert_equal "testing two", tags[1].k - assert_equal "modified in way version 4", tags[1].v + taglist.sort_by!(&:k).each_index do |i| + assert_equal taglist[i].k, tags[i].k + assert_equal taglist[i].v, tags[i].v + end end def test_tags way = current_ways(:way_with_versions) + taglist = create_list(:way_tag, 2, :way => way) tags = Way.find(way.id).tags assert_equal 2, tags.size - assert_equal "added in way version 3", tags["testing"] - assert_equal "modified in way version 4", tags["testing two"] + taglist.each do |tag| + assert_equal tag.v, tags[tag.k] + end end def test_containing_relation_members diff --git a/test/test_helper.rb b/test/test_helper.rb index f8fb69916..8dd61448a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -23,9 +23,8 @@ module ActiveSupport fixtures :current_ways set_fixture_class :current_ways => Way - fixtures :current_way_nodes, :current_way_tags + fixtures :current_way_nodes set_fixture_class :current_way_nodes => WayNode - set_fixture_class :current_way_tags => WayTag fixtures :ways set_fixture_class :ways => OldWay -- 2.39.5