Refactoring Tickets
- Tickets now independent from registration - Implemented money gem #419
This commit is contained in:
parent
5485fe0e7f
commit
5f8a8ac6d9
63 changed files with 1581 additions and 573 deletions
105
spec/models/ticket_purchase_spec.rb
Normal file
105
spec/models/ticket_purchase_spec.rb
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe TicketPurchase do
|
||||
|
||||
describe 'validations' do
|
||||
it 'has a valid factory' do
|
||||
expect(build(:ticket_purchase)).to be_valid
|
||||
end
|
||||
|
||||
it 'is not valid without a conference_id' do
|
||||
should validate_presence_of(:conference_id)
|
||||
end
|
||||
|
||||
it 'is not valid without a ticket_id' do
|
||||
should validate_presence_of(:ticket_id)
|
||||
end
|
||||
|
||||
it 'is not valid without a user_id' do
|
||||
should validate_presence_of(:user_id)
|
||||
end
|
||||
|
||||
it 'is not valid without a quantity' do
|
||||
should validate_presence_of(:quantity)
|
||||
end
|
||||
|
||||
it 'is not valid with a quantity equals zero' do
|
||||
should_not allow_value(0).for(:quantity)
|
||||
end
|
||||
|
||||
it 'is not valid with a quantity smaller than zero' do
|
||||
should_not allow_value(-1).for(:quantity)
|
||||
end
|
||||
|
||||
it 'is valid with a quantity greater than zero' do
|
||||
should allow_value(1).for(:quantity)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe 'self#purchase' do
|
||||
let!(:participant) { create(:user) }
|
||||
let!(:ticket_1) { create(:ticket) }
|
||||
let!(:ticket_2) { create(:ticket) }
|
||||
let!(:conference) { create(:conference, tickets: [ticket_1, ticket_2]) }
|
||||
|
||||
it 'creates a purchase for one ticket' do
|
||||
tickets = { ticket_1.id.to_s => '1' }
|
||||
message = TicketPurchase.purchase(conference, participant, tickets)
|
||||
purchase = TicketPurchase.where(conference_id: conference.id,
|
||||
user_id: participant.id,
|
||||
ticket_id: ticket_1.id).first
|
||||
|
||||
expect(TicketPurchase.count).to eq(1)
|
||||
expect(purchase.quantity).to eq(1)
|
||||
expect(message.blank?).to be true
|
||||
end
|
||||
|
||||
it 'creates several purchases for more than one ticket' do
|
||||
tickets = { ticket_1.id.to_s => '1', ticket_2.id.to_s => '1' }
|
||||
message = TicketPurchase.purchase(conference, participant, tickets)
|
||||
purchase_1 = TicketPurchase.where(conference_id: conference.id,
|
||||
user_id: participant.id,
|
||||
ticket_id: ticket_1.id).first
|
||||
|
||||
purchase_2 = TicketPurchase.where(conference_id: conference.id,
|
||||
user_id: participant.id,
|
||||
ticket_id: ticket_2.id).first
|
||||
|
||||
expect(TicketPurchase.count).to eq(2)
|
||||
expect(purchase_1.quantity).to eq(1)
|
||||
expect(purchase_2.quantity).to eq(1)
|
||||
expect(message.blank?).to be true
|
||||
end
|
||||
|
||||
it 'creates no purchase if quantity is less than 1' do
|
||||
tickets = { ticket_1.id.to_s => '-1' }
|
||||
TicketPurchase.purchase(conference, participant, tickets)
|
||||
|
||||
expect(TicketPurchase.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'creates no purchase if quantity is 0' do
|
||||
tickets = { ticket_1.id.to_s => '0' }
|
||||
TicketPurchase.purchase(conference, participant, tickets)
|
||||
|
||||
expect(TicketPurchase.count).to eq(0)
|
||||
end
|
||||
|
||||
it 'updates the quantity if the user already bought this ticket' do
|
||||
purchase = create(:ticket_purchase,
|
||||
conference: conference,
|
||||
user: participant,
|
||||
ticket: ticket_1,
|
||||
quantity: 5)
|
||||
|
||||
tickets = { ticket_1.id.to_s => '10' }
|
||||
message = TicketPurchase.purchase(conference, participant, tickets)
|
||||
purchase.reload
|
||||
|
||||
expect(TicketPurchase.count).to eq(1)
|
||||
expect(purchase.quantity).to eq(10)
|
||||
expect(message.blank?).to be true
|
||||
end
|
||||
end
|
||||
end
|
||||
92
spec/models/ticket_spec.rb
Normal file
92
spec/models/ticket_spec.rb
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Ticket do
|
||||
let(:conference) { create(:conference) }
|
||||
let(:ticket) { create(:ticket, price: 50, conference: conference) }
|
||||
let(:user) { create(:user) }
|
||||
|
||||
describe 'validations' do
|
||||
it 'has a valid factory' do
|
||||
expect(build(:ticket)).to be_valid
|
||||
end
|
||||
|
||||
it 'is not valid without a title' do
|
||||
should validate_presence_of(:title)
|
||||
end
|
||||
|
||||
it 'is not valid without a price_cents' do
|
||||
should validate_presence_of(:price_cents)
|
||||
end
|
||||
|
||||
it 'is not valid without a price_currency' do
|
||||
should validate_presence_of(:price_currency)
|
||||
end
|
||||
|
||||
it 'is not valid with a price_cents equals zero' do
|
||||
should_not allow_value(0).for(:price_cents)
|
||||
end
|
||||
|
||||
it 'is not valid with a price_cents smaller than zero' do
|
||||
should_not allow_value(-1).for(:price_cents)
|
||||
end
|
||||
|
||||
it 'is valid with a price_cents greater than zero' do
|
||||
should allow_value(1).for(:price_cents)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#bought?' do
|
||||
it 'returns true if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket)
|
||||
expect(ticket.bought?(user)).to eq(true)
|
||||
end
|
||||
|
||||
it 'returns true if the user has bought this ticket' do
|
||||
expect(ticket.bought?(user)).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#quantity_bought_by' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.quantity_bought_by(user)).to eq(20)
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(ticket.quantity_bought_by(user)).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#total_price' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.total_price(user)).to eq(20 * 50)
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(ticket.total_price(user)).to eq(0)
|
||||
end
|
||||
end
|
||||
|
||||
describe 'self#total_price' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(Ticket.total_price(conference, user)).to eq(20 * 50)
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(Ticket.total_price(conference, user)).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue