Replace fixtures with factory for way_tags

This commit is contained in:
Andy Allan 2016-10-31 11:49:51 +01:00
parent 3026af170a
commit 97d63db369
7 changed files with 47 additions and 77 deletions

View file

@ -43,6 +43,11 @@ class SearchControllerTest < ActionController::TestCase
## ##
# test searching ways # test searching ways
def test_search_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" get :search_ways, :type => "test"
assert_response :service_unavailable assert_response :service_unavailable
assert_equal "Searching for a key without value is currently unavailable", response.headers["Error"] assert_equal "Searching for a key without value is currently unavailable", response.headers["Error"]

View file

@ -538,10 +538,12 @@ class WayControllerTest < ActionController::TestCase
# setup auth # setup auth
basic_authorization(users(:normal_user).email, "test") basic_authorization(users(:normal_user).email, "test")
existing = create(:way_tag, :way => current_ways(:visible_way))
# add an identical tag to the way # add an identical tag to the way
tag_xml = XML::Node.new("tag") tag_xml = XML::Node.new("tag")
tag_xml["k"] = current_way_tags(:t1).k tag_xml["k"] = existing.k
tag_xml["v"] = current_way_tags(:t1).v tag_xml["v"] = existing.v
# add the tag into the existing xml # add the tag into the existing xml
way_xml = current_ways(:visible_way).to_xml way_xml = current_ways(:visible_way).to_xml
@ -559,8 +561,8 @@ class WayControllerTest < ActionController::TestCase
# add an identical tag to the way # add an identical tag to the way
tag_xml = XML::Node.new("tag") tag_xml = XML::Node.new("tag")
tag_xml["k"] = current_way_tags(:t1).k tag_xml["k"] = existing.k
tag_xml["v"] = current_way_tags(:t1).v tag_xml["v"] = existing.v
# add the tag into the existing xml # add the tag into the existing xml
way_xml = current_ways(:visible_way).to_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 put :update, :id => current_ways(:visible_way).id
assert_response :bad_request, assert_response :bad_request,
"adding a duplicate tag to a way should fail with '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 end
## ##

View file

@ -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

View file

@ -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'

View file

@ -1,54 +1,34 @@
require "test_helper" require "test_helper"
class WayTagTest < ActiveSupport::TestCase class WayTagTest < ActiveSupport::TestCase
api_fixtures
def test_way_tag_count
assert_equal 6, WayTag.count
end
def test_length_key_valid def test_length_key_valid
key = "k" tag = create(:way_tag)
(0..255).each do |i| (0..255).each do |i|
tag = WayTag.new tag.k = "k" * i
tag.way_id = current_way_tags(:t1).way_id
tag.k = key * i
tag.v = current_way_tags(:t1).v
assert tag.valid? assert tag.valid?
end end
end end
def test_length_value_valid def test_length_value_valid
val = "v" tag = create(:way_tag)
(0..255).each do |i| (0..255).each do |i|
tag = WayTag.new tag.v = "v" * i
tag.way_id = current_way_tags(:t1).way_id
tag.k = "k"
tag.v = val * i
assert tag.valid? assert tag.valid?
end end
end end
def test_length_key_invalid def test_length_key_invalid
["k" * 256].each do |i| tag = create(:way_tag)
tag = WayTag.new tag.k = "k" * 256
tag.way_id = current_way_tags(:t1).way_id assert !tag.valid?, "Key should be too long"
tag.k = i assert tag.errors[:k].any?
tag.v = "v"
assert !tag.valid?, "Key #{i} should be too long"
assert tag.errors[:k].any?
end
end end
def test_length_value_invalid def test_length_value_invalid
["v" * 256].each do |i| tag = create(:way_tag)
tag = WayTag.new tag.v = "v" * 256
tag.way_id = current_way_tags(:t1).way_id assert !tag.valid?, "Value should be too long"
tag.k = "k" assert tag.errors[:v].any?
tag.v = i
assert !tag.valid?, "Value #{i} should be too long"
assert tag.errors[:v].any?
end
end end
def test_empty_tag_invalid def test_empty_tag_invalid
@ -58,10 +38,11 @@ class WayTagTest < ActiveSupport::TestCase
end end
def test_uniqueness def test_uniqueness
existing = create(:way_tag)
tag = WayTag.new tag = WayTag.new
tag.way_id = current_way_tags(:t1).way_id tag.way_id = existing.way_id
tag.k = current_way_tags(:t1).k tag.k = existing.k
tag.v = current_way_tags(:t1).v tag.v = existing.v
assert tag.new_record? assert tag.new_record?
assert !tag.valid? assert !tag.valid?
assert_raise(ActiveRecord::RecordInvalid) { tag.save! } assert_raise(ActiveRecord::RecordInvalid) { tag.save! }

View file

@ -165,20 +165,23 @@ class WayTest < ActiveSupport::TestCase
def test_way_tags def test_way_tags
way = current_ways(:way_with_versions) way = current_ways(:way_with_versions)
taglist = create_list(:way_tag, 2, :way => way)
tags = Way.find(way.id).way_tags.order(:k) tags = Way.find(way.id).way_tags.order(:k)
assert_equal 2, tags.count assert_equal 2, tags.count
assert_equal "testing", tags[0].k taglist.sort_by!(&:k).each_index do |i|
assert_equal "added in way version 3", tags[0].v assert_equal taglist[i].k, tags[i].k
assert_equal "testing two", tags[1].k assert_equal taglist[i].v, tags[i].v
assert_equal "modified in way version 4", tags[1].v end
end end
def test_tags def test_tags
way = current_ways(:way_with_versions) way = current_ways(:way_with_versions)
taglist = create_list(:way_tag, 2, :way => way)
tags = Way.find(way.id).tags tags = Way.find(way.id).tags
assert_equal 2, tags.size assert_equal 2, tags.size
assert_equal "added in way version 3", tags["testing"] taglist.each do |tag|
assert_equal "modified in way version 4", tags["testing two"] assert_equal tag.v, tags[tag.k]
end
end end
def test_containing_relation_members def test_containing_relation_members

View file

@ -23,9 +23,8 @@ module ActiveSupport
fixtures :current_ways fixtures :current_ways
set_fixture_class :current_ways => Way 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_nodes => WayNode
set_fixture_class :current_way_tags => WayTag
fixtures :ways fixtures :ways
set_fixture_class :ways => OldWay set_fixture_class :ways => OldWay