@@ -1,768 +1,769 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2011 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class QueryTest < ActiveSupport::TestCase |
|
21 | 21 | fixtures :projects, :enabled_modules, :users, :members, |
|
22 | 22 | :member_roles, :roles, :trackers, :issue_statuses, |
|
23 | 23 | :issue_categories, :enumerations, :issues, |
|
24 | 24 | :watchers, :custom_fields, :custom_values, :versions, |
|
25 | :queries | |
|
25 | :queries, | |
|
26 | :projects_trackers | |
|
26 | 27 | |
|
27 | 28 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
28 | 29 | query = Query.new(:project => nil, :name => '_') |
|
29 | 30 | assert query.available_filters.has_key?('cf_1') |
|
30 | 31 | assert !query.available_filters.has_key?('cf_3') |
|
31 | 32 | end |
|
32 | 33 | |
|
33 | 34 | def test_system_shared_versions_should_be_available_in_global_queries |
|
34 | 35 | Version.find(2).update_attribute :sharing, 'system' |
|
35 | 36 | query = Query.new(:project => nil, :name => '_') |
|
36 | 37 | assert query.available_filters.has_key?('fixed_version_id') |
|
37 | 38 | assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'} |
|
38 | 39 | end |
|
39 | 40 | |
|
40 | 41 | def test_project_filter_in_global_queries |
|
41 | 42 | query = Query.new(:project => nil, :name => '_') |
|
42 | 43 | project_filter = query.available_filters["project_id"] |
|
43 | 44 | assert_not_nil project_filter |
|
44 | 45 | project_ids = project_filter[:values].map{|p| p[1]} |
|
45 | 46 | assert project_ids.include?("1") #public project |
|
46 | 47 | assert !project_ids.include?("2") #private project user cannot see |
|
47 | 48 | end |
|
48 | 49 | |
|
49 | 50 | def find_issues_with_query(query) |
|
50 | 51 | Issue.find :all, |
|
51 | 52 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
52 | 53 | :conditions => query.statement |
|
53 | 54 | end |
|
54 | 55 | |
|
55 | 56 | def assert_find_issues_with_query_is_successful(query) |
|
56 | 57 | assert_nothing_raised do |
|
57 | 58 | find_issues_with_query(query) |
|
58 | 59 | end |
|
59 | 60 | end |
|
60 | 61 | |
|
61 | 62 | def assert_query_statement_includes(query, condition) |
|
62 | 63 | assert query.statement.include?(condition), "Query statement condition not found in: #{query.statement}" |
|
63 | 64 | end |
|
64 | 65 | |
|
65 | 66 | def test_query_should_allow_shared_versions_for_a_project_query |
|
66 | 67 | subproject_version = Version.find(4) |
|
67 | 68 | query = Query.new(:project => Project.find(1), :name => '_') |
|
68 | 69 | query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s]) |
|
69 | 70 | |
|
70 | 71 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')") |
|
71 | 72 | end |
|
72 | 73 | |
|
73 | 74 | def test_query_with_multiple_custom_fields |
|
74 | 75 | query = Query.find(1) |
|
75 | 76 | assert query.valid? |
|
76 | 77 | assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") |
|
77 | 78 | issues = find_issues_with_query(query) |
|
78 | 79 | assert_equal 1, issues.length |
|
79 | 80 | assert_equal Issue.find(3), issues.first |
|
80 | 81 | end |
|
81 | 82 | |
|
82 | 83 | def test_operator_none |
|
83 | 84 | query = Query.new(:project => Project.find(1), :name => '_') |
|
84 | 85 | query.add_filter('fixed_version_id', '!*', ['']) |
|
85 | 86 | query.add_filter('cf_1', '!*', ['']) |
|
86 | 87 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") |
|
87 | 88 | assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") |
|
88 | 89 | find_issues_with_query(query) |
|
89 | 90 | end |
|
90 | 91 | |
|
91 | 92 | def test_operator_none_for_integer |
|
92 | 93 | query = Query.new(:project => Project.find(1), :name => '_') |
|
93 | 94 | query.add_filter('estimated_hours', '!*', ['']) |
|
94 | 95 | issues = find_issues_with_query(query) |
|
95 | 96 | assert !issues.empty? |
|
96 | 97 | assert issues.all? {|i| !i.estimated_hours} |
|
97 | 98 | end |
|
98 | 99 | |
|
99 | 100 | def test_operator_all |
|
100 | 101 | query = Query.new(:project => Project.find(1), :name => '_') |
|
101 | 102 | query.add_filter('fixed_version_id', '*', ['']) |
|
102 | 103 | query.add_filter('cf_1', '*', ['']) |
|
103 | 104 | assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") |
|
104 | 105 | assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") |
|
105 | 106 | find_issues_with_query(query) |
|
106 | 107 | end |
|
107 | 108 | |
|
108 | 109 | def test_numeric_filter_should_not_accept_non_numeric_values |
|
109 | 110 | query = Query.new(:name => '_') |
|
110 | 111 | query.add_filter('estimated_hours', '=', ['a']) |
|
111 | 112 | |
|
112 | 113 | assert query.has_filter?('estimated_hours') |
|
113 | 114 | assert !query.valid? |
|
114 | 115 | end |
|
115 | 116 | |
|
116 | 117 | def test_operator_is_on_float |
|
117 | 118 | Issue.update_all("estimated_hours = 171.2", "id=2") |
|
118 | 119 | |
|
119 | 120 | query = Query.new(:name => '_') |
|
120 | 121 | query.add_filter('estimated_hours', '=', ['171.20']) |
|
121 | 122 | issues = find_issues_with_query(query) |
|
122 | 123 | assert_equal 1, issues.size |
|
123 | 124 | assert_equal 2, issues.first.id |
|
124 | 125 | end |
|
125 | 126 | |
|
126 | 127 | def test_operator_greater_than |
|
127 | 128 | query = Query.new(:project => Project.find(1), :name => '_') |
|
128 | 129 | query.add_filter('done_ratio', '>=', ['40']) |
|
129 | 130 | assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0") |
|
130 | 131 | find_issues_with_query(query) |
|
131 | 132 | end |
|
132 | 133 | |
|
133 | 134 | def test_operator_greater_than_a_float |
|
134 | 135 | query = Query.new(:project => Project.find(1), :name => '_') |
|
135 | 136 | query.add_filter('estimated_hours', '>=', ['40.5']) |
|
136 | 137 | assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5") |
|
137 | 138 | find_issues_with_query(query) |
|
138 | 139 | end |
|
139 | 140 | |
|
140 | 141 | def test_operator_greater_than_on_custom_field |
|
141 | 142 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
142 | 143 | query = Query.new(:project => Project.find(1), :name => '_') |
|
143 | 144 | query.add_filter("cf_#{f.id}", '>=', ['40']) |
|
144 | 145 | assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) >= 40.0") |
|
145 | 146 | find_issues_with_query(query) |
|
146 | 147 | end |
|
147 | 148 | |
|
148 | 149 | def test_operator_lesser_than |
|
149 | 150 | query = Query.new(:project => Project.find(1), :name => '_') |
|
150 | 151 | query.add_filter('done_ratio', '<=', ['30']) |
|
151 | 152 | assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0") |
|
152 | 153 | find_issues_with_query(query) |
|
153 | 154 | end |
|
154 | 155 | |
|
155 | 156 | def test_operator_lesser_than_on_custom_field |
|
156 | 157 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
157 | 158 | query = Query.new(:project => Project.find(1), :name => '_') |
|
158 | 159 | query.add_filter("cf_#{f.id}", '<=', ['30']) |
|
159 | 160 | assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30.0") |
|
160 | 161 | find_issues_with_query(query) |
|
161 | 162 | end |
|
162 | 163 | |
|
163 | 164 | def test_operator_between |
|
164 | 165 | query = Query.new(:project => Project.find(1), :name => '_') |
|
165 | 166 | query.add_filter('done_ratio', '><', ['30', '40']) |
|
166 | 167 | assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement |
|
167 | 168 | find_issues_with_query(query) |
|
168 | 169 | end |
|
169 | 170 | |
|
170 | 171 | def test_operator_between_on_custom_field |
|
171 | 172 | f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) |
|
172 | 173 | query = Query.new(:project => Project.find(1), :name => '_') |
|
173 | 174 | query.add_filter("cf_#{f.id}", '><', ['30', '40']) |
|
174 | 175 | assert_include "CAST(custom_values.value AS decimal(60,3)) BETWEEN 30.0 AND 40.0", query.statement |
|
175 | 176 | find_issues_with_query(query) |
|
176 | 177 | end |
|
177 | 178 | |
|
178 | 179 | def test_date_filter_should_not_accept_non_date_values |
|
179 | 180 | query = Query.new(:name => '_') |
|
180 | 181 | query.add_filter('created_on', '=', ['a']) |
|
181 | 182 | |
|
182 | 183 | assert query.has_filter?('created_on') |
|
183 | 184 | assert !query.valid? |
|
184 | 185 | end |
|
185 | 186 | |
|
186 | 187 | def test_date_filter_should_not_accept_invalid_date_values |
|
187 | 188 | query = Query.new(:name => '_') |
|
188 | 189 | query.add_filter('created_on', '=', ['2011-01-34']) |
|
189 | 190 | |
|
190 | 191 | assert query.has_filter?('created_on') |
|
191 | 192 | assert !query.valid? |
|
192 | 193 | end |
|
193 | 194 | |
|
194 | 195 | def test_relative_date_filter_should_not_accept_non_integer_values |
|
195 | 196 | query = Query.new(:name => '_') |
|
196 | 197 | query.add_filter('created_on', '>t-', ['a']) |
|
197 | 198 | |
|
198 | 199 | assert query.has_filter?('created_on') |
|
199 | 200 | assert !query.valid? |
|
200 | 201 | end |
|
201 | 202 | |
|
202 | 203 | def test_operator_date_equals |
|
203 | 204 | query = Query.new(:name => '_') |
|
204 | 205 | query.add_filter('due_date', '=', ['2011-07-10']) |
|
205 | 206 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
206 | 207 | find_issues_with_query(query) |
|
207 | 208 | end |
|
208 | 209 | |
|
209 | 210 | def test_operator_date_lesser_than |
|
210 | 211 | query = Query.new(:name => '_') |
|
211 | 212 | query.add_filter('due_date', '<=', ['2011-07-10']) |
|
212 | 213 | assert_match /issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
213 | 214 | find_issues_with_query(query) |
|
214 | 215 | end |
|
215 | 216 | |
|
216 | 217 | def test_operator_date_greater_than |
|
217 | 218 | query = Query.new(:name => '_') |
|
218 | 219 | query.add_filter('due_date', '>=', ['2011-07-10']) |
|
219 | 220 | assert_match /issues\.due_date > '2011-07-09 23:59:59(\.9+)?'/, query.statement |
|
220 | 221 | find_issues_with_query(query) |
|
221 | 222 | end |
|
222 | 223 | |
|
223 | 224 | def test_operator_date_between |
|
224 | 225 | query = Query.new(:name => '_') |
|
225 | 226 | query.add_filter('due_date', '><', ['2011-06-23', '2011-07-10']) |
|
226 | 227 | assert_match /issues\.due_date > '2011-06-22 23:59:59(\.9+)?' AND issues\.due_date <= '2011-07-10 23:59:59(\.9+)?/, query.statement |
|
227 | 228 | find_issues_with_query(query) |
|
228 | 229 | end |
|
229 | 230 | |
|
230 | 231 | def test_operator_in_more_than |
|
231 | 232 | Issue.find(7).update_attribute(:due_date, (Date.today + 15)) |
|
232 | 233 | query = Query.new(:project => Project.find(1), :name => '_') |
|
233 | 234 | query.add_filter('due_date', '>t+', ['15']) |
|
234 | 235 | issues = find_issues_with_query(query) |
|
235 | 236 | assert !issues.empty? |
|
236 | 237 | issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} |
|
237 | 238 | end |
|
238 | 239 | |
|
239 | 240 | def test_operator_in_less_than |
|
240 | 241 | query = Query.new(:project => Project.find(1), :name => '_') |
|
241 | 242 | query.add_filter('due_date', '<t+', ['15']) |
|
242 | 243 | issues = find_issues_with_query(query) |
|
243 | 244 | assert !issues.empty? |
|
244 | 245 | issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} |
|
245 | 246 | end |
|
246 | 247 | |
|
247 | 248 | def test_operator_less_than_ago |
|
248 | 249 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
249 | 250 | query = Query.new(:project => Project.find(1), :name => '_') |
|
250 | 251 | query.add_filter('due_date', '>t-', ['3']) |
|
251 | 252 | issues = find_issues_with_query(query) |
|
252 | 253 | assert !issues.empty? |
|
253 | 254 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} |
|
254 | 255 | end |
|
255 | 256 | |
|
256 | 257 | def test_operator_more_than_ago |
|
257 | 258 | Issue.find(7).update_attribute(:due_date, (Date.today - 10)) |
|
258 | 259 | query = Query.new(:project => Project.find(1), :name => '_') |
|
259 | 260 | query.add_filter('due_date', '<t-', ['10']) |
|
260 | 261 | assert query.statement.include?("#{Issue.table_name}.due_date <=") |
|
261 | 262 | issues = find_issues_with_query(query) |
|
262 | 263 | assert !issues.empty? |
|
263 | 264 | issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} |
|
264 | 265 | end |
|
265 | 266 | |
|
266 | 267 | def test_operator_in |
|
267 | 268 | Issue.find(7).update_attribute(:due_date, (Date.today + 2)) |
|
268 | 269 | query = Query.new(:project => Project.find(1), :name => '_') |
|
269 | 270 | query.add_filter('due_date', 't+', ['2']) |
|
270 | 271 | issues = find_issues_with_query(query) |
|
271 | 272 | assert !issues.empty? |
|
272 | 273 | issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} |
|
273 | 274 | end |
|
274 | 275 | |
|
275 | 276 | def test_operator_ago |
|
276 | 277 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) |
|
277 | 278 | query = Query.new(:project => Project.find(1), :name => '_') |
|
278 | 279 | query.add_filter('due_date', 't-', ['3']) |
|
279 | 280 | issues = find_issues_with_query(query) |
|
280 | 281 | assert !issues.empty? |
|
281 | 282 | issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} |
|
282 | 283 | end |
|
283 | 284 | |
|
284 | 285 | def test_operator_today |
|
285 | 286 | query = Query.new(:project => Project.find(1), :name => '_') |
|
286 | 287 | query.add_filter('due_date', 't', ['']) |
|
287 | 288 | issues = find_issues_with_query(query) |
|
288 | 289 | assert !issues.empty? |
|
289 | 290 | issues.each {|issue| assert_equal Date.today, issue.due_date} |
|
290 | 291 | end |
|
291 | 292 | |
|
292 | 293 | def test_operator_this_week_on_date |
|
293 | 294 | query = Query.new(:project => Project.find(1), :name => '_') |
|
294 | 295 | query.add_filter('due_date', 'w', ['']) |
|
295 | 296 | find_issues_with_query(query) |
|
296 | 297 | end |
|
297 | 298 | |
|
298 | 299 | def test_operator_this_week_on_datetime |
|
299 | 300 | query = Query.new(:project => Project.find(1), :name => '_') |
|
300 | 301 | query.add_filter('created_on', 'w', ['']) |
|
301 | 302 | find_issues_with_query(query) |
|
302 | 303 | end |
|
303 | 304 | |
|
304 | 305 | def test_operator_contains |
|
305 | 306 | query = Query.new(:project => Project.find(1), :name => '_') |
|
306 | 307 | query.add_filter('subject', '~', ['uNable']) |
|
307 | 308 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'") |
|
308 | 309 | result = find_issues_with_query(query) |
|
309 | 310 | assert result.empty? |
|
310 | 311 | result.each {|issue| assert issue.subject.downcase.include?('unable') } |
|
311 | 312 | end |
|
312 | 313 | |
|
313 | 314 | def test_range_for_this_week_with_week_starting_on_monday |
|
314 | 315 | I18n.locale = :fr |
|
315 | 316 | assert_equal '1', I18n.t(:general_first_day_of_week) |
|
316 | 317 | |
|
317 | 318 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
318 | 319 | |
|
319 | 320 | query = Query.new(:project => Project.find(1), :name => '_') |
|
320 | 321 | query.add_filter('due_date', 'w', ['']) |
|
321 | 322 | assert query.statement.match(/issues\.due_date > '2011-04-24 23:59:59(\.9+)?' AND issues\.due_date <= '2011-05-01 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
322 | 323 | I18n.locale = :en |
|
323 | 324 | end |
|
324 | 325 | |
|
325 | 326 | def test_range_for_this_week_with_week_starting_on_sunday |
|
326 | 327 | I18n.locale = :en |
|
327 | 328 | assert_equal '7', I18n.t(:general_first_day_of_week) |
|
328 | 329 | |
|
329 | 330 | Date.stubs(:today).returns(Date.parse('2011-04-29')) |
|
330 | 331 | |
|
331 | 332 | query = Query.new(:project => Project.find(1), :name => '_') |
|
332 | 333 | query.add_filter('due_date', 'w', ['']) |
|
333 | 334 | assert query.statement.match(/issues\.due_date > '2011-04-23 23:59:59(\.9+)?' AND issues\.due_date <= '2011-04-30 23:59:59(\.9+)?/), "range not found in #{query.statement}" |
|
334 | 335 | end |
|
335 | 336 | |
|
336 | 337 | def test_operator_does_not_contains |
|
337 | 338 | query = Query.new(:project => Project.find(1), :name => '_') |
|
338 | 339 | query.add_filter('subject', '!~', ['uNable']) |
|
339 | 340 | assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'") |
|
340 | 341 | find_issues_with_query(query) |
|
341 | 342 | end |
|
342 | 343 | |
|
343 | 344 | def test_filter_assigned_to_me |
|
344 | 345 | user = User.find(2) |
|
345 | 346 | group = Group.find(10) |
|
346 | 347 | User.current = user |
|
347 | 348 | i1 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => user) |
|
348 | 349 | i2 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => group) |
|
349 | 350 | i3 = Issue.generate!(:project_id => 1, :tracker_id => 1, :assigned_to => Group.find(11)) |
|
350 | 351 | group.users << user |
|
351 | 352 | |
|
352 | 353 | query = Query.new(:name => '_', :filters => { 'assigned_to_id' => {:operator => '=', :values => ['me']}}) |
|
353 | 354 | result = query.issues |
|
354 | 355 | assert_equal Issue.visible.all(:conditions => {:assigned_to_id => ([2] + user.reload.group_ids)}).sort_by(&:id), result.sort_by(&:id) |
|
355 | 356 | |
|
356 | 357 | assert result.include?(i1) |
|
357 | 358 | assert result.include?(i2) |
|
358 | 359 | assert !result.include?(i3) |
|
359 | 360 | end |
|
360 | 361 | |
|
361 | 362 | def test_filter_watched_issues |
|
362 | 363 | User.current = User.find(1) |
|
363 | 364 | query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) |
|
364 | 365 | result = find_issues_with_query(query) |
|
365 | 366 | assert_not_nil result |
|
366 | 367 | assert !result.empty? |
|
367 | 368 | assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) |
|
368 | 369 | User.current = nil |
|
369 | 370 | end |
|
370 | 371 | |
|
371 | 372 | def test_filter_unwatched_issues |
|
372 | 373 | User.current = User.find(1) |
|
373 | 374 | query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) |
|
374 | 375 | result = find_issues_with_query(query) |
|
375 | 376 | assert_not_nil result |
|
376 | 377 | assert !result.empty? |
|
377 | 378 | assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) |
|
378 | 379 | User.current = nil |
|
379 | 380 | end |
|
380 | 381 | |
|
381 | 382 | def test_statement_should_be_nil_with_no_filters |
|
382 | 383 | q = Query.new(:name => '_') |
|
383 | 384 | q.filters = {} |
|
384 | 385 | |
|
385 | 386 | assert q.valid? |
|
386 | 387 | assert_nil q.statement |
|
387 | 388 | end |
|
388 | 389 | |
|
389 | 390 | def test_default_columns |
|
390 | 391 | q = Query.new |
|
391 | 392 | assert !q.columns.empty? |
|
392 | 393 | end |
|
393 | 394 | |
|
394 | 395 | def test_set_column_names |
|
395 | 396 | q = Query.new |
|
396 | 397 | q.column_names = ['tracker', :subject, '', 'unknonw_column'] |
|
397 | 398 | assert_equal [:tracker, :subject], q.columns.collect {|c| c.name} |
|
398 | 399 | c = q.columns.first |
|
399 | 400 | assert q.has_column?(c) |
|
400 | 401 | end |
|
401 | 402 | |
|
402 | 403 | def test_groupable_columns_should_include_custom_fields |
|
403 | 404 | q = Query.new |
|
404 | 405 | assert q.groupable_columns.detect {|c| c.is_a? QueryCustomFieldColumn} |
|
405 | 406 | end |
|
406 | 407 | |
|
407 | 408 | def test_grouped_with_valid_column |
|
408 | 409 | q = Query.new(:group_by => 'status') |
|
409 | 410 | assert q.grouped? |
|
410 | 411 | assert_not_nil q.group_by_column |
|
411 | 412 | assert_equal :status, q.group_by_column.name |
|
412 | 413 | assert_not_nil q.group_by_statement |
|
413 | 414 | assert_equal 'status', q.group_by_statement |
|
414 | 415 | end |
|
415 | 416 | |
|
416 | 417 | def test_grouped_with_invalid_column |
|
417 | 418 | q = Query.new(:group_by => 'foo') |
|
418 | 419 | assert !q.grouped? |
|
419 | 420 | assert_nil q.group_by_column |
|
420 | 421 | assert_nil q.group_by_statement |
|
421 | 422 | end |
|
422 | 423 | |
|
423 | 424 | def test_default_sort |
|
424 | 425 | q = Query.new |
|
425 | 426 | assert_equal [], q.sort_criteria |
|
426 | 427 | end |
|
427 | 428 | |
|
428 | 429 | def test_set_sort_criteria_with_hash |
|
429 | 430 | q = Query.new |
|
430 | 431 | q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} |
|
431 | 432 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
432 | 433 | end |
|
433 | 434 | |
|
434 | 435 | def test_set_sort_criteria_with_array |
|
435 | 436 | q = Query.new |
|
436 | 437 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
437 | 438 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
438 | 439 | end |
|
439 | 440 | |
|
440 | 441 | def test_create_query_with_sort |
|
441 | 442 | q = Query.new(:name => 'Sorted') |
|
442 | 443 | q.sort_criteria = [['priority', 'desc'], 'tracker'] |
|
443 | 444 | assert q.save |
|
444 | 445 | q.reload |
|
445 | 446 | assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria |
|
446 | 447 | end |
|
447 | 448 | |
|
448 | 449 | def test_sort_by_string_custom_field_asc |
|
449 | 450 | q = Query.new |
|
450 | 451 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
451 | 452 | assert c |
|
452 | 453 | assert c.sortable |
|
453 | 454 | issues = Issue.find :all, |
|
454 | 455 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
455 | 456 | :conditions => q.statement, |
|
456 | 457 | :order => "#{c.sortable} ASC" |
|
457 | 458 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
458 | 459 | assert !values.empty? |
|
459 | 460 | assert_equal values.sort, values |
|
460 | 461 | end |
|
461 | 462 | |
|
462 | 463 | def test_sort_by_string_custom_field_desc |
|
463 | 464 | q = Query.new |
|
464 | 465 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } |
|
465 | 466 | assert c |
|
466 | 467 | assert c.sortable |
|
467 | 468 | issues = Issue.find :all, |
|
468 | 469 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
469 | 470 | :conditions => q.statement, |
|
470 | 471 | :order => "#{c.sortable} DESC" |
|
471 | 472 | values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} |
|
472 | 473 | assert !values.empty? |
|
473 | 474 | assert_equal values.sort.reverse, values |
|
474 | 475 | end |
|
475 | 476 | |
|
476 | 477 | def test_sort_by_float_custom_field_asc |
|
477 | 478 | q = Query.new |
|
478 | 479 | c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' } |
|
479 | 480 | assert c |
|
480 | 481 | assert c.sortable |
|
481 | 482 | issues = Issue.find :all, |
|
482 | 483 | :include => [ :assigned_to, :status, :tracker, :project, :priority ], |
|
483 | 484 | :conditions => q.statement, |
|
484 | 485 | :order => "#{c.sortable} ASC" |
|
485 | 486 | values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact |
|
486 | 487 | assert !values.empty? |
|
487 | 488 | assert_equal values.sort, values |
|
488 | 489 | end |
|
489 | 490 | |
|
490 | 491 | def test_invalid_query_should_raise_query_statement_invalid_error |
|
491 | 492 | q = Query.new |
|
492 | 493 | assert_raise Query::StatementInvalid do |
|
493 | 494 | q.issues(:conditions => "foo = 1") |
|
494 | 495 | end |
|
495 | 496 | end |
|
496 | 497 | |
|
497 | 498 | def test_issue_count |
|
498 | 499 | q = Query.new(:name => '_') |
|
499 | 500 | issue_count = q.issue_count |
|
500 | 501 | assert_equal q.issues.size, issue_count |
|
501 | 502 | end |
|
502 | 503 | |
|
503 | 504 | def test_issue_count_with_archived_issues |
|
504 | 505 | p = Project.generate!( :status => Project::STATUS_ARCHIVED ) |
|
505 | 506 | i = Issue.generate!( :project => p, :tracker => p.trackers.first ) |
|
506 | 507 | assert !i.visible? |
|
507 | 508 | |
|
508 | 509 | test_issue_count |
|
509 | 510 | end |
|
510 | 511 | |
|
511 | 512 | def test_issue_count_by_association_group |
|
512 | 513 | q = Query.new(:name => '_', :group_by => 'assigned_to') |
|
513 | 514 | count_by_group = q.issue_count_by_group |
|
514 | 515 | assert_kind_of Hash, count_by_group |
|
515 | 516 | assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
516 | 517 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
517 | 518 | assert count_by_group.has_key?(User.find(3)) |
|
518 | 519 | end |
|
519 | 520 | |
|
520 | 521 | def test_issue_count_by_list_custom_field_group |
|
521 | 522 | q = Query.new(:name => '_', :group_by => 'cf_1') |
|
522 | 523 | count_by_group = q.issue_count_by_group |
|
523 | 524 | assert_kind_of Hash, count_by_group |
|
524 | 525 | assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
525 | 526 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
526 | 527 | assert count_by_group.has_key?('MySQL') |
|
527 | 528 | end |
|
528 | 529 | |
|
529 | 530 | def test_issue_count_by_date_custom_field_group |
|
530 | 531 | q = Query.new(:name => '_', :group_by => 'cf_8') |
|
531 | 532 | count_by_group = q.issue_count_by_group |
|
532 | 533 | assert_kind_of Hash, count_by_group |
|
533 | 534 | assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort |
|
534 | 535 | assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq |
|
535 | 536 | end |
|
536 | 537 | |
|
537 | 538 | def test_label_for |
|
538 | 539 | q = Query.new |
|
539 | 540 | assert_equal 'assigned_to', q.label_for('assigned_to_id') |
|
540 | 541 | end |
|
541 | 542 | |
|
542 | 543 | def test_editable_by |
|
543 | 544 | admin = User.find(1) |
|
544 | 545 | manager = User.find(2) |
|
545 | 546 | developer = User.find(3) |
|
546 | 547 | |
|
547 | 548 | # Public query on project 1 |
|
548 | 549 | q = Query.find(1) |
|
549 | 550 | assert q.editable_by?(admin) |
|
550 | 551 | assert q.editable_by?(manager) |
|
551 | 552 | assert !q.editable_by?(developer) |
|
552 | 553 | |
|
553 | 554 | # Private query on project 1 |
|
554 | 555 | q = Query.find(2) |
|
555 | 556 | assert q.editable_by?(admin) |
|
556 | 557 | assert !q.editable_by?(manager) |
|
557 | 558 | assert q.editable_by?(developer) |
|
558 | 559 | |
|
559 | 560 | # Private query for all projects |
|
560 | 561 | q = Query.find(3) |
|
561 | 562 | assert q.editable_by?(admin) |
|
562 | 563 | assert !q.editable_by?(manager) |
|
563 | 564 | assert q.editable_by?(developer) |
|
564 | 565 | |
|
565 | 566 | # Public query for all projects |
|
566 | 567 | q = Query.find(4) |
|
567 | 568 | assert q.editable_by?(admin) |
|
568 | 569 | assert !q.editable_by?(manager) |
|
569 | 570 | assert !q.editable_by?(developer) |
|
570 | 571 | end |
|
571 | 572 | |
|
572 | 573 | def test_visible_scope |
|
573 | 574 | query_ids = Query.visible(User.anonymous).map(&:id) |
|
574 | 575 | |
|
575 | 576 | assert query_ids.include?(1), 'public query on public project was not visible' |
|
576 | 577 | assert query_ids.include?(4), 'public query for all projects was not visible' |
|
577 | 578 | assert !query_ids.include?(2), 'private query on public project was visible' |
|
578 | 579 | assert !query_ids.include?(3), 'private query for all projects was visible' |
|
579 | 580 | assert !query_ids.include?(7), 'public query on private project was visible' |
|
580 | 581 | end |
|
581 | 582 | |
|
582 | 583 | context "#available_filters" do |
|
583 | 584 | setup do |
|
584 | 585 | @query = Query.new(:name => "_") |
|
585 | 586 | end |
|
586 | 587 | |
|
587 | 588 | should "include users of visible projects in cross-project view" do |
|
588 | 589 | users = @query.available_filters["assigned_to_id"] |
|
589 | 590 | assert_not_nil users |
|
590 | 591 | assert users[:values].map{|u|u[1]}.include?("3") |
|
591 | 592 | end |
|
592 | 593 | |
|
593 | 594 | should "include visible projects in cross-project view" do |
|
594 | 595 | projects = @query.available_filters["project_id"] |
|
595 | 596 | assert_not_nil projects |
|
596 | 597 | assert projects[:values].map{|u|u[1]}.include?("1") |
|
597 | 598 | end |
|
598 | 599 | |
|
599 | 600 | context "'member_of_group' filter" do |
|
600 | 601 | should "be present" do |
|
601 | 602 | assert @query.available_filters.keys.include?("member_of_group") |
|
602 | 603 | end |
|
603 | 604 | |
|
604 | 605 | should "be an optional list" do |
|
605 | 606 | assert_equal :list_optional, @query.available_filters["member_of_group"][:type] |
|
606 | 607 | end |
|
607 | 608 | |
|
608 | 609 | should "have a list of the groups as values" do |
|
609 | 610 | Group.destroy_all # No fixtures |
|
610 | 611 | group1 = Group.generate!.reload |
|
611 | 612 | group2 = Group.generate!.reload |
|
612 | 613 | |
|
613 | 614 | expected_group_list = [ |
|
614 | 615 | [group1.name, group1.id.to_s], |
|
615 | 616 | [group2.name, group2.id.to_s] |
|
616 | 617 | ] |
|
617 | 618 | assert_equal expected_group_list.sort, @query.available_filters["member_of_group"][:values].sort |
|
618 | 619 | end |
|
619 | 620 | |
|
620 | 621 | end |
|
621 | 622 | |
|
622 | 623 | context "'assigned_to_role' filter" do |
|
623 | 624 | should "be present" do |
|
624 | 625 | assert @query.available_filters.keys.include?("assigned_to_role") |
|
625 | 626 | end |
|
626 | 627 | |
|
627 | 628 | should "be an optional list" do |
|
628 | 629 | assert_equal :list_optional, @query.available_filters["assigned_to_role"][:type] |
|
629 | 630 | end |
|
630 | 631 | |
|
631 | 632 | should "have a list of the Roles as values" do |
|
632 | 633 | assert @query.available_filters["assigned_to_role"][:values].include?(['Manager','1']) |
|
633 | 634 | assert @query.available_filters["assigned_to_role"][:values].include?(['Developer','2']) |
|
634 | 635 | assert @query.available_filters["assigned_to_role"][:values].include?(['Reporter','3']) |
|
635 | 636 | end |
|
636 | 637 | |
|
637 | 638 | should "not include the built in Roles as values" do |
|
638 | 639 | assert ! @query.available_filters["assigned_to_role"][:values].include?(['Non member','4']) |
|
639 | 640 | assert ! @query.available_filters["assigned_to_role"][:values].include?(['Anonymous','5']) |
|
640 | 641 | end |
|
641 | 642 | |
|
642 | 643 | end |
|
643 | 644 | |
|
644 | 645 | end |
|
645 | 646 | |
|
646 | 647 | context "#statement" do |
|
647 | 648 | context "with 'member_of_group' filter" do |
|
648 | 649 | setup do |
|
649 | 650 | Group.destroy_all # No fixtures |
|
650 | 651 | @user_in_group = User.generate! |
|
651 | 652 | @second_user_in_group = User.generate! |
|
652 | 653 | @user_in_group2 = User.generate! |
|
653 | 654 | @user_not_in_group = User.generate! |
|
654 | 655 | |
|
655 | 656 | @group = Group.generate!.reload |
|
656 | 657 | @group.users << @user_in_group |
|
657 | 658 | @group.users << @second_user_in_group |
|
658 | 659 | |
|
659 | 660 | @group2 = Group.generate!.reload |
|
660 | 661 | @group2.users << @user_in_group2 |
|
661 | 662 | |
|
662 | 663 | end |
|
663 | 664 | |
|
664 | 665 | should "search assigned to for users in the group" do |
|
665 | 666 | @query = Query.new(:name => '_') |
|
666 | 667 | @query.add_filter('member_of_group', '=', [@group.id.to_s]) |
|
667 | 668 | |
|
668 | 669 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}')" |
|
669 | 670 | assert_find_issues_with_query_is_successful @query |
|
670 | 671 | end |
|
671 | 672 | |
|
672 | 673 | should "search not assigned to any group member (none)" do |
|
673 | 674 | @query = Query.new(:name => '_') |
|
674 | 675 | @query.add_filter('member_of_group', '!*', ['']) |
|
675 | 676 | |
|
676 | 677 | # Users not in a group |
|
677 | 678 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}')" |
|
678 | 679 | assert_find_issues_with_query_is_successful @query |
|
679 | 680 | end |
|
680 | 681 | |
|
681 | 682 | should "search assigned to any group member (all)" do |
|
682 | 683 | @query = Query.new(:name => '_') |
|
683 | 684 | @query.add_filter('member_of_group', '*', ['']) |
|
684 | 685 | |
|
685 | 686 | # Only users in a group |
|
686 | 687 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@user_in_group.id}','#{@second_user_in_group.id}','#{@user_in_group2.id}')" |
|
687 | 688 | assert_find_issues_with_query_is_successful @query |
|
688 | 689 | end |
|
689 | 690 | |
|
690 | 691 | should "return an empty set with = empty group" do |
|
691 | 692 | @empty_group = Group.generate! |
|
692 | 693 | @query = Query.new(:name => '_') |
|
693 | 694 | @query.add_filter('member_of_group', '=', [@empty_group.id.to_s]) |
|
694 | 695 | |
|
695 | 696 | assert_equal [], find_issues_with_query(@query) |
|
696 | 697 | end |
|
697 | 698 | |
|
698 | 699 | should "return issues with ! empty group" do |
|
699 | 700 | @empty_group = Group.generate! |
|
700 | 701 | @query = Query.new(:name => '_') |
|
701 | 702 | @query.add_filter('member_of_group', '!', [@empty_group.id.to_s]) |
|
702 | 703 | |
|
703 | 704 | assert_find_issues_with_query_is_successful @query |
|
704 | 705 | end |
|
705 | 706 | end |
|
706 | 707 | |
|
707 | 708 | context "with 'assigned_to_role' filter" do |
|
708 | 709 | setup do |
|
709 | 710 | # No fixtures |
|
710 | 711 | MemberRole.delete_all |
|
711 | 712 | Member.delete_all |
|
712 | 713 | Role.delete_all |
|
713 | 714 | |
|
714 | 715 | @manager_role = Role.generate!(:name => 'Manager') |
|
715 | 716 | @developer_role = Role.generate!(:name => 'Developer') |
|
716 | 717 | |
|
717 | 718 | @project = Project.generate! |
|
718 | 719 | @manager = User.generate! |
|
719 | 720 | @developer = User.generate! |
|
720 | 721 | @boss = User.generate! |
|
721 | 722 | User.add_to_project(@manager, @project, @manager_role) |
|
722 | 723 | User.add_to_project(@developer, @project, @developer_role) |
|
723 | 724 | User.add_to_project(@boss, @project, [@manager_role, @developer_role]) |
|
724 | 725 | end |
|
725 | 726 | |
|
726 | 727 | should "search assigned to for users with the Role" do |
|
727 | 728 | @query = Query.new(:name => '_') |
|
728 | 729 | @query.add_filter('assigned_to_role', '=', [@manager_role.id.to_s]) |
|
729 | 730 | |
|
730 | 731 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@manager.id}','#{@boss.id}')" |
|
731 | 732 | assert_find_issues_with_query_is_successful @query |
|
732 | 733 | end |
|
733 | 734 | |
|
734 | 735 | should "search assigned to for users not assigned to any Role (none)" do |
|
735 | 736 | @query = Query.new(:name => '_') |
|
736 | 737 | @query.add_filter('assigned_to_role', '!*', ['']) |
|
737 | 738 | |
|
738 | 739 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IS NULL OR #{Issue.table_name}.assigned_to_id NOT IN ('#{@manager.id}','#{@developer.id}','#{@boss.id}')" |
|
739 | 740 | assert_find_issues_with_query_is_successful @query |
|
740 | 741 | end |
|
741 | 742 | |
|
742 | 743 | should "search assigned to for users assigned to any Role (all)" do |
|
743 | 744 | @query = Query.new(:name => '_') |
|
744 | 745 | @query.add_filter('assigned_to_role', '*', ['']) |
|
745 | 746 | |
|
746 | 747 | assert_query_statement_includes @query, "#{Issue.table_name}.assigned_to_id IN ('#{@manager.id}','#{@developer.id}','#{@boss.id}')" |
|
747 | 748 | assert_find_issues_with_query_is_successful @query |
|
748 | 749 | end |
|
749 | 750 | |
|
750 | 751 | should "return an empty set with empty role" do |
|
751 | 752 | @empty_role = Role.generate! |
|
752 | 753 | @query = Query.new(:name => '_') |
|
753 | 754 | @query.add_filter('assigned_to_role', '=', [@empty_role.id.to_s]) |
|
754 | 755 | |
|
755 | 756 | assert_equal [], find_issues_with_query(@query) |
|
756 | 757 | end |
|
757 | 758 | |
|
758 | 759 | should "return issues with ! empty role" do |
|
759 | 760 | @empty_role = Role.generate! |
|
760 | 761 | @query = Query.new(:name => '_') |
|
761 | 762 | @query.add_filter('member_of_group', '!', [@empty_role.id.to_s]) |
|
762 | 763 | |
|
763 | 764 | assert_find_issues_with_query_is_successful @query |
|
764 | 765 | end |
|
765 | 766 | end |
|
766 | 767 | end |
|
767 | 768 | |
|
768 | 769 | end |
General Comments 0
You need to be logged in to leave comments.
Login now