]> git.openstreetmap.org Git - rails.git/commitdiff
Replace fixtures with factory for way_tags
authorAndy Allan <git@gravitystorm.co.uk>
Mon, 31 Oct 2016 10:49:51 +0000 (11:49 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Mon, 31 Oct 2016 10:49:51 +0000 (11:49 +0100)
test/controllers/search_controller_test.rb
test/controllers/way_controller_test.rb
test/factories/way_tags.rb [new file with mode: 0644]
test/fixtures/current_way_tags.yml [deleted file]
test/models/way_tag_test.rb
test/models/way_test.rb
test/test_helper.rb

index d3300452b05d15f3eeab17a13728b171dc3ace40..a3b862419a4b8864c6bb5b19d6af14aab117b866 100644 (file)
@@ -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"]
index ccf29945579ab1571551ab94aa7362151d5dd5be..650977078a1c31c1899e453ebd63e79bb1fcdaf1 100644 (file)
@@ -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 (file)
index 0000000..3bc3192
--- /dev/null
@@ -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 (file)
index 2566c54..0000000
+++ /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'
index 46be06c9417c03b887de63b4fd12c74459223a4d..cde5bd4158d283ddee959c2024133a6fbf01c221 100644 (file)
@@ -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! }
index 6c8b0f80b52eb60e1196b456417017981a9cac37..654076eecb0ebcd09c0792eb399b8a04e1d22c53 100644 (file)
@@ -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
index f8fb69916829ed780ab9504a0d4d63ecd832380f..8dd61448a9ae2d34f4a617a7ba8c1d06bca60409 100644 (file)
@@ -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