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
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"]

View 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
##

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"
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! }

View 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

View 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