Fix validation failures for trace tags
[rails.git] / test / models / trace_test.rb
1 require "test_helper"
2
3 class TraceTest < ActiveSupport::TestCase
4   api_fixtures
5
6   def setup
7     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
8     Object.const_set("GPX_TRACE_DIR", File.dirname(__FILE__) + "/../traces")
9   end
10
11   def teardown
12     Object.send("remove_const", "GPX_TRACE_DIR")
13     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
14   end
15
16   def test_trace_count
17     assert_equal 5, Trace.count
18   end
19
20   def test_visible
21     check_query(Trace.visible, [:public_trace_file, :anon_trace_file, :trackable_trace_file, :identifiable_trace_file])
22   end
23
24   def test_visible_to
25     check_query(Trace.visible_to(1), [:public_trace_file, :identifiable_trace_file])
26     check_query(Trace.visible_to(2), [:public_trace_file, :anon_trace_file, :trackable_trace_file, :identifiable_trace_file])
27     check_query(Trace.visible_to(3), [:public_trace_file, :identifiable_trace_file])
28   end
29
30   def test_visible_to_all
31     check_query(Trace.visible_to_all, [:public_trace_file, :identifiable_trace_file, :deleted_trace_file])
32   end
33
34   def test_tagged
35     check_query(Trace.tagged("London"), [:public_trace_file, :anon_trace_file])
36     check_query(Trace.tagged("Birmingham"), [:anon_trace_file, :identifiable_trace_file])
37     check_query(Trace.tagged("Unknown"), [])
38   end
39
40   def test_validations
41     trace_valid({})
42     trace_valid({ :user_id => nil }, false)
43     trace_valid(:name => "a" * 255)
44     trace_valid({ :name => "a" * 256 }, false)
45     trace_valid({ :description => nil }, false)
46     trace_valid(:description => "a" * 255)
47     trace_valid({ :description => "a" * 256 }, false)
48     trace_valid(:visibility => "private")
49     trace_valid(:visibility => "public")
50     trace_valid(:visibility => "trackable")
51     trace_valid(:visibility => "identifiable")
52     trace_valid({ :visibility => "foo" }, false)
53   end
54
55   def test_tagstring
56     trace = Trace.new(gpx_files(:public_trace_file).attributes)
57     trace.tagstring = "foo bar baz"
58     assert trace.valid?
59     assert_equal 3, trace.tags.length
60     assert_equal "foo", trace.tags[0].tag
61     assert_equal "bar", trace.tags[1].tag
62     assert_equal "baz", trace.tags[2].tag
63     assert_equal "foo, bar, baz", trace.tagstring
64     trace.tagstring = "foo, bar baz ,qux"
65     assert trace.valid?
66     assert_equal 3, trace.tags.length
67     assert_equal "foo", trace.tags[0].tag
68     assert_equal "bar baz", trace.tags[1].tag
69     assert_equal "qux", trace.tags[2].tag
70     assert_equal "foo, bar baz, qux", trace.tagstring
71   end
72
73   def test_public?
74     assert_equal true, gpx_files(:public_trace_file).public?
75     assert_equal false, gpx_files(:anon_trace_file).public?
76     assert_equal false, gpx_files(:trackable_trace_file).public?
77     assert_equal true, gpx_files(:identifiable_trace_file).public?
78     assert_equal true, gpx_files(:deleted_trace_file).public?
79   end
80
81   def test_trackable?
82     assert_equal false, gpx_files(:public_trace_file).trackable?
83     assert_equal false, gpx_files(:anon_trace_file).trackable?
84     assert_equal true, gpx_files(:trackable_trace_file).trackable?
85     assert_equal true, gpx_files(:identifiable_trace_file).trackable?
86     assert_equal false, gpx_files(:deleted_trace_file).trackable?
87   end
88
89   def test_identifiable?
90     assert_equal false, gpx_files(:public_trace_file).identifiable?
91     assert_equal false, gpx_files(:anon_trace_file).identifiable?
92     assert_equal false, gpx_files(:trackable_trace_file).identifiable?
93     assert_equal true, gpx_files(:identifiable_trace_file).identifiable?
94     assert_equal false, gpx_files(:deleted_trace_file).identifiable?
95   end
96
97   def test_mime_type
98     assert_equal "application/gpx+xml", gpx_files(:public_trace_file).mime_type
99     assert_equal "application/gpx+xml", gpx_files(:anon_trace_file).mime_type
100     assert_equal "application/x-bzip2", gpx_files(:trackable_trace_file).mime_type
101     assert_equal "application/x-gzip", gpx_files(:identifiable_trace_file).mime_type
102   end
103
104   def test_extension_name
105     assert_equal ".gpx", gpx_files(:public_trace_file).extension_name
106     assert_equal ".gpx", gpx_files(:anon_trace_file).extension_name
107     assert_equal ".gpx.bz2", gpx_files(:trackable_trace_file).extension_name
108     assert_equal ".gpx.gz", gpx_files(:identifiable_trace_file).extension_name
109   end
110
111   private
112
113   def check_query(query, traces)
114     traces = traces.map { |t| gpx_files(t) }.sort
115     assert_equal traces, query.order(:id)
116   end
117
118   def trace_valid(attrs, result = true)
119     entry = Trace.new(gpx_files(:public_trace_file).attributes)
120     entry.assign_attributes(attrs)
121     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
122   end
123 end