]> git.openstreetmap.org Git - rails.git/blob - test/unit/trace_test.rb
Add some more unit tests for the Trace model
[rails.git] / test / unit / trace_test.rb
1 require File.dirname(__FILE__) + '/../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_public
31     check_query(Trace.public, [: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
57     trace.tagstring = "foo bar baz"
58     assert_equal 3, trace.tags.length
59     assert_equal "foo", trace.tags[0].tag
60     assert_equal "bar", trace.tags[1].tag
61     assert_equal "baz", trace.tags[2].tag
62     assert_equal "foo, bar, baz", trace.tagstring
63     trace.tagstring = "foo, bar baz ,qux"
64     assert_equal 3, trace.tags.length
65     assert_equal "foo", trace.tags[0].tag
66     assert_equal "bar baz", trace.tags[1].tag
67     assert_equal "qux", trace.tags[2].tag
68     assert_equal "foo, bar baz, qux", trace.tagstring
69   end
70
71   def test_public?
72     assert_equal true, gpx_files(:public_trace_file).public?
73     assert_equal false, gpx_files(:anon_trace_file).public?
74     assert_equal false, gpx_files(:trackable_trace_file).public?
75     assert_equal true, gpx_files(:identifiable_trace_file).public?
76     assert_equal true, gpx_files(:deleted_trace_file).public?
77   end
78
79   def test_trackable?
80     assert_equal false, gpx_files(:public_trace_file).trackable?
81     assert_equal false, gpx_files(:anon_trace_file).trackable?
82     assert_equal true, gpx_files(:trackable_trace_file).trackable?
83     assert_equal true, gpx_files(:identifiable_trace_file).trackable?
84     assert_equal false, gpx_files(:deleted_trace_file).trackable?
85   end
86
87   def test_identifiable?
88     assert_equal false, gpx_files(:public_trace_file).identifiable?
89     assert_equal false, gpx_files(:anon_trace_file).identifiable?
90     assert_equal false, gpx_files(:trackable_trace_file).identifiable?
91     assert_equal true, gpx_files(:identifiable_trace_file).identifiable?
92     assert_equal false, gpx_files(:deleted_trace_file).identifiable?
93   end
94
95   def test_mime_type
96     assert_equal "application/gpx+xml", gpx_files(:public_trace_file).mime_type
97     assert_equal "application/gpx+xml", gpx_files(:anon_trace_file).mime_type
98     assert_equal "application/x-bzip2", gpx_files(:trackable_trace_file).mime_type
99     assert_equal "application/x-gzip", gpx_files(:identifiable_trace_file).mime_type
100   end
101
102   def test_extension_name
103     assert_equal ".gpx", gpx_files(:public_trace_file).extension_name
104     assert_equal ".gpx", gpx_files(:anon_trace_file).extension_name
105     assert_equal ".gpx.bz2", gpx_files(:trackable_trace_file).extension_name
106     assert_equal ".gpx.gz", gpx_files(:identifiable_trace_file).extension_name
107   end
108
109 private
110
111   def check_query(query, traces)
112     traces = traces.map { |t| gpx_files(t) }.sort
113     assert_equal traces, query.order(:id)
114   end
115
116   def trace_valid(attrs, result = true)
117     entry = Trace.new(gpx_files(:public_trace_file).attributes)
118     entry.assign_attributes(attrs)
119     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
120   end
121 end