when "c"
sql = sql + "#{IssueStatus.table_name}.is_closed=#{connection.quoted_true}" if field == "status_id"
when ">t-"
- 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)]
+ sql = sql + date_range_clause(db_table, db_field, - v.first.to_i, 0)
when "<t-"
- sql = sql + "#{db_table}.#{db_field} <= '%s'" % connection.quoted_date((Date.today - v.first.to_i).to_time)
+ sql = sql + date_range_clause(db_table, db_field, nil, - v.first.to_i)
when "t-"
- 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)]
+ sql = sql + date_range_clause(db_table, db_field, - v.first.to_i, - v.first.to_i)
when ">t+"
- sql = sql + "#{db_table}.#{db_field} >= '%s'" % connection.quoted_date((Date.today + v.first.to_i).to_time)
+ sql = sql + date_range_clause(db_table, db_field, v.first.to_i, nil)
when "<t+"
- 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)]
+ sql = sql + date_range_clause(db_table, db_field, 0, v.first.to_i)
when "t+"
- 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)]
+ sql = sql + date_range_clause(db_table, db_field, v.first.to_i, v.first.to_i)
when "t"
- 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)]
+ sql = sql + date_range_clause(db_table, db_field, 0, 0)
when "w"
from = l(:general_first_day_of_week) == '7' ?
# week starts on sunday
@available_filters["cf_#{field.id}"] = options.merge({ :name => field.name })
end
end
+
+ # Returns a SQL clause for a date or datetime field.
+ def date_range_clause(table, field, from, to)
+ s = []
+ if from
+ s << ("#{table}.#{field} > '%s'" % [connection.quoted_date((Date.yesterday + from).to_time.end_of_day)])
+ end
+ if to
+ s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date((Date.today + to).to_time.end_of_day)])
+ end
+ s.join(' AND ')
+ end
end
require File.dirname(__FILE__) + '/../test_helper'
class QueryTest < Test::Unit::TestCase
- fixtures :projects, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :queries
+ fixtures :projects, :enabled_modules, :users, :members, :roles, :trackers, :issue_statuses, :issue_categories, :enumerations, :issues, :custom_fields, :custom_values, :versions, :queries
def test_custom_fields_for_all_projects_should_be_available_in_global_queries
query = Query.new(:project => nil, :name => '_')
end
def test_operator_in_more_than
+ Issue.find(7).update_attribute(:due_date, (Date.today + 15))
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '>t+', ['15'])
- assert query.statement.include?("#{Issue.table_name}.due_date >=")
- find_issues_with_query(query)
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert(issue.due_date >= (Date.today + 15))}
end
def test_operator_in_less_than
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', '<t+', ['15'])
- assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN")
- find_issues_with_query(query)
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert(issue.due_date >= Date.today && issue.due_date <= (Date.today + 15))}
+ end
+
+ def test_operator_less_than_ago
+ Issue.find(7).update_attribute(:due_date, (Date.today - 3))
+ query = Query.new(:project => Project.find(1), :name => '_')
+ query.add_filter('due_date', '>t-', ['3'])
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert(issue.due_date >= (Date.today - 3) && issue.due_date <= Date.today)}
+ end
+
+ def test_operator_more_than_ago
+ Issue.find(7).update_attribute(:due_date, (Date.today - 10))
+ query = Query.new(:project => Project.find(1), :name => '_')
+ query.add_filter('due_date', '<t-', ['10'])
+ assert query.statement.include?("#{Issue.table_name}.due_date <=")
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert(issue.due_date <= (Date.today - 10))}
+ end
+
+ def test_operator_in
+ Issue.find(7).update_attribute(:due_date, (Date.today + 2))
+ query = Query.new(:project => Project.find(1), :name => '_')
+ query.add_filter('due_date', 't+', ['2'])
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert_equal((Date.today + 2), issue.due_date)}
+ end
+
+ def test_operator_ago
+ Issue.find(7).update_attribute(:due_date, (Date.today - 3))
+ query = Query.new(:project => Project.find(1), :name => '_')
+ query.add_filter('due_date', 't-', ['3'])
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert_equal((Date.today - 3), issue.due_date)}
end
def test_operator_today
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 't', [''])
- assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN")
- find_issues_with_query(query)
+ issues = find_issues_with_query(query)
+ assert !issues.empty?
+ issues.each {|issue| assert_equal Date.today, issue.due_date}
end
def test_operator_this_week_on_date
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('due_date', 'w', [''])
- assert query.statement.include?("#{Issue.table_name}.due_date BETWEEN")
find_issues_with_query(query)
end
def test_operator_this_week_on_datetime
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('created_on', 'w', [''])
- assert query.statement.include?("#{Issue.table_name}.created_on BETWEEN")
find_issues_with_query(query)
end