Mercurial > hg > soundsoftware-site
comparison test/unit/.svn/text-base/query_test.rb.svn-base @ 0:513646585e45
* Import Redmine trunk SVN rev 3859
author | Chris Cannam |
---|---|
date | Fri, 23 Jul 2010 15:52:44 +0100 |
parents | |
children | 1d32c0a0efbf |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:513646585e45 |
---|---|
1 # redMine - project management software | |
2 # Copyright (C) 2006-2008 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
14 # You should have received a copy of the GNU General Public License | |
15 # along with this program; if not, write to the Free Software | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require File.dirname(__FILE__) + '/../test_helper' | |
19 | |
20 class QueryTest < ActiveSupport::TestCase | |
21 fixtures :projects, :enabled_modules, :users, :members, :member_roles, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :watchers, :custom_fields, :custom_values, :versions, :queries | |
22 | |
23 def test_custom_fields_for_all_projects_should_be_available_in_global_queries | |
24 query = Query.new(:project => nil, :name => '_') | |
25 assert query.available_filters.has_key?('cf_1') | |
26 assert !query.available_filters.has_key?('cf_3') | |
27 end | |
28 | |
29 def test_system_shared_versions_should_be_available_in_global_queries | |
30 Version.find(2).update_attribute :sharing, 'system' | |
31 query = Query.new(:project => nil, :name => '_') | |
32 assert query.available_filters.has_key?('fixed_version_id') | |
33 assert query.available_filters['fixed_version_id'][:values].detect {|v| v.last == '2'} | |
34 end | |
35 | |
36 def find_issues_with_query(query) | |
37 Issue.find :all, | |
38 :include => [ :assigned_to, :status, :tracker, :project, :priority ], | |
39 :conditions => query.statement | |
40 end | |
41 | |
42 def test_query_should_allow_shared_versions_for_a_project_query | |
43 subproject_version = Version.find(4) | |
44 query = Query.new(:project => Project.find(1), :name => '_') | |
45 query.add_filter('fixed_version_id', '=', [subproject_version.id.to_s]) | |
46 | |
47 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IN ('4')") | |
48 end | |
49 | |
50 def test_query_with_multiple_custom_fields | |
51 query = Query.find(1) | |
52 assert query.valid? | |
53 assert query.statement.include?("#{CustomValue.table_name}.value IN ('MySQL')") | |
54 issues = find_issues_with_query(query) | |
55 assert_equal 1, issues.length | |
56 assert_equal Issue.find(3), issues.first | |
57 end | |
58 | |
59 def test_operator_none | |
60 query = Query.new(:project => Project.find(1), :name => '_') | |
61 query.add_filter('fixed_version_id', '!*', ['']) | |
62 query.add_filter('cf_1', '!*', ['']) | |
63 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NULL") | |
64 assert query.statement.include?("#{CustomValue.table_name}.value IS NULL OR #{CustomValue.table_name}.value = ''") | |
65 find_issues_with_query(query) | |
66 end | |
67 | |
68 def test_operator_none_for_integer | |
69 query = Query.new(:project => Project.find(1), :name => '_') | |
70 query.add_filter('estimated_hours', '!*', ['']) | |
71 issues = find_issues_with_query(query) | |
72 assert !issues.empty? | |
73 assert issues.all? {|i| !i.estimated_hours} | |
74 end | |
75 | |
76 def test_operator_all | |
77 query = Query.new(:project => Project.find(1), :name => '_') | |
78 query.add_filter('fixed_version_id', '*', ['']) | |
79 query.add_filter('cf_1', '*', ['']) | |
80 assert query.statement.include?("#{Issue.table_name}.fixed_version_id IS NOT NULL") | |
81 assert query.statement.include?("#{CustomValue.table_name}.value IS NOT NULL AND #{CustomValue.table_name}.value <> ''") | |
82 find_issues_with_query(query) | |
83 end | |
84 | |
85 def test_operator_greater_than | |
86 query = Query.new(:project => Project.find(1), :name => '_') | |
87 query.add_filter('done_ratio', '>=', ['40']) | |
88 assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40") | |
89 find_issues_with_query(query) | |
90 end | |
91 | |
92 def test_operator_in_more_than | |
93 Issue.find(7).update_attribute(:due_date, (Date.today + 15)) | |
94 query = Query.new(:project => Project.find(1), :name => '_') | |
95 query.add_filter('due_date', '>t+', ['15']) | |
96 issues = find_issues_with_query(query) | |
97 assert !issues.empty? | |
98 issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} | |
99 end | |
100 | |
101 def test_operator_in_less_than | |
102 query = Query.new(:project => Project.find(1), :name => '_') | |
103 query.add_filter('due_date', '<t+', ['15']) | |
104 issues = find_issues_with_query(query) | |
105 assert !issues.empty? | |
106 issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} | |
107 end | |
108 | |
109 def test_operator_less_than_ago | |
110 Issue.find(7).update_attribute(:due_date, (Date.today - 3)) | |
111 query = Query.new(:project => Project.find(1), :name => '_') | |
112 query.add_filter('due_date', '>t-', ['3']) | |
113 issues = find_issues_with_query(query) | |
114 assert !issues.empty? | |
115 issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} | |
116 end | |
117 | |
118 def test_operator_more_than_ago | |
119 Issue.find(7).update_attribute(:due_date, (Date.today - 10)) | |
120 query = Query.new(:project => Project.find(1), :name => '_') | |
121 query.add_filter('due_date', '<t-', ['10']) | |
122 assert query.statement.include?("#{Issue.table_name}.due_date <=") | |
123 issues = find_issues_with_query(query) | |
124 assert !issues.empty? | |
125 issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} | |
126 end | |
127 | |
128 def test_operator_in | |
129 Issue.find(7).update_attribute(:due_date, (Date.today + 2)) | |
130 query = Query.new(:project => Project.find(1), :name => '_') | |
131 query.add_filter('due_date', 't+', ['2']) | |
132 issues = find_issues_with_query(query) | |
133 assert !issues.empty? | |
134 issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} | |
135 end | |
136 | |
137 def test_operator_ago | |
138 Issue.find(7).update_attribute(:due_date, (Date.today - 3)) | |
139 query = Query.new(:project => Project.find(1), :name => '_') | |
140 query.add_filter('due_date', 't-', ['3']) | |
141 issues = find_issues_with_query(query) | |
142 assert !issues.empty? | |
143 issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} | |
144 end | |
145 | |
146 def test_operator_today | |
147 query = Query.new(:project => Project.find(1), :name => '_') | |
148 query.add_filter('due_date', 't', ['']) | |
149 issues = find_issues_with_query(query) | |
150 assert !issues.empty? | |
151 issues.each {|issue| assert_equal Date.today, issue.due_date} | |
152 end | |
153 | |
154 def test_operator_this_week_on_date | |
155 query = Query.new(:project => Project.find(1), :name => '_') | |
156 query.add_filter('due_date', 'w', ['']) | |
157 find_issues_with_query(query) | |
158 end | |
159 | |
160 def test_operator_this_week_on_datetime | |
161 query = Query.new(:project => Project.find(1), :name => '_') | |
162 query.add_filter('created_on', 'w', ['']) | |
163 find_issues_with_query(query) | |
164 end | |
165 | |
166 def test_operator_contains | |
167 query = Query.new(:project => Project.find(1), :name => '_') | |
168 query.add_filter('subject', '~', ['uNable']) | |
169 assert query.statement.include?("LOWER(#{Issue.table_name}.subject) LIKE '%unable%'") | |
170 result = find_issues_with_query(query) | |
171 assert result.empty? | |
172 result.each {|issue| assert issue.subject.downcase.include?('unable') } | |
173 end | |
174 | |
175 def test_operator_does_not_contains | |
176 query = Query.new(:project => Project.find(1), :name => '_') | |
177 query.add_filter('subject', '!~', ['uNable']) | |
178 assert query.statement.include?("LOWER(#{Issue.table_name}.subject) NOT LIKE '%unable%'") | |
179 find_issues_with_query(query) | |
180 end | |
181 | |
182 def test_filter_watched_issues | |
183 User.current = User.find(1) | |
184 query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '=', :values => ['me']}}) | |
185 result = find_issues_with_query(query) | |
186 assert_not_nil result | |
187 assert !result.empty? | |
188 assert_equal Issue.visible.watched_by(User.current).sort_by(&:id), result.sort_by(&:id) | |
189 User.current = nil | |
190 end | |
191 | |
192 def test_filter_unwatched_issues | |
193 User.current = User.find(1) | |
194 query = Query.new(:name => '_', :filters => { 'watcher_id' => {:operator => '!', :values => ['me']}}) | |
195 result = find_issues_with_query(query) | |
196 assert_not_nil result | |
197 assert !result.empty? | |
198 assert_equal((Issue.visible - Issue.watched_by(User.current)).sort_by(&:id).size, result.sort_by(&:id).size) | |
199 User.current = nil | |
200 end | |
201 | |
202 def test_default_columns | |
203 q = Query.new | |
204 assert !q.columns.empty? | |
205 end | |
206 | |
207 def test_set_column_names | |
208 q = Query.new | |
209 q.column_names = ['tracker', :subject, '', 'unknonw_column'] | |
210 assert_equal [:tracker, :subject], q.columns.collect {|c| c.name} | |
211 c = q.columns.first | |
212 assert q.has_column?(c) | |
213 end | |
214 | |
215 def test_groupable_columns_should_include_custom_fields | |
216 q = Query.new | |
217 assert q.groupable_columns.detect {|c| c.is_a? QueryCustomFieldColumn} | |
218 end | |
219 | |
220 def test_default_sort | |
221 q = Query.new | |
222 assert_equal [], q.sort_criteria | |
223 end | |
224 | |
225 def test_set_sort_criteria_with_hash | |
226 q = Query.new | |
227 q.sort_criteria = {'0' => ['priority', 'desc'], '2' => ['tracker']} | |
228 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria | |
229 end | |
230 | |
231 def test_set_sort_criteria_with_array | |
232 q = Query.new | |
233 q.sort_criteria = [['priority', 'desc'], 'tracker'] | |
234 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria | |
235 end | |
236 | |
237 def test_create_query_with_sort | |
238 q = Query.new(:name => 'Sorted') | |
239 q.sort_criteria = [['priority', 'desc'], 'tracker'] | |
240 assert q.save | |
241 q.reload | |
242 assert_equal [['priority', 'desc'], ['tracker', 'asc']], q.sort_criteria | |
243 end | |
244 | |
245 def test_sort_by_string_custom_field_asc | |
246 q = Query.new | |
247 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } | |
248 assert c | |
249 assert c.sortable | |
250 issues = Issue.find :all, | |
251 :include => [ :assigned_to, :status, :tracker, :project, :priority ], | |
252 :conditions => q.statement, | |
253 :order => "#{c.sortable} ASC" | |
254 values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} | |
255 assert !values.empty? | |
256 assert_equal values.sort, values | |
257 end | |
258 | |
259 def test_sort_by_string_custom_field_desc | |
260 q = Query.new | |
261 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'string' } | |
262 assert c | |
263 assert c.sortable | |
264 issues = Issue.find :all, | |
265 :include => [ :assigned_to, :status, :tracker, :project, :priority ], | |
266 :conditions => q.statement, | |
267 :order => "#{c.sortable} DESC" | |
268 values = issues.collect {|i| i.custom_value_for(c.custom_field).to_s} | |
269 assert !values.empty? | |
270 assert_equal values.sort.reverse, values | |
271 end | |
272 | |
273 def test_sort_by_float_custom_field_asc | |
274 q = Query.new | |
275 c = q.available_columns.find {|col| col.is_a?(QueryCustomFieldColumn) && col.custom_field.field_format == 'float' } | |
276 assert c | |
277 assert c.sortable | |
278 issues = Issue.find :all, | |
279 :include => [ :assigned_to, :status, :tracker, :project, :priority ], | |
280 :conditions => q.statement, | |
281 :order => "#{c.sortable} ASC" | |
282 values = issues.collect {|i| begin; Kernel.Float(i.custom_value_for(c.custom_field).to_s); rescue; nil; end}.compact | |
283 assert !values.empty? | |
284 assert_equal values.sort, values | |
285 end | |
286 | |
287 def test_invalid_query_should_raise_query_statement_invalid_error | |
288 q = Query.new | |
289 assert_raise Query::StatementInvalid do | |
290 q.issues(:conditions => "foo = 1") | |
291 end | |
292 end | |
293 | |
294 def test_issue_count_by_association_group | |
295 q = Query.new(:name => '_', :group_by => 'assigned_to') | |
296 count_by_group = q.issue_count_by_group | |
297 assert_kind_of Hash, count_by_group | |
298 assert_equal %w(NilClass User), count_by_group.keys.collect {|k| k.class.name}.uniq.sort | |
299 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq | |
300 assert count_by_group.has_key?(User.find(3)) | |
301 end | |
302 | |
303 def test_issue_count_by_list_custom_field_group | |
304 q = Query.new(:name => '_', :group_by => 'cf_1') | |
305 count_by_group = q.issue_count_by_group | |
306 assert_kind_of Hash, count_by_group | |
307 assert_equal %w(NilClass String), count_by_group.keys.collect {|k| k.class.name}.uniq.sort | |
308 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq | |
309 assert count_by_group.has_key?('MySQL') | |
310 end | |
311 | |
312 def test_issue_count_by_date_custom_field_group | |
313 q = Query.new(:name => '_', :group_by => 'cf_8') | |
314 count_by_group = q.issue_count_by_group | |
315 assert_kind_of Hash, count_by_group | |
316 assert_equal %w(Date NilClass), count_by_group.keys.collect {|k| k.class.name}.uniq.sort | |
317 assert_equal %w(Fixnum), count_by_group.values.collect {|k| k.class.name}.uniq | |
318 end | |
319 | |
320 def test_label_for | |
321 q = Query.new | |
322 assert_equal 'assigned_to', q.label_for('assigned_to_id') | |
323 end | |
324 | |
325 def test_editable_by | |
326 admin = User.find(1) | |
327 manager = User.find(2) | |
328 developer = User.find(3) | |
329 | |
330 # Public query on project 1 | |
331 q = Query.find(1) | |
332 assert q.editable_by?(admin) | |
333 assert q.editable_by?(manager) | |
334 assert !q.editable_by?(developer) | |
335 | |
336 # Private query on project 1 | |
337 q = Query.find(2) | |
338 assert q.editable_by?(admin) | |
339 assert !q.editable_by?(manager) | |
340 assert q.editable_by?(developer) | |
341 | |
342 # Private query for all projects | |
343 q = Query.find(3) | |
344 assert q.editable_by?(admin) | |
345 assert !q.editable_by?(manager) | |
346 assert q.editable_by?(developer) | |
347 | |
348 # Public query for all projects | |
349 q = Query.find(4) | |
350 assert q.editable_by?(admin) | |
351 assert !q.editable_by?(manager) | |
352 assert !q.editable_by?(developer) | |
353 end | |
354 end |