Add basic tests for the local_discomfort formula

This commit is contained in:
Manuel Bustillo 2024-07-25 11:24:49 +02:00
parent f15fbf5431
commit d50ee7cb06
5 changed files with 61 additions and 0 deletions

View File

@ -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

View File

@ -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

8
spec/factories/guest.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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