]> git.openstreetmap.org Git - rails.git/blob - test/models/trace_test.rb
Add factories for notes and note_comments
[rails.git] / test / models / trace_test.rb
1 require "test_helper"
2 require "digest"
3
4 class TraceTest < ActiveSupport::TestCase
5   api_fixtures
6
7   def setup
8     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
9     Object.const_set("GPX_TRACE_DIR", File.dirname(__FILE__) + "/../traces")
10
11     @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
12     Object.const_set("GPX_IMAGE_DIR", File.dirname(__FILE__) + "/../traces")
13   end
14
15   def teardown
16     Object.send("remove_const", "GPX_TRACE_DIR")
17     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
18
19     Object.send("remove_const", "GPX_IMAGE_DIR")
20     Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
21   end
22
23   def test_trace_count
24     assert_equal 10, Trace.count
25   end
26
27   def test_visible
28     check_query(Trace.visible, [
29                   :public_trace_file, :anon_trace_file, :trackable_trace_file,
30                   :identifiable_trace_file, :zipped_trace_file, :tar_trace_file,
31                   :tar_gzip_trace_file, :tar_bzip_trace_file, :pending_trace_file
32                 ])
33   end
34
35   def test_visible_to
36     check_query(Trace.visible_to(1), [
37                   :public_trace_file, :identifiable_trace_file, :pending_trace_file
38                 ])
39     check_query(Trace.visible_to(2), [
40                   :public_trace_file, :anon_trace_file, :trackable_trace_file,
41                   :identifiable_trace_file, :pending_trace_file
42                 ])
43     check_query(Trace.visible_to(3), [
44                   :public_trace_file, :identifiable_trace_file, :pending_trace_file
45                 ])
46   end
47
48   def test_visible_to_all
49     check_query(Trace.visible_to_all, [
50                   :public_trace_file, :identifiable_trace_file,
51                   :deleted_trace_file, :pending_trace_file
52                 ])
53   end
54
55   def test_tagged
56     check_query(Trace.tagged("London"), [:public_trace_file, :anon_trace_file])
57     check_query(Trace.tagged("Birmingham"), [:anon_trace_file, :identifiable_trace_file])
58     check_query(Trace.tagged("Unknown"), [])
59   end
60
61   def test_validations
62     trace_valid({})
63     trace_valid({ :user_id => nil }, false)
64     trace_valid(:name => "a" * 255)
65     trace_valid({ :name => "a" * 256 }, false)
66     trace_valid({ :description => nil }, false)
67     trace_valid(:description => "a" * 255)
68     trace_valid({ :description => "a" * 256 }, false)
69     trace_valid(:visibility => "private")
70     trace_valid(:visibility => "public")
71     trace_valid(:visibility => "trackable")
72     trace_valid(:visibility => "identifiable")
73     trace_valid({ :visibility => "foo" }, false)
74   end
75
76   def test_tagstring
77     trace = Trace.new(gpx_files(:public_trace_file).attributes)
78     trace.tagstring = "foo bar baz"
79     assert trace.valid?
80     assert_equal 3, trace.tags.length
81     assert_equal "foo", trace.tags[0].tag
82     assert_equal "bar", trace.tags[1].tag
83     assert_equal "baz", trace.tags[2].tag
84     assert_equal "foo, bar, baz", trace.tagstring
85     trace.tagstring = "foo, bar baz ,qux"
86     assert trace.valid?
87     assert_equal 3, trace.tags.length
88     assert_equal "foo", trace.tags[0].tag
89     assert_equal "bar baz", trace.tags[1].tag
90     assert_equal "qux", trace.tags[2].tag
91     assert_equal "foo, bar baz, qux", trace.tagstring
92   end
93
94   def test_public?
95     assert_equal true, gpx_files(:public_trace_file).public?
96     assert_equal false, gpx_files(:anon_trace_file).public?
97     assert_equal false, gpx_files(:trackable_trace_file).public?
98     assert_equal true, gpx_files(:identifiable_trace_file).public?
99     assert_equal true, gpx_files(:deleted_trace_file).public?
100   end
101
102   def test_trackable?
103     assert_equal false, gpx_files(:public_trace_file).trackable?
104     assert_equal false, gpx_files(:anon_trace_file).trackable?
105     assert_equal true, gpx_files(:trackable_trace_file).trackable?
106     assert_equal true, gpx_files(:identifiable_trace_file).trackable?
107     assert_equal false, gpx_files(:deleted_trace_file).trackable?
108   end
109
110   def test_identifiable?
111     assert_equal false, gpx_files(:public_trace_file).identifiable?
112     assert_equal false, gpx_files(:anon_trace_file).identifiable?
113     assert_equal false, gpx_files(:trackable_trace_file).identifiable?
114     assert_equal true, gpx_files(:identifiable_trace_file).identifiable?
115     assert_equal false, gpx_files(:deleted_trace_file).identifiable?
116   end
117
118   def test_mime_type
119     assert_equal "application/gpx+xml", gpx_files(:public_trace_file).mime_type
120     assert_equal "application/gpx+xml", gpx_files(:anon_trace_file).mime_type
121     assert_equal "application/x-bzip2", gpx_files(:trackable_trace_file).mime_type
122     assert_equal "application/x-gzip", gpx_files(:identifiable_trace_file).mime_type
123     assert_equal "application/x-zip", gpx_files(:zipped_trace_file).mime_type
124     assert_equal "application/x-tar", gpx_files(:tar_trace_file).mime_type
125     assert_equal "application/x-gzip", gpx_files(:tar_gzip_trace_file).mime_type
126     assert_equal "application/x-bzip2", gpx_files(:tar_bzip_trace_file).mime_type
127   end
128
129   def test_extension_name
130     assert_equal ".gpx", gpx_files(:public_trace_file).extension_name
131     assert_equal ".gpx", gpx_files(:anon_trace_file).extension_name
132     assert_equal ".gpx.bz2", gpx_files(:trackable_trace_file).extension_name
133     assert_equal ".gpx.gz", gpx_files(:identifiable_trace_file).extension_name
134     assert_equal ".zip", gpx_files(:zipped_trace_file).extension_name
135     assert_equal ".tar", gpx_files(:tar_trace_file).extension_name
136     assert_equal ".tar.gz", gpx_files(:tar_gzip_trace_file).extension_name
137     assert_equal ".tar.bz2", gpx_files(:tar_bzip_trace_file).extension_name
138   end
139
140   def test_xml_file
141     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:public_trace_file).xml_file)
142     assert_equal "66179ca44f1e93d8df62e2b88cbea732", md5sum(gpx_files(:anon_trace_file).xml_file)
143     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:trackable_trace_file).xml_file)
144     assert_equal "abd6675fdf3024a84fc0a1deac147c0d", md5sum(gpx_files(:identifiable_trace_file).xml_file)
145     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:zipped_trace_file).xml_file)
146     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:tar_trace_file).xml_file)
147     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:tar_gzip_trace_file).xml_file)
148     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:tar_bzip_trace_file).xml_file)
149   end
150
151   def test_large_picture
152     picture = gpx_files(:public_trace_file).large_picture
153     trace = Trace.create
154
155     trace.large_picture = picture
156     assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
157     assert_equal picture, trace.large_picture
158
159     trace.destroy
160   end
161
162   def test_icon_picture
163     picture = gpx_files(:public_trace_file).icon_picture
164     trace = Trace.create
165
166     trace.icon_picture = picture
167     assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
168     assert_equal picture, trace.icon_picture
169
170     trace.destroy
171   end
172
173   private
174
175   def check_query(query, traces)
176     traces = traces.map { |t| gpx_files(t).id }.sort
177     assert_equal traces, query.order(:id).ids
178   end
179
180   def trace_valid(attrs, result = true)
181     entry = Trace.new(gpx_files(:public_trace_file).attributes)
182     entry.assign_attributes(attrs)
183     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
184   end
185
186   def md5sum(io)
187     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
188   end
189 end