Some checks failed
		
		
	
	Run unit tests / copyright_notice (pull_request) Successful in 1m50s
				
			Run unit tests / rubocop (pull_request) Failing after 2m12s
				
			Run unit tests / check-licenses (pull_request) Successful in 2m50s
				
			Run unit tests / unit_tests (pull_request) Successful in 3m48s
				
			Run unit tests / build-static-assets (pull_request) Successful in 1h45m6s
				
			
		
			
				
	
	
		
			63 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			63 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
# Copyright (C) 2024-2025 LibreWeddingPlanner contributors
 | 
						|
 | 
						|
# frozen_string_literal: true
 | 
						|
 | 
						|
require 'rails_helper'
 | 
						|
 | 
						|
module Tables
 | 
						|
  RSpec.describe Distribution do
 | 
						|
    let(:tables_arrangement) { TablesArrangement.create! }
 | 
						|
 | 
						|
    around do |example|
 | 
						|
      ActsAsTenant.with_tenant(create(:wedding)) do
 | 
						|
        example.run
 | 
						|
      end
 | 
						|
    end
 | 
						|
 | 
						|
    describe '#save!' do
 | 
						|
      let(:people) { create_list(:guest, 2, status: :invited) }
 | 
						|
      let(:distribution) do
 | 
						|
        described_class.new(min_per_table: 5, max_per_table: 10, tables_arrangement_id: tables_arrangement.id)
 | 
						|
                       .tap { |d| d.random_distribution(people) }
 | 
						|
      end
 | 
						|
 | 
						|
      context 'when tables_arrangement_id is nil' do
 | 
						|
        it { expect { distribution.save! }.to change(TablesArrangement, :count).by(1) }
 | 
						|
        it { expect { distribution.save! }.to change(Seat, :count).by(2) }
 | 
						|
      end
 | 
						|
 | 
						|
      context 'when tables_arrangement_id is set' do
 | 
						|
        before do
 | 
						|
          existing_arrangement = TablesArrangement.create!
 | 
						|
 | 
						|
          existing_arrangement.seats.create!(guest: people.first, table_number: 1)
 | 
						|
          distribution.tables_arrangement_id = existing_arrangement.id
 | 
						|
        end
 | 
						|
 | 
						|
        it { expect { distribution.save! }.not_to(change(TablesArrangement, :count)) }
 | 
						|
        it { expect { distribution.save! }.to change(Seat, :count).by(1) }
 | 
						|
      end
 | 
						|
    end
 | 
						|
 | 
						|
    describe '#random_distribution' do
 | 
						|
      subject(:distribution) do
 | 
						|
        described_class.new(min_per_table: 5, max_per_table: 10, tables_arrangement_id: tables_arrangement.id)
 | 
						|
      end
 | 
						|
 | 
						|
      context 'when there are fewer people than the minimum per table' do
 | 
						|
        it 'creates one table' do
 | 
						|
          distribution.random_distribution([1, 2, 3, 4])
 | 
						|
          expect(distribution.tables.count).to eq(1)
 | 
						|
        end
 | 
						|
      end
 | 
						|
 | 
						|
      context 'when there are more people than the maximum per table' do
 | 
						|
        it 'creates multiple tables' do
 | 
						|
          distribution.random_distribution([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
 | 
						|
          expect(distribution.tables.count).to be > 1
 | 
						|
        end
 | 
						|
      end
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |