wedding-planner/spec/services/tables/discomfort_calculator_spec.rb

232 lines
7.8 KiB
Ruby
Raw Normal View History

2024-12-28 18:07:22 +01:00
# frozen_string_literal: true
2024-10-28 22:07:35 +00:00
# Copyright (C) 2024 Manuel Bustillo
require 'rails_helper'
module Tables
RSpec.describe DiscomfortCalculator do
let(:calculator) { described_class.new(table:) }
let(:family) { create(:group, name: 'family') }
let(:friends) { create(:group, name: 'friends') }
let(:work) { create(:group, name: 'work') }
let(:school) { create(:group, name: 'school') }
2024-11-10 11:34:26 +01:00
describe '#calculate' do
before do
2024-12-28 18:07:22 +01:00
allow(calculator).to receive_messages(table_size_penalty: 2, cohesion_discomfort: 3)
2024-11-10 11:34:26 +01:00
end
let(:table) { Table.new(create_list(:guest, 6)) }
it 'returns the sum of the table size penalty and the average cohesion penalty', :aggregate_failures do
expect(calculator.calculate).to eq(7)
expect(calculator.breakdown).to eq(table_size_penalty: 2, cohesion_penalty: 5)
2024-11-10 11:34:26 +01:00
end
end
describe '#table_size_penalty' do
before do
table.min_per_table = 5
table.max_per_table = 7
end
2024-12-28 18:07:22 +01:00
context 'when the number of guests is in the lower bound' do
let(:table) { Table.new(create_list(:guest, 5)) }
it { expect(calculator.send(:table_size_penalty)).to eq(0) }
end
context 'when the number of guests is within the table size limits' do
let(:table) { Table.new(create_list(:guest, 6)) }
it { expect(calculator.send(:table_size_penalty)).to eq(0) }
end
context 'when the number of guests is in the upper bound' do
let(:table) { Table.new(create_list(:guest, 7)) }
it { expect(calculator.send(:table_size_penalty)).to eq(0) }
end
context 'when the number of guests is one unit below the lower bound' do
let(:table) { Table.new(create_list(:guest, 4)) }
it { expect(calculator.send(:table_size_penalty)).to eq(5) }
end
context 'when the number of guests is two units below the lower bound' do
let(:table) { Table.new(create_list(:guest, 3)) }
it { expect(calculator.send(:table_size_penalty)).to eq(10) }
end
context 'when the number of guests is one unit above the upper bound' do
let(:table) { Table.new(create_list(:guest, 8)) }
it { expect(calculator.send(:table_size_penalty)).to eq(5) }
end
context 'when the number of guests is two units above the upper bound' do
let(:table) { Table.new(create_list(:guest, 9)) }
it { expect(calculator.send(:table_size_penalty)).to eq(10) }
end
end
describe '#cohesion_discomfort' do
before do
# Overridden in each test except trivial cases
allow(AffinityGroupsHierarchy.instance).to receive(:distance).and_call_original
%w[family friends work school].each do |group|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(group, group).and_return(0)
end
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(family.id, friends.id).and_return(nil)
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(friends.id, work.id).and_return(1)
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(family.id, work.id).and_return(2)
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(family.id, school.id).and_return(3)
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(friends.id, school.id).and_return(4)
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with(work.id, school.id).and_return(5)
end
2024-12-28 18:07:22 +01:00
context 'when the table contains just two guests' do
context 'when they belong to the same group' do
let(:table) { create_list(:guest, 2, group: family) }
it { expect(calculator.send(:cohesion_discomfort)).to eq(0) }
end
context 'when they belong to completely unrelated groups' do
let(:table) do
[
create(:guest, group: family),
create(:guest, group: friends)
]
end
2024-12-28 18:07:22 +01:00
it { expect(calculator.send(:cohesion_discomfort)).to eq(1) }
end
context 'when they belong to groups at a distance of 1' do
let(:table) do
[
create(:guest, group: friends),
create(:guest, group: work)
]
end
it { expect(calculator.send(:cohesion_discomfort)).to eq(0.5) }
end
context 'when they belong to groups at a distance of 2' do
let(:table) do
[
create(:guest, group: family),
create(:guest, group: work)
]
end
it { expect(calculator.send(:cohesion_discomfort)).to eq(Rational(2, 3)) }
end
context 'when they belong to groups at a distance of 3' do
let(:table) do
[
create(:guest, group: family),
create(:guest, group: school)
]
end
it { expect(calculator.send(:cohesion_discomfort)).to eq(Rational(3, 4)) }
end
end
context 'when the table contains three guests' do
let(:table) do
[
create(:guest, group: family),
create(:guest, group: friends),
create(:guest, group: work)
]
end
it 'returns the sum of the penalties for each pair of guests' do
expect(calculator.send(:cohesion_discomfort)).to eq(1 + Rational(1, 2) + Rational(2, 3))
end
end
context 'when the table contains four guests of different groups' do
let(:table) do
[
create(:guest, group: family),
create(:guest, group: friends),
create(:guest, group: work),
create(:guest, group: school)
]
end
it 'returns the sum of the penalties for each pair of guests' do
expect(calculator.send(:cohesion_discomfort))
.to eq(1 + Rational(1, 2) + Rational(2, 3) + Rational(3, 4) + Rational(4, 5) + Rational(5, 6))
end
end
context 'when the table contains four guests of two evenly split groups' do
let(:table) do
[
create_list(:guest, 2, group: family),
create_list(:guest, 2, group: friends)
].flatten
end
it 'returns the sum of the penalties for each pair of guests' do
expect(calculator.send(:cohesion_discomfort)).to eq(4)
end
end
context 'when the table contains six guests of two unevenly split groups' do
let(:table) do
[
create_list(:guest, 2, group: family),
create_list(:guest, 4, group: friends)
].flatten
end
it 'returns the sum of the penalties for each pair of guests' do
expect(calculator.send(:cohesion_discomfort)).to eq(8)
end
end
context 'when the table contains six guests of three evenly split groups' do
let(:table) do
[
create_list(:guest, 2, group: family),
create_list(:guest, 2, group: friends),
create_list(:guest, 2, group: work)
].flatten
end
it 'returns the sum of the penalties for each pair of guests' do
2024-12-28 18:07:22 +01:00
expect(calculator.send(:cohesion_discomfort)).to eq((4 * 1) + (4 * Rational(1, 2)) + (4 * Rational(2, 3)))
end
end
context 'when the table contains six guests of three unevenly split groups' do
let(:table) do
[
create_list(:guest, 3, group: family),
create_list(:guest, 2, group: friends),
create_list(:guest, 1, group: work)
].flatten
end
it 'returns the sum of the penalties for each pair of guests' do
2024-12-28 18:07:22 +01:00
expect(calculator.send(:cohesion_discomfort)).to eq((6 * 1) + (2 * Rational(1, 2)) + (3 * Rational(2, 3)))
end
end
end
end
end