From d50ee7cb0653095374d4e4a62fb9676506e58f9a Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 25 Jul 2024 11:24:49 +0200 Subject: [PATCH] Add basic tests for the local_discomfort formula --- Gemfile | 1 + Gemfile.lock | 6 +++ spec/factories/guest.rb | 8 ++++ spec/rails_helper.rb | 1 + spec/services/tables/distribution_spec.rb | 45 +++++++++++++++++++++++ 5 files changed, 61 insertions(+) create mode 100644 spec/factories/guest.rb create mode 100644 spec/services/tables/distribution_spec.rb diff --git a/Gemfile b/Gemfile index 7ff1257..85d74c3 100644 --- a/Gemfile +++ b/Gemfile @@ -50,6 +50,7 @@ group :development, :test do gem 'rspec-rails', '~> 6.1.0' gem 'faker' gem 'pry' + gem "factory_bot_rails" end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index 90a4379..44a43e5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -100,6 +100,11 @@ GEM diff-lcs (1.5.0) drb (2.2.1) erubi (1.13.0) + factory_bot (6.4.6) + activesupport (>= 5.0.0) + factory_bot_rails (6.4.3) + factory_bot (~> 6.4) + railties (>= 5.0.0) faker (3.1.1) i18n (>= 1.8.11, < 2) globalid (1.2.1) @@ -268,6 +273,7 @@ DEPENDENCIES acts-as-taggable-on bootsnap debug + factory_bot_rails faker importmap-rails jbuilder diff --git a/spec/factories/guest.rb b/spec/factories/guest.rb new file mode 100644 index 0000000..f761dd9 --- /dev/null +++ b/spec/factories/guest.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :guest do + first_name { Faker::Name.first_name } + last_name { Faker::Name.last_name } + email { Faker::Internet.email } + phone { Faker::PhoneNumber.cell_phone } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index a15455f..c10abff 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -62,4 +62,5 @@ RSpec.configure do |config| config.filter_rails_from_backtrace! # arbitrary gems may also be filtered via: # config.filter_gems_from_backtrace("gem name") + config.include FactoryBot::Syntax::Methods end diff --git a/spec/services/tables/distribution_spec.rb b/spec/services/tables/distribution_spec.rb new file mode 100644 index 0000000..e9aca83 --- /dev/null +++ b/spec/services/tables/distribution_spec.rb @@ -0,0 +1,45 @@ +require 'rails_helper' + +module Tables + RSpec.describe Distribution do + describe '#local_discomfort' do + let(:service) { described_class.new(min_per_table: 5, max_per_table: 5) } + before { service.tables = [table] } + + context 'when there is just one group in the table' do + let(:table) do + create_list(:guest, 3).each do |guest| + guest.affinity_group_list.add('family') + guest.save! + end + end + + it { expect(service.send(:local_discomfort, table)).to eq(0) } + end + + context 'when the table contains two groups' do + let(:table) do + guests = create_list(:guest, 3) + guests[0].affinity_group_list.add('family') + guests[1].affinity_group_list.add('friends') + guests[2].affinity_group_list.add('family') + guests.each(&:save!) + end + + it { expect(service.send(:local_discomfort, table)).to eq(10) } + end + + context 'when the table contains three groups' do + let(:table) do + guests = create_list(:guest, 3) + guests[0].affinity_group_list.add('family') + guests[1].affinity_group_list.add('friends') + guests[2].affinity_group_list.add('work') + guests.each(&:save!) + end + + it { expect(service.send(:local_discomfort, table)).to eq(20) } + end + end + end +end