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)
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
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?
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?
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?
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")
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")
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")
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")
157 assert_equal picture, trace.large_picture
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")
164 assert_equal picture, trace.icon_picture
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
176 assert_equal 0, Tracepoint.where(:latitude => 54321).count
179 def test_import_creates_tracepoints
180 trace = create(:trace, :fixture => "a")
181 assert_equal 0, Tracepoint.where(:gpx_id => trace.id).count
186 assert_equal 1, Tracepoint.where(:gpx_id => trace.id).count
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
193 def test_import_creates_icon
194 trace = create(:trace, :inserted => false, :fixture => "a")
196 assert_not trace.icon.attached?
200 assert_predicate trace.icon, :attached?
203 def test_import_creates_large_picture
204 trace = create(:trace, :inserted => false, :fixture => "a")
206 assert_not trace.image.attached?
210 assert_predicate trace.image, :attached?
213 def test_import_handles_bz2
214 trace = create(:trace, :inserted => false, :fixture => "c")
218 assert_equal 1, trace.size
221 def test_import_handles_plain
222 trace = create(:trace, :inserted => false, :fixture => "a")
226 assert_equal 1, trace.size
229 def test_import_handles_plain_with_bom
230 trace = create(:trace, :inserted => false, :fixture => "b")
234 assert_equal 1, trace.size
237 def test_import_handles_gz
238 trace = create(:trace, :inserted => false, :fixture => "d")
242 assert_equal 1, trace.size
245 def test_import_handles_zip
246 trace = create(:trace, :inserted => false, :fixture => "f")
250 assert_equal 2, trace.size
253 def test_import_handles_tar
254 trace = create(:trace, :inserted => false, :fixture => "g")
258 assert_equal 2, trace.size
261 def test_import_handles_tar_gz
262 trace = create(:trace, :inserted => false, :fixture => "h")
266 assert_equal 2, trace.size
269 def test_import_handles_tar_bz2
270 trace = create(:trace, :inserted => false, :fixture => "i")
274 assert_equal 2, trace.size
279 def check_query(query, traces)
280 traces = traces.map(&:id).sort
281 assert_equal traces, query.order(:id).ids
284 def check_mime_type(id, mime_type)
285 assert_equal mime_type, create(:trace, :fixture => id).mime_type
288 def check_extension_name(id, extension_name)
289 assert_equal extension_name, create(:trace, :fixture => id).extension_name
292 def check_xml_file(id, md5sum)
293 assert_equal md5sum, md5sum(create(:trace, :fixture => id).xml_file)
296 def trace_valid(attrs, valid: true)
297 entry = build(:trace, attrs)
298 assert_equal valid, entry.valid?, "Expected #{attrs.inspect} to be #{valid}"
302 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest