@@ -329,19 +329,19 class Query < ActiveRecord::Base | |||
|
329 | 329 | when "c" |
|
330 | 330 | sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id" |
|
331 | 331 | when ">t-" |
|
332 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today + 1).to_time)] | |
|
332 | sql = sql + date_range_clause(db_table, db_field, - v.first.to_i, 0) | |
|
333 | 333 | when "<t-" |
|
334 | sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time) | |
|
334 | sql = sql + date_range_clause(db_table, db_field, nil, - v.first.to_i) | |
|
335 | 335 | when "t-" |
|
336 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today - v.first.to_i).to_time), connection.quoted_date((Date.today - v.first.to_i + 1).to_time)] | |
|
336 | sql = sql + date_range_clause(db_table, db_field, - v.first.to_i, - v.first.to_i) | |
|
337 | 337 | when ">t+" |
|
338 | sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time) | |
|
338 | sql = sql + date_range_clause(db_table, db_field, v.first.to_i, nil) | |
|
339 | 339 | when "<t+" |
|
340 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] | |
|
340 | sql = sql + date_range_clause(db_table, db_field, 0, v.first.to_i) | |
|
341 | 341 | when "t+" |
|
342 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date((Date.today + v.first.to_i).to_time), connection.quoted_date((Date.today + v.first.to_i + 1).to_time)] | |
|
342 | sql = sql + date_range_clause(db_table, db_field, v.first.to_i, v.first.to_i) | |
|
343 | 343 | when "t" |
|
344 | sql = sql + "#{db_table}.#{db_field} BETWEEN '%s' AND '%s'" % [connection.quoted_date(Date.today.to_time), connection.quoted_date((Date.today+1).to_time)] | |
|
344 | sql = sql + date_range_clause(db_table, db_field, 0, 0) | |
|
345 | 345 | when "w" |
|
346 | 346 | from = l(:general_first_day_of_week) == '7' ? |
|
347 | 347 | # week starts on sunday |
@@ -382,4 +382,16 class Query < ActiveRecord::Base | |||
|
382 | 382 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name }) |
|
383 | 383 | end |
|
384 | 384 | end |
|
385 | ||
|
386 | # Returns a SQL clause for a date or datetime field. | |
|
387 | def date_range_clause(table, field, from, to) | |
|
388 | s = [] | |
|
389 | if from | |
|
390 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date((Date.yesterday + from).to_time.end_of_day)]) | |
|
391 | end | |
|
392 | if to | |
|
393 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date((Date.today + to).to_time.end_of_day)]) | |
|
394 | end | |
|
395 | s.join(' AND ') | |
|
396 | end | |
|
385 | 397 | end |
@@ -91,4 +91,21 issues_006: | |||
|
91 | 91 | status_id: 1 |
|
92 | 92 | start_date: <%= Date.today.to_s(:db) %> |
|
93 | 93 | due_date: <%= 1.days.from_now.to_date.to_s(:db) %> |
|
94 | issues_007: | |
|
95 | created_on: <%= 10.days.ago.to_date.to_s(:db) %> | |
|
96 | project_id: 1 | |
|
97 | updated_on: <%= 10.days.ago.to_date.to_s(:db) %> | |
|
98 | priority_id: 3 | |
|
99 | subject: Issue due today | |
|
100 | id: 7 | |
|
101 | fixed_version_id: | |
|
102 | category_id: | |
|
103 | description: This is an issue that is due today | |
|
104 | tracker_id: 1 | |
|
105 | assigned_to_id: | |
|
106 | author_id: 2 | |
|
107 | status_id: 1 | |
|
108 | start_date: <%= 10.days.ago.to_s(:db) %> | |
|
109 | due_date: <%= Date.today.to_s(:db) %> | |
|
110 | lock_version: 0 | |
|
94 | 111 | No newline at end of file |
@@ -18,7 +18,7 | |||
|
18 | 18 | require File.dirname(__FILE__) + '/../test_helper' |
|
19 | 19 | |
|
20 | 20 | class QueryTest < Test::Unit::TestCase |
|
21 | fixtures :projects, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries | |
|
21 | fixtures :projects, :enabled_modules, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :versions, :queries | |
|
22 | 22 | |
|
23 | 23 | def test_custom_fields_for_all_projects_should_be_available_in_global_queries |
|
24 | 24 | query = Query.new(:project => nil, :name => '_') |
@@ -75,37 +75,76 class QueryTest < Test::Unit::TestCase | |||
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def test_operator_in_more_than |
|
78 | Issue.find(7).update_attribute(:due_date, (Date.today + 15)) | |
|
78 | 79 | query = Query.new(:project => Project.find(1), :name => '_') |
|
79 | 80 | query.add_filter('due_date', '>t+', ['15']) |
|
80 | assert query.statement.include?("#{Issue.table_name}.due_date >=") | |
|
81 | find_issues_with_query(query) | |
|
81 | issues = find_issues_with_query(query) | |
|
82 | assert !issues.empty? | |
|
83 | issues.each {|issue| assert(issue.due_date >= (Date.today + 15))} | |
|
82 | 84 | end |
|
83 | 85 | |
|
84 | 86 | def test_operator_in_less_than |
|
85 | 87 | query = Query.new(:project => Project.find(1), :name => '_') |
|
86 | 88 | query.add_filter('due_date', '<t+', ['15']) |
|
87 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |
|
88 | find_issues_with_query(query) | |
|
89 | issues = find_issues_with_query(query) | |
|
90 | assert !issues.empty? | |
|
91 | issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))} | |
|
92 | end | |
|
93 | ||
|
94 | def test_operator_less_than_ago | |
|
95 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) | |
|
96 | query = Query.new(:project => Project.find(1), :name => '_') | |
|
97 | query.add_filter('due_date', '>t-', ['3']) | |
|
98 | issues = find_issues_with_query(query) | |
|
99 | assert !issues.empty? | |
|
100 | issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)} | |
|
101 | end | |
|
102 | ||
|
103 | def test_operator_more_than_ago | |
|
104 | Issue.find(7).update_attribute(:due_date, (Date.today - 10)) | |
|
105 | query = Query.new(:project => Project.find(1), :name => '_') | |
|
106 | query.add_filter('due_date', '<t-', ['10']) | |
|
107 | assert query.statement.include?("#{Issue.table_name}.due_date <=") | |
|
108 | issues = find_issues_with_query(query) | |
|
109 | assert !issues.empty? | |
|
110 | issues.each {|issue| assert(issue.due_date <= (Date.today - 10))} | |
|
111 | end | |
|
112 | ||
|
113 | def test_operator_in | |
|
114 | Issue.find(7).update_attribute(:due_date, (Date.today + 2)) | |
|
115 | query = Query.new(:project => Project.find(1), :name => '_') | |
|
116 | query.add_filter('due_date', 't+', ['2']) | |
|
117 | issues = find_issues_with_query(query) | |
|
118 | assert !issues.empty? | |
|
119 | issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)} | |
|
120 | end | |
|
121 | ||
|
122 | def test_operator_ago | |
|
123 | Issue.find(7).update_attribute(:due_date, (Date.today - 3)) | |
|
124 | query = Query.new(:project => Project.find(1), :name => '_') | |
|
125 | query.add_filter('due_date', 't-', ['3']) | |
|
126 | issues = find_issues_with_query(query) | |
|
127 | assert !issues.empty? | |
|
128 | issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)} | |
|
89 | 129 | end |
|
90 | 130 | |
|
91 | 131 | def test_operator_today |
|
92 | 132 | query = Query.new(:project => Project.find(1), :name => '_') |
|
93 | 133 | query.add_filter('due_date', 't', ['']) |
|
94 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |
|
95 | find_issues_with_query(query) | |
|
134 | issues = find_issues_with_query(query) | |
|
135 | assert !issues.empty? | |
|
136 | issues.each {|issue| assert_equal Date.today, issue.due_date} | |
|
96 | 137 | end |
|
97 | 138 | |
|
98 | 139 | def test_operator_this_week_on_date |
|
99 | 140 | query = Query.new(:project => Project.find(1), :name => '_') |
|
100 | 141 | query.add_filter('due_date', 'w', ['']) |
|
101 | assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN") | |
|
102 | 142 | find_issues_with_query(query) |
|
103 | 143 | end |
|
104 | 144 | |
|
105 | 145 | def test_operator_this_week_on_datetime |
|
106 | 146 | query = Query.new(:project => Project.find(1), :name => '_') |
|
107 | 147 | query.add_filter('created_on', 'w', ['']) |
|
108 | assert query.statement.include?("#{Issue.table_name}.created_on BETWEEN") | |
|
109 | 148 | find_issues_with_query(query) |
|
110 | 149 | end |
|
111 | 150 |
General Comments 0
You need to be logged in to leave comments.
Login now