Validations on the way tags, with unit tests
[rails.git] / test / unit / way_tag_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class WayTagTest < Test::Unit::TestCase
4   fixtures :current_way_tags
5   set_fixture_class :current_way_tags => WayTag
6   
7   def test_way_tag_count
8     assert_equal 3, WayTag.count
9   end
10   
11   def test_length_key_valid
12     key = "k"
13     (0..255).each do |i|
14       tag = WayTag.new
15       tag.id = current_way_tags(:t1).id
16       tag.k = key*i
17       tag.v = current_way_tags(:t1).v
18       assert_valid tag
19     end
20   end
21   
22   def test_length_value_valid
23     val = "v"
24     (0..255).each do |i|
25       tag = WayTag.new
26       tag.id = current_way_tags(:t1).id
27       tag.k = "k"
28       tag.v = val*i
29       assert_valid tag
30     end
31   end
32   
33   def test_length_key_invalid
34     ["k"*256].each do |i|
35       tag = WayTag.new
36       tag.id = current_way_tags(:t1).id
37       tag.k = i
38       tag.v = "v"
39       assert !tag.valid?, "Key #{i} should be too long"
40       assert tag.errors.invalid?(:k)
41     end
42   end
43   
44   def test_length_value_invalid
45     ["k"*256].each do |i|
46       tag = WayTag.new
47       tag.id = current_way_tags(:t1).id
48       tag.k = "k"
49       tag.v = i
50       assert !tag.valid?, "Value #{i} should be too long"
51     end
52   end
53   
54   def test_empty_tag_invalid
55     tag = WayTag.new
56     assert !tag.valid?, "Empty way tag should be invalid"
57     assert tag.errors.invalid?(:id)
58   end
59   
60   def test_uniquess
61     tag = WayTag.new
62     tag.id = current_way_tags(:t1).id
63     tag.k = current_way_tags(:t1).k
64     tag.v = current_way_tags(:t1).v
65     assert tag.new_record?
66     assert !tag.valid?
67     assert_raise(ActiveRecord::RecordInvalid) {tag.save!}
68     assert tag.new_record?
69   end
70 end