Merge pull request #838 from sonalkr132/ticket-tests

Tests for ticket model
This commit is contained in:
Stella Rouzi 2016-03-09 12:03:52 +02:00
commit e560d263c9
2 changed files with 70 additions and 16 deletions

View file

@ -18,7 +18,7 @@ class Ticket < ActiveRecord::Base
end end
def paid?(user) def paid?(user)
ticket_purchases.where(user_id: user.id, paid: false).count == 0 ticket_purchases.find_by(user: user, paid: true).present?
end end
def quantity_bought_by(user) def quantity_bought_by(user)
@ -52,7 +52,7 @@ class Ticket < ActiveRecord::Base
def tickets_of_conference_have_same_currency def tickets_of_conference_have_same_currency
unless Ticket.where(conference_id: conference_id).all?{|t| t.price_currency == self.price_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 end
end end

View file

@ -2,10 +2,10 @@ require 'spec_helper'
describe Ticket do describe Ticket do
let(:conference) { create(:conference) } 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) } let(:user) { create(:user) }
describe 'validations' do describe 'validation' do
it 'has a valid factory' do it 'has a valid factory' do
expect(build(:ticket)).to be_valid expect(build(:ticket)).to be_valid
end end
@ -33,6 +33,22 @@ describe Ticket do
it 'is valid with a price_cents greater than zero' do it 'is valid with a price_cents greater than zero' do
should allow_value(1).for(:price_cents) should allow_value(1).for(:price_cents)
end 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 end
describe '#bought?' do describe '#bought?' do
@ -43,11 +59,29 @@ describe Ticket do
expect(ticket.bought?(user)).to eq(true) expect(ticket.bought?(user)).to eq(true)
end 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) expect(ticket.bought?(user)).to eq(false)
end end
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 describe '#quantity_bought_by' do
it 'returns the correct value if the user has bought this ticket' do it 'returns the correct value if the user has bought this ticket' do
create(:ticket_purchase, create(:ticket_purchase,
@ -68,7 +102,7 @@ describe Ticket do
user: user, user: user,
ticket: ticket, ticket: ticket,
quantity: 20) 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 end
it 'returns zero if the user has not bought this ticket' do it 'returns zero if the user has not bought this ticket' do
@ -76,17 +110,37 @@ describe Ticket do
end end
end end
describe 'self#total_price' do describe 'self.total_price' do
it 'returns the correct value if the user has bought this ticket' do let(:diversity_supporter_ticket) { create(:ticket, conference: conference, price: 500) }
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
it 'returns zero if the user has not bought this ticket' do describe 'user has bought' do
expect(Ticket.total_price(conference, user)).to eq(Money.new(0, 'USD')) 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 end
end end