From aa185bf4f88e12bad895abaabc9948514e9f1ec5 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Mon, 31 Oct 2016 12:30:57 +0100 Subject: [PATCH] Replace fixtures with factory for relation_tags --- test/controllers/amf_controller_test.rb | 3 ++ test/controllers/relation_controller_test.rb | 2 + test/controllers/search_controller_test.rb | 5 ++ test/factories/relation_tags.rb | 9 ++++ test/fixtures/current_relation_tags.yml | 49 ------------------- test/models/relation_tag_test.rb | 51 ++++++-------------- test/models/relation_test.rb | 17 ++++--- test/test_helper.rb | 3 +- 8 files changed, 47 insertions(+), 92 deletions(-) create mode 100644 test/factories/relation_tags.rb delete mode 100644 test/fixtures/current_relation_tags.yml diff --git a/test/controllers/amf_controller_test.rb b/test/controllers/amf_controller_test.rb index 7c8c64ba0..fde8883b0 100644 --- a/test/controllers/amf_controller_test.rb +++ b/test/controllers/amf_controller_test.rb @@ -531,7 +531,10 @@ class AmfControllerTest < ActionController::TestCase def test_findrelations_by_tags visible_relation = current_relations(:visible_relation) + create(:relation_tag, :relation => visible_relation, :k => "test", :v => "yes") used_relation = current_relations(:used_relation) + create(:relation_tag, :relation => used_relation, :k => "test", :v => "yes") + create(:relation_tag, :relation => used_relation, :k => "name", :v => "Test Relation") amf_content "findrelations", "/1", ["yes"] post :amf_read diff --git a/test/controllers/relation_controller_test.rb b/test/controllers/relation_controller_test.rb index ab0aca45e..0366ceef0 100644 --- a/test/controllers/relation_controller_test.rb +++ b/test/controllers/relation_controller_test.rb @@ -337,6 +337,7 @@ class RelationControllerTest < ActionController::TestCase def test_update_relation_tags basic_authorization "test@example.com", "test" rel_id = current_relations(:multi_tag_relation).id + create_list(:relation_tag, 4, :relation => current_relations(:multi_tag_relation)) cs_id = changesets(:public_user_first_change).id with_relation(rel_id) do |rel| @@ -366,6 +367,7 @@ class RelationControllerTest < ActionController::TestCase def test_update_relation_tags_via_upload basic_authorization users(:public_user).email, "test" rel_id = current_relations(:multi_tag_relation).id + create_list(:relation_tag, 4, :relation => current_relations(:multi_tag_relation)) cs_id = changesets(:public_user_first_change).id with_relation(rel_id) do |rel| diff --git a/test/controllers/search_controller_test.rb b/test/controllers/search_controller_test.rb index a3b862419..2e835a587 100644 --- a/test/controllers/search_controller_test.rb +++ b/test/controllers/search_controller_test.rb @@ -64,6 +64,11 @@ class SearchControllerTest < ActionController::TestCase ## # test searching relations def test_search_relations + [:visible_relation, :invisible_relation, :used_relation].each do |relation| + create(:relation_tag, :relation => current_relations(relation), :k => "test", :v => "yes") + end + create(:relation_tag, :relation => current_relations(:used_relation), :k => "name", :v => "Test Relation") + get :search_relations, :type => "test" assert_response :service_unavailable assert_equal "Searching for a key without value is currently unavailable", response.headers["Error"] diff --git a/test/factories/relation_tags.rb b/test/factories/relation_tags.rb new file mode 100644 index 000000000..00ad4dbdc --- /dev/null +++ b/test/factories/relation_tags.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :relation_tag do + sequence(:k) { |n| "Key #{n}" } + sequence(:v) { |n| "Value #{n}" } + + # Fixme requires relation factory + relation_id 1 + end +end diff --git a/test/fixtures/current_relation_tags.yml b/test/fixtures/current_relation_tags.yml deleted file mode 100644 index 505a76130..000000000 --- a/test/fixtures/current_relation_tags.yml +++ /dev/null @@ -1,49 +0,0 @@ -t1: - relation_id: 1 - k: 'test' - v: 'yes' - -t2: - relation_id: 2 - k: 'test' - v: 'yes' - -t3: - relation_id: 3 - k: 'test' - v: 'yes' - -t3_2: - relation_id: 3 - k: 'name' - v: 'Test Relation' - -mt_1: - relation_id: 4 - k: 'tag1' - v: 'val1' - -mt_2: - relation_id: 4 - k: 'tag2' - v: 'val2' - -mt_3: - relation_id: 4 - k: 'tag3' - v: 'val3' - -mt_4: - relation_id: 4 - k: 'tag4' - v: 'val4' - -rv_t1: - relation_id: 8 - k: 'testing' - v: 'added in relation version 3' - -rv_t2: - relation_id: 8 - k: 'testing two' - v: 'modified in relation version 4' diff --git a/test/models/relation_tag_test.rb b/test/models/relation_tag_test.rb index ef635cc94..3b4d2cdfc 100644 --- a/test/models/relation_tag_test.rb +++ b/test/models/relation_tag_test.rb @@ -1,54 +1,34 @@ require "test_helper" class RelationTagTest < ActiveSupport::TestCase - api_fixtures - - def test_relation_tag_count - assert_equal 10, RelationTag.count - end - def test_length_key_valid - key = "k" + tag = create(:relation_tag) (0..255).each do |i| - tag = RelationTag.new - tag.relation_id = 1 - tag.k = key * i - tag.v = "v" + tag.k = "k" * i assert tag.valid? end end def test_length_value_valid - val = "v" + tag = create(:relation_tag) (0..255).each do |i| - tag = RelationTag.new - tag.relation_id = 1 - 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 = RelationTag.new - tag.relation_id = 1 - tag.k = i - tag.v = "v" - assert !tag.valid?, "Key #{i} should be too long" - assert tag.errors[:k].any? - end + tag = create(:relation_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 = RelationTag.new - tag.relation_id = 1 - tag.k = "k" - tag.v = i - assert !tag.valid?, "Value #{i} should be too long" - assert tag.errors[:v].any? - end + tag = create(:relation_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 RelationTagTest < ActiveSupport::TestCase end def test_uniquness + existing = create(:relation_tag) tag = RelationTag.new - tag.relation_id = current_relation_tags(:t1).relation_id - tag.k = current_relation_tags(:t1).k - tag.v = current_relation_tags(:t1).v + tag.relation_id = existing.relation_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/relation_test.rb b/test/models/relation_test.rb index 028cd7d48..cb3330abe 100644 --- a/test/models/relation_test.rb +++ b/test/models/relation_test.rb @@ -129,20 +129,25 @@ class RelationTest < ActiveSupport::TestCase def test_relation_tags relation = current_relations(:relation_with_versions) + taglist = create_list(:relation_tag, 2, :relation => relation) + tags = Relation.find(relation.id).relation_tags.order(:k) assert_equal 2, tags.count - assert_equal "testing", tags[0].k - assert_equal "added in relation version 3", tags[0].v - assert_equal "testing two", tags[1].k - assert_equal "modified in relation 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 relation = current_relations(:relation_with_versions) + taglist = create_list(:relation_tag, 2, :relation => relation) + tags = Relation.find(relation.id).tags assert_equal 2, tags.size - assert_equal "added in relation version 3", tags["testing"] - assert_equal "modified in relation 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 8dd61448a..e3d1d7a86 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -35,9 +35,8 @@ module ActiveSupport fixtures :current_relations set_fixture_class :current_relations => Relation - fixtures :current_relation_members, :current_relation_tags + fixtures :current_relation_members set_fixture_class :current_relation_members => RelationMember - set_fixture_class :current_relation_tags => RelationTag fixtures :relations set_fixture_class :relations => OldRelation -- 2.39.5