|
|
|
@ -26,7 +26,7 @@ |
|
|
|
|
# See COPYRIGHT and LICENSE files for more details. |
|
|
|
|
#++ |
|
|
|
|
|
|
|
|
|
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') |
|
|
|
|
require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper") |
|
|
|
|
|
|
|
|
|
describe CostQuery, type: :model, reporting_query_helper: true do |
|
|
|
|
minimal_query |
|
|
|
@ -35,7 +35,7 @@ describe CostQuery, type: :model, reporting_query_helper: true do |
|
|
|
|
let!(:project2) { create(:project, name: "project2", created_at: 6.minutes.ago) } |
|
|
|
|
|
|
|
|
|
describe CostQuery::Operator do |
|
|
|
|
def query(table, field, operator, *values) |
|
|
|
|
def cost_query(table, field, operator, *values) |
|
|
|
|
sql = CostQuery::SqlStatement.new table |
|
|
|
|
yield sql if block_given? |
|
|
|
|
operator.to_operator.modify sql, field, *values |
|
|
|
@ -53,11 +53,11 @@ describe CostQuery, type: :model, reporting_query_helper: true do |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does =" do |
|
|
|
|
expect(query('projects', 'id', '=', project1.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '=', project1.id).size).to eq(1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does = for multiple values" do |
|
|
|
|
expect(query('projects', 'id', '=', project1.id, project2.id).size).to eq(2) |
|
|
|
|
expect(cost_query('projects', 'id', '=', project1.id, project2.id).size).to eq(2) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does = for no values" do |
|
|
|
@ -68,172 +68,172 @@ describe CostQuery, type: :model, reporting_query_helper: true do |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does = for nil" do |
|
|
|
|
expect(query('projects', 'id', '=', nil).size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'id', '=', nil).size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does = for empty string" do |
|
|
|
|
expect(query('projects', 'id', '=', '').size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'id', '=', '').size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does <=" do |
|
|
|
|
expect(query('projects', 'id', '<=', project2.id - 1).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '<=', project2.id - 1).size).to eq(1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does >=" do |
|
|
|
|
expect(query('projects', 'id', '>=', project1.id + 1).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '>=', project1.id + 1).size).to eq(1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does !" do |
|
|
|
|
expect(query('projects', 'id', '!', project1.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '!', project1.id).size).to eq(1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does ! for empty string" do |
|
|
|
|
expect(query('projects', 'id', '!', '').size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'id', '!', '').size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does ! for multiple values" do |
|
|
|
|
expect(query('projects', 'id', '!', project1.id, project2.id).size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'id', '!', project1.id, project2.id).size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does !*" do |
|
|
|
|
expect(query('cost_entries', 'project_id', '!*', []).size).to eq(0) |
|
|
|
|
expect(cost_query('cost_entries', 'project_id', '!*', []).size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does ~ (contains)" do |
|
|
|
|
expect(query('projects', 'name', '~', 'o').size).to eq(Project.all.select { |p| p.name =~ /o/ }.count) |
|
|
|
|
expect(query('projects', 'name', '~', 'test').size).to eq(Project.all.select { |p| p.name =~ /test/ }.count) |
|
|
|
|
expect(query('projects', 'name', '~', 'child').size).to eq(Project.all.select { |p| p.name =~ /child/ }.count) |
|
|
|
|
expect(cost_query('projects', 'name', '~', 'o').size).to eq(Project.all.select { |p| p.name =~ /o/ }.count) |
|
|
|
|
expect(cost_query('projects', 'name', '~', 'test').size).to eq(Project.all.select { |p| p.name =~ /test/ }.count) |
|
|
|
|
expect(cost_query('projects', 'name', '~', 'child').size).to eq(Project.all.select { |p| p.name =~ /child/ }.count) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does !~ (not contains)" do |
|
|
|
|
expect(query('projects', 'name', '!~', 'o').size).to eq(Project.all.select { |p| p.name !~ /o/ }.count) |
|
|
|
|
expect(query('projects', 'name', '!~', 'test').size).to eq(Project.all.select { |p| p.name !~ /test/ }.count) |
|
|
|
|
expect(query('projects', 'name', '!~', 'child').size).to eq(Project.all.select { |p| p.name !~ /child/ }.count) |
|
|
|
|
expect(cost_query('projects', 'name', '!~', 'o').size).to eq(Project.all.reject { |p| p.name =~ /o/ }.count) |
|
|
|
|
expect(cost_query('projects', 'name', '!~', 'test').size).to eq(Project.all.reject { |p| p.name =~ /test/ }.count) |
|
|
|
|
expect(cost_query('projects', 'name', '!~', 'child').size).to eq(Project.all.reject { |p| p.name =~ /child/ }.count) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does c (closed work_package)" do |
|
|
|
|
expect(query('work_packages', 'status_id', 'c') { |s| s.join Status => [WorkPackage, :status] }.size).to be >= 0 |
|
|
|
|
expect(cost_query('work_packages', 'status_id', 'c') { |s| s.join Status => [WorkPackage, :status] }.size).to be >= 0 |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does o (open work_package)" do |
|
|
|
|
expect(query('work_packages', 'status_id', 'o') { |s| s.join Status => [WorkPackage, :status] }.size).to be >= 0 |
|
|
|
|
expect(cost_query('work_packages', 'status_id', 'o') { |s| s.join Status => [WorkPackage, :status] }.size).to be >= 0 |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does give the correct number of results when counting closed and open work_packages" do |
|
|
|
|
a = query('work_packages', 'status_id', 'o') { |s| s.join Status => [WorkPackage, :status] }.size |
|
|
|
|
b = query('work_packages', 'status_id', 'c') { |s| s.join Status => [WorkPackage, :status] }.size |
|
|
|
|
a = cost_query('work_packages', 'status_id', 'o') { |s| s.join Status => [WorkPackage, :status] }.size |
|
|
|
|
b = cost_query('work_packages', 'status_id', 'c') { |s| s.join Status => [WorkPackage, :status] }.size |
|
|
|
|
expect(WorkPackage.count).to eq(a + b) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does w (this week)" do |
|
|
|
|
# somehow this test doesn't work on sundays |
|
|
|
|
n = query('projects', 'created_at', 'w').size |
|
|
|
|
day_in_this_week = Time.now.at_beginning_of_week + 1.day |
|
|
|
|
n = cost_query('projects', 'created_at', 'w').size |
|
|
|
|
day_in_this_week = Time.zone.now.at_beginning_of_week + 1.day |
|
|
|
|
create(:project, created_at: day_in_this_week) |
|
|
|
|
expect(query('projects', 'created_at', 'w').size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 'w').size).to eq(n + 1) |
|
|
|
|
create(:project, created_at: day_in_this_week + 7.days) |
|
|
|
|
create(:project, created_at: day_in_this_week - 7.days) |
|
|
|
|
expect(query('projects', 'created_at', 'w').size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 'w').size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does t (today)" do |
|
|
|
|
s = query('projects', 'created_at', 't').size |
|
|
|
|
s = cost_query('projects', 'created_at', 't').size |
|
|
|
|
create(:project, created_at: Date.yesterday) |
|
|
|
|
expect(query('projects', 'created_at', 't').size).to eq(s) |
|
|
|
|
create(:project, created_at: Time.now) |
|
|
|
|
expect(query('projects', 'created_at', 't').size).to eq(s + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 't').size).to eq(s) |
|
|
|
|
create(:project, created_at: Time.zone.now) |
|
|
|
|
expect(cost_query('projects', 'created_at', 't').size).to eq(s + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does <t+ (before the day which is n days in the future)" do |
|
|
|
|
n = query('projects', 'created_at', '<t+', 2).size |
|
|
|
|
n = cost_query('projects', 'created_at', '<t+', 2).size |
|
|
|
|
create(:project, created_at: Date.tomorrow + 1) |
|
|
|
|
expect(query('projects', 'created_at', '<t+', 2).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '<t+', 2).size).to eq(n + 1) |
|
|
|
|
create(:project, created_at: Date.tomorrow + 2) |
|
|
|
|
expect(query('projects', 'created_at', '<t+', 2).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '<t+', 2).size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does t+ (n days in the future)" do |
|
|
|
|
n = query('projects', 'created_at', 't+', 1).size |
|
|
|
|
n = cost_query('projects', 'created_at', 't+', 1).size |
|
|
|
|
create(:project, created_at: Date.tomorrow) |
|
|
|
|
expect(query('projects', 'created_at', 't+', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 't+', 1).size).to eq(n + 1) |
|
|
|
|
create(:project, created_at: Date.tomorrow + 2) |
|
|
|
|
expect(query('projects', 'created_at', 't+', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 't+', 1).size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does >t+ (after the day which is n days in the future)" do |
|
|
|
|
n = query('projects', 'created_at', '>t+', 1).size |
|
|
|
|
create(:project, created_at: Time.now) |
|
|
|
|
expect(query('projects', 'created_at', '>t+', 1).size).to eq(n) |
|
|
|
|
n = cost_query('projects', 'created_at', '>t+', 1).size |
|
|
|
|
create(:project, created_at: Time.zone.now) |
|
|
|
|
expect(cost_query('projects', 'created_at', '>t+', 1).size).to eq(n) |
|
|
|
|
create(:project, created_at: Date.tomorrow + 1) |
|
|
|
|
expect(query('projects', 'created_at', '>t+', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '>t+', 1).size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does >t- (after the day which is n days ago)" do |
|
|
|
|
n = query('projects', 'created_at', '>t-', 1).size |
|
|
|
|
n = cost_query('projects', 'created_at', '>t-', 1).size |
|
|
|
|
create(:project, created_at: Date.today) |
|
|
|
|
expect(query('projects', 'created_at', '>t-', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '>t-', 1).size).to eq(n + 1) |
|
|
|
|
create(:project, created_at: Date.yesterday - 1) |
|
|
|
|
expect(query('projects', 'created_at', '>t-', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '>t-', 1).size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does t- (n days ago)" do |
|
|
|
|
n = query('projects', 'created_at', 't-', 1).size |
|
|
|
|
n = cost_query('projects', 'created_at', 't-', 1).size |
|
|
|
|
create(:project, created_at: Date.yesterday) |
|
|
|
|
expect(query('projects', 'created_at', 't-', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 't-', 1).size).to eq(n + 1) |
|
|
|
|
create(:project, created_at: Date.yesterday - 2) |
|
|
|
|
expect(query('projects', 'created_at', 't-', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', 't-', 1).size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does <t- (before the day which is n days ago)" do |
|
|
|
|
n = query('projects', 'created_at', '<t-', 1).size |
|
|
|
|
n = cost_query('projects', 'created_at', '<t-', 1).size |
|
|
|
|
create(:project, created_at: Date.today) |
|
|
|
|
expect(query('projects', 'created_at', '<t-', 1).size).to eq(n) |
|
|
|
|
expect(cost_query('projects', 'created_at', '<t-', 1).size).to eq(n) |
|
|
|
|
create(:project, created_at: Date.yesterday - 1) |
|
|
|
|
expect(query('projects', 'created_at', '<t-', 1).size).to eq(n + 1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '<t-', 1).size).to eq(n + 1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# Our own operators |
|
|
|
|
it "does =_child_projects" do |
|
|
|
|
expect(query('projects', 'id', '=_child_projects', project1.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '=_child_projects', project1.id).size).to eq(1) |
|
|
|
|
p_c1 = create_project parent: project1 |
|
|
|
|
expect(query('projects', 'id', '=_child_projects', project1.id).size).to eq(2) |
|
|
|
|
expect(cost_query('projects', 'id', '=_child_projects', project1.id).size).to eq(2) |
|
|
|
|
create_project parent: p_c1 |
|
|
|
|
expect(query('projects', 'id', '=_child_projects', project1.id).size).to eq(3) |
|
|
|
|
expect(cost_query('projects', 'id', '=_child_projects', project1.id).size).to eq(3) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does =_child_projects on multiple projects" do |
|
|
|
|
expect(query('projects', 'id', '=_child_projects', project1.id, project2.id).size).to eq(2) |
|
|
|
|
expect(cost_query('projects', 'id', '=_child_projects', project1.id, project2.id).size).to eq(2) |
|
|
|
|
p1_c1 = create_project parent: project1 |
|
|
|
|
p2_c1 = create_project parent: project2 |
|
|
|
|
expect(query('projects', 'id', '=_child_projects', project1.id, project2.id).size).to eq(4) |
|
|
|
|
expect(cost_query('projects', 'id', '=_child_projects', project1.id, project2.id).size).to eq(4) |
|
|
|
|
p1_c1_c1 = create_project parent: p1_c1 |
|
|
|
|
create_project parent: p1_c1_c1 |
|
|
|
|
create_project parent: p2_c1 |
|
|
|
|
expect(query('projects', 'id', '=_child_projects', project1.id, project2.id).size).to eq(7) |
|
|
|
|
expect(cost_query('projects', 'id', '=_child_projects', project1.id, project2.id).size).to eq(7) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does !_child_projects" do |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id).size).to eq(1) |
|
|
|
|
p_c1 = create_project parent: project1 |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id).size).to eq(1) |
|
|
|
|
create_project parent: project1 |
|
|
|
|
create_project parent: p_c1 |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id).size).to eq(1) |
|
|
|
|
create_project |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id).size).to eq(2) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id).size).to eq(2) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does !_child_projects on multiple projects" do |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id, project2.id).size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id, project2.id).size).to eq(0) |
|
|
|
|
p1_c1 = create_project parent: project1 |
|
|
|
|
p2_c1 = create_project parent: project2 |
|
|
|
|
create_project |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id, project2.id).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id, project2.id).size).to eq(1) |
|
|
|
|
p1_c1_c1 = create_project parent: p1_c1 |
|
|
|
|
create_project parent: p1_c1_c1 |
|
|
|
|
create_project parent: p2_c1 |
|
|
|
|
create_project |
|
|
|
|
expect(query('projects', 'id', '!_child_projects', project1.id, project2.id).size).to eq(2) |
|
|
|
|
expect(cost_query('projects', 'id', '!_child_projects', project1.id, project2.id).size).to eq(2) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does =n" do |
|
|
|
@ -248,7 +248,7 @@ describe CostQuery, type: :model, reporting_query_helper: true do |
|
|
|
|
|
|
|
|
|
# y/n seem are for filtering overridden costs |
|
|
|
|
it "does y" do |
|
|
|
|
expect(query_on_entries('overridden_costs', 'y').size).to eq(Entry.all.select { |e| !e.overridden_costs.nil? }.count) |
|
|
|
|
expect(query_on_entries('overridden_costs', 'y').size).to eq(Entry.all.reject { |e| e.overridden_costs.nil? }.count) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does n" do |
|
|
|
@ -257,20 +257,20 @@ describe CostQuery, type: :model, reporting_query_helper: true do |
|
|
|
|
|
|
|
|
|
it "does =d" do |
|
|
|
|
# assuming that there aren't more than one project created at the same time |
|
|
|
|
expect(query('projects', 'created_at', '=d', Project.order(Arel.sql('id ASC')).first.created_at).size).to eq(1) |
|
|
|
|
expect(cost_query('projects', 'created_at', '=d', Project.order(Arel.sql('id ASC')).first.created_at).size).to eq(1) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does <d" do |
|
|
|
|
expect(query('projects', 'created_at', '<d', Time.now).size).to eq(Project.count) |
|
|
|
|
expect(cost_query('projects', 'created_at', '<d', Time.zone.now).size).to eq(Project.count) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does <>d" do |
|
|
|
|
expect(query('projects', 'created_at', '<>d', Time.now, 5.minutes.from_now).size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'created_at', '<>d', Time.zone.now, 5.minutes.from_now).size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
it "does >d" do |
|
|
|
|
# assuming that all projects were created in the past |
|
|
|
|
expect(query('projects', 'created_at', '>d', Time.now).size).to eq(0) |
|
|
|
|
expect(cost_query('projects', 'created_at', '>d', Time.zone.now).size).to eq(0) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
describe 'arity' do |
|
|
|
|