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.
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"))
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
22 public_trace_file = create(:trace)
23 create(:trace, :deleted)
24 check_query(Trace.visible, [public_trace_file])
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)
39 check_query(Trace.visible_to(first_user), [
40 public_trace_file, identifiable_trace_file, pending_trace_file
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
46 check_query(Trace.visible_to(fourth_user), [
47 public_trace_file, identifiable_trace_file, pending_trace_file
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)
59 check_query(Trace.visible_to_all, [
60 public_trace_file, identifiable_trace_file,
61 deleted_trace_file, pending_trace_file
66 london_trace_file = create(:trace) do |trace|
67 create(:tracetag, :trace => trace, :tag => "London")
69 birmingham_trace_file = create(:trace) do |trace|
70 create(:tracetag, :trace => trace, :tag => "Birmingham")
72 check_query(Trace.tagged("London"), [london_trace_file])
73 check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
74 check_query(Trace.tagged("Unknown"), [])
79 trace_valid({ :user_id => nil }, :valid => false)
80 trace_valid({ :name => "a" * 255 })
81 trace_valid({ :name => "a" * 256 }, :valid => false)
82 trace_valid({ :description => nil }, :valid => false)
83 trace_valid({ :description => "a" * 255 })
84 trace_valid({ :description => "a" * 256 }, :valid => 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" }, :valid => false)
94 trace.tagstring = "foo bar baz"
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"
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
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?
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?
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?
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")
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")
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")
169 def test_large_picture
170 picture = File.read(Rails.root.join("test/gpx/fixtures/a.gif"), :mode => "rb")
173 trace.large_picture = picture
174 assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
175 assert_equal picture, trace.large_picture
180 def test_icon_picture
181 picture = File.read(Rails.root.join("test/gpx/fixtures/a_icon.gif"), :mode => "rb")
184 trace.icon_picture = picture
185 assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
186 assert_equal picture, trace.icon_picture
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
200 assert_equal 0, Tracepoint.where(:latitude => 54321).count
203 def test_import_creates_tracepoints
204 trace = create(:trace, :fixture => "a")
205 assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
210 assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
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
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)
225 assert_path_exists(icon_path)
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)
236 assert_path_exists(large_picture_path)
239 def test_import_handles_bz2
240 trace = create(:trace, :fixture => "c")
244 assert_equal 1, trace.size
247 def test_import_handles_plain
248 trace = create(:trace, :fixture => "a")
252 assert_equal 1, trace.size
255 def test_import_handles_plain_with_bom
256 trace = create(:trace, :fixture => "b")
260 assert_equal 1, trace.size
263 def test_import_handles_gz
264 trace = create(:trace, :fixture => "d")
268 assert_equal 1, trace.size
271 def test_import_handles_zip
272 trace = create(:trace, :fixture => "f")
276 assert_equal 2, trace.size
279 def test_import_handles_tar
280 trace = create(:trace, :fixture => "g")
284 assert_equal 2, trace.size
287 def test_import_handles_tar_gz
288 trace = create(:trace, :fixture => "h")
292 assert_equal 2, trace.size
295 def test_import_handles_tar_bz2
296 trace = create(:trace, :fixture => "i")
300 assert_equal 2, trace.size
305 def check_query(query, traces)
306 traces = traces.map(&:id).sort
307 assert_equal traces, query.order(:id).ids
310 def check_mime_type(id, mime_type)
311 assert_equal mime_type, create(:trace, :fixture => id).mime_type
314 def check_extension_name(id, extension_name)
315 assert_equal extension_name, create(:trace, :fixture => id).extension_name
318 def check_xml_file(id, md5sum)
319 assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
322 def trace_valid(attrs, valid: true)
323 entry = build(:trace, attrs)
324 assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
328 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest