##// END OF EJS Templates
remove unneeded Relation#all from PrincipalTest...
Toshi MARUYAMA -
r12437:e746e5dd835e
parent child
Show More
@@ -1,119 +1,120
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2013 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 require File.expand_path('../../test_helper', __FILE__)
21 21
22 22 class PrincipalTest < ActiveSupport::TestCase
23 23 fixtures :users, :projects, :members, :member_roles
24 24
25 25 def test_active_scope_should_return_groups_and_active_users
26 26 result = Principal.active.all
27 27 assert_include Group.first, result
28 28 assert_not_nil result.detect {|p| p.is_a?(User)}
29 29 assert_nil result.detect {|p| p.is_a?(User) && !p.active?}
30 30 assert_nil result.detect {|p| p.is_a?(AnonymousUser)}
31 31 end
32 32
33 33 def test_member_of_scope_should_return_the_union_of_all_members
34 34 projects = Project.find([1])
35 35 assert_equal [3, 2], Principal.member_of(projects).sort.map(&:id)
36 36 projects = Project.find([1, 2])
37 37 assert_equal [3, 2, 8, 11], Principal.member_of(projects).sort.map(&:id)
38 38 end
39 39
40 40 def test_member_of_scope_should_be_empty_for_no_projects
41 41 assert_equal [], Principal.member_of([]).sort
42 42 end
43 43
44 44 def test_not_member_of_scope_should_return_users_that_have_no_memberships
45 45 [[1], [1, 2]].each do |ids|
46 46 projects = Project.find(ids)
47 47 assert_equal ids.size, projects.count
48 48 expected = (Principal.all - projects.map(&:memberships).flatten.map(&:principal)).sort
49 49 assert_equal expected, Principal.not_member_of(projects).sort
50 50 end
51 51 end
52 52
53 53 def test_not_member_of_scope_should_be_empty_for_no_projects
54 54 assert_equal [], Principal.not_member_of([]).sort
55 55 end
56 56
57 57 def test_sorted_scope_should_sort_users_before_groups
58 58 scope = Principal.where("type <> ?", 'AnonymousUser')
59 59 expected_order = scope.all.sort do |a, b|
60 60 if a.is_a?(User) && b.is_a?(Group)
61 61 -1
62 62 elsif a.is_a?(Group) && b.is_a?(User)
63 63 1
64 64 else
65 65 a.name.downcase <=> b.name.downcase
66 66 end
67 67 end
68 assert_equal expected_order.map(&:name).map(&:downcase), scope.sorted.all.map(&:name).map(&:downcase)
68 assert_equal expected_order.map(&:name).map(&:downcase),
69 scope.sorted.map(&:name).map(&:downcase)
69 70 end
70 71
71 72 test "like scope should search login" do
72 73 results = Principal.like('jsmi')
73 74
74 75 assert results.any?
75 76 assert results.all? {|u| u.login.match(/jsmi/i) }
76 77 end
77 78
78 79 test "like scope should search firstname" do
79 80 results = Principal.like('john')
80 81
81 82 assert results.any?
82 83 assert results.all? {|u| u.firstname.match(/john/i) }
83 84 end
84 85
85 86 test "like scope should search lastname" do
86 87 results = Principal.like('smi')
87 88
88 89 assert results.any?
89 90 assert results.all? {|u| u.lastname.match(/smi/i) }
90 91 end
91 92
92 93 test "like scope should search mail" do
93 94 results = Principal.like('somenet')
94 95
95 96 assert results.any?
96 97 assert results.all? {|u| u.mail.match(/somenet/i) }
97 98 end
98 99
99 100 test "like scope should search firstname and lastname" do
100 101 results = Principal.like('john smi')
101 102
102 103 assert_equal 1, results.count
103 104 assert_equal User.find(2), results.first
104 105 end
105 106
106 107 test "like scope should search lastname and firstname" do
107 108 results = Principal.like('smith joh')
108 109
109 110 assert_equal 1, results.count
110 111 assert_equal User.find(2), results.first
111 112 end
112 113
113 114 def test_like_scope_with_cyrillic_name
114 115 user = User.generate!(:firstname => 'Соболев', :lastname => 'Денис')
115 116 results = Principal.like('Собо')
116 117 assert_equal 1, results.count
117 118 assert_equal user, results.first
118 119 end
119 120 end
General Comments 0
You need to be logged in to leave comments. Login now