require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') describe CostQuery do before { @query = CostQuery.new } fixtures :users fixtures :cost_types fixtures :cost_entries fixtures :rates fixtures :projects fixtures :issues fixtures :trackers fixtures :time_entries fixtures :enumerations fixtures :issue_statuses fixtures :roles fixtures :issue_categories fixtures :versions describe CostQuery::Operator do def query(table, field, operator, *values) sql = CostQuery::SqlStatement.new table yield sql if block_given? operator.to_operator.modify sql, field, *values ActiveRecord::Base.connection.select_all sql.to_s end def query_on_entries(field, operator, *values) sql = CostQuery::SqlStatement.for_entries operator.to_operator.modify sql, field, *values result = ActiveRecord::Base.connection.select_all sql.to_s end it "does =" do query('projects', 'id', '=', 1).size.should == 1 end it "does = for multiple values" do query('projects', 'id', '=', 1, 2).size.should == 2 end it "does = for no values" do sql = CostQuery::SqlStatement.new 'projects' "=".to_operator.modify sql, 'id' count = ActiveRecord::Base.connection.select_all sql.to_s count.should == 0 end it "does = for nil" do query('projects', 'id', '=', nil).size.should == 0 end it "does <=" do query('projects', 'id', '<=', Project.count - 1).size.should == Project.count - 1 end it "does >=" do query('projects', 'id', '>=', Project.first.id + 1).size.should == Project.count - 1 end it "does !" do query('projects', 'id', '!', 1).size.should == Project.count - 1 end it "does ! for multiple values" do query('projects', 'id', '!', 1, 2).size.should == Project.count - 2 end it "does !*" do query('cost_entries', 'project_id', '!*', []).size.should == 0 end it "does !~ (not contains)" do query('projects', 'name', '!~', 'o').size.should == Project.all.select { |p| p.name !~ /o/ }.count end it "does c (closed issue)" do query('issues', 'status_id', 'c') { |s| s.join IssueStatus => [Issue, :status] }.size.should >= 0 end it "does o (open issue)" do query('issues', 'status_id', 'o') { |s| s.join IssueStatus => [Issue, :status] }.size.should >= 0 end it "does give the correct number of results when counting closed and open issues" do a = query('issues', 'status_id', 'o') { |s| s.join IssueStatus => [Issue, :status] }.size b = query('issues', 'status_id', 'c') { |s| s.join IssueStatus => [Issue, :status] }.size Issue.count.should == a + b end it "does w (this week)" do #somehow this test doesn't work on sundays n = query('projects', 'created_on', 'w').size Project.generate! :created_on => Time.now query('projects', 'created_on', 'w').size.should == n + 1 Project.generate! :created_on => Date.today + 7 Project.generate! :created_on => Date.today - 7 query('projects', 'created_on', 'w').size.should == n + 1 end it "does t (today)" do s = query('projects', 'created_on', 't').size Project.generate! :created_on => Date.yesterday query('projects', 'created_on', 't').size.should == s Project.generate! :created_on => Time.now query('projects', 'created_on', 't').size.should == s + 1 end it "does Date.tomorrow + 1 query('projects', 'created_on', ' Date.tomorrow + 2 query('projects', 'created_on', ' Date.tomorrow query('projects', 'created_on', 't+', 1).size.should == n + 1 Project.generate! :created_on => Date.tomorrow + 2 query('projects', 'created_on', 't+', 1).size.should == n + 1 end it "does >t+ (after the day which is n days in the furure)" do n = query('projects', 'created_on', '>t+', 1).size Project.generate! :created_on => Time.now query('projects', 'created_on', '>t+', 1).size.should == n Project.generate! :created_on => Date.tomorrow + 1 query('projects', 'created_on', '>t+', 1).size.should == n + 1 end it "does >t- (after the day which is n days ago)" do n = query('projects', 'created_on', '>t-', 1).size Project.generate! :created_on => Date.today query('projects', 'created_on', '>t-', 1).size.should == n + 1 Project.generate! :created_on => Date.yesterday - 1 query('projects', 'created_on', '>t-', 1).size.should == n + 1 end it "does t- (n days ago)" do n = query('projects', 'created_on', 't-', 1).size Project.generate! :created_on => Date.yesterday query('projects', 'created_on', 't-', 1).size.should == n + 1 Project.generate! :created_on => Date.yesterday - 2 query('projects', 'created_on', 't-', 1).size.should == n + 1 end it "does Date.today query('projects', 'created_on', ' Date.yesterday - 1 query('projects', 'created_on', 'd" do query('projects', 'created_on', '<>d', Time.now, 5.minutes.from_now).size.should == 0 end it "does >d" do #assuming that all projects were created in the past query('projects', 'created_on', '>d', Time.now).size.should == 0 end describe 'arity' do arities = {'t' => 0} arities.each do |o,a| it("#{o} should take #{a} values") { o.to_operator.arity.should == a } end end end end