Fixes filters on float values.

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@6227 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2011-07-10 18:09:40 +00:00
parent 4507aa5014
commit bf15b06d98
2 changed files with 30 additions and 12 deletions

View File

@ -123,6 +123,7 @@ class Query < ActiveRecord::Base
:date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w" ], :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w" ],
:string => [ "=", "~", "!", "!~" ], :string => [ "=", "~", "!", "!~" ],
:text => [ "~", "!~" ], :text => [ "~", "!~" ],
# TODO: should be :numeric
:integer => [ "=", ">=", "<=", "><", "!*", "*" ] } :integer => [ "=", ">=", "<=", "><", "!*", "*" ] }
cattr_reader :operators_by_filter_type cattr_reader :operators_by_filter_type
@ -633,9 +634,9 @@ class Query < ActiveRecord::Base
sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil)
else else
if is_custom_filter if is_custom_filter
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_i}" sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f}"
else else
sql = "#{db_table}.#{db_field} >= #{value.first.to_i}" sql = "#{db_table}.#{db_field} >= #{value.first.to_f}"
end end
end end
when "<=" when "<="
@ -643,9 +644,9 @@ class Query < ActiveRecord::Base
sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil))
else else
if is_custom_filter if is_custom_filter
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_i}" sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f}"
else else
sql = "#{db_table}.#{db_field} <= #{value.first.to_i}" sql = "#{db_table}.#{db_field} <= #{value.first.to_f}"
end end
end end
when "><" when "><"
@ -653,9 +654,9 @@ class Query < ActiveRecord::Base
sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil))
else else
if is_custom_filter if is_custom_filter
sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_i} AND #{value[1].to_i}" sql = "CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
else else
sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_i} AND #{value[1].to_i}" sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}"
end end
end end
when "o" when "o"

View File

@ -101,10 +101,27 @@ class QueryTest < ActiveSupport::TestCase
find_issues_with_query(query) find_issues_with_query(query)
end end
def test_operator_is_on_float
Issue.update_all("estimated_hours = 171.2", "id=2")
query = Query.new(:name => '_')
query.add_filter('estimated_hours', '=', ['171.20'])
issues = find_issues_with_query(query)
assert_equal 1, issues.size
assert_equal 2, issues.first.id
end
def test_operator_greater_than def test_operator_greater_than
query = Query.new(:project => Project.find(1), :name => '_') query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '>=', ['40']) query.add_filter('done_ratio', '>=', ['40'])
assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40") assert query.statement.include?("#{Issue.table_name}.done_ratio >= 40.0")
find_issues_with_query(query)
end
def test_operator_greater_than_a_float
query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('estimated_hours', '>=', ['40.5'])
assert query.statement.include?("#{Issue.table_name}.estimated_hours >= 40.5")
find_issues_with_query(query) find_issues_with_query(query)
end end
@ -112,14 +129,14 @@ class QueryTest < ActiveSupport::TestCase
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
query = Query.new(:project => Project.find(1), :name => '_') query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '>=', ['40']) query.add_filter("cf_#{f.id}", '>=', ['40'])
assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) >= 40") assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) >= 40.0")
find_issues_with_query(query) find_issues_with_query(query)
end end
def test_operator_lesser_than def test_operator_lesser_than
query = Query.new(:project => Project.find(1), :name => '_') query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '<=', ['30']) query.add_filter('done_ratio', '<=', ['30'])
assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30") assert query.statement.include?("#{Issue.table_name}.done_ratio <= 30.0")
find_issues_with_query(query) find_issues_with_query(query)
end end
@ -127,14 +144,14 @@ class QueryTest < ActiveSupport::TestCase
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
query = Query.new(:project => Project.find(1), :name => '_') query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '<=', ['30']) query.add_filter("cf_#{f.id}", '<=', ['30'])
assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30") assert query.statement.include?("CAST(custom_values.value AS decimal(60,3)) <= 30.0")
find_issues_with_query(query) find_issues_with_query(query)
end end
def test_operator_between def test_operator_between
query = Query.new(:project => Project.find(1), :name => '_') query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter('done_ratio', '><', ['30', '40']) query.add_filter('done_ratio', '><', ['30', '40'])
assert_include "#{Issue.table_name}.done_ratio BETWEEN 30 AND 40", query.statement assert_include "#{Issue.table_name}.done_ratio BETWEEN 30.0 AND 40.0", query.statement
find_issues_with_query(query) find_issues_with_query(query)
end end
@ -142,7 +159,7 @@ class QueryTest < ActiveSupport::TestCase
f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true) f = IssueCustomField.create!(:name => 'filter', :field_format => 'int', :is_filter => true, :is_for_all => true)
query = Query.new(:project => Project.find(1), :name => '_') query = Query.new(:project => Project.find(1), :name => '_')
query.add_filter("cf_#{f.id}", '><', ['30', '40']) query.add_filter("cf_#{f.id}", '><', ['30', '40'])
assert_include "CAST(custom_values.value AS decimal(60,3)) BETWEEN 30 AND 40", query.statement assert_include "CAST(custom_values.value AS decimal(60,3)) BETWEEN 30.0 AND 40.0", query.statement
find_issues_with_query(query) find_issues_with_query(query)
end end