]> git.openstreetmap.org Git - rails.git/blob - test/models/trace_test.rb
Merge remote-tracking branch 'upstream/pull/2863'
[rails.git] / test / models / trace_test.rb
1 require "test_helper"
2 require "gpx"
3
4 class TraceTest < ActiveSupport::TestCase
5   # Use temporary directories with unique names for each test
6   # This allows the tests to be run in parallel.
7   def setup
8     @gpx_trace_dir_orig = Settings.gpx_trace_dir
9     @gpx_image_dir_orig = Settings.gpx_image_dir
10     Settings.gpx_trace_dir = Dir.mktmpdir("trace", Rails.root.join("test/gpx"))
11     Settings.gpx_image_dir = Dir.mktmpdir("image", Rails.root.join("test/gpx"))
12   end
13
14   def teardown
15     FileUtils.remove_dir(Settings.gpx_trace_dir)
16     FileUtils.remove_dir(Settings.gpx_image_dir)
17     Settings.gpx_trace_dir = @gpx_trace_dir_orig
18     Settings.gpx_image_dir = @gpx_image_dir_orig
19   end
20
21   def test_visible
22     public_trace_file = create(:trace)
23     create(:trace, :deleted)
24     check_query(Trace.visible, [public_trace_file])
25   end
26
27   def test_visible_to
28     first_user = create(:user)
29     second_user = create(:user)
30     third_user = create(:user)
31     fourth_user = create(:user)
32     public_trace_file = create(:trace, :visibility => "public", :user => first_user)
33     anon_trace_file = create(:trace, :visibility => "private", :user => second_user)
34     identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => first_user)
35     pending_trace_file = create(:trace, :visibility => "public", :user => second_user, :inserted => false)
36     trackable_trace_file = create(:trace, :visibility => "trackable", :user => second_user)
37     _other_trace_file = create(:trace, :visibility => "private", :user => third_user)
38
39     check_query(Trace.visible_to(first_user), [
40                   public_trace_file, identifiable_trace_file, pending_trace_file
41                 ])
42     check_query(Trace.visible_to(second_user), [
43                   public_trace_file, anon_trace_file, trackable_trace_file,
44                   identifiable_trace_file, pending_trace_file
45                 ])
46     check_query(Trace.visible_to(fourth_user), [
47                   public_trace_file, identifiable_trace_file, pending_trace_file
48                 ])
49   end
50
51   def test_visible_to_all
52     public_trace_file = create(:trace, :visibility => "public")
53     _private_trace_file = create(:trace, :visibility => "private")
54     identifiable_trace_file = create(:trace, :visibility => "identifiable")
55     _trackable_trace_file = create(:trace, :visibility => "trackable")
56     deleted_trace_file = create(:trace, :deleted, :visibility => "public")
57     pending_trace_file = create(:trace, :visibility => "public", :inserted => false)
58
59     check_query(Trace.visible_to_all, [
60                   public_trace_file, identifiable_trace_file,
61                   deleted_trace_file, pending_trace_file
62                 ])
63   end
64
65   def test_tagged
66     london_trace_file = create(:trace) do |trace|
67       create(:tracetag, :trace => trace, :tag => "London")
68     end
69     birmingham_trace_file = create(:trace) do |trace|
70       create(:tracetag, :trace => trace, :tag => "Birmingham")
71     end
72     check_query(Trace.tagged("London"), [london_trace_file])
73     check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
74     check_query(Trace.tagged("Unknown"), [])
75   end
76
77   def test_validations
78     trace_valid({})
79     trace_valid({ :user_id => nil }, false)
80     trace_valid(:name => "a" * 255)
81     trace_valid({ :name => "a" * 256 }, false)
82     trace_valid({ :description => nil }, false)
83     trace_valid(:description => "a" * 255)
84     trace_valid({ :description => "a" * 256 }, false)
85     trace_valid(:visibility => "private")
86     trace_valid(:visibility => "public")
87     trace_valid(:visibility => "trackable")
88     trace_valid(:visibility => "identifiable")
89     trace_valid({ :visibility => "foo" }, false)
90   end
91
92   def test_tagstring
93     trace = build(:trace)
94     trace.tagstring = "foo bar baz"
95     assert trace.valid?
96     assert_equal 3, trace.tags.length
97     assert_equal "foo", trace.tags[0].tag
98     assert_equal "bar", trace.tags[1].tag
99     assert_equal "baz", trace.tags[2].tag
100     assert_equal "foo, bar, baz", trace.tagstring
101     trace.tagstring = "foo, bar baz ,qux"
102     assert trace.valid?
103     assert_equal 3, trace.tags.length
104     assert_equal "foo", trace.tags[0].tag
105     assert_equal "bar baz", trace.tags[1].tag
106     assert_equal "qux", trace.tags[2].tag
107     assert_equal "foo, bar baz, qux", trace.tagstring
108   end
109
110   def test_public?
111     assert build(:trace, :visibility => "public").public?
112     assert_not build(:trace, :visibility => "private").public?
113     assert_not build(:trace, :visibility => "trackable").public?
114     assert build(:trace, :visibility => "identifiable").public?
115     assert build(:trace, :deleted, :visibility => "public").public?
116   end
117
118   def test_trackable?
119     assert_not build(:trace, :visibility => "public").trackable?
120     assert_not build(:trace, :visibility => "private").trackable?
121     assert build(:trace, :visibility => "trackable").trackable?
122     assert build(:trace, :visibility => "identifiable").trackable?
123     assert_not build(:trace, :deleted, :visibility => "public").trackable?
124   end
125
126   def test_identifiable?
127     assert_not build(:trace, :visibility => "public").identifiable?
128     assert_not build(:trace, :visibility => "private").identifiable?
129     assert_not build(:trace, :visibility => "trackable").identifiable?
130     assert build(:trace, :visibility => "identifiable").identifiable?
131     assert_not build(:trace, :deleted, :visibility => "public").identifiable?
132   end
133
134   def test_mime_type
135     # The ids refer to the .gpx fixtures in test/traces
136     check_mime_type("a", "application/gpx+xml")
137     check_mime_type("b", "application/gpx+xml")
138     check_mime_type("c", "application/x-bzip2")
139     check_mime_type("d", "application/x-gzip")
140     check_mime_type("f", "application/x-zip")
141     check_mime_type("g", "application/x-tar")
142     check_mime_type("h", "application/x-gzip")
143     check_mime_type("i", "application/x-bzip2")
144   end
145
146   def test_extension_name
147     # The ids refer to the .gpx fixtures in test/traces
148     check_extension_name("a", ".gpx")
149     check_extension_name("b", ".gpx")
150     check_extension_name("c", ".gpx.bz2")
151     check_extension_name("d", ".gpx.gz")
152     check_extension_name("f", ".zip")
153     check_extension_name("g", ".tar")
154     check_extension_name("h", ".tar.gz")
155     check_extension_name("i", ".tar.bz2")
156   end
157
158   def test_xml_file
159     check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
160     check_xml_file("b", "db4cb5ed2d7d2b627b3b504296c4f701")
161     check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
162     check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
163     check_xml_file("f", "a7c05d676c77dc14369c21be216a3713")
164     check_xml_file("g", "a7c05d676c77dc14369c21be216a3713")
165     check_xml_file("h", "a7c05d676c77dc14369c21be216a3713")
166     check_xml_file("i", "a7c05d676c77dc14369c21be216a3713")
167   end
168
169   def test_large_picture
170     picture = File.read(Rails.root.join("test/gpx/fixtures/a.gif"), :mode => "rb")
171
172     trace = Trace.create
173     trace.large_picture = picture
174     assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
175     assert_equal picture, trace.large_picture
176
177     trace.destroy
178   end
179
180   def test_icon_picture
181     picture = File.read(Rails.root.join("test/gpx/fixtures/a_icon.gif"), :mode => "rb")
182
183     trace = Trace.create
184     trace.icon_picture = picture
185     assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
186     assert_equal picture, trace.icon_picture
187
188     trace.destroy
189   end
190
191   def test_import_removes_previous_tracepoints
192     trace = create(:trace, :fixture => "a")
193     # Tracepoints don't have a primary key, so we use a specific latitude to
194     # check for successful deletion
195     create(:tracepoint, :latitude => 54321, :trace => trace)
196     assert_equal 1, Tracepoint.where(:latitude => 54321).count
197
198     trace.import
199
200     assert_equal 0, Tracepoint.where(:latitude => 54321).count
201   end
202
203   def test_import_creates_tracepoints
204     trace = create(:trace, :fixture => "a")
205     assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
206
207     trace.import
208
209     trace.reload
210     assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
211
212     # Check that the tile has been set prior to the bulk import
213     # i.e. that the callbacks have been run correctly
214     assert_equal 3221331576, Tracepoint.where(:gpx_id => trace.id).first.tile
215   end
216
217   def test_import_creates_icon
218     trace = create(:trace, :fixture => "a")
219     icon_path = File.join(Settings.gpx_image_dir, "#{trace.id}_icon.gif")
220     FileUtils.rm(icon_path)
221     assert_not File.exist?(icon_path)
222
223     trace.import
224
225     assert_path_exists(icon_path)
226   end
227
228   def test_import_creates_large_picture
229     trace = create(:trace, :fixture => "a")
230     large_picture_path = File.join(Settings.gpx_image_dir, "#{trace.id}.gif")
231     FileUtils.rm(large_picture_path)
232     assert_not File.exist?(large_picture_path)
233
234     trace.import
235
236     assert_path_exists(large_picture_path)
237   end
238
239   def test_import_handles_bz2
240     trace = create(:trace, :fixture => "c")
241
242     trace.import
243
244     assert_equal 1, trace.size
245   end
246
247   def test_import_handles_plain
248     trace = create(:trace, :fixture => "a")
249
250     trace.import
251
252     assert_equal 1, trace.size
253   end
254
255   def test_import_handles_plain_with_bom
256     trace = create(:trace, :fixture => "b")
257
258     trace.import
259
260     assert_equal 1, trace.size
261   end
262
263   def test_import_handles_gz
264     trace = create(:trace, :fixture => "d")
265
266     trace.import
267
268     assert_equal 1, trace.size
269   end
270
271   def test_import_handles_zip
272     trace = create(:trace, :fixture => "f")
273
274     trace.import
275
276     assert_equal 2, trace.size
277   end
278
279   def test_import_handles_tar
280     trace = create(:trace, :fixture => "g")
281
282     trace.import
283
284     assert_equal 2, trace.size
285   end
286
287   def test_import_handles_tar_gz
288     trace = create(:trace, :fixture => "h")
289
290     trace.import
291
292     assert_equal 2, trace.size
293   end
294
295   def test_import_handles_tar_bz2
296     trace = create(:trace, :fixture => "i")
297
298     trace.import
299
300     assert_equal 2, trace.size
301   end
302
303   private
304
305   def check_query(query, traces)
306     traces = traces.map(&:id).sort
307     assert_equal traces, query.order(:id).ids
308   end
309
310   def check_mime_type(id, mime_type)
311     assert_equal mime_type, create(:trace, :fixture => id).mime_type
312   end
313
314   def check_extension_name(id, extension_name)
315     assert_equal extension_name, create(:trace, :fixture => id).extension_name
316   end
317
318   def check_xml_file(id, md5sum)
319     assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
320   end
321
322   def trace_valid(attrs, result = true)
323     entry = build(:trace, attrs)
324     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
325   end
326
327   def md5sum(io)
328     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
329   end
330 end