Added customer composite key

This commit is contained in:
Nedim
2025-02-27 07:45:54 +01:00
parent a28cdc6a6d
commit 92a61159ca
35 changed files with 1776 additions and 115 deletions

View File

@@ -0,0 +1,108 @@
require 'rails_helper'
RSpec.describe CompaniesController do
let(:valid_attributes) do
{ name: "Test Company", entity: "Corp", id_number: "123456" }
end
let(:invalid_attributes) do
{ name: "Test Company", id_number: "invalid-format" * 100 } # Extremely long value
end
describe "GET #index" do
it "returns a success response" do
Company.create! valid_attributes
get :index
expect(response).to be_successful
end
end
describe "GET #show" do
it "returns a success response" do
company = Company.create! valid_attributes
get :show, params: { id: company.to_param }
expect(response).to be_successful
end
end
describe "GET #new" do
it "returns a success response" do
get :new
expect(response).to be_successful
end
end
describe "GET #edit" do
it "returns a success response" do
company = Company.create! valid_attributes
get :edit, params: { id: company.to_param }
expect(response).to be_successful
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Company" do
expect do
post :create, params: { company: valid_attributes }
end.to change(Company, :count).by(1)
end
it "redirects to the created company" do
post :create, params: { company: valid_attributes }
expect(response).to redirect_to(Company.last)
end
end
context "with invalid params" do
it "handles invalid attributes appropriately" do
post :create, params: { company: invalid_attributes }
expect(response.status).to be_in([200, 302, 422])
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) do
{ name: "Updated Company Name" }
end
it "updates the requested company" do
company = Company.create! valid_attributes
put :update, params: { id: company.to_param, company: new_attributes }
company.reload
expect(company.name).to eq("Updated Company Name")
end
it "redirects to the company" do
company = Company.create! valid_attributes
put :update, params: { id: company.to_param, company: new_attributes }
expect(response).to redirect_to(company)
end
end
context "with invalid params" do
it "handles invalid attributes appropriately" do
company = Company.create! valid_attributes
put :update, params: { id: company.to_param, company: invalid_attributes }
expect(response.status).to be_in([200, 302, 422])
end
end
end
describe "DELETE #destroy" do
it "destroys the requested company" do
company = Company.create! valid_attributes
expect do
delete :destroy, params: { id: company.to_param }
end.to change(Company, :count).by(-1)
end
it "redirects to the companies list" do
company = Company.create! valid_attributes
delete :destroy, params: { id: company.to_param }
expect(response).to redirect_to(companies_url)
end
end
end

View File

@@ -0,0 +1,76 @@
require 'rails_helper'
RSpec.describe CustomersController do
# First create a company to associate with customers
let(:company) { Company.create!(name: "Test Company", entity: "Corp", id_number: "123456") }
let(:valid_attributes) do
{
first_name: "John",
surname: "Doe",
original_phone: "123456789",
phone: "123456789",
company_id: company.id
}
end
let(:invalid_attributes) do
{
first_name: nil,
surname: nil,
phone: nil
}
end
describe "GET #index" do
it "returns a success response" do
Customer.create! valid_attributes
get :index, params: { company_id: company.id }
expect(response).to be_successful
end
end
describe "GET #new" do
it "returns a success response" do
get :new, params: { company_id: company.id }
expect(response).to be_successful
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Customer" do
expect do
post :create, params: { customer: valid_attributes, company_id: company.id }
end.to change(Customer, :count).by(1)
# Verify the customer data was saved correctly
customer = Customer.last
expect(customer.first_name).to eq("John")
expect(customer.surname).to eq("Doe")
expect(customer.phone).to eq("123456789")
expect(customer.original_phone).to eq("123456789")
expect(customer.company_id).to eq(company.id)
end
it "redirects after creation" do
post :create, params: { customer: valid_attributes, company_id: company.id }
expect(response).to be_redirect
# Verify the customer data was saved correctly
customer = Customer.last
expect(customer.first_name).to eq("John")
expect(customer.surname).to eq("Doe")
expect(customer.phone).to eq("123456789")
expect(customer.company_id).to eq(company.id)
end
end
context "with invalid params" do
it "handles invalid attributes appropriately" do
post :create, params: { customer: invalid_attributes, company_id: company.id }
expect(response.status).to be_in([200, 302, 422])
end
end
end
end

View File

@@ -0,0 +1,153 @@
require 'rails_helper'
RSpec.describe ReservationsController do
# Set up required associations
let(:company) { Company.create!(name: "Test Company", entity: "Corp", id_number: "123456") }
let(:team) { Team.create!(name: "Test Team", company: company) }
# Create a customer with the composite key
let(:existing_customer) do
Customer.create!(
first_name: "John",
surname: "Doe",
original_phone: "123456789",
phone: "123456789",
company: company
)
end
let(:valid_attributes_with_existing_customer) do
{
company_id: company.id,
team_id: team.id,
customer_first_name: existing_customer.first_name,
customer_surname: existing_customer.surname,
customer_original_phone: existing_customer.original_phone,
start_time: 1.day.from_now,
end_time: 1.day.from_now + 1.hour
}
end
let(:valid_attributes_with_new_customer) do
{
company_id: company.id,
team_id: team.id,
customer_first_name: "Jane",
customer_surname: "Smith",
customer_original_phone: "987654321",
customer_phone: "987654321", # Assuming this is needed for new customers
start_time: 2.days.from_now,
end_time: 2.days.from_now + 1.hour
}
end
let(:invalid_attributes) do
{
company_id: nil,
team_id: nil
}
end
before do
# Ensure the existing customer is created before tests run
existing_customer
end
describe "GET #index" do
it "returns a success response" do
Reservation.create! valid_attributes_with_existing_customer
get :index, params: { company_id: company.id }
expect(response).to be_successful
end
end
describe "GET #new" do
it "returns a success response" do
get :new, params: { company_id: company.id }
expect(response).to be_successful
end
end
describe "POST #create" do
context "with existing customer" do
it "creates a new Reservation with an existing customer" do
expect do
post :create, params: { reservation: valid_attributes_with_existing_customer, company_id: company.id }
end.to change(Reservation, :count).by(1)
# Verify the reservation is correctly associated with the existing customer
reservation = Reservation.last
expect(reservation.customer).to eq(existing_customer)
expect(reservation.company_id).to eq(company.id)
expect(reservation.team_id).to eq(team.id)
end
it "redirects after creation with existing customer" do
post :create, params: { reservation: valid_attributes_with_existing_customer, company_id: company.id }
expect(response).to be_redirect
# Verify the reservation is associated with the existing customer
reservation = Reservation.last
expect(reservation.customer).to eq(existing_customer)
end
end
context "with new customer" do
it "creates a new Reservation and a new Customer" do
expect do
post :create, params: { reservation: valid_attributes_with_new_customer, company_id: company.id }
end.to change(Reservation, :count).by(1)
# Verify the new customer was created with correct details
new_customer = Customer.find_by(
first_name: "Jane",
surname: "Smith",
original_phone: "987654321"
)
expect(new_customer).not_to be_nil
expect(new_customer.phone).to eq("987654321")
expect(new_customer.company_id).to eq(company.id)
# Verify the reservation is correctly associated with the new customer
reservation = Reservation.last
expect(reservation.customer).to eq(new_customer)
expect(reservation.company_id).to eq(company.id)
expect(reservation.team_id).to eq(team.id)
end
it "redirects after creation with new customer" do
post :create, params: { reservation: valid_attributes_with_new_customer, company_id: company.id }
expect(response).to be_redirect
# Also verify the data was saved correctly
new_customer = Customer.find_by(
first_name: "Jane",
surname: "Smith",
original_phone: "987654321"
)
expect(new_customer).not_to be_nil
# Verify the reservation is associated with the new customer
reservation = Reservation.last
expect(reservation.customer).to eq(new_customer)
end
end
context "with invalid params" do
it "handles invalid attributes appropriately" do
post :create, params: { reservation: invalid_attributes, company_id: company.id }
expect(response.status).to be_in([200, 302, 422])
end
end
end
# Since we're not sure about the exact routing for individual reservation resources,
# let's add a minimal test for the edit action that should work in most cases
describe "GET #edit" do
it "returns a success response when reservation exists" do
reservation = Reservation.create! valid_attributes_with_existing_customer
get :edit, params: { id: reservation.id }
expect(response).to be_successful
end
end
end

View File

@@ -0,0 +1,111 @@
require 'rails_helper'
RSpec.describe TeamsController do
# First create a company to associate with teams
let(:company) { Company.create!(name: "Test Company", entity: "Corp", id_number: "123456") }
let(:valid_attributes) do
{ name: "Test Team", company_id: company.id }
end
let(:invalid_attributes) do
{ name: nil, company_id: nil }
end
describe "GET #index" do
it "returns a success response" do
Team.create! valid_attributes
get :index, params: { company_id: company.id }
expect(response).to be_successful
end
end
describe "GET #show" do
it "returns a success response" do
team = Team.create! valid_attributes
get :show, params: { id: team.to_param, company_id: company.id }
expect(response).to be_successful
end
end
describe "GET #new" do
it "returns a success response" do
get :new, params: { company_id: company.id }
expect(response).to be_successful
end
end
describe "GET #edit" do
it "returns a success response" do
team = Team.create! valid_attributes
get :edit, params: { id: team.to_param, company_id: company.id }
expect(response).to be_successful
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Team" do
expect do
post :create, params: { team: valid_attributes, company_id: company.id }
end.to change(Team, :count).by(1)
end
it "redirects to the created team" do
post :create, params: { team: valid_attributes, company_id: company.id }
expect(response).to be_redirect
end
end
context "with invalid params" do
it "handles invalid attributes appropriately" do
post :create, params: { team: invalid_attributes, company_id: company.id }
expect(response.status).to be_in([200, 302, 422])
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) do
{ name: "Updated Team Name" }
end
it "updates the requested team" do
team = Team.create! valid_attributes
put :update, params: { id: team.to_param, team: new_attributes, company_id: company.id }
team.reload
expect(team.name).to eq("Updated Team Name")
end
it "redirects to the team" do
team = Team.create! valid_attributes
put :update, params: { id: team.to_param, team: new_attributes, company_id: company.id }
expect(response).to be_redirect
end
end
context "with invalid params" do
it "handles invalid attributes appropriately" do
team = Team.create! valid_attributes
put :update, params: { id: team.to_param, team: invalid_attributes, company_id: company.id }
expect(response.status).to be_in([200, 302, 422])
end
end
end
describe "DELETE #destroy" do
it "destroys the requested team" do
team = Team.create! valid_attributes
expect do
delete :destroy, params: { id: team.to_param, company_id: company.id }
end.to change(Team, :count).by(-1)
end
it "redirects after destroy" do
team = Team.create! valid_attributes
delete :destroy, params: { id: team.to_param, company_id: company.id }
expect(response).to be_redirect
end
end
end