From 87577ab14e4d3fe4023566c5653fdf19bfc09b56 Mon Sep 17 00:00:00 2001 From: Louis Knapp Date: Thu, 12 May 2016 21:34:35 -0500 Subject: [PATCH] lk | add some tests for bike#available_for_freecyclery --- app/models/bike.rb | 7 +++++-- spec/factories/bike_factory.rb | 11 ----------- spec/factories/bikes.rb | 19 +++++++++++++++++++ .../{client_factory.rb => clients.rb} | 0 spec/factories/{user_factory.rb => users.rb} | 0 spec/models/bike_spec.rb | 18 ++++++++++++++++++ 6 files changed, 42 insertions(+), 13 deletions(-) delete mode 100644 spec/factories/bike_factory.rb create mode 100644 spec/factories/bikes.rb rename spec/factories/{client_factory.rb => clients.rb} (100%) rename spec/factories/{user_factory.rb => users.rb} (100%) diff --git a/app/models/bike.rb b/app/models/bike.rb index 4489760..a241237 100644 --- a/app/models/bike.rb +++ b/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 diff --git a/spec/factories/bike_factory.rb b/spec/factories/bike_factory.rb deleted file mode 100644 index 32f88cf..0000000 --- a/spec/factories/bike_factory.rb +++ /dev/null @@ -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 diff --git a/spec/factories/bikes.rb b/spec/factories/bikes.rb new file mode 100644 index 0000000..d6e770a --- /dev/null +++ b/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 diff --git a/spec/factories/client_factory.rb b/spec/factories/clients.rb similarity index 100% rename from spec/factories/client_factory.rb rename to spec/factories/clients.rb diff --git a/spec/factories/user_factory.rb b/spec/factories/users.rb similarity index 100% rename from spec/factories/user_factory.rb rename to spec/factories/users.rb diff --git a/spec/models/bike_spec.rb b/spec/models/bike_spec.rb index 415ab36..dcf1d2f 100644 --- a/spec/models/bike_spec.rb +++ b/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