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