2024-10-31 23:34:15 +00:00
|
|
|
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
|
2024-10-28 22:38:47 +01:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
module Expenses
|
|
|
|
RSpec.describe TotalQuery do
|
|
|
|
describe '#call' do
|
|
|
|
let(:response) { described_class.new.call }
|
|
|
|
|
|
|
|
before do
|
|
|
|
create_list(:guest, 2, status: :confirmed)
|
|
|
|
create_list(:guest, 3, status: :considered)
|
|
|
|
create_list(:guest, 4, status: :invited)
|
|
|
|
create_list(:guest, 5, status: :tentative)
|
|
|
|
create_list(:guest, 6, status: :declined)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there is no expense" do
|
|
|
|
it "returns zero in all values", :aggregate_failures do
|
|
|
|
expect(response["fixed"]).to be_zero
|
|
|
|
expect(response["fixed_count"]).to be_zero
|
|
|
|
expect(response["variable"]).to be_zero
|
|
|
|
expect(response["variable_count"]).to be_zero
|
|
|
|
expect(response["total"]).to be_zero
|
|
|
|
expect(response["total_count"]).to be_zero
|
|
|
|
expect(response["max_projected"]).to be_zero
|
|
|
|
expect(response["per_person"]).to be_zero
|
2024-10-28 23:01:00 +01:00
|
|
|
expect(response["confirmed_guests"]).to eq(2)
|
|
|
|
expect(response["projected_guests"]).to eq(2 + 4 + 5)
|
2024-10-28 22:38:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are only fixed expenses" do
|
|
|
|
before do
|
|
|
|
create(:expense, :fixed, amount: 100)
|
|
|
|
create(:expense, :fixed, amount: 200)
|
|
|
|
end
|
|
|
|
|
2024-10-28 22:57:03 +01:00
|
|
|
it "returns the sum of fixed expenses", :aggregate_failures do
|
2024-10-28 22:38:47 +01:00
|
|
|
expect(response["fixed"]).to eq(300)
|
|
|
|
expect(response["fixed_count"]).to eq(2)
|
|
|
|
expect(response["variable"]).to be_zero
|
|
|
|
expect(response["variable_count"]).to be_zero
|
|
|
|
expect(response["total"]).to eq(300)
|
|
|
|
expect(response["total_count"]).to eq(2)
|
|
|
|
expect(response["max_projected"]).to eq(300)
|
|
|
|
expect(response["per_person"]).to eq(150)
|
2024-10-28 23:01:00 +01:00
|
|
|
expect(response["confirmed_guests"]).to eq(2)
|
|
|
|
expect(response["projected_guests"]).to eq(2 + 4 + 5)
|
2024-10-28 22:38:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are only variable expenses" do
|
|
|
|
before do
|
|
|
|
create(:expense, :per_person, amount: 100)
|
|
|
|
create(:expense, :per_person, amount: 200)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns zero in the values and nonzero in the count", :aggregate_failures do
|
|
|
|
expect(response["fixed"]).to be_zero
|
|
|
|
expect(response["fixed_count"]).to be_zero
|
|
|
|
expect(response["variable"]).to eq(300)
|
|
|
|
expect(response["variable_count"]).to eq(2)
|
|
|
|
expect(response["total"]).to eq(2*300)
|
|
|
|
expect(response["total_count"]).to eq(2)
|
|
|
|
expect(response["max_projected"]).to eq(11*300)
|
2024-10-28 23:01:00 +01:00
|
|
|
expect(response["confirmed_guests"]).to eq(2)
|
|
|
|
expect(response["projected_guests"]).to eq(2 + 4 + 5)
|
2024-10-28 22:38:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there are both fixed and variable expenses" do
|
|
|
|
before do
|
|
|
|
create(:expense, :fixed, amount: 100)
|
|
|
|
create(:expense, :fixed, amount: 200)
|
|
|
|
create(:expense, :per_person, amount: 50)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the sum of fixed and variable expenses", :aggregate_failures do
|
|
|
|
expect(response["fixed"]).to eq(300)
|
|
|
|
expect(response["fixed_count"]).to eq(2)
|
|
|
|
expect(response["variable"]).to eq(50)
|
|
|
|
expect(response["variable_count"]).to eq(1)
|
|
|
|
expect(response["total"]).to eq(100 + 200 + 50 * 2)
|
|
|
|
expect(response["total_count"]).to eq(3)
|
|
|
|
expect(response["max_projected"]).to eq(100 + 200 + 11*50)
|
|
|
|
expect(response["per_person"]).to eq(200)
|
2024-10-28 23:01:00 +01:00
|
|
|
expect(response["confirmed_guests"]).to eq(2)
|
|
|
|
expect(response["projected_guests"]).to eq(2 + 4 + 5)
|
2024-10-28 22:38:47 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|