3 require "minitest/mock"
5 class TraceTest < ActiveSupport::TestCase
9 @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
10 Object.const_set("GPX_TRACE_DIR", File.dirname(__FILE__) + "/../traces")
12 @gpx_image_dir = Object.send("remove_const", "GPX_IMAGE_DIR")
13 Object.const_set("GPX_IMAGE_DIR", File.dirname(__FILE__) + "/../traces")
17 Object.send("remove_const", "GPX_TRACE_DIR")
18 Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
20 Object.send("remove_const", "GPX_IMAGE_DIR")
21 Object.const_set("GPX_IMAGE_DIR", @gpx_image_dir)
25 public_trace_file = create(:trace)
26 create(:trace, :deleted)
27 check_query(Trace.visible, [public_trace_file])
31 public_trace_file = create(:trace, :visibility => "public", :user => users(:normal_user))
32 anon_trace_file = create(:trace, :visibility => "private", :user => users(:public_user))
33 identifiable_trace_file = create(:trace, :visibility => "identifiable", :user => users(:normal_user))
34 pending_trace_file = create(:trace, :visibility => "public", :user => users(:public_user), :inserted => false)
35 trackable_trace_file = create(:trace, :visibility => "trackable", :user => users(:public_user))
36 _other_trace_file = create(:trace, :visibility => "private", :user => users(:second_public_user))
38 check_query(Trace.visible_to(users(:normal_user).id), [
39 public_trace_file, identifiable_trace_file, pending_trace_file
41 check_query(Trace.visible_to(users(:public_user)), [
42 public_trace_file, anon_trace_file, trackable_trace_file,
43 identifiable_trace_file, pending_trace_file
45 check_query(Trace.visible_to(users(:inactive_user)), [
46 public_trace_file, identifiable_trace_file, pending_trace_file
50 def test_visible_to_all
51 public_trace_file = create(:trace, :visibility => "public")
52 _private_trace_file = create(:trace, :visibility => "private")
53 identifiable_trace_file = create(:trace, :visibility => "identifiable")
54 _trackable_trace_file = create(:trace, :visibility => "trackable")
55 deleted_trace_file = create(:trace, :deleted, :visibility => "public")
56 pending_trace_file = create(:trace, :visibility => "public", :inserted => false)
58 check_query(Trace.visible_to_all, [
59 public_trace_file, identifiable_trace_file,
60 deleted_trace_file, pending_trace_file
65 london_trace_file = create(:trace) do |trace|
66 create(:tracetag, :trace => trace, :tag => "London")
68 birmingham_trace_file = create(:trace) do |trace|
69 create(:tracetag, :trace => trace, :tag => "Birmingham")
71 check_query(Trace.tagged("London"), [london_trace_file])
72 check_query(Trace.tagged("Birmingham"), [birmingham_trace_file])
73 check_query(Trace.tagged("Unknown"), [])
78 trace_valid({ :user_id => nil }, false)
79 trace_valid(:name => "a" * 255)
80 trace_valid({ :name => "a" * 256 }, false)
81 trace_valid({ :description => nil }, false)
82 trace_valid(:description => "a" * 255)
83 trace_valid({ :description => "a" * 256 }, false)
84 trace_valid(:visibility => "private")
85 trace_valid(:visibility => "public")
86 trace_valid(:visibility => "trackable")
87 trace_valid(:visibility => "identifiable")
88 trace_valid({ :visibility => "foo" }, false)
93 trace.tagstring = "foo bar baz"
95 assert_equal 3, trace.tags.length
96 assert_equal "foo", trace.tags[0].tag
97 assert_equal "bar", trace.tags[1].tag
98 assert_equal "baz", trace.tags[2].tag
99 assert_equal "foo, bar, baz", trace.tagstring
100 trace.tagstring = "foo, bar baz ,qux"
102 assert_equal 3, trace.tags.length
103 assert_equal "foo", trace.tags[0].tag
104 assert_equal "bar baz", trace.tags[1].tag
105 assert_equal "qux", trace.tags[2].tag
106 assert_equal "foo, bar baz, qux", trace.tagstring
110 assert_equal true, build(:trace, :visibility => "public").public?
111 assert_equal false, build(:trace, :visibility => "private").public?
112 assert_equal false, build(:trace, :visibility => "trackable").public?
113 assert_equal true, build(:trace, :visibility => "identifiable").public?
114 assert_equal true, build(:trace, :deleted, :visibility => "public").public?
118 assert_equal false, build(:trace, :visibility => "public").trackable?
119 assert_equal false, build(:trace, :visibility => "private").trackable?
120 assert_equal true, build(:trace, :visibility => "trackable").trackable?
121 assert_equal true, build(:trace, :visibility => "identifiable").trackable?
122 assert_equal false, build(:trace, :deleted, :visibility => "public").trackable?
125 def test_identifiable?
126 assert_equal false, build(:trace, :visibility => "public").identifiable?
127 assert_equal false, build(:trace, :visibility => "private").identifiable?
128 assert_equal false, build(:trace, :visibility => "trackable").identifiable?
129 assert_equal true, build(:trace, :visibility => "identifiable").identifiable?
130 assert_equal false, build(:trace, :deleted, :visibility => "public").identifiable?
134 # The ids refer to the .gpx fixtures in test/traces
135 check_mime_type(1, "application/gpx+xml")
136 check_mime_type(2, "application/gpx+xml")
137 check_mime_type(3, "application/x-bzip2")
138 check_mime_type(4, "application/x-gzip")
139 check_mime_type(6, "application/x-zip")
140 check_mime_type(7, "application/x-tar")
141 check_mime_type(8, "application/x-gzip")
142 check_mime_type(9, "application/x-bzip2")
145 def test_extension_name
146 # The ids refer to the .gpx fixtures in test/traces
147 check_extension_name(1, ".gpx")
148 check_extension_name(2, ".gpx")
149 check_extension_name(3, ".gpx.bz2")
150 check_extension_name(4, ".gpx.gz")
151 check_extension_name(6, ".zip")
152 check_extension_name(7, ".tar")
153 check_extension_name(8, ".tar.gz")
154 check_extension_name(9, ".tar.bz2")
158 check_xml_file(1, "848caa72f2f456d1bd6a0fdf228aa1b9")
159 check_xml_file(2, "66179ca44f1e93d8df62e2b88cbea732")
160 check_xml_file(3, "848caa72f2f456d1bd6a0fdf228aa1b9")
161 check_xml_file(4, "abd6675fdf3024a84fc0a1deac147c0d")
162 check_xml_file(6, "848caa72f2f456d1bd6a0fdf228aa1b9")
163 check_xml_file(7, "848caa72f2f456d1bd6a0fdf228aa1b9")
164 check_xml_file(8, "848caa72f2f456d1bd6a0fdf228aa1b9")
165 check_xml_file(9, "848caa72f2f456d1bd6a0fdf228aa1b9")
168 def test_large_picture
169 trace = create(:trace)
170 picture = trace.stub :large_picture_name, "#{GPX_IMAGE_DIR}/1.gif" do
175 trace.large_picture = picture
176 assert_equal "7c841749e084ee4a5d13f12cd3bef456", md5sum(File.new(trace.large_picture_name))
177 assert_equal picture, trace.large_picture
182 def test_icon_picture
183 trace = create(:trace)
184 picture = trace.stub :icon_picture_name, "#{GPX_IMAGE_DIR}/1_icon.gif" do
189 trace.icon_picture = picture
190 assert_equal "b47baf22ed0e85d77e808694fad0ee27", md5sum(File.new(trace.icon_picture_name))
191 assert_equal picture, trace.icon_picture
198 def check_query(query, traces)
199 traces = traces.map(&:id).sort
200 assert_equal traces, query.order(:id).ids
203 def check_mime_type(id, mime_type)
204 trace = create(:trace)
205 trace.stub :trace_name, "#{GPX_TRACE_DIR}/#{id}.gpx" do
206 assert_equal mime_type, trace.mime_type
210 def check_extension_name(id, extension_name)
211 trace = create(:trace)
212 trace.stub :trace_name, "#{GPX_TRACE_DIR}/#{id}.gpx" do
213 assert_equal extension_name, trace.extension_name
217 def check_xml_file(id, md5sum)
218 trace = create(:trace)
219 trace.stub :trace_name, "#{GPX_TRACE_DIR}/#{id}.gpx" do
220 assert_equal md5sum, md5sum(trace.xml_file)
224 def trace_valid(attrs, result = true)
225 entry = build(:trace)
226 entry.assign_attributes(attrs)
227 assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
231 io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest