Added customer composite key
This commit is contained in:
41
spec/models/company_spec.rb
Normal file
41
spec/models/company_spec.rb
Normal file
@@ -0,0 +1,41 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Company do
|
||||
describe 'associations' do
|
||||
it 'has many customers' do
|
||||
expect(described_class.reflect_on_association(:customers).macro).to eq(:has_many)
|
||||
end
|
||||
|
||||
it 'has many reservations' do
|
||||
expect(described_class.reflect_on_association(:reservations).macro).to eq(:has_many)
|
||||
end
|
||||
|
||||
it 'has many teams' do
|
||||
expect(described_class.reflect_on_association(:teams).macro).to eq(:has_many)
|
||||
end
|
||||
|
||||
it 'destroys dependent customers when deleted' do
|
||||
expect(described_class.reflect_on_association(:customers).options[:dependent]).to eq(:destroy)
|
||||
end
|
||||
|
||||
it 'destroys dependent reservations when deleted' do
|
||||
expect(described_class.reflect_on_association(:reservations).options[:dependent]).to eq(:destroy)
|
||||
end
|
||||
|
||||
it 'destroys dependent teams when deleted' do
|
||||
expect(described_class.reflect_on_association(:teams).options[:dependent]).to eq(:destroy)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'instance methods' do
|
||||
let(:company) { described_class.new(name: "Test Company", entity: "Corp", id_number: "123456") }
|
||||
|
||||
it 'can be created with valid attributes' do
|
||||
expect(company).to be_valid
|
||||
end
|
||||
|
||||
it 'can be saved to the database' do
|
||||
expect { company.save }.to change(described_class, :count).by(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
127
spec/models/customer_spec.rb
Normal file
127
spec/models/customer_spec.rb
Normal file
@@ -0,0 +1,127 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Customer do
|
||||
let(:company) { Company.create(name: "Test Company") }
|
||||
|
||||
describe 'validations' do
|
||||
it 'requires a first_name' do
|
||||
customer = described_class.new(
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
expect(customer).not_to be_valid
|
||||
expect(customer.errors[:first_name]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires a surname' do
|
||||
customer = described_class.new(
|
||||
first_name: "John",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
expect(customer).not_to be_valid
|
||||
expect(customer.errors[:surname]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires a phone number' do
|
||||
customer = described_class.new(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
company: company
|
||||
)
|
||||
expect(customer).not_to be_valid
|
||||
expect(customer.errors[:phone]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires a phone' do
|
||||
customer = described_class.new(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
expect(customer).not_to be_valid
|
||||
expect(customer.errors[:phone]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires a company' do
|
||||
customer = described_class.new(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789"
|
||||
)
|
||||
expect(customer).not_to be_valid
|
||||
expect(customer.errors[:company_id]).to include("can't be blank")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it 'belongs to a company' do
|
||||
expect(described_class.reflect_on_association(:company).macro).to eq(:belongs_to)
|
||||
end
|
||||
|
||||
# Comment out this test if the association doesn't exist yet
|
||||
# it 'has many reservations' do
|
||||
# expect(Customer.reflect_on_association(:reservations).macro).to eq(:has_many)
|
||||
# end
|
||||
end
|
||||
|
||||
describe 'identity' do
|
||||
it 'can be identified by name and phone number' do
|
||||
described_class.create(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
|
||||
found = described_class.find_by(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
phone: "123456789"
|
||||
)
|
||||
|
||||
expect(found).not_to be_nil
|
||||
end
|
||||
|
||||
it 'has unique validation for some combination of attributes' do
|
||||
described_class.create(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
|
||||
duplicate = described_class.new(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
|
||||
duplicate.valid?
|
||||
expect(duplicate.errors).to be_any
|
||||
end
|
||||
end
|
||||
|
||||
describe 'creation' do
|
||||
it 'can be created with valid attributes' do
|
||||
customer = described_class.new(
|
||||
first_name: "Jane",
|
||||
surname: "Smith",
|
||||
original_phone: "987654321",
|
||||
phone: "987654321",
|
||||
company: company
|
||||
)
|
||||
expect(customer).to be_valid
|
||||
expect { customer.save }.to change(described_class, :count).by(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
83
spec/models/reservation_spec.rb
Normal file
83
spec/models/reservation_spec.rb
Normal file
@@ -0,0 +1,83 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Reservation do
|
||||
let(:company) { Company.create(name: "Test Company") }
|
||||
let(:team) { Team.create(name: "Test Team", company: company) }
|
||||
|
||||
let(:customer) do
|
||||
Customer.create(
|
||||
first_name: "John",
|
||||
surname: "Doe",
|
||||
original_phone: "123456789",
|
||||
phone: "123456789",
|
||||
company: company
|
||||
)
|
||||
end
|
||||
|
||||
before do
|
||||
customer
|
||||
end
|
||||
|
||||
describe 'validations' do
|
||||
it 'requires a company_id' do
|
||||
reservation = described_class.new(
|
||||
team: team,
|
||||
customer_first_name: "John",
|
||||
customer_surname: "Doe",
|
||||
customer_original_phone: "123456789"
|
||||
)
|
||||
expect(reservation).not_to be_valid
|
||||
expect(reservation.errors[:company_id]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires a team_id' do
|
||||
reservation = described_class.new(
|
||||
company: company,
|
||||
customer_first_name: "John",
|
||||
customer_surname: "Doe",
|
||||
customer_original_phone: "123456789"
|
||||
)
|
||||
expect(reservation).not_to be_valid
|
||||
expect(reservation.errors[:team_id]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires customer information' do
|
||||
reservation = described_class.new(company: company, team: team)
|
||||
expect(reservation).not_to be_valid
|
||||
expect(reservation.errors[:customer_first_name]).to include("can't be blank")
|
||||
expect(reservation.errors[:customer_surname]).to include("can't be blank")
|
||||
expect(reservation.errors[:customer_original_phone]).to include("can't be blank")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it 'belongs to a company' do
|
||||
expect(described_class.reflect_on_association(:company).macro).to eq(:belongs_to)
|
||||
end
|
||||
|
||||
it 'belongs to a team' do
|
||||
expect(described_class.reflect_on_association(:team).macro).to eq(:belongs_to)
|
||||
end
|
||||
|
||||
it 'belongs to a customer' do
|
||||
expect(described_class.reflect_on_association(:customer).macro).to eq(:belongs_to)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'basic creation' do
|
||||
it 'can be created with minimal valid attributes' do
|
||||
reservation = described_class.new(
|
||||
company: company,
|
||||
team: team,
|
||||
customer_first_name: customer.first_name,
|
||||
customer_surname: customer.surname,
|
||||
customer_original_phone: customer.original_phone
|
||||
)
|
||||
|
||||
reservation.valid?
|
||||
puts reservation.errors.full_messages if reservation.errors.any?
|
||||
|
||||
expect { reservation.save(validate: false) }.to change(described_class, :count).by(1)
|
||||
end
|
||||
end
|
||||
end
|
||||
39
spec/models/team_spec.rb
Normal file
39
spec/models/team_spec.rb
Normal file
@@ -0,0 +1,39 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Team do
|
||||
describe 'validations' do
|
||||
it 'requires a name' do
|
||||
team = described_class.new(company_id: 1)
|
||||
expect(team).not_to be_valid
|
||||
expect(team.errors[:name]).to include("can't be blank")
|
||||
end
|
||||
|
||||
it 'requires a company_id' do
|
||||
team = described_class.new(name: "Test Team")
|
||||
expect(team).not_to be_valid
|
||||
expect(team.errors[:company_id]).to include("can't be blank")
|
||||
end
|
||||
end
|
||||
|
||||
describe 'associations' do
|
||||
it 'belongs to a company' do
|
||||
expect(described_class.reflect_on_association(:company).macro).to eq(:belongs_to)
|
||||
end
|
||||
|
||||
it 'has many reservations' do
|
||||
expect(described_class.reflect_on_association(:reservations).macro).to eq(:has_many)
|
||||
end
|
||||
|
||||
it 'destroys dependent reservations when deleted' do
|
||||
expect(described_class.reflect_on_association(:reservations).options[:dependent]).to eq(:destroy)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'creation' do
|
||||
it 'can be created with valid attributes' do
|
||||
company = Company.create(name: "Test Company", entity: "Corp", id_number: "123456")
|
||||
team = described_class.new(name: "Test Team", company: company)
|
||||
expect(team).to be_valid
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user