]> git.openstreetmap.org Git - rails.git/blob - test/unit/trace_test.rb
Treat messages received by email as plain text
[rails.git] / test / unit / trace_test.rb
1 require File.dirname(__FILE__) + '/../test_helper'
2
3 class TraceTest < ActiveSupport::TestCase
4   api_fixtures
5   
6   def test_trace_count
7     assert_equal 5, Trace.count
8   end
9
10   def test_visible
11     check_query(Trace.visible, :public_trace_file, :anon_trace_file, :trackable_trace_file, :identifiable_trace_file)
12   end
13
14   def test_visible_to
15     check_query(Trace.visible_to(1), :public_trace_file, :identifiable_trace_file)
16     check_query(Trace.visible_to(2), :public_trace_file, :anon_trace_file, :trackable_trace_file, :identifiable_trace_file)
17     check_query(Trace.visible_to(3), :public_trace_file, :identifiable_trace_file)
18   end
19
20   def test_public
21     check_query(Trace.public, :public_trace_file, :identifiable_trace_file, :deleted_trace_file)
22   end
23
24   def test_validations
25     trace_valid({})
26     trace_valid({:user_id => nil}, false)
27     trace_valid({:name => 'a'*255})
28     trace_valid({:name => 'a'*256}, false)
29     trace_valid({:description => nil}, false)
30     trace_valid({:description => 'a'*255})
31     trace_valid({:description => 'a'*256}, false)
32     trace_valid({:visibility => "private"})
33     trace_valid({:visibility => "public"})
34     trace_valid({:visibility => "trackable"})
35     trace_valid({:visibility => "identifiable"})
36     trace_valid({:visibility => "foo"}, false)
37   end
38
39   def test_tagstring
40     trace = Trace.new
41     trace.tagstring = "foo bar baz"
42     assert_equal 3, trace.tags.length
43     assert_equal "foo", trace.tags[0].tag
44     assert_equal "bar", trace.tags[1].tag
45     assert_equal "baz", trace.tags[2].tag
46     assert_equal "foo, bar, baz", trace.tagstring
47     trace.tagstring = "foo, bar baz ,qux"
48     assert_equal 3, trace.tags.length
49     assert_equal "foo", trace.tags[0].tag
50     assert_equal "bar baz", trace.tags[1].tag
51     assert_equal "qux", trace.tags[2].tag
52     assert_equal "foo, bar baz, qux", trace.tagstring
53   end
54
55   def test_public?
56     assert_equal true, gpx_files(:public_trace_file).public?
57     assert_equal false, gpx_files(:anon_trace_file).public?
58     assert_equal false, gpx_files(:trackable_trace_file).public?
59     assert_equal true, gpx_files(:identifiable_trace_file).public?
60     assert_equal true, gpx_files(:deleted_trace_file).public?
61   end
62
63   def test_trackable?
64     assert_equal false, gpx_files(:public_trace_file).trackable?
65     assert_equal false, gpx_files(:anon_trace_file).trackable?
66     assert_equal true, gpx_files(:trackable_trace_file).trackable?
67     assert_equal true, gpx_files(:identifiable_trace_file).trackable?
68     assert_equal false, gpx_files(:deleted_trace_file).trackable?
69   end
70
71   def test_identifiable?
72     assert_equal false, gpx_files(:public_trace_file).identifiable?
73     assert_equal false, gpx_files(:anon_trace_file).identifiable?
74     assert_equal false, gpx_files(:trackable_trace_file).identifiable?
75     assert_equal true, gpx_files(:identifiable_trace_file).identifiable?
76     assert_equal false, gpx_files(:deleted_trace_file).identifiable?
77   end
78
79 private
80
81   def check_query(query, *traces)
82     traces = traces.map { |t| gpx_files(t) }.sort
83     assert_equal traces, query.order(:id)
84   end
85
86   def trace_valid(attrs, result = true)
87     entry = Trace.new(gpx_files(:public_trace_file).attributes, :without_protection => true)
88     entry.assign_attributes(attrs, :without_protection => true)
89     assert_equal result, entry.valid?, "Expected #{attrs.inspect} to be #{result}"
90   end
91 end