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/gzip")
140 check_mime_type("f", "application/zip")
141 check_mime_type("g", "application/x-tar")
142 check_mime_type("h", "application/x-tar+gzip")
143 check_mime_type("i", "application/x-tar+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")
171 trace = create(:trace, :fixture => "a")
173 assert_equal picture, trace.large_picture
176 def test_icon_picture
177 picture = File.read(Rails.root.join("test/gpx/fixtures/a_icon.gif"), :mode => "rb")
178 trace = create(:trace, :fixture => "a")
180 assert_equal picture, trace.icon_picture
183 def test_import_removes_previous_tracepoints
184 trace = create(:trace, :fixture => "a")
185 # Tracepoints don't have a primary key, so we use a specific latitude to
186 # check for successful deletion
187 create(:tracepoint, :latitude => 54321, :trace => trace)
188 assert_equal 1, Tracepoint.where(:latitude => 54321).count
192 assert_equal 0, Tracepoint.where(:latitude => 54321).count
195 def test_import_creates_tracepoints
196 trace = create(:trace, :fixture => "a")
197 assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
202 assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
204 # Check that the tile has been set prior to the bulk import
205 # i.e. that the callbacks have been run correctly
206 assert_equal 3221331576, Tracepoint.where(:gpx_id => trace.id).first.tile
209 def test_import_creates_icon
210 trace = create(:trace, :inserted => false, :fixture => "a")
212 assert_not trace.icon.attached?
216 assert trace.icon.attached?
219 def test_import_creates_large_picture
220 trace = create(:trace, :inserted => false, :fixture => "a")
222 assert_not trace.image.attached?
226 assert trace.image.attached?
229 def test_import_handles_bz2
230 trace = create(:trace, :inserted => false, :fixture => "c")
234 assert_equal 1, trace.size
237 def test_import_handles_plain
238 trace = create(:trace, :inserted => false, :fixture => "a")
242 assert_equal 1, trace.size
245 def test_import_handles_plain_with_bom
246 trace = create(:trace, :inserted => false, :fixture => "b")
250 assert_equal 1, trace.size
253 def test_import_handles_gz
254 trace = create(:trace, :inserted => false, :fixture => "d")
258 assert_equal 1, trace.size
261 def test_import_handles_zip
262 trace = create(:trace, :inserted => false, :fixture => "f")
266 assert_equal 2, trace.size
269 def test_import_handles_tar
270 trace = create(:trace, :inserted => false, :fixture => "g")
274 assert_equal 2, trace.size
277 def test_import_handles_tar_gz
278 trace = create(:trace, :inserted => false, :fixture => "h")
282 assert_equal 2, trace.size
285 def test_import_handles_tar_bz2
286 trace = create(:trace, :inserted => false, :fixture => "i")
290 assert_equal 2, trace.size
295 def check_query(query, traces)
296 traces = traces.map(&:id).sort
297 assert_equal traces, query.order(:id).ids
300 def check_mime_type(id, mime_type)
301 assert_equal mime_type, create(:trace, :fixture => id).mime_type
304 def check_extension_name(id, extension_name)
305 assert_equal extension_name, create(:trace, :fixture => id).extension_name
308 def check_xml_file(id, md5sum)
309 assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
312 def trace_valid(attrs, valid: true)
313 entry = build(:trace, attrs)
314 assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
318 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest