More work on test coverage
[rails.git] / test / models / trace_test.rb
1 require "test_helper"
2 require "digest"
3
4 class TraceTest < ActiveSupport::TestCase
5   api_fixtures
6
7   def setup
8     @gpx_trace_dir = Object.send("remove_const", "GPX_TRACE_DIR")
9     Object.const_set("GPX_TRACE_DIR", File.dirname(__FILE__) + "/../traces")
10   end
11
12   def teardown
13     Object.send("remove_const", "GPX_TRACE_DIR")
14     Object.const_set("GPX_TRACE_DIR", @gpx_trace_dir)
15   end
16
17   def test_trace_count
18     assert_equal 9, Trace.count
19   end
20
21   def test_visible
22     check_query(Trace.visible, [
23       :public_trace_file, :anon_trace_file, :trackable_trace_file,
24       :identifiable_trace_file, :zipped_trace_file, :tar_trace_file,
25       :tar_gzip_trace_file, :tar_bzip_trace_file
26     ])
27   end
28
29   def test_visible_to
30     check_query(Trace.visible_to(1), [
31       :public_trace_file, :identifiable_trace_file
32     ])
33     check_query(Trace.visible_to(2), [
34       :public_trace_file, :anon_trace_file, :trackable_trace_file,
35       :identifiable_trace_file
36     ])
37     check_query(Trace.visible_to(3), [
38       :public_trace_file, :identifiable_trace_file
39     ])
40   end
41
42   def test_visible_to_all
43     check_query(Trace.visible_to_all, [
44       :public_trace_file, :identifiable_trace_file, :deleted_trace_file
45     ])
46   end
47
48   def test_tagged
49     check_query(Trace.tagged("London"), [:public_trace_file, :anon_trace_file])
50     check_query(Trace.tagged("Birmingham"), [:anon_trace_file, :identifiable_trace_file])
51     check_query(Trace.tagged("Unknown"), [])
52   end
53
54   def test_validations
55     trace_valid({})
56     trace_valid({ :user_id => nil }, false)
57     trace_valid(:name => "a" * 255)
58     trace_valid({ :name => "a" * 256 }, false)
59     trace_valid({ :description => nil }, false)
60     trace_valid(:description => "a" * 255)
61     trace_valid({ :description => "a" * 256 }, false)
62     trace_valid(:visibility => "private")
63     trace_valid(:visibility => "public")
64     trace_valid(:visibility => "trackable")
65     trace_valid(:visibility => "identifiable")
66     trace_valid({ :visibility => "foo" }, false)
67   end
68
69   def test_tagstring
70     trace = Trace.new(gpx_files(:public_trace_file).attributes)
71     trace.tagstring = "foo bar baz"
72     assert trace.valid?
73     assert_equal 3, trace.tags.length
74     assert_equal "foo", trace.tags[0].tag
75     assert_equal "bar", trace.tags[1].tag
76     assert_equal "baz", trace.tags[2].tag
77     assert_equal "foo, bar, baz", trace.tagstring
78     trace.tagstring = "foo, bar baz ,qux"
79     assert trace.valid?
80     assert_equal 3, trace.tags.length
81     assert_equal "foo", trace.tags[0].tag
82     assert_equal "bar baz", trace.tags[1].tag
83     assert_equal "qux", trace.tags[2].tag
84     assert_equal "foo, bar baz, qux", trace.tagstring
85   end
86
87   def test_public?
88     assert_equal true, gpx_files(:public_trace_file).public?
89     assert_equal false, gpx_files(:anon_trace_file).public?
90     assert_equal false, gpx_files(:trackable_trace_file).public?
91     assert_equal true, gpx_files(:identifiable_trace_file).public?
92     assert_equal true, gpx_files(:deleted_trace_file).public?
93   end
94
95   def test_trackable?
96     assert_equal false, gpx_files(:public_trace_file).trackable?
97     assert_equal false, gpx_files(:anon_trace_file).trackable?
98     assert_equal true, gpx_files(:trackable_trace_file).trackable?
99     assert_equal true, gpx_files(:identifiable_trace_file).trackable?
100     assert_equal false, gpx_files(:deleted_trace_file).trackable?
101   end
102
103   def test_identifiable?
104     assert_equal false, gpx_files(:public_trace_file).identifiable?
105     assert_equal false, gpx_files(:anon_trace_file).identifiable?
106     assert_equal false, gpx_files(:trackable_trace_file).identifiable?
107     assert_equal true, gpx_files(:identifiable_trace_file).identifiable?
108     assert_equal false, gpx_files(:deleted_trace_file).identifiable?
109   end
110
111   def test_mime_type
112     assert_equal "application/gpx+xml", gpx_files(:public_trace_file).mime_type
113     assert_equal "application/gpx+xml", gpx_files(:anon_trace_file).mime_type
114     assert_equal "application/x-bzip2", gpx_files(:trackable_trace_file).mime_type
115     assert_equal "application/x-gzip", gpx_files(:identifiable_trace_file).mime_type
116     assert_equal "application/x-zip", gpx_files(:zipped_trace_file).mime_type
117     assert_equal "application/x-tar", gpx_files(:tar_trace_file).mime_type
118     assert_equal "application/x-gzip", gpx_files(:tar_gzip_trace_file).mime_type
119     assert_equal "application/x-bzip2", gpx_files(:tar_bzip_trace_file).mime_type
120   end
121
122   def test_extension_name
123     assert_equal ".gpx", gpx_files(:public_trace_file).extension_name
124     assert_equal ".gpx", gpx_files(:anon_trace_file).extension_name
125     assert_equal ".gpx.bz2", gpx_files(:trackable_trace_file).extension_name
126     assert_equal ".gpx.gz", gpx_files(:identifiable_trace_file).extension_name
127     assert_equal ".zip", gpx_files(:zipped_trace_file).extension_name
128     assert_equal ".tar", gpx_files(:tar_trace_file).extension_name
129     assert_equal ".tar.gz", gpx_files(:tar_gzip_trace_file).extension_name
130     assert_equal ".tar.bz2", gpx_files(:tar_bzip_trace_file).extension_name
131   end
132
133   def test_xml_file
134     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:public_trace_file).xml_file)
135     assert_equal "66179ca44f1e93d8df62e2b88cbea732", md5sum(gpx_files(:anon_trace_file).xml_file)
136     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:trackable_trace_file).xml_file)
137     assert_equal "abd6675fdf3024a84fc0a1deac147c0d", md5sum(gpx_files(:identifiable_trace_file).xml_file)
138     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:zipped_trace_file).xml_file)
139     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:tar_trace_file).xml_file)
140     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:tar_gzip_trace_file).xml_file)
141     assert_equal "848caa72f2f456d1bd6a0fdf228aa1b9", md5sum(gpx_files(:tar_bzip_trace_file).xml_file)
142   end
143
144   private
145
146   def check_query(query, traces)
147     traces = traces.map { |t| gpx_files(t).id }.sort
148     assert_equal traces, query.order(:id).ids
149   end
150
151   def trace_valid(attrs, result = true)
152     entry = Trace.new(gpx_files(:public_trace_file).attributes)
153     entry.assign_attributes(attrs)
154     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
155   end
156
157   def md5sum(io)
158     io.each_with_object(Digest::MD5.new) { |l, d| d.update(l) }.hexdigest
159   end
160 end