4 class TraceTest < ActiveSupport::TestCase
6 public_trace_file = create(:trace)
7 create(:trace, :deleted)
8 check_query(Trace.visible, [public_trace_file])
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)
23 check_query(Trace.visible_to(first_user), [
24 public_trace_file, identifiable_trace_file, pending_trace_file
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
30 check_query(Trace.visible_to(fourth_user), [
31 public_trace_file, identifiable_trace_file, pending_trace_file
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)
43 check_query(Trace.visible_to_all, [
44 public_trace_file, identifiable_trace_file,
45 deleted_trace_file, pending_trace_file
50 london_trace_file = create(:trace) do |trace|
51 create(:tracetag, :trace => trace, :tag => "London")
53 birmingham_trace_file = create(:trace) do |trace|
54 create(:tracetag, :trace => trace, :tag => "Birmingham")
56 check_query(Trace.tagged("London"), [london_trace_file])
57 check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
58 check_query(Trace.tagged("Unknown"), [])
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)
76 def test_tagstring_handles_space_separated_tags
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
87 def test_tagstring_handles_comma_separated_tags
89 trace.tagstring = "foo, bar baz ,qux"
90 assert_predicate trace, :valid?
91 assert_equal 3, trace.tags.length
92 assert_equal "foo", trace.tags[0].tag
93 assert_equal "bar baz", trace.tags[1].tag
94 assert_equal "qux", trace.tags[2].tag
95 assert_equal "foo, bar baz, qux", trace.tagstring
98 def test_tagstring_strips_whitespace
100 trace.tagstring = " zero , one , two "
101 assert_predicate trace, :valid?
102 assert_equal 3, trace.tags.length
103 assert_equal "zero", trace.tags[0].tag
104 assert_equal "one", trace.tags[1].tag
105 assert_equal "two", trace.tags[2].tag
106 assert_equal "zero, one, two", trace.tagstring
110 assert_predicate build(:trace, :visibility => "public"), :public?
111 assert_not build(:trace, :visibility => "private").public?
112 assert_not build(:trace, :visibility => "trackable").public?
113 assert_predicate build(:trace, :visibility => "identifiable"), :public?
114 assert_predicate build(:trace, :deleted, :visibility => "public"), :public?
118 assert_not build(:trace, :visibility => "public").trackable?
119 assert_not build(:trace, :visibility => "private").trackable?
120 assert_predicate build(:trace, :visibility => "trackable"), :trackable?
121 assert_predicate build(:trace, :visibility => "identifiable"), :trackable?
122 assert_not build(:trace, :deleted, :visibility => "public").trackable?
125 def test_identifiable?
126 assert_not build(:trace, :visibility => "public").identifiable?
127 assert_not build(:trace, :visibility => "private").identifiable?
128 assert_not build(:trace, :visibility => "trackable").identifiable?
129 assert_predicate build(:trace, :visibility => "identifiable"), :identifiable?
130 assert_not build(:trace, :deleted, :visibility => "public").identifiable?
134 # The ids refer to the .gpx fixtures in test/traces
135 check_mime_type("a", "application/gpx+xml")
136 check_mime_type("b", "application/gpx+xml")
137 check_mime_type("c", "application/x-bzip2")
138 check_mime_type("d", "application/gzip")
139 check_mime_type("f", "application/zip")
140 check_mime_type("g", "application/x-tar")
141 check_mime_type("h", "application/x-tar+gzip")
142 check_mime_type("i", "application/x-tar+x-bzip2")
145 def test_extension_name
146 # The ids refer to the .gpx fixtures in test/traces
147 check_extension_name("a", ".gpx")
148 check_extension_name("b", ".gpx")
149 check_extension_name("c", ".gpx.bz2")
150 check_extension_name("d", ".gpx.gz")
151 check_extension_name("f", ".zip")
152 check_extension_name("g", ".tar")
153 check_extension_name("h", ".tar.gz")
154 check_extension_name("i", ".tar.bz2")
158 check_xml_file("a", "848caa72f2f456d1bd6a0fdf228aa1b9")
159 check_xml_file("b", "db4cb5ed2d7d2b627b3b504296c4f701")
160 check_xml_file("c", "848caa72f2f456d1bd6a0fdf228aa1b9")
161 check_xml_file("d", "abd6675fdf3024a84fc0a1deac147c0d")
162 check_xml_file("f", "a7c05d676c77dc14369c21be216a3713")
163 check_xml_file("g", "a7c05d676c77dc14369c21be216a3713")
164 check_xml_file("h", "a7c05d676c77dc14369c21be216a3713")
165 check_xml_file("i", "a7c05d676c77dc14369c21be216a3713")
168 def test_large_picture
169 picture = Rails.root.join("test/gpx/fixtures/a.gif").read(:mode => "rb")
170 trace = create(:trace, :fixture => "a")
172 assert_equal picture, trace.large_picture
175 def test_icon_picture
176 picture = Rails.root.join("test/gpx/fixtures/a_icon.gif").read(:mode => "rb")
177 trace = create(:trace, :fixture => "a")
179 assert_equal picture, trace.icon_picture
182 def test_import_removes_previous_tracepoints
183 trace = create(:trace, :fixture => "a")
184 # Tracepoints don't have a primary key, so we use a specific latitude to
185 # check for successful deletion
186 create(:tracepoint, :latitude => 54321, :trace => trace)
187 assert_equal 1, Tracepoint.where(:latitude => 54321).count
191 assert_equal 0, Tracepoint.where(:latitude => 54321).count
194 def test_import_creates_tracepoints
195 trace = create(:trace, :fixture => "a")
196 assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
201 assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
203 # Check that the tile has been set prior to the bulk import
204 # i.e. that the callbacks have been run correctly
205 assert_equal 3221331576, Tracepoint.where(:gpx_id => trace.id).first.tile
208 def test_import_creates_icon
209 trace = create(:trace, :inserted => false, :fixture => "a")
211 assert_not trace.icon.attached?
215 assert_predicate trace.icon, :attached?
218 def test_import_creates_large_picture
219 trace = create(:trace, :inserted => false, :fixture => "a")
221 assert_not trace.image.attached?
225 assert_predicate trace.image, :attached?
228 def test_import_handles_bz2
229 trace = create(:trace, :inserted => false, :fixture => "c")
233 assert_equal 1, trace.size
236 def test_import_handles_plain
237 trace = create(:trace, :inserted => false, :fixture => "a")
241 assert_equal 1, trace.size
244 def test_import_handles_plain_with_bom
245 trace = create(:trace, :inserted => false, :fixture => "b")
249 assert_equal 1, trace.size
252 def test_import_handles_gz
253 trace = create(:trace, :inserted => false, :fixture => "d")
257 assert_equal 1, trace.size
260 def test_import_handles_zip
261 trace = create(:trace, :inserted => false, :fixture => "f")
265 assert_equal 2, trace.size
268 def test_import_handles_tar
269 trace = create(:trace, :inserted => false, :fixture => "g")
273 assert_equal 2, trace.size
276 def test_import_handles_tar_gz
277 trace = create(:trace, :inserted => false, :fixture => "h")
281 assert_equal 2, trace.size
284 def test_import_handles_tar_bz2
285 trace = create(:trace, :inserted => false, :fixture => "i")
289 assert_equal 2, trace.size
294 def check_query(query, traces)
295 traces = traces.map(&:id).sort
296 assert_equal traces, query.order(:id).ids
299 def check_mime_type(id, mime_type)
300 assert_equal mime_type, create(:trace, :fixture => id).mime_type
303 def check_extension_name(id, extension_name)
304 assert_equal extension_name, create(:trace, :fixture => id).extension_name
307 def check_xml_file(id, md5sum)
308 assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
311 def trace_valid(attrs, valid: true)
312 entry = build(:trace, attrs)
313 assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
317 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest