From 2dd11bb83dae12cc7e3ed261a3ecf9ac29c60720 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 1 Aug 2024 20:45:01 +0200 Subject: [PATCH 1/4] Initial tests for the swap class --- app/services/tables/distribution.rb | 1 + spec/services/tables/swap_spec.rb | 33 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 spec/services/tables/swap_spec.rb diff --git a/app/services/tables/distribution.rb b/app/services/tables/distribution.rb index d10d73f..42924c2 100644 --- a/app/services/tables/distribution.rb +++ b/app/services/tables/distribution.rb @@ -7,6 +7,7 @@ module Tables def initialize(min_per_table:, max_per_table:) @min_per_table = min_per_table @max_per_table = max_per_table + @tables = [] end def random_distribution(people) diff --git a/spec/services/tables/swap_spec.rb b/spec/services/tables/swap_spec.rb new file mode 100644 index 0000000..bd246e2 --- /dev/null +++ b/spec/services/tables/swap_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +module Tables + RSpec.describe Swap do + describe '#each' do + let(:initial_solution) do + Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| + distribution.tables << %i[a b] + distribution.tables << %i[c d] + end + end + + let(:swaps) do + acc = [] + described_class.new(initial_solution).each do |solution| + acc << solution.tables.map(&:dup) + end + acc + end + + it 'yields all possible swaps' do + expect(swaps.count).to eq(4) + end + + it 'swaps the people' do + expect(swaps).to include([%i[a d], %i[c b]]) + expect(swaps).to include([%i[b c], %i[d a]]) + expect(swaps).to include([%i[a c], %i[d b]]) + expect(swaps).to include([%i[b d], %i[c a]]) + end + end + end +end From b21d323e364474e7ae3310f756f6bc7e29027a1b Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 1 Aug 2024 20:46:11 +0200 Subject: [PATCH 2/4] Refactor specs --- spec/services/tables/swap_spec.rb | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/spec/services/tables/swap_spec.rb b/spec/services/tables/swap_spec.rb index bd246e2..c2c68cb 100644 --- a/spec/services/tables/swap_spec.rb +++ b/spec/services/tables/swap_spec.rb @@ -18,15 +18,13 @@ module Tables acc end - it 'yields all possible swaps' do - expect(swaps.count).to eq(4) - end - - it 'swaps the people' do - expect(swaps).to include([%i[a d], %i[c b]]) - expect(swaps).to include([%i[b c], %i[d a]]) - expect(swaps).to include([%i[a c], %i[d b]]) - expect(swaps).to include([%i[b d], %i[c a]]) + it 'yields all possible swaps between the tables' do + expect(swaps).to contain_exactly( + [%i[a d], %i[c b]], + [%i[b c], %i[d a]], + [%i[a c], %i[d b]], + [%i[b d], %i[c a]] + ) end end end From a38fefeb1fb340c8e6583097af97d141cba9206b Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 1 Aug 2024 20:49:07 +0200 Subject: [PATCH 3/4] Include additional specs --- spec/services/tables/swap_spec.rb | 53 +++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/spec/services/tables/swap_spec.rb b/spec/services/tables/swap_spec.rb index c2c68cb..4087ec4 100644 --- a/spec/services/tables/swap_spec.rb +++ b/spec/services/tables/swap_spec.rb @@ -3,13 +3,6 @@ require 'rails_helper' module Tables RSpec.describe Swap do describe '#each' do - let(:initial_solution) do - Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| - distribution.tables << %i[a b] - distribution.tables << %i[c d] - end - end - let(:swaps) do acc = [] described_class.new(initial_solution).each do |solution| @@ -18,13 +11,45 @@ module Tables acc end - it 'yields all possible swaps between the tables' do - expect(swaps).to contain_exactly( - [%i[a d], %i[c b]], - [%i[b c], %i[d a]], - [%i[a c], %i[d b]], - [%i[b d], %i[c a]] - ) + context 'when there are two tables with two people each' do + let(:initial_solution) do + Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| + distribution.tables << %i[a b] + distribution.tables << %i[c d] + end + end + + it 'yields all possible swaps between the tables' do + expect(swaps).to contain_exactly( + [%i[a d], %i[c b]], + [%i[b c], %i[d a]], + [%i[a c], %i[d b]], + [%i[b d], %i[c a]] + ) + end + end + + context 'when there are two tables with three people each' do + let(:initial_solution) do + Distribution.new(min_per_table: 3, max_per_table: 3).tap do |distribution| + distribution.tables << %i[a b c] + distribution.tables << %i[d e f] + end + end + + it 'yields all possible swaps between the tables' do + expect(swaps).to contain_exactly( + [%i[b c d], %i[e f a]], + [%i[b c e], %i[f d a]], + [%i[b c f], %i[d e a]], + [%i[c a d], %i[e f b]], + [%i[c a e], %i[f d b]], + [%i[c a f], %i[d e b]], + [%i[a b d], %i[e f c]], + [%i[a b e], %i[f d c]], + [%i[a b f], %i[d e c]] + ) + end end end end From ebc75866704a62606b5ad80c26c4f60a36c1417e Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 1 Aug 2024 20:50:26 +0200 Subject: [PATCH 4/4] Include examples with three tables --- spec/services/tables/swap_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/services/tables/swap_spec.rb b/spec/services/tables/swap_spec.rb index 4087ec4..3663d6c 100644 --- a/spec/services/tables/swap_spec.rb +++ b/spec/services/tables/swap_spec.rb @@ -51,6 +51,33 @@ module Tables ) end end + + context 'when there are three tables with two people each' do + let(:initial_solution) do + Distribution.new(min_per_table: 2, max_per_table: 2).tap do |distribution| + distribution.tables << %i[a b] + distribution.tables << %i[c d] + distribution.tables << %i[e f] + end + end + + it 'yields all possible swaps between the tables' do + expect(swaps).to contain_exactly( + [%i[b c], %i[d a], %i[e f]], + [%i[b d], %i[c a], %i[e f]], + [%i[a c], %i[d b], %i[e f]], + [%i[a d], %i[c b], %i[e f]], + [%i[b e], %i[c d], %i[f a]], + [%i[b f], %i[c d], %i[e a]], + [%i[a e], %i[c d], %i[f b]], + [%i[a f], %i[c d], %i[e b]], + [%i[a b], %i[d e], %i[f c]], + [%i[a b], %i[d f], %i[e c]], + [%i[a b], %i[c e], %i[f d]], + [%i[a b], %i[c f], %i[e d]] + ) + end + end end end end