2024-07-25 11:24:49 +02:00
|
|
|
require 'rails_helper'
|
|
|
|
module Tables
|
2024-07-31 22:53:19 +02:00
|
|
|
RSpec.describe DiscomfortCalculator do
|
2024-08-01 18:43:56 +02:00
|
|
|
let(:calculator) { described_class.new(table) }
|
2024-08-01 18:52:22 +02:00
|
|
|
|
2024-08-01 18:43:56 +02:00
|
|
|
describe '#cohesion_penalty' do
|
|
|
|
context 'when the table contains just two guests' do
|
2024-07-25 11:24:49 +02:00
|
|
|
let(:table) do
|
2024-08-01 18:43:56 +02:00
|
|
|
[
|
|
|
|
create(:guest, affinity_group_list: ['family']),
|
|
|
|
create(:guest, affinity_group_list: ['friends'])
|
|
|
|
]
|
2024-07-25 11:24:49 +02:00
|
|
|
end
|
|
|
|
|
2024-08-01 18:43:56 +02:00
|
|
|
before do
|
|
|
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).and_return(distance)
|
|
|
|
end
|
2024-07-25 11:24:49 +02:00
|
|
|
|
2024-08-01 18:43:56 +02:00
|
|
|
context 'when they belong to the same group' do
|
|
|
|
let(:distance) { 0 }
|
|
|
|
|
|
|
|
it { expect(calculator.send(:cohesion_penalty)).to eq(0) }
|
2024-07-25 11:24:49 +02:00
|
|
|
end
|
|
|
|
|
2024-08-01 18:43:56 +02:00
|
|
|
context 'when they belong to completely unrelated groups' do
|
|
|
|
let(:distance) { nil }
|
2024-07-25 11:24:49 +02:00
|
|
|
|
2024-08-01 18:43:56 +02:00
|
|
|
it { expect(calculator.send(:cohesion_penalty)).to eq(1) }
|
2024-07-25 11:24:49 +02:00
|
|
|
end
|
|
|
|
|
2024-08-01 18:43:56 +02:00
|
|
|
context 'when they belong to groups at a distance of 1' do
|
|
|
|
let(:distance) { 1 }
|
|
|
|
|
|
|
|
it { expect(calculator.send(:cohesion_penalty)).to eq(0.5) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when they belong to groups at a distance of 2' do
|
|
|
|
let(:distance) { 2 }
|
|
|
|
|
|
|
|
it { expect(calculator.send(:cohesion_penalty)).to eq(Rational(2, 3)) }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when they belong to groups at a distance of 3' do
|
|
|
|
let(:distance) { 3 }
|
|
|
|
|
|
|
|
it { expect(calculator.send(:cohesion_penalty)).to eq(Rational(3, 4)) }
|
|
|
|
end
|
2024-07-25 11:24:49 +02:00
|
|
|
end
|
2024-08-01 18:52:22 +02:00
|
|
|
|
|
|
|
context 'when the table contains three guests' do
|
|
|
|
let(:table) do
|
|
|
|
[
|
|
|
|
create(:guest, affinity_group_list: ['family']),
|
|
|
|
create(:guest, affinity_group_list: ['friends']),
|
|
|
|
create(:guest, affinity_group_list: ['work'])
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'friends').and_return(nil)
|
|
|
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('friends', 'work').and_return(1)
|
|
|
|
allow(AffinityGroupsHierarchy.instance).to receive(:distance).with('family', 'work').and_return(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the sum of the penalties for each pair of guests' do
|
|
|
|
expect(calculator.send(:cohesion_penalty)).to eq(1 + Rational(1, 2) + Rational(2, 3))
|
|
|
|
end
|
|
|
|
end
|
2024-07-25 11:24:49 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|