mirror of
https://github.com/openSUSE/osem.git
synced 2026-08-13 19:54:02 +00:00
Tests for ticket model
Change in error message of validation cause previously it was: "Price currency Currency is different.." `paid?` method returned true even when `ticket_purchases` is empty for a given user.
This commit is contained in:
parent
c5c392d4c7
commit
ebd01f064a
2 changed files with 70 additions and 16 deletions
|
|
@ -18,7 +18,7 @@ class Ticket < ActiveRecord::Base
|
|||
end
|
||||
|
||||
def paid?(user)
|
||||
ticket_purchases.where(user_id: user.id, paid: false).count == 0
|
||||
ticket_purchases.find_by(user: user, paid: true).present?
|
||||
end
|
||||
|
||||
def quantity_bought_by(user)
|
||||
|
|
@ -52,7 +52,7 @@ class Ticket < ActiveRecord::Base
|
|||
|
||||
def tickets_of_conference_have_same_currency
|
||||
unless Ticket.where(conference_id: conference_id).all?{|t| t.price_currency == self.price_currency }
|
||||
errors.add(:price_currency, 'Currency is different from the exist ticktes of this conference.')
|
||||
errors.add(:price_currency, 'is different from the existing tickets of this conference.')
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -2,10 +2,10 @@ require 'spec_helper'
|
|||
|
||||
describe Ticket do
|
||||
let(:conference) { create(:conference) }
|
||||
let(:ticket) { create(:ticket, price: 50, conference: conference) }
|
||||
let(:ticket) { create(:ticket, price: 50, price_currency: 'USD', conference: conference) }
|
||||
let(:user) { create(:user) }
|
||||
|
||||
describe 'validations' do
|
||||
describe 'validation' do
|
||||
it 'has a valid factory' do
|
||||
expect(build(:ticket)).to be_valid
|
||||
end
|
||||
|
|
@ -33,6 +33,22 @@ describe Ticket do
|
|||
it 'is valid with a price_cents greater than zero' do
|
||||
should allow_value(1).for(:price_cents)
|
||||
end
|
||||
|
||||
it 'is not valid if tickets of conference do not have same currency' do
|
||||
conflicting_currency_ticket = build(:ticket,
|
||||
conference: ticket.conference,
|
||||
price_currency: 'INR')
|
||||
expected_error_message = 'Price currency is different from the existing tickets of this conference.'
|
||||
|
||||
expect(conflicting_currency_ticket).not_to be_valid
|
||||
expect(conflicting_currency_ticket.errors.full_messages).to eq([expected_error_message])
|
||||
end
|
||||
end
|
||||
|
||||
describe 'association' do
|
||||
it { should belong_to(:conference) }
|
||||
it { should have_many(:ticket_purchases).dependent(:destroy) }
|
||||
it { should have_many(:buyers).through(:ticket_purchases).source(:user) }
|
||||
end
|
||||
|
||||
describe '#bought?' do
|
||||
|
|
@ -43,11 +59,29 @@ describe Ticket do
|
|||
expect(ticket.bought?(user)).to eq(true)
|
||||
end
|
||||
|
||||
it 'returns true if the user has bought this ticket' do
|
||||
it 'returns false if the user has not bought this ticket' do
|
||||
expect(ticket.bought?(user)).to eq(false)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#paid?' do
|
||||
let!(:ticket_purchase) { create(:ticket_purchase, user: user, ticket: ticket) }
|
||||
|
||||
context 'user has paid' do
|
||||
before { ticket_purchase.update_attributes(paid: true) }
|
||||
|
||||
it 'returns true' do
|
||||
expect(ticket.paid?(user)).to eq(true)
|
||||
end
|
||||
end
|
||||
|
||||
context 'user has not paid' do
|
||||
it 'returns false' do
|
||||
expect(ticket.paid?(user)).to eq(false)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#quantity_bought_by' do
|
||||
it 'returns the correct value if the user has bought this ticket' do
|
||||
create(:ticket_purchase,
|
||||
|
|
@ -68,7 +102,7 @@ describe Ticket do
|
|||
user: user,
|
||||
ticket: ticket,
|
||||
quantity: 20)
|
||||
expect(ticket.total_price(user)).to eq(Money.new(20 * ticket.price_cents, 'USD'))
|
||||
expect(ticket.total_price(user)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
|
|
@ -76,17 +110,37 @@ describe Ticket do
|
|||
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(Money.new(20 * ticket.price_cents, 'USD'))
|
||||
end
|
||||
describe 'self.total_price' do
|
||||
let(:diversity_supporter_ticket) { create(:ticket, conference: conference, price: 500) }
|
||||
|
||||
it 'returns zero if the user has not bought this ticket' do
|
||||
expect(Ticket.total_price(conference, user)).to eq(Money.new(0, 'USD'))
|
||||
describe 'user has bought' do
|
||||
context 'no tickets' do
|
||||
it 'returns zero' do
|
||||
expect(Ticket.total_price(conference, user)).to eq(Money.new(0, 'USD'))
|
||||
end
|
||||
end
|
||||
|
||||
context 'one type of ticket' do
|
||||
before do
|
||||
create(:ticket_purchase, ticket: ticket, user: user, quantity: 20)
|
||||
end
|
||||
|
||||
it 'returns the correct total price' do
|
||||
expect(Ticket.total_price(conference, user)).to eq(Money.new(100000, 'USD'))
|
||||
end
|
||||
end
|
||||
|
||||
context 'multiple types of tickets' do
|
||||
before do
|
||||
create(:ticket_purchase, ticket: ticket, user: user, quantity: 20)
|
||||
create(:ticket_purchase, ticket: diversity_supporter_ticket, user: user, quantity: 2)
|
||||
end
|
||||
|
||||
it 'returns the correct total price' do
|
||||
total_price = Money.new(200000, 'USD')
|
||||
expect(Ticket.total_price(conference, user)).to eq(total_price)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue