3 require "minitest/mock"
5 class TraceTest < ActiveSupport::TestCase
7 File.unlink(*Dir.glob(File.join(Settings.gpx_trace_dir, "*.gpx")))
8 File.unlink(*Dir.glob(File.join(Settings.gpx_image_dir, "*.gif")))
12 public_trace_file = create(:trace)
13 create(:trace, :deleted)
14 check_query(Trace.visible, [public_trace_file])
18 first_user = create(:user)
19 second_user = create(:user)
20 third_user = create(:user)
21 fourth_user = create(:user)
22 public_trace_file = create(:trace, :visibility => "public", :user => first_user)
23 anon_trace_file = create(:trace, :visibility => "private", :user => second_user)
24 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => first_user)
25 pending_trace_file = create(:trace, :visibility => "public", :user => second_user, :inserted => false)
26 trackable_trace_file = create(:trace, :visibility => "trackable", :user => second_user)
27 _other_trace_file = create(:trace, :visibility => "private", :user => third_user)
29 check_query(Trace.visible_to(first_user), [
30 public_trace_file, identifiable_trace_file, pending_trace_file
32 check_query(Trace.visible_to(second_user), [
33 public_trace_file, anon_trace_file, trackable_trace_file,
34 identifiable_trace_file, pending_trace_file
36 check_query(Trace.visible_to(fourth_user), [
37 public_trace_file, identifiable_trace_file, pending_trace_file
41 def test_visible_to_all
42 public_trace_file = create(:trace, :visibility => "public")
43 _private_trace_file = create(:trace, :visibility => "private")
44 identifiable_trace_file = create(:trace, :visibility => "identifiable")
45 _trackable_trace_file = create(:trace, :visibility => "trackable")
46 deleted_trace_file = create(:trace, :deleted, :visibility => "public")
47 pending_trace_file = create(:trace, :visibility => "public", :inserted => false)
49 check_query(Trace.visible_to_all, [
50 public_trace_file, identifiable_trace_file,
51 deleted_trace_file, pending_trace_file
56 london_trace_file = create(:trace) do |trace|
57 create(:tracetag, :trace => trace, :tag => "London")
59 birmingham_trace_file = create(:trace) do |trace|
60 create(:tracetag, :trace => trace, :tag => "Birmingham")
62 check_query(Trace.tagged("London"), [london_trace_file])
63 check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
64 check_query(Trace.tagged("Unknown"), [])
69 trace_valid({ :user_id => nil }, false)
70 trace_valid(:name => "a" * 255)
71 trace_valid({ :name => "a" * 256 }, false)
72 trace_valid({ :description => nil }, false)
73 trace_valid(:description => "a" * 255)
74 trace_valid({ :description => "a" * 256 }, false)
75 trace_valid(:visibility => "private")
76 trace_valid(:visibility => "public")
77 trace_valid(:visibility => "trackable")
78 trace_valid(:visibility => "identifiable")
79 trace_valid({ :visibility => "foo" }, false)
84 trace.tagstring = "foo bar baz"
86 assert_equal 3, trace.tags.length
87 assert_equal "foo", trace.tags[0].tag
88 assert_equal "bar", trace.tags[1].tag
89 assert_equal "baz", trace.tags[2].tag
90 assert_equal "foo, bar, baz", trace.tagstring
91 trace.tagstring = "foo, bar baz ,qux"
93 assert_equal 3, trace.tags.length
94 assert_equal "foo", trace.tags[0].tag
95 assert_equal "bar baz", trace.tags[1].tag
96 assert_equal "qux", trace.tags[2].tag
97 assert_equal "foo, bar baz, qux", trace.tagstring
101 assert_equal true, build(:trace, :visibility => "public").public?
102 assert_equal false, build(:trace, :visibility => "private").public?
103 assert_equal false, build(:trace, :visibility => "trackable").public?
104 assert_equal true, build(:trace, :visibility => "identifiable").public?
105 assert_equal true, build(:trace, :deleted, :visibility => "public").public?
109 assert_equal false, build(:trace, :visibility => "public").trackable?
110 assert_equal false, build(:trace, :visibility => "private").trackable?
111 assert_equal true, build(:trace, :visibility => "trackable").trackable?
112 assert_equal true, build(:trace, :visibility => "identifiable").trackable?
113 assert_equal false, build(:trace, :deleted, :visibility => "public").trackable?
116 def test_identifiable?
117 assert_equal false, build(:trace, :visibility => "public").identifiable?
118 assert_equal false, build(:trace, :visibility => "private").identifiable?
119 assert_equal false, build(:trace, :visibility => "trackable").identifiable?
120 assert_equal true, build(:trace, :visibility => "identifiable").identifiable?
121 assert_equal false, build(:trace, :deleted, :visibility => "public").identifiable?
125 # The ids refer to the .gpx fixtures in test/traces
126 check_mime_type("a", "application/gpx+xml")
127 check_mime_type("b", "application/gpx+xml")
128 check_mime_type("c", "application/x-bzip2")
129 check_mime_type("d", "application/x-gzip")
130 check_mime_type("f", "application/x-zip")
131 check_mime_type("g", "application/x-tar")
132 check_mime_type("h", "application/x-gzip")
133 check_mime_type("i", "application/x-bzip2")
136 def test_extension_name
137 # The ids refer to the .gpx fixtures in test/traces
138 check_extension_name("a", ".gpx")
139 check_extension_name("b", ".gpx")
140 check_extension_name("c", ".gpx.bz2")
141 check_extension_name("d", ".gpx.gz")
142 check_extension_name("f", ".zip")
143 check_extension_name("g", ".tar")
144 check_extension_name("h", ".tar.gz")
145 check_extension_name("i", ".tar.bz2")
149 check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
150 check_xml_file("b", "db4cb5ed2d7d2b627b3b504296c4f701")
151 check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
152 check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
153 check_xml_file("f", "a7c05d676c77dc14369c21be216a3713")
154 check_xml_file("g", "a7c05d676c77dc14369c21be216a3713")
155 check_xml_file("h", "a7c05d676c77dc14369c21be216a3713")
156 check_xml_file("i", "a7c05d676c77dc14369c21be216a3713")
159 def test_large_picture
160 picture = File.read(Rails.root.join("test", "gpx", "fixtures", "a.gif"), :mode => "rb")
163 trace.large_picture = picture
164 assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
165 assert_equal picture, trace.large_picture
170 def test_icon_picture
171 picture = File.read(Rails.root.join("test", "gpx", "fixtures", "a_icon.gif"), :mode => "rb")
174 trace.icon_picture = picture
175 assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
176 assert_equal picture, trace.icon_picture
181 # When testing the trace.import method, care needs to be taken regarding the icon
182 # fixture files, since the fixtures could be overwritten by newly generated files.
183 # We use FakeFS to temporarily protect the real fixture files from being overwritten.
185 def test_import_removes_previous_tracepoints
187 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
188 trace = create(:trace, :fixture => "a")
189 # Tracepoints don't have a primary key, so we use a specific latitude to
190 # check for successful deletion
191 create(:tracepoint, :latitude => 54321, :trace => trace)
192 assert_equal 1, Tracepoint.where(:latitude => 54321).count
196 assert_equal 0, Tracepoint.where(:latitude => 54321).count
200 def test_import_creates_tracepoints
202 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
203 trace = create(:trace, :fixture => "a")
204 assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
209 assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
211 # Check that the tile has been set prior to the bulk import
212 # i.e. that the callbacks have been run correctly
213 assert_equal 3221331576, Tracepoint.where(:gpx_id => trace.id).first.tile
217 def test_import_creates_icon
219 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
220 trace = create(:trace, :fixture => "a")
221 icon_path = File.join(Settings.gpx_image_dir, "#{trace.id}_icon.gif")
222 FileUtils.rm(icon_path)
223 assert_equal false, File.exist?(icon_path)
227 assert_equal true, File.exist?(icon_path)
231 def test_import_creates_large_picture
233 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
234 trace = create(:trace, :fixture => "a")
235 large_picture_path = File.join(Settings.gpx_image_dir, "#{trace.id}.gif")
236 FileUtils.rm(large_picture_path)
237 assert_equal false, File.exist?(large_picture_path)
241 assert_equal true, File.exist?(large_picture_path)
245 def test_import_handles_bz2
247 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
248 trace = create(:trace, :fixture => "c")
252 assert_equal 1, trace.size
256 def test_import_handles_plain
258 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
259 trace = create(:trace, :fixture => "a")
263 assert_equal 1, trace.size
267 def test_import_handles_plain_with_bom
269 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
270 trace = create(:trace, :fixture => "b")
274 assert_equal 1, trace.size
278 def test_import_handles_gz
279 trace = create(:trace, :fixture => "d")
282 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
285 assert_equal 1, trace.size
291 def test_import_handles_zip
292 trace = create(:trace, :fixture => "f")
295 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
298 assert_equal 2, trace.size
304 def test_import_handles_tar
305 trace = create(:trace, :fixture => "g")
308 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
311 assert_equal 2, trace.size
317 def test_import_handles_tar_gz
318 trace = create(:trace, :fixture => "h")
321 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
324 assert_equal 2, trace.size
330 def test_import_handles_tar_bz2
331 trace = create(:trace, :fixture => "i")
334 FakeFS::FileSystem.clone(Rails.root.join("test", "gpx"))
337 assert_equal 2, trace.size
345 def check_query(query, traces)
346 traces = traces.map(&:id).sort
347 assert_equal traces, query.order(:id).ids
350 def check_mime_type(id, mime_type)
351 assert_equal mime_type, create(:trace, :fixture => id).mime_type
354 def check_extension_name(id, extension_name)
355 assert_equal extension_name, create(:trace, :fixture => id).extension_name
358 def check_xml_file(id, md5sum)
359 assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
362 def trace_valid(attrs, result = true)
363 entry = build(:trace, attrs)
364 assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
368 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest