Browse Source

lk | add some tests for bike#available_for_freecyclery

master
Louis Knapp 9 years ago
parent
commit
87577ab14e
  1. 7
      app/models/bike.rb
  2. 11
      spec/factories/bike_factory.rb
  3. 19
      spec/factories/bikes.rb
  4. 0
      spec/factories/clients.rb
  5. 0
      spec/factories/users.rb
  6. 18
      spec/models/bike_spec.rb

7
app/models/bike.rb

@ -7,6 +7,9 @@ class Bike < ActiveRecord::Base
validates :serial_number, presence: true
has_one :client
FREECYCLERY = "Freecyclery"
SALE = "Sale"
def self.bike_types
[
["BMX", "BMX"],
@ -35,8 +38,8 @@ class Bike < ActiveRecord::Base
end
def self.available_for_freecyclery
assigned_bikes = Client.all.select{|c| !c.bike_id.nil?}.map(&:bike)
all_freecyclery_bikes = Bike.order(log_number: :desc).select{|bike| bike.purpose == "Freecyclery"}
assigned_bikes = Client.all.includes(:bike).select{|c| !c.bike_id.nil?}.map(&:bike)
all_freecyclery_bikes = Bike.where(purpose: FREECYCLERY).order(log_number: :desc)
all_freecyclery_bikes - assigned_bikes
end

11
spec/factories/bike_factory.rb

@ -1,11 +0,0 @@
FactoryGirl.define do
factory :bike do
sequence(:log_number)
brand "Windsor"
model "Clockwork"
bike_type "Fixed Gear"
color "Black"
sequence(:serial_number)
sequence(:bike_index_id)
end
end

19
spec/factories/bikes.rb

@ -0,0 +1,19 @@
FactoryGirl.define do
factory :bike do
sequence(:log_number)
brand "Schwinn"
model "Madison"
bike_type "Fixed Gear"
color "Yellow"
sequence(:serial_number)
sequence(:bike_index_id)
end
trait :sale do
purpose Bike::SALE
end
trait :freecyclery do
purpose Bike::FREECYCLERY
end
end

0
spec/factories/client_factory.rb → spec/factories/clients.rb

0
spec/factories/user_factory.rb → spec/factories/users.rb

18
spec/models/bike_spec.rb

@ -12,4 +12,22 @@ describe Bike do
expect(bike.post_to_bike_index).to be_truthy
end
end
describe "#available_for_freecyclery" do
it "does not return bikes that are assigned to clients" do
bike = create :bike, :freecyclery
create :client, bike_id: bike.id
expect(Bike.available_for_freecyclery).to be_empty
end
it "does not return sales bikes" do
bike = create :bike, :sale
create :client, bike_id: bike.id
expect(Bike.available_for_freecyclery).to be_empty
end
it "does return bikes without clients" do
bike = create :bike, :freecyclery
expect(Bike.available_for_freecyclery).to include(bike)
end
end
end

Loading…
Cancel
Save