From f8f7fe0c9c056e79340143d0f35abc88387013c9 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 11:06:24 -0800 Subject: [PATCH 01/33] Set up Rakefile, spec_helper, ride_share.rb, and ran initial tests --- .gitignore | 1 + Rakefile | 10 +++++++++- lib/drivers.rb | 1 + lib/riders.rb | 0 lib/trips.rb | 0 ride_share.rb | 7 +++++++ specs/drivers_spec.rb | 11 +++++++++++ specs/riders_spec.rb | 1 + specs/spec_helper.rb | 6 ++++-- specs/trips_spec.rb | 1 + 10 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 .gitignore create mode 100644 lib/drivers.rb create mode 100644 lib/riders.rb create mode 100644 lib/trips.rb create mode 100644 ride_share.rb create mode 100644 specs/drivers_spec.rb create mode 100644 specs/riders_spec.rb create mode 100644 specs/trips_spec.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..7053dc17e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/coverage/ diff --git a/Rakefile b/Rakefile index c556a763c..deb52f2cd 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,9 @@ -# Fill me in! +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib"] + t.warning = true + t.test_files = FileList['specs/*_spec.rb'] +end + +task default: :test diff --git a/lib/drivers.rb b/lib/drivers.rb new file mode 100644 index 000000000..56d9b483c --- /dev/null +++ b/lib/drivers.rb @@ -0,0 +1 @@ +TEST_CONTSANT = 11 diff --git a/lib/riders.rb b/lib/riders.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/trips.rb b/lib/trips.rb new file mode 100644 index 000000000..e69de29bb diff --git a/ride_share.rb b/ride_share.rb new file mode 100644 index 000000000..1f1ae807a --- /dev/null +++ b/ride_share.rb @@ -0,0 +1,7 @@ +require 'csv' + +module RideShare; end + +require_relative 'lib/drivers.rb' +require_relative 'lib/trips.rb' +require_relative 'lib/riders.rb' diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb new file mode 100644 index 000000000..deba464e0 --- /dev/null +++ b/specs/drivers_spec.rb @@ -0,0 +1,11 @@ +require_relative 'spec_helper' + +describe 'tests' do + it "should run tests" do + true.must_equal false + end + + it "should have access to drivers.rb" do + TEST_CONTSANT.must_equal 11 + end +end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/riders_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 4d1e3fdc8..3223d206b 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,8 +1,10 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# Require_relative your lib files here! +require_relative '../ride_share' diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/trips_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' From f93ac041422c10353b9c0c7568ac601dd188e9e0 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 13:06:04 -0800 Subject: [PATCH 02/33] renamed files, created first tests and initialze for Trips --- lib/{drivers.rb => driver.rb} | 0 lib/{riders.rb => rider.rb} | 0 lib/trip.rb | 20 ++++++++++ lib/trips.rb | 0 pseudocode.txt | 70 +++++++++++++++++++++++++++++++++++ ride_share.rb | 9 +++-- specs/drivers_spec.rb | 18 ++++----- specs/trips_spec.rb | 16 ++++++++ 8 files changed, 121 insertions(+), 12 deletions(-) rename lib/{drivers.rb => driver.rb} (100%) rename lib/{riders.rb => rider.rb} (100%) create mode 100644 lib/trip.rb delete mode 100644 lib/trips.rb create mode 100644 pseudocode.txt diff --git a/lib/drivers.rb b/lib/driver.rb similarity index 100% rename from lib/drivers.rb rename to lib/driver.rb diff --git a/lib/riders.rb b/lib/rider.rb similarity index 100% rename from lib/riders.rb rename to lib/rider.rb diff --git a/lib/trip.rb b/lib/trip.rb new file mode 100644 index 000000000..9f2b0bddd --- /dev/null +++ b/lib/trip.rb @@ -0,0 +1,20 @@ +# module RideShare + + class RideShare::Trip + attr_reader :id + + def initialize(id, driver_id, rider_id, date, rating) + @id = id + @driver_id = driver_id + @rider_id = rider_id + @date = date + + unless rating > 0 && rating < 6 + raise InvalidRatingError.new("Ride rating must be 1-5") + end + @rating = rating + end + + end + +# end diff --git a/lib/trips.rb b/lib/trips.rb deleted file mode 100644 index e69de29bb..000000000 diff --git a/pseudocode.txt b/pseudocode.txt new file mode 100644 index 000000000..46f2481d1 --- /dev/null +++ b/pseudocode.txt @@ -0,0 +1,70 @@ +Trip CLASS + + Each trip should be INSTANTIATED (initialize) with an ID, rider ID, driver ID, date, rating (each rating should be within an acceptable range 1-5) + input: Trip.new + output: new Trip object w/ applicable ratings + + Retrieve associated driver instance through driver ID + input: Driver ID + output: Driver Object associated w/ trip + + Retrieve associated Rider instance through Rider ID + input: Rider ID + output: Rider object associated w/ trip + + Find all trip instances (driver) CLASS METHOD + input: Driver ID + output: list (ARRAY) of all trip instances for that Driver + + Find all trip instances (rider) CLASS METHOD + input: Rider ID + output: list (ARRAY) of all trip instances for that Rider + + Retrieve all trips from CSV file CLASS METHOD + input: calling Class method Trips.find_all + output: list all trips from CSV file. + +Driver CLASS + + Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must be specific length to make sure it is actually a vin) + input: Driver.new + output: new instance of Driver with associated ID, Name and VIN. Each of those should be ACCESSIBLE (attr_reader) + + with Driver ID we need to retrieve a list of trip instances (objects class: trip) that only this driver has taken. + input: Driver ID + output: ARRAY of Trips that this driver has taken + + retrieve an average rating for that driver based on all trips taken. + input: ARRAY of trips that this driver has taken + output: Average star rating FIXNUM + + Retrieve all drivers from the CSV file + input: calling the CLASS METHOD + output: list of all drivers (ARRAY) from CSV file + + Find a specific driver using numeric ID + input: Numeric Driver_ID + output: Specific driver from list of drivers. + + +Riders CLASS + + Each rider should be instantiated (INITIALIZE) with an ID, name and phone number + input: Rider.new + output: new rider object. + + Retrieve a list of trips only this rider has taken + input: Rider ID + output: Array of Trip instances + + Retrieve a list of all previous driver instances this rider has rode with. + input: Array of Trip instances (from above) + output: Array of the associated drivers. + + Retrieve all riders from CSV file + input: calling CLASS METHOD + output: list of all RIDERS + + Retrieve specific rider using numeric ID + input: Rider ID + output: Specific rider (from list above) diff --git a/ride_share.rb b/ride_share.rb index 1f1ae807a..31a9752d7 100644 --- a/ride_share.rb +++ b/ride_share.rb @@ -2,6 +2,9 @@ module RideShare; end -require_relative 'lib/drivers.rb' -require_relative 'lib/trips.rb' -require_relative 'lib/riders.rb' +require_relative 'lib/driver.rb' +require_relative 'lib/trip.rb' +require_relative 'lib/rider.rb' + +class InvalidRatingError < StandardError +end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index deba464e0..4151fb138 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -1,11 +1,11 @@ require_relative 'spec_helper' -describe 'tests' do - it "should run tests" do - true.must_equal false - end - - it "should have access to drivers.rb" do - TEST_CONTSANT.must_equal 11 - end -end +# describe 'tests' do +# it "should run tests" do +# true.must_equal false +# end +# +# it "should have access to drivers.rb" do +# TEST_CONTSANT.must_equal 11 +# end +# end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index ae9c220ea..2ea6fe887 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -1 +1,17 @@ require_relative 'spec_helper' + +describe "Trip" do + + describe "Trip#initialize" do + it "takes an ID, Driver ID, Rider ID, Date and Rating to initialize" do + my_trip = RideShare::Trip.new(1, 2, 3, 4, 5) + my_trip.must_respond_to :id + end + + it "rating must be between 1 - 5" do + proc {RideShare::Trip.new(1, 2, 3, 4, 17)}.must_raise InvalidRatingError + end + end + + +end From 84f6ec951cd14ae4530cb86a2de5e20155c8a119 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 13:17:41 -0800 Subject: [PATCH 03/33] Adding let to specs, created test for find_all, failing --- lib/trip.rb | 48 +++++++++++++++++++++++++++++++++------------ specs/trips_spec.rb | 9 ++++++++- 2 files changed, 43 insertions(+), 14 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 9f2b0bddd..4762e58ef 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,20 +1,42 @@ -# module RideShare - class RideShare::Trip - attr_reader :id +class RideShare::Trip + attr_reader :id - def initialize(id, driver_id, rider_id, date, rating) - @id = id - @driver_id = driver_id - @rider_id = rider_id - @date = date + def initialize(id, driver_id, rider_id, date, rating) + @id = id + @driver_id = driver_id + @rider_id = rider_id + @date = date - unless rating > 0 && rating < 6 - raise InvalidRatingError.new("Ride rating must be 1-5") - end - @rating = rating + unless rating > 0 && rating < 6 + raise InvalidRatingError.new("Ride rating must be 1-5") end + @rating = rating + end + def self.find_all + # Retrieve all trips from CSV file CLASS METHOD + # input: calling Class method Trips.find_all + # output: list all trips from CSV file as an array. end -# end + # Retrieve associated driver instance through driver ID + # input: Driver ID + # output: Driver Object associated w/ trip + # + # Retrieve associated Rider instance through Rider ID + # input: Rider ID + # output: Rider object associated w/ trip + # + # Find all trip instances (driver) CLASS METHOD + # input: Driver ID + # output: list (ARRAY) of all trip instances for that Driver + # + # Find all trip instances (rider) CLASS METHOD + # input: Rider ID + # output: list (ARRAY) of all trip instances for that Rider + # + # Retrieve all trips from CSV file CLASS METHOD + # input: calling Class method Trips.find_all + # output: list all trips from CSV file. +end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 2ea6fe887..523e077fc 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -1,10 +1,11 @@ require_relative 'spec_helper' describe "Trip" do + let(:my_trip) {RideShare::Trip.new(1, 2, 3, 4, 5)} + let(:all_trips) {RideShare::Trip.find_all} describe "Trip#initialize" do it "takes an ID, Driver ID, Rider ID, Date and Rating to initialize" do - my_trip = RideShare::Trip.new(1, 2, 3, 4, 5) my_trip.must_respond_to :id end @@ -13,5 +14,11 @@ end end + describe "Testing Trip class methods" do + it "returns an array of Trip instances" do + all_trips.must_be_instance_of(Array) + end + + end end From 30547eb91d992bf65b36bdf35c4335af2d0f8ff0 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 13:30:35 -0800 Subject: [PATCH 04/33] created find_all method --- lib/trip.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/trip.rb b/lib/trip.rb index 4762e58ef..d0450e507 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -9,12 +9,22 @@ def initialize(id, driver_id, rider_id, date, rating) @date = date unless rating > 0 && rating < 6 - raise InvalidRatingError.new("Ride rating must be 1-5") + raise InvalidRatingError.new("Ride rating must be 1-5, your rating was #{ rating }") end @rating = rating end def self.find_all + trips = [] + begin + CSV.open("support/trips.csv").each do |trip| + trips << RideShare::Trip.new(trip[0].to_i, trip[1].to_i, trip[2].to_i, trip[3], trip[4].to_i) + end + rescue InvalidRatingError => e + puts " #{e} " + end + + return trips # Retrieve all trips from CSV file CLASS METHOD # input: calling Class method Trips.find_all # output: list all trips from CSV file as an array. From 343941fd7a059c5237d2d12c37f725c671ec7b1a Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 14:12:11 -0800 Subject: [PATCH 05/33] added and fixed test for find all --- lib/trip.rb | 13 +++++-------- specs/trips_spec.rb | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index d0450e507..56330ca13 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -16,18 +16,15 @@ def initialize(id, driver_id, rider_id, date, rating) def self.find_all trips = [] - begin - CSV.open("support/trips.csv").each do |trip| + CSV.open("support/trips.csv").each do |trip| + begin trips << RideShare::Trip.new(trip[0].to_i, trip[1].to_i, trip[2].to_i, trip[3], trip[4].to_i) + rescue InvalidRatingError => e + puts "#{ e }" end - rescue InvalidRatingError => e - puts " #{e} " end - + trips.shift return trips - # Retrieve all trips from CSV file CLASS METHOD - # input: calling Class method Trips.find_all - # output: list all trips from CSV file as an array. end # Retrieve associated driver instance through driver ID diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 523e077fc..5d9c1adb5 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -1,4 +1,6 @@ require_relative 'spec_helper' +require 'pry' + describe "Trip" do let(:my_trip) {RideShare::Trip.new(1, 2, 3, 4, 5)} @@ -19,6 +21,19 @@ all_trips.must_be_instance_of(Array) end + it "each item is of class Trip" do + all_trips.each do |trip| + trip.must_be_instance_of RideShare::Trip + end + end + + # it "number of owners matches number of lines in CSV - 1 for headder line" do + # csv_lines = CSV.read("support/trips.csv") + # + # all_trips.length.must_equal + # end + # #number of owners match number of lines - 1 in CSV + #Id of first and last match ID of first and last in CSV end end From 8c4dadaee20df729023369b7a3aff866e03db25e Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 14:24:37 -0800 Subject: [PATCH 06/33] finished testing find_all method for trips --- lib/trip.rb | 4 ++-- specs/trips_spec.rb | 17 ++++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 56330ca13..79f78ea11 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,6 +1,6 @@ class RideShare::Trip - attr_reader :id + attr_reader :id, :driver_id, :rider_id, :date, :rating def initialize(id, driver_id, rider_id, date, rating) @id = id @@ -23,7 +23,7 @@ def self.find_all puts "#{ e }" end end - trips.shift + # trips.shift return trips end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 5d9c1adb5..bbda748bb 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -5,6 +5,7 @@ describe "Trip" do let(:my_trip) {RideShare::Trip.new(1, 2, 3, 4, 5)} let(:all_trips) {RideShare::Trip.find_all} + let(:csv) {CSV.read("support/trips.csv")} describe "Trip#initialize" do it "takes an ID, Driver ID, Rider ID, Date and Rating to initialize" do @@ -27,13 +28,15 @@ end end - # it "number of owners matches number of lines in CSV - 1 for headder line" do - # csv_lines = CSV.read("support/trips.csv") - # - # all_trips.length.must_equal - # end - # #number of owners match number of lines - 1 in CSV - #Id of first and last match ID of first and last in CSV + it "number of owners matches number of lines in CSV - 1 for headder line" do + csv_length = csv.length + all_trips.length.must_equal(csv_length - 1) + end + + it "date of first & last match date of first & last in CSV" do + all_trips[0].date.must_equal(csv[1][3]) + all_trips[-1].date.must_equal(csv[-1][3]) + end end end From 5fc78e25fb7bc3e51fec7baaf83b139f745ed64c Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 14:35:50 -0800 Subject: [PATCH 07/33] tested and created find_all_driver method --- lib/trip.rb | 14 +++++++++++++- specs/trips_spec.rb | 10 +++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 79f78ea11..62e0d4485 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -23,10 +23,22 @@ def self.find_all puts "#{ e }" end end - # trips.shift + # trips.shift => I guess I don't need this. return trips end + def self.find_all_driver(driver_id) + all_trips = find_all + driver_trips = [] + + all_trips.each do |trip| + if trip.driver_id == driver_id + driver_trips << trip + end + end + + return driver_trips + end # Retrieve associated driver instance through driver ID # input: Driver ID # output: Driver Object associated w/ trip diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index bbda748bb..1e07b40ae 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -5,6 +5,7 @@ describe "Trip" do let(:my_trip) {RideShare::Trip.new(1, 2, 3, 4, 5)} let(:all_trips) {RideShare::Trip.find_all} + let(:all_driver_trips) {RideShare::Trip.find_all_driver(39)} let(:csv) {CSV.read("support/trips.csv")} describe "Trip#initialize" do @@ -17,7 +18,7 @@ end end - describe "Testing Trip class methods" do + describe "Testing Trip#find_all class method" do it "returns an array of Trip instances" do all_trips.must_be_instance_of(Array) end @@ -39,4 +40,11 @@ end end + describe "Testing Trip#find_all_driver class method" do + it "returns an array of Trip instances" do + all_driver_trips.must_be_instance_of(Array) + end + + end + end From 9995b0ba4fa5290871abe23a53dd18fb2956ff23 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 14:53:25 -0800 Subject: [PATCH 08/33] adding find_all_rider method and tests --- lib/trip.rb | 40 +++++++++++++++++++++++++--------------- specs/trips_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 62e0d4485..b27ba7025 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -16,15 +16,17 @@ def initialize(id, driver_id, rider_id, date, rating) def self.find_all trips = [] - CSV.open("support/trips.csv").each do |trip| - begin - trips << RideShare::Trip.new(trip[0].to_i, trip[1].to_i, trip[2].to_i, trip[3], trip[4].to_i) - rescue InvalidRatingError => e - puts "#{ e }" - end + + CSV.open("support/trips.csv").each do |trip| + begin + trips << RideShare::Trip.new(trip[0].to_i, trip[1].to_i, trip[2].to_i, trip[3], trip[4].to_i) + rescue InvalidRatingError => e + puts "#{ e }" end + end + # trips.shift => I guess I don't need this. - return trips + return trips end def self.find_all_driver(driver_id) @@ -37,7 +39,22 @@ def self.find_all_driver(driver_id) end end - return driver_trips + return 0 if driver_trips.empty? + driver_trips + end + + def self.find_all_rider(rider_id) + all_trips = find_all + rider_trips = [] + + all_trips.each do |trip| + if trip.rider_id == rider_id + rider_trips << trip + end + end + + return 0 if rider_trips.empty? + rider_trips end # Retrieve associated driver instance through driver ID # input: Driver ID @@ -47,15 +64,8 @@ def self.find_all_driver(driver_id) # input: Rider ID # output: Rider object associated w/ trip # - # Find all trip instances (driver) CLASS METHOD - # input: Driver ID - # output: list (ARRAY) of all trip instances for that Driver - # # Find all trip instances (rider) CLASS METHOD # input: Rider ID # output: list (ARRAY) of all trip instances for that Rider # - # Retrieve all trips from CSV file CLASS METHOD - # input: calling Class method Trips.find_all - # output: list all trips from CSV file. end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 1e07b40ae..077a6a08f 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -6,6 +6,7 @@ let(:my_trip) {RideShare::Trip.new(1, 2, 3, 4, 5)} let(:all_trips) {RideShare::Trip.find_all} let(:all_driver_trips) {RideShare::Trip.find_all_driver(39)} + let(:all_rider_trips) {RideShare::Trip.find_all_rider(54)} let(:csv) {CSV.read("support/trips.csv")} describe "Trip#initialize" do @@ -45,6 +46,32 @@ all_driver_trips.must_be_instance_of(Array) end + it "Each item in array is of class Trip" do + all_driver_trips.each do |trip| + trip.must_be_instance_of RideShare::Trip + end + end + + it "Returns 0 if driver ID not found" do + bad_id = RideShare::Trip.find_all_driver("bad driver ID") + bad_id.must_equal 0 + end end + describe "Testing Trip#find_all_rider class method" do + it "returns an array of Trip instances" do + all_rider_trips.must_be_instance_of(Array) + end + + it "Each item in array is of class Trip" do + all_rider_trips.each do |trip| + trip.must_be_instance_of RideShare::Trip + end + end + + it "Returns 0 if ID not found" do + bad_id = RideShare::Trip.find_all_rider("bad rider ID") + bad_id.must_equal 0 + end + end end From d7d5826ac20953af841459a17132d9f79c39bf50 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:02:48 -0800 Subject: [PATCH 09/33] added init tests for Rider and Driver classes --- lib/driver.rb | 8 +++++++- lib/rider.rb | 7 +++++++ lib/trip.rb | 9 +++++---- specs/drivers_spec.rb | 19 ++++++++++--------- specs/riders_spec.rb | 11 +++++++++++ 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 56d9b483c..9e07b9d1b 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1 +1,7 @@ -TEST_CONTSANT = 11 + +class RideShare::Driver + + # Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must be specific length to make sure it is actually a vin) + # input: Driver.new + # output: new instance of Driver with associated ID, Name and VIN. Each of those should be ACCESSIBLE (attr_reader) +end diff --git a/lib/rider.rb b/lib/rider.rb index e69de29bb..bfdc6b347 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -0,0 +1,7 @@ + +class RideShare::Rider + # Each rider should be instantiated (INITIALIZE) with an ID, name and phone number + # input: Rider.new + # output: new rider object. + +end diff --git a/lib/trip.rb b/lib/trip.rb index b27ba7025..d315bc767 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -56,6 +56,10 @@ def self.find_all_rider(rider_id) return 0 if rider_trips.empty? rider_trips end + + def self.find_driver(trip_id) + + end # Retrieve associated driver instance through driver ID # input: Driver ID # output: Driver Object associated w/ trip @@ -64,8 +68,5 @@ def self.find_all_rider(rider_id) # input: Rider ID # output: Rider object associated w/ trip # - # Find all trip instances (rider) CLASS METHOD - # input: Rider ID - # output: list (ARRAY) of all trip instances for that Rider - # + end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 4151fb138..48932bd17 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -1,11 +1,12 @@ require_relative 'spec_helper' -# describe 'tests' do -# it "should run tests" do -# true.must_equal false -# end -# -# it "should have access to drivers.rb" do -# TEST_CONTSANT.must_equal 11 -# end -# end +describe "Driver" do + let(:my_driver) {RideShare::Driver.new(1, 2, 3)} + + describe "Diver#initialize" do + it "takes an ID, Name, and VIN to initialize" do + my_driver.must_respond_to :id + end + end + +end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index ae9c220ea..d6994e7fe 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -1 +1,12 @@ require_relative 'spec_helper' + +describe "rider_id" do + let(:my_rider) {RideShare::Rider.new(1, 2, 3)} + + describe "Diver#initialize" do + it "takes an ID, Name, and Phone Number to initialize" do + my_driver.must_respond_to :id + end + end + +end From c52a7d5972904299b85bbf4de19b3266e1082793 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:07:04 -0800 Subject: [PATCH 10/33] passing all init tests --- lib/driver.rb | 8 +++++++- lib/rider.rb | 7 +++++++ specs/riders_spec.rb | 2 +- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 9e07b9d1b..c2abb6e82 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,7 +1,13 @@ class RideShare::Driver + attr_reader :id, :name, :vin - # Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must be specific length to make sure it is actually a vin) + def initialize(id, name, vin) + @id = id + @name = name + @vin = vin + end + # Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must bse specific length to make sure it is actually a vin) # input: Driver.new # output: new instance of Driver with associated ID, Name and VIN. Each of those should be ACCESSIBLE (attr_reader) end diff --git a/lib/rider.rb b/lib/rider.rb index bfdc6b347..498f09c5c 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,5 +1,12 @@ class RideShare::Rider + attr_reader :id, :name, :phone + + def initialize(id, name, phone) + @id = id + @name = name + @phone = phone + end # Each rider should be instantiated (INITIALIZE) with an ID, name and phone number # input: Rider.new # output: new rider object. diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index d6994e7fe..9e0aa1297 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -5,7 +5,7 @@ describe "Diver#initialize" do it "takes an ID, Name, and Phone Number to initialize" do - my_driver.must_respond_to :id + my_rider.must_respond_to :id end end From 8e4621bfed1dc2fa011fbd2bf166a1c54e6984a2 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:15:42 -0800 Subject: [PATCH 11/33] updated test for vin == 17 characters --- lib/driver.rb | 4 ++++ ride_share.rb | 3 +++ specs/drivers_spec.rb | 8 +++++++- specs/trips_spec.rb | 4 ++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index c2abb6e82..86137bb26 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -5,6 +5,10 @@ class RideShare::Driver def initialize(id, name, vin) @id = id @name = name + + unless vin.length == 17 + raise InvalidVinError.new("VIN numbers should be 17 characters") + end @vin = vin end # Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must bse specific length to make sure it is actually a vin) diff --git a/ride_share.rb b/ride_share.rb index 31a9752d7..9830bf4b1 100644 --- a/ride_share.rb +++ b/ride_share.rb @@ -8,3 +8,6 @@ module RideShare; end class InvalidRatingError < StandardError end + +class InvalidVinError < StandardError +end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 48932bd17..caac432ce 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -1,11 +1,17 @@ require_relative 'spec_helper' describe "Driver" do - let(:my_driver) {RideShare::Driver.new(1, 2, 3)} + let(:my_driver) {RideShare::Driver.new(1, 2, "XF9HHMKS402GD41NF")} describe "Diver#initialize" do it "takes an ID, Name, and VIN to initialize" do my_driver.must_respond_to :id + my_driver.must_respond_to :name + my_driver.must_respond_to :vin + end + + it "VIN numbers should be 17 characters" do + proc {RideShare::Driver.new(1, 2, "3")}.must_raise InvalidVinError end end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 077a6a08f..557e80d44 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -12,6 +12,10 @@ describe "Trip#initialize" do it "takes an ID, Driver ID, Rider ID, Date and Rating to initialize" do my_trip.must_respond_to :id + my_trip.must_respond_to :driver_id + my_trip.must_respond_to :rider_id + my_trip.must_respond_to :date + my_trip.must_respond_to :rating end it "rating must be between 1 - 5" do From 34199e25d29f0c87d87b56a8b3bf712fcf1f7ac5 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:42:57 -0800 Subject: [PATCH 12/33] added test and method for Driver method trips --- lib/driver.rb | 14 +++++++++++--- specs/drivers_spec.rb | 31 ++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 86137bb26..67244f03f 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -11,7 +11,15 @@ def initialize(id, name, vin) end @vin = vin end - # Each driver should be INSTANTIATED (initialize) with an ID, name and VIN (VIN must bse specific length to make sure it is actually a vin) - # input: Driver.new - # output: new instance of Driver with associated ID, Name and VIN. Each of those should be ACCESSIBLE (attr_reader) + + def trips + RideShare::Trip.find_all_driver(@id) + end + + # def average_rating + # retrieve an average rating for that driver based on all trips taken. + # input: ARRAY of trips that this driver has taken + # output: Average star rating FIXNUM + # + # end end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index caac432ce..4ce49146b 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -1,7 +1,8 @@ require_relative 'spec_helper' describe "Driver" do - let(:my_driver) {RideShare::Driver.new(1, 2, "XF9HHMKS402GD41NF")} + let(:my_driver) {RideShare::Driver.new(77, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF")} + let(:trips_csv) {CSV.read("support/trips.csv")} describe "Diver#initialize" do it "takes an ID, Name, and VIN to initialize" do @@ -15,4 +16,32 @@ end end + describe "Driver#trips" do + it "returns an array of trips that this driver has taken" do + my_driver.trips.must_be_instance_of Array + end + + it "each item is of class Trip" do + my_driver.trips.each do |trip| + trip.must_be_instance_of RideShare::Trip + end + end + + it "trips.length matches number of trips from CSV file for that driver" do + trips_number = my_driver.trips.length + lines_from_csv = [] + trips_csv.each do |line| + if line[1].to_i == my_driver.id + lines_from_csv << line + end + end + lines_from_csv.length.must_equal trips_number + end + + it "Returns 0 if driver is not found" do + bad_id = RideShare::Driver.new(77777, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF") + bad_id.trips.must_equal 0 + end + end + end From 4a41b21a94b1b03546c741aae68fd6b3377f3053 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:52:05 -0800 Subject: [PATCH 13/33] created average_rating method for Driver --- lib/driver.rb | 20 ++++++++++++++------ specs/drivers_spec.rb | 7 +++++++ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 67244f03f..3bb6a5c9e 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -16,10 +16,18 @@ def trips RideShare::Trip.find_all_driver(@id) end - # def average_rating - # retrieve an average rating for that driver based on all trips taken. - # input: ARRAY of trips that this driver has taken - # output: Average star rating FIXNUM - # - # end + def average_rating + trips + total = 0 + + trips.each do |trip| + total += trip.rating + end + + return (total / trips.length) + # retrieve an average rating for that driver based on all trips taken. + # input: ARRAY of trips that this driver has taken + # output: Average star rating FIXNUM + + end end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 4ce49146b..9bc68a4c3 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -44,4 +44,11 @@ end end + describe "Driver#averate_rating" do + it "returns an average_rating based on all trips given" do + my_driver.average_rating.must_be_instance_of Integer + my_driver.average_rating.must_equal 4 + end + end + end From 3470bb344bb218077cdbc6dd0b0e5ed1f245fca8 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:57:35 -0800 Subject: [PATCH 14/33] created find_all initial test and method for Drivers. More testing needed --- lib/driver.rb | 16 +++++++++++++--- specs/drivers_spec.rb | 7 +++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 3bb6a5c9e..56df70763 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -25,9 +25,19 @@ def average_rating end return (total / trips.length) - # retrieve an average rating for that driver based on all trips taken. - # input: ARRAY of trips that this driver has taken - # output: Average star rating FIXNUM + #returns an Integer. Possible turn into a float? + end + + def self.find_all + drivers = [] + CSV.open("support/drivers.csv").each do |driver| + begin + drivers << RideShare::Driver.new(driver[0].to_i, driver[1], driver[2]) + rescue InvalidVinError => e + puts "#{ e }" + end + end + return drivers end end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 9bc68a4c3..11caf85f3 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -49,6 +49,13 @@ my_driver.average_rating.must_be_instance_of Integer my_driver.average_rating.must_equal 4 end + + #PROBABLY SHOULD WRITE SOME MORE TESTS HERE!! end + describe "find_all Driver class method" do + it "returns an array of Driver instances" do + RideShare::Driver.find_all.must_be_instance_of Array + end + end end From 02c03f78ce21cac0ad8c63a6719cf768de282f2f Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Tue, 7 Mar 2017 16:58:56 -0800 Subject: [PATCH 15/33] Left note where testing should continue --- specs/drivers_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 11caf85f3..b725f1470 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -57,5 +57,7 @@ it "returns an array of Driver instances" do RideShare::Driver.find_all.must_be_instance_of Array end + + #Definitely need more tests - look at trips spec end end From ed0308c5214dcd4cb121c1206e66fd1791c91926 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 12:49:55 -0800 Subject: [PATCH 16/33] finishing testing on Drivers find_all method --- specs/drivers_spec.rb | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index b725f1470..16a09a23c 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -3,6 +3,8 @@ describe "Driver" do let(:my_driver) {RideShare::Driver.new(77, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF")} let(:trips_csv) {CSV.read("support/trips.csv")} + let(:all_drivers) {RideShare::Driver.find_all} + let(:drivers_csv) {CSV.read("support/drivers.csv")} describe "Diver#initialize" do it "takes an ID, Name, and VIN to initialize" do @@ -55,9 +57,26 @@ describe "find_all Driver class method" do it "returns an array of Driver instances" do - RideShare::Driver.find_all.must_be_instance_of Array + all_drivers.must_be_instance_of Array end - #Definitely need more tests - look at trips spec + #Definitely need more tests - look at trips specit "each item is of class Trip" do + it "each item is of class Driver" do + all_drivers.each do |driver| + driver.must_be_instance_of RideShare::Driver + end + end + + it "number of drivers matches number of lines in CSV - 1 for headder line" do + csv_length = drivers_csv.length + all_drivers.length.must_equal(csv_length - 1) + end + + it "id of first & last match id of first & last in CSV" do + all_drivers[0].id.must_equal(drivers_csv[1][0].to_i) + all_drivers[-1].id.must_equal(drivers_csv[-1][0].to_i) + end end + + end From 825c7052080610c5d0a803f17817e069c31accff Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 13:00:31 -0800 Subject: [PATCH 17/33] added testing for Driver find_driver method and tests --- lib/driver.rb | 13 ++++++++++++- specs/drivers_spec.rb | 15 +++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index 56df70763..7a3dcab73 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -23,7 +23,6 @@ def average_rating trips.each do |trip| total += trip.rating end - return (total / trips.length) #returns an Integer. Possible turn into a float? end @@ -40,4 +39,16 @@ def self.find_all end return drivers end + + def self.find_driver(id) + all_drivers = RideShare::Driver.find_all + specific_driver = 0 + all_drivers.each do |driver| + if driver.id == id + specific_driver = driver + end + end + return specific_driver + end + end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 16a09a23c..9d573bc2e 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -78,5 +78,20 @@ end end + describe "find_driver Driver class method" do + it "should return one Driver based on numeric ID" do + my_driver = RideShare::Driver.find_driver(53) + my_driver.must_be_instance_of RideShare::Driver + end + + #possibly want to return different cases if enter in + #a number that's not a driver ID vs a string + + it "should return 0 if no driver found by that ID" do + bad_id = RideShare::Driver.find_driver("apple") + bad_id.must_equal 0 + end + + end end From c1be632f7d92654c8895e7cd66f77b05ec90b5af Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 13:04:31 -0800 Subject: [PATCH 18/33] added initial testing for Rider.trips and method --- lib/rider.rb | 11 +++++++---- specs/riders_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/rider.rb b/lib/rider.rb index 498f09c5c..1da0c083d 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,14 +1,17 @@ class RideShare::Rider attr_reader :id, :name, :phone - + def initialize(id, name, phone) @id = id @name = name @phone = phone end - # Each rider should be instantiated (INITIALIZE) with an ID, name and phone number - # input: Rider.new - # output: new rider object. + def trips + RideShare::Trip.find_all_rider(@id) + end + # Retrieve a list of trips only this rider has taken + # input: Rider ID + # output: Array of Trip instances end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index 9e0aa1297..e7fc430b4 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -6,7 +6,37 @@ describe "Diver#initialize" do it "takes an ID, Name, and Phone Number to initialize" do my_rider.must_respond_to :id + my_rider.must_respond_to :name + my_rider.must_respond_to :phone end end + describe "Rider#trips" do + it "returns an array of trips that this rider has taken" do + my_rider.trips.must_be_instance_of Array + end + + # it "each item is of class Trip" do + # my_driver.trips.each do |trip| + # trip.must_be_instance_of RideShare::Trip + # end + # end + # + # it "trips.length matches number of trips from CSV file for that driver" do + # trips_number = my_driver.trips.length + # lines_from_csv = [] + # trips_csv.each do |line| + # if line[1].to_i == my_driver.id + # lines_from_csv << line + # end + # end + # lines_from_csv.length.must_equal trips_number + # end + # + # it "Returns 0 if driver is not found" do + # bad_id = RideShare::Driver.new(77777, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF") + # bad_id.trips.must_equal 0 + # end + end + end From 7a5e266ad5c6de22ee56b9839b825e77060ea7f8 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 13:08:37 -0800 Subject: [PATCH 19/33] finish testing for Rider.trips --- specs/riders_spec.rb | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index e7fc430b4..a8d3f6d75 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -2,6 +2,7 @@ describe "rider_id" do let(:my_rider) {RideShare::Rider.new(1, 2, 3)} + let(:trips_csv) {CSV.read("support/trips.csv")} describe "Diver#initialize" do it "takes an ID, Name, and Phone Number to initialize" do @@ -16,27 +17,27 @@ my_rider.trips.must_be_instance_of Array end - # it "each item is of class Trip" do - # my_driver.trips.each do |trip| - # trip.must_be_instance_of RideShare::Trip - # end - # end - # - # it "trips.length matches number of trips from CSV file for that driver" do - # trips_number = my_driver.trips.length - # lines_from_csv = [] - # trips_csv.each do |line| - # if line[1].to_i == my_driver.id - # lines_from_csv << line - # end - # end - # lines_from_csv.length.must_equal trips_number - # end - # - # it "Returns 0 if driver is not found" do - # bad_id = RideShare::Driver.new(77777, "Mr. Shanie Gusikowski", "XF9HHMKS402GD41NF") - # bad_id.trips.must_equal 0 - # end + it "each item is of class Trip" do + my_rider.trips.each do |trip| + trip.must_be_instance_of RideShare::Trip + end + end + + it "trips.length matches number of trips from CSV file for that rider" do + trips_number = my_rider.trips.length + lines_from_csv = [] + trips_csv.each do |line| + if line[2].to_i == my_rider.id + lines_from_csv << line + end + end + lines_from_csv.length.must_equal trips_number + end + + it "Returns 0 if driver is not found" do + bad_id = RideShare::Rider.new(901291029102, "Nope", "Nope") + bad_id.trips.must_equal 0 + end end end From 2dc3da82c8a3486b3bc93bf9d34ba0b7855d528d Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 13:35:39 -0800 Subject: [PATCH 20/33] created test and method for trip find_driver --- lib/rider.rb | 13 ++++++++++--- lib/trip.rb | 14 ++++++++------ pseudocode.txt | 4 ++-- specs/riders_spec.rb | 7 +++++++ specs/trips_spec.rb | 14 +++++++++++++- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/rider.rb b/lib/rider.rb index 1da0c083d..bc2e4632d 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -11,7 +11,14 @@ def initialize(id, name, phone) def trips RideShare::Trip.find_all_rider(@id) end - # Retrieve a list of trips only this rider has taken - # input: Rider ID - # output: Array of Trip instances + + def drivers + #leaving this to come back + #send trips back to trips to find the driver instances! + # driver_ids = trips.map { |trip| trip.driver_id } + end + # + # Retrieve a list of all previous driver instances this rider has rode with. + # input: Array of Trip instances (from above) + # output: Array of the associated drivers. end diff --git a/lib/trip.rb b/lib/trip.rb index d315bc767..7ae7bed63 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -57,13 +57,15 @@ def self.find_all_rider(rider_id) rider_trips end - def self.find_driver(trip_id) - + def find_driver + RideShare::Driver.find_driver(@driver_id) + # Retrieve associated driver instance through driver ID + # input: Driver ID + # output: Driver Object associated w/ trip + # end - # Retrieve associated driver instance through driver ID - # input: Driver ID - # output: Driver Object associated w/ trip - # + + # Retrieve associated Rider instance through Rider ID # input: Rider ID # output: Rider object associated w/ trip diff --git a/pseudocode.txt b/pseudocode.txt index 46f2481d1..2809e41f4 100644 --- a/pseudocode.txt +++ b/pseudocode.txt @@ -5,8 +5,8 @@ Trip CLASS output: new Trip object w/ applicable ratings Retrieve associated driver instance through driver ID - input: Driver ID - output: Driver Object associated w/ trip + input: Driver ID - send this information to Driver class + output: Driver Class returns Driver Object associated w/ trip Retrieve associated Rider instance through Rider ID input: Rider ID diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index a8d3f6d75..73e0b773f 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -40,4 +40,11 @@ end end + # Leaving this to continue to build out rest of driver and trips + # describe "Rider#drivers" do + # it "Retrieve a list of all previous driver instances this rider has rode with" do + # my_rider.drivers.must_be_instance_of Array + # end + # end + end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 557e80d44..7cf2bb2b6 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -3,7 +3,7 @@ describe "Trip" do - let(:my_trip) {RideShare::Trip.new(1, 2, 3, 4, 5)} + let(:my_trip) {RideShare::Trip.new(1, 1, 54, 2016-04-05, 3)} let(:all_trips) {RideShare::Trip.find_all} let(:all_driver_trips) {RideShare::Trip.find_all_driver(39)} let(:all_rider_trips) {RideShare::Trip.find_all_rider(54)} @@ -78,4 +78,16 @@ bad_id.must_equal 0 end end + + describe "Testing self.find_driver class method" do + it "returns an a specific driver instance" do + my_trip.find_driver.must_be_instance_of RideShare::Driver + end + + #Is this an appropriate edge case? What if given empty string?? + # it "should return 0 if no driver associated with this instance" do + # + # end + + end end From 45877ef3b7434d8506e89212b0309ddb84d8440d Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 13:42:39 -0800 Subject: [PATCH 21/33] added initial test and method for find_all Class method in Riders --- lib/rider.rb | 16 ++++++++++++++++ lib/trip.rb | 5 +++-- specs/drivers_spec.rb | 1 - specs/riders_spec.rb | 27 +++++++++++++++++++++++++++ specs/trips_spec.rb | 8 +++++++- 5 files changed, 53 insertions(+), 4 deletions(-) diff --git a/lib/rider.rb b/lib/rider.rb index bc2e4632d..a961e507c 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -21,4 +21,20 @@ def drivers # Retrieve a list of all previous driver instances this rider has rode with. # input: Array of Trip instances (from above) # output: Array of the associated drivers. + + def self.find_all + riders = [] + + CSV.open("support/riders.csv").each do |rider| + begin + riders << RideShare::Rider.new(rider[0].to_i, rider[1], rider[2]) + rescue InvalidVinError => e + puts "#{ e }" + end + end + return riders + # Retrieve all riders from CSV file + # input: calling CLASS METHOD + # output: list of all RIDERS + end end diff --git a/lib/trip.rb b/lib/trip.rb index 7ae7bed63..7f3587c5a 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -65,10 +65,11 @@ def find_driver # end - + def find_rider + RideShare::Rider.find_rider(@rider_id) # Retrieve associated Rider instance through Rider ID # input: Rider ID # output: Rider object associated w/ trip - # + end end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 9d573bc2e..87690db78 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -60,7 +60,6 @@ all_drivers.must_be_instance_of Array end - #Definitely need more tests - look at trips specit "each item is of class Trip" do it "each item is of class Driver" do all_drivers.each do |driver| driver.must_be_instance_of RideShare::Driver diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index 73e0b773f..389efe72c 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -3,6 +3,7 @@ describe "rider_id" do let(:my_rider) {RideShare::Rider.new(1, 2, 3)} let(:trips_csv) {CSV.read("support/trips.csv")} + let(:all_riders) {RideShare::Rider.find_all} describe "Diver#initialize" do it "takes an ID, Name, and Phone Number to initialize" do @@ -47,4 +48,30 @@ # end # end + describe "find_all Rider class method" do + it "returns an array of Rider instances" do + all_riders.must_be_instance_of Array + end + + it "each item is of class Driver" do + skip + all_drivers.each do |driver| + driver.must_be_instance_of RideShare::Driver + end + end + + it "number of drivers matches number of lines in CSV - 1 for headder line" do + skip + csv_length = drivers_csv.length + all_drivers.length.must_equal(csv_length - 1) + end + + it "id of first & last match id of first & last in CSV" do + skip + all_drivers[0].id.must_equal(drivers_csv[1][0].to_i) + all_drivers[-1].id.must_equal(drivers_csv[-1][0].to_i) + end + end + + end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 7cf2bb2b6..0fb9c1eaa 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -79,7 +79,7 @@ end end - describe "Testing self.find_driver class method" do + describe "Testing Trip#find_driver method" do it "returns an a specific driver instance" do my_trip.find_driver.must_be_instance_of RideShare::Driver end @@ -88,6 +88,12 @@ # it "should return 0 if no driver associated with this instance" do # # end + end + describe "Testing Trip#find_rider class method" do + it "returns an a specific driver instance" do + skip + my_trip.find_rider.must_be_instance_of RideShare::Rider + end end end From 13425073e66cf85656e64fe20de461c94033bf19 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 13:48:30 -0800 Subject: [PATCH 22/33] finishing find_all for Riders, adding rescue in reading CSV --- lib/rider.rb | 10 ++++++---- ride_share.rb | 3 +++ specs/riders_spec.rb | 19 +++++++++---------- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/rider.rb b/lib/rider.rb index a961e507c..69cf34965 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -3,6 +3,10 @@ class RideShare::Rider attr_reader :id, :name, :phone def initialize(id, name, phone) + unless id > 0 + raise InvalidIDError.new("Rider IDs must be an Integer grater than 0") + end + @id = id @name = name @phone = phone @@ -28,13 +32,11 @@ def self.find_all CSV.open("support/riders.csv").each do |rider| begin riders << RideShare::Rider.new(rider[0].to_i, rider[1], rider[2]) - rescue InvalidVinError => e + rescue InvalidIDError => e puts "#{ e }" end end return riders - # Retrieve all riders from CSV file - # input: calling CLASS METHOD - # output: list of all RIDERS + end end diff --git a/ride_share.rb b/ride_share.rb index 9830bf4b1..b2076de72 100644 --- a/ride_share.rb +++ b/ride_share.rb @@ -11,3 +11,6 @@ class InvalidRatingError < StandardError class InvalidVinError < StandardError end + +class InvalidIDError < StandardError +end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index 389efe72c..45e09c217 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -4,6 +4,7 @@ let(:my_rider) {RideShare::Rider.new(1, 2, 3)} let(:trips_csv) {CSV.read("support/trips.csv")} let(:all_riders) {RideShare::Rider.find_all} + let(:riders_csv) {CSV.read("support/riders.csv")} describe "Diver#initialize" do it "takes an ID, Name, and Phone Number to initialize" do @@ -53,23 +54,21 @@ all_riders.must_be_instance_of Array end - it "each item is of class Driver" do - skip - all_drivers.each do |driver| - driver.must_be_instance_of RideShare::Driver + it "each item is of class Rider" do + all_riders.each do |rider| + rider.must_be_instance_of RideShare::Rider end end - it "number of drivers matches number of lines in CSV - 1 for headder line" do - skip - csv_length = drivers_csv.length - all_drivers.length.must_equal(csv_length - 1) + it "number of riders matches number of lines in CSV - 1 for headder line" do + csv_length = riders_csv.length + all_riders.length.must_equal(csv_length - 1) end it "id of first & last match id of first & last in CSV" do skip - all_drivers[0].id.must_equal(drivers_csv[1][0].to_i) - all_drivers[-1].id.must_equal(drivers_csv[-1][0].to_i) + all_riders[0].id.must_equal(riders_csv[1][0].to_i) + all_riders[-1].id.must_equal(riders_csv[-1][0].to_i) end end From 649b5307ffa861985bccf1890a62db3173d48223 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 14:00:07 -0800 Subject: [PATCH 23/33] finished initial test for find_rider Rider method. Refractored find_driver method as well --- lib/driver.rb | 10 +++------- lib/rider.rb | 18 ++++++++++++++---- specs/riders_spec.rb | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 7a3dcab73..2f459b215 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -42,13 +42,9 @@ def self.find_all def self.find_driver(id) all_drivers = RideShare::Driver.find_all - specific_driver = 0 - all_drivers.each do |driver| - if driver.id == id - specific_driver = driver - end - end - return specific_driver + found_driver = all_drivers.find { |driver| driver.id == id } + return 0 if found_driver == nil + return found_driver end end diff --git a/lib/rider.rb b/lib/rider.rb index 69cf34965..0400c1064 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -20,11 +20,11 @@ def drivers #leaving this to come back #send trips back to trips to find the driver instances! # driver_ids = trips.map { |trip| trip.driver_id } + # + # Retrieve a list of all previous driver instances this rider has rode with. + # input: Array of Trip instances (from above) + # output: Array of the associated drivers. end - # - # Retrieve a list of all previous driver instances this rider has rode with. - # input: Array of Trip instances (from above) - # output: Array of the associated drivers. def self.find_all riders = [] @@ -37,6 +37,16 @@ def self.find_all end end return riders + end + + def self.find_rider(id) + all_riders = RideShare::Rider.find_all + found_rider = all_riders.find { |rider| rider.id == id } + return 0 if found_rider == nil + return found_rider + # Retrieve specific rider using numeric ID + # input: Rider ID + # output: Specific rider (from list above) end end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index 45e09c217..238895ee7 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -72,5 +72,20 @@ end end + describe "find_rider Rider class method" do + it "should return one rider based on numeric ID" do + my_rider = RideShare::Rider.find_rider(1) + my_rider.must_be_instance_of RideShare::Rider + end + + #possibly want to return different cases if enter in + #a number that's not a driver ID vs a string + + it "should return 0 if no driver found by that ID" do + bad_id = RideShare::Rider.find_rider("apple") + bad_id.must_equal 0 + end + + end end From 670f3f1844b051585a4da15ed4184f3177bcb3a3 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 14:27:48 -0800 Subject: [PATCH 24/33] Rider.drivers initial test and method complete --- lib/driver.rb | 4 ++-- lib/rider.rb | 24 +++++++++++------------- lib/trip.rb | 9 --------- specs/riders_spec.rb | 21 +++++++++++---------- specs/trips_spec.rb | 1 - 5 files changed, 24 insertions(+), 35 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 2f459b215..591b177a7 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -40,9 +40,9 @@ def self.find_all return drivers end - def self.find_driver(id) + def self.find_driver(driver_id) all_drivers = RideShare::Driver.find_all - found_driver = all_drivers.find { |driver| driver.id == id } + found_driver = all_drivers.find { |driver| driver.id == driver_id } return 0 if found_driver == nil return found_driver end diff --git a/lib/rider.rb b/lib/rider.rb index 0400c1064..fba29683e 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -17,13 +17,15 @@ def trips end def drivers - #leaving this to come back - #send trips back to trips to find the driver instances! - # driver_ids = trips.map { |trip| trip.driver_id } - # - # Retrieve a list of all previous driver instances this rider has rode with. - # input: Array of Trip instances (from above) - # output: Array of the associated drivers. + rider_trips = trips + rider_drivers = [] + + rider_trips.each do |trip| + rider_drivers << RideShare::Driver.find_driver(trip.driver_id) + end + + return rider_drivers + end def self.find_all @@ -39,14 +41,10 @@ def self.find_all return riders end - def self.find_rider(id) + def self.find_rider(rider_id) all_riders = RideShare::Rider.find_all - found_rider = all_riders.find { |rider| rider.id == id } + found_rider = all_riders.find { |rider| rider.id == rider_id } return 0 if found_rider == nil return found_rider - # Retrieve specific rider using numeric ID - # input: Rider ID - # output: Specific rider (from list above) - end end diff --git a/lib/trip.rb b/lib/trip.rb index 7f3587c5a..8a757afdd 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -24,8 +24,6 @@ def self.find_all puts "#{ e }" end end - - # trips.shift => I guess I don't need this. return trips end @@ -59,17 +57,10 @@ def self.find_all_rider(rider_id) def find_driver RideShare::Driver.find_driver(@driver_id) - # Retrieve associated driver instance through driver ID - # input: Driver ID - # output: Driver Object associated w/ trip - # end def find_rider RideShare::Rider.find_rider(@rider_id) - # Retrieve associated Rider instance through Rider ID - # input: Rider ID - # output: Rider object associated w/ trip end end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index 238895ee7..7c88297cd 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -42,12 +42,17 @@ end end - # Leaving this to continue to build out rest of driver and trips - # describe "Rider#drivers" do - # it "Retrieve a list of all previous driver instances this rider has rode with" do - # my_rider.drivers.must_be_instance_of Array - # end - # end + describe "Rider#drivers" do + it "Retrieve an array of all previous driver instances this rider has rode with" do + my_rider.drivers.must_be_instance_of Array + my_rider.drivers[0].must_be_instance_of RideShare::Driver + my_rider.drivers[-1].must_be_instance_of RideShare::Driver + end + + it "Returns accurate drivers associated with trips" do + #driverobject.id must equal trip.driver_id + end + end describe "find_all Rider class method" do it "returns an array of Rider instances" do @@ -66,7 +71,6 @@ end it "id of first & last match id of first & last in CSV" do - skip all_riders[0].id.must_equal(riders_csv[1][0].to_i) all_riders[-1].id.must_equal(riders_csv[-1][0].to_i) end @@ -78,9 +82,6 @@ my_rider.must_be_instance_of RideShare::Rider end - #possibly want to return different cases if enter in - #a number that's not a driver ID vs a string - it "should return 0 if no driver found by that ID" do bad_id = RideShare::Rider.find_rider("apple") bad_id.must_equal 0 diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 0fb9c1eaa..27750fa0a 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -92,7 +92,6 @@ describe "Testing Trip#find_rider class method" do it "returns an a specific driver instance" do - skip my_trip.find_rider.must_be_instance_of RideShare::Rider end end From 9631371724ceed04fdb16041ee3dfc8964e243ad Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 14:46:44 -0800 Subject: [PATCH 25/33] changed rating to be float instead of integer. Adjusted tests --- lib/driver.rb | 3 ++- specs/drivers_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 591b177a7..98e7a912d 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -23,7 +23,8 @@ def average_rating trips.each do |trip| total += trip.rating end - return (total / trips.length) + rating = (total.to_f / trips.length) + return rating.round(1) #returns an Integer. Possible turn into a float? end diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index 87690db78..bc3548425 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -48,8 +48,8 @@ describe "Driver#averate_rating" do it "returns an average_rating based on all trips given" do - my_driver.average_rating.must_be_instance_of Integer - my_driver.average_rating.must_equal 4 + my_driver.average_rating.must_be_instance_of Float + my_driver.average_rating.must_equal 4.2 end #PROBABLY SHOULD WRITE SOME MORE TESTS HERE!! From c07995440084e0d6cd65c9efc67f773902028996 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 15:19:42 -0800 Subject: [PATCH 26/33] drying up find methods with find & find_all enumerables --- lib/driver.rb | 9 +++------ lib/rider.rb | 4 ++-- lib/trip.rb | 15 +++++---------- specs/drivers_spec.rb | 1 - 4 files changed, 10 insertions(+), 19 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 98e7a912d..ea8a48226 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -17,20 +17,15 @@ def trips end def average_rating - trips total = 0 + trips.each { |trip| total += trip.rating } - trips.each do |trip| - total += trip.rating - end rating = (total.to_f / trips.length) return rating.round(1) - #returns an Integer. Possible turn into a float? end def self.find_all drivers = [] - CSV.open("support/drivers.csv").each do |driver| begin drivers << RideShare::Driver.new(driver[0].to_i, driver[1], driver[2]) @@ -38,12 +33,14 @@ def self.find_all puts "#{ e }" end end + return drivers end def self.find_driver(driver_id) all_drivers = RideShare::Driver.find_all found_driver = all_drivers.find { |driver| driver.id == driver_id } + return 0 if found_driver == nil return found_driver end diff --git a/lib/rider.rb b/lib/rider.rb index fba29683e..243199a93 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -17,15 +17,15 @@ def trips end def drivers + #adjust to be unique list?? rider_trips = trips - rider_drivers = [] + rider_drivers = [] rider_trips.each do |trip| rider_drivers << RideShare::Driver.find_driver(trip.driver_id) end return rider_drivers - end def self.find_all diff --git a/lib/trip.rb b/lib/trip.rb index 8a757afdd..42d934250 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -24,17 +24,15 @@ def self.find_all puts "#{ e }" end end + return trips end def self.find_all_driver(driver_id) all_trips = find_all - driver_trips = [] - all_trips.each do |trip| - if trip.driver_id == driver_id - driver_trips << trip - end + driver_trips = all_trips.find_all do |trip| + trip.driver_id == driver_id end return 0 if driver_trips.empty? @@ -43,12 +41,9 @@ def self.find_all_driver(driver_id) def self.find_all_rider(rider_id) all_trips = find_all - rider_trips = [] - all_trips.each do |trip| - if trip.rider_id == rider_id - rider_trips << trip - end + rider_trips = all_trips.find_all do |trip| + trip.rider_id == rider_id end return 0 if rider_trips.empty? diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index bc3548425..ef72d439f 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -52,7 +52,6 @@ my_driver.average_rating.must_equal 4.2 end - #PROBABLY SHOULD WRITE SOME MORE TESTS HERE!! end describe "find_all Driver class method" do From efae1b93c54d75d18f1ba193221ff2d9cf384fa2 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 15:50:38 -0800 Subject: [PATCH 27/33] refractored Rider.trips to go through Trips class instead of directly to Drivers class. --- lib/driver.rb | 2 -- lib/rider.rb | 13 +++---------- lib/trip.rb | 22 ++++++++-------------- specs/trips_spec.rb | 34 +++++++++++++++++----------------- 4 files changed, 28 insertions(+), 43 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index ea8a48226..29f87c017 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -33,14 +33,12 @@ def self.find_all puts "#{ e }" end end - return drivers end def self.find_driver(driver_id) all_drivers = RideShare::Driver.find_all found_driver = all_drivers.find { |driver| driver.id == driver_id } - return 0 if found_driver == nil return found_driver end diff --git a/lib/rider.rb b/lib/rider.rb index 243199a93..fc9092ae9 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -17,20 +17,13 @@ def trips end def drivers - #adjust to be unique list?? - rider_trips = trips - - rider_drivers = [] - rider_trips.each do |trip| - rider_drivers << RideShare::Driver.find_driver(trip.driver_id) + trips.map do |trip| + trip.driver end - - return rider_drivers end def self.find_all riders = [] - CSV.open("support/riders.csv").each do |rider| begin riders << RideShare::Rider.new(rider[0].to_i, rider[1], rider[2]) @@ -45,6 +38,6 @@ def self.find_rider(rider_id) all_riders = RideShare::Rider.find_all found_rider = all_riders.find { |rider| rider.id == rider_id } return 0 if found_rider == nil - return found_rider + found_rider end end diff --git a/lib/trip.rb b/lib/trip.rb index 42d934250..5efafc24a 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -14,9 +14,16 @@ def initialize(id, driver_id, rider_id, date, rating) @rating = rating end + def driver + RideShare::Driver.find_driver(@driver_id) + end + + def rider + RideShare::Rider.find_rider(@rider_id) + end + def self.find_all trips = [] - CSV.open("support/trips.csv").each do |trip| begin trips << RideShare::Trip.new(trip[0].to_i, trip[1].to_i, trip[2].to_i, trip[3], trip[4].to_i) @@ -24,38 +31,25 @@ def self.find_all puts "#{ e }" end end - return trips end def self.find_all_driver(driver_id) all_trips = find_all - driver_trips = all_trips.find_all do |trip| trip.driver_id == driver_id end - return 0 if driver_trips.empty? driver_trips end def self.find_all_rider(rider_id) all_trips = find_all - rider_trips = all_trips.find_all do |trip| trip.rider_id == rider_id end - return 0 if rider_trips.empty? rider_trips end - def find_driver - RideShare::Driver.find_driver(@driver_id) - end - - def find_rider - RideShare::Rider.find_rider(@rider_id) - end - end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index 27750fa0a..b17e51544 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -23,6 +23,23 @@ end end + describe "Testing #driver method" do + it "returns an a specific driver instance" do + my_trip.driver.must_be_instance_of RideShare::Driver + end + + #Is this an appropriate edge case? What if given empty string?? + # it "should return 0 if no driver associated with this instance" do + # + # end + end + + describe "Testing #rider method" do + it "returns an a specific driver instance" do + my_trip.rider.must_be_instance_of RideShare::Rider + end + end + describe "Testing Trip#find_all class method" do it "returns an array of Trip instances" do all_trips.must_be_instance_of(Array) @@ -78,21 +95,4 @@ bad_id.must_equal 0 end end - - describe "Testing Trip#find_driver method" do - it "returns an a specific driver instance" do - my_trip.find_driver.must_be_instance_of RideShare::Driver - end - - #Is this an appropriate edge case? What if given empty string?? - # it "should return 0 if no driver associated with this instance" do - # - # end - end - - describe "Testing Trip#find_rider class method" do - it "returns an a specific driver instance" do - my_trip.find_rider.must_be_instance_of RideShare::Rider - end - end end From 961322f9d554d01a633880ca41669a32f5ebf3c5 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Wed, 8 Mar 2017 16:33:54 -0800 Subject: [PATCH 28/33] working on test for .trips in Rider. Testing WIP --- specs/drivers_spec.rb | 4 ---- specs/riders_spec.rb | 5 ++++- specs/trips_spec.rb | 4 ---- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/specs/drivers_spec.rb b/specs/drivers_spec.rb index ef72d439f..8860f7988 100644 --- a/specs/drivers_spec.rb +++ b/specs/drivers_spec.rb @@ -82,14 +82,10 @@ my_driver.must_be_instance_of RideShare::Driver end - #possibly want to return different cases if enter in - #a number that's not a driver ID vs a string - it "should return 0 if no driver found by that ID" do bad_id = RideShare::Driver.find_driver("apple") bad_id.must_equal 0 end - end end diff --git a/specs/riders_spec.rb b/specs/riders_spec.rb index 7c88297cd..c27d8b1da 100644 --- a/specs/riders_spec.rb +++ b/specs/riders_spec.rb @@ -49,8 +49,11 @@ my_rider.drivers[-1].must_be_instance_of RideShare::Driver end + #WIP FROM HERE!! it "Returns accurate drivers associated with trips" do - #driverobject.id must equal trip.driver_id + drivers = my_rider.drivers + drivers[0].must_equal #find something this should equal? + #driver.id must equal trip.driver_id end end diff --git a/specs/trips_spec.rb b/specs/trips_spec.rb index b17e51544..436901931 100644 --- a/specs/trips_spec.rb +++ b/specs/trips_spec.rb @@ -28,10 +28,6 @@ my_trip.driver.must_be_instance_of RideShare::Driver end - #Is this an appropriate edge case? What if given empty string?? - # it "should return 0 if no driver associated with this instance" do - # - # end end describe "Testing #rider method" do From 0ee2b95825611c6986b74d5e30190492f4ad86e1 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Thu, 9 Mar 2017 09:19:14 -0800 Subject: [PATCH 29/33] updated Rider.drivers to handle duplicate driver instances & .trips to handle if Rider has no trips --- lib/rider.rb | 3 ++- specs/{drivers_spec.rb => driver_spec.rb} | 0 specs/{riders_spec.rb => rider_spec.rb} | 23 +++++++++++++++++++---- specs/{trips_spec.rb => trip_spec.rb} | 0 4 files changed, 21 insertions(+), 5 deletions(-) rename specs/{drivers_spec.rb => driver_spec.rb} (100%) rename specs/{riders_spec.rb => rider_spec.rb} (79%) rename specs/{trips_spec.rb => trip_spec.rb} (100%) diff --git a/lib/rider.rb b/lib/rider.rb index fc9092ae9..b1dc60190 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -17,9 +17,10 @@ def trips end def drivers - trips.map do |trip| + drivers = trips.map do |trip| trip.driver end + return drivers.uniq { |driver| driver.id } end def self.find_all diff --git a/specs/drivers_spec.rb b/specs/driver_spec.rb similarity index 100% rename from specs/drivers_spec.rb rename to specs/driver_spec.rb diff --git a/specs/riders_spec.rb b/specs/rider_spec.rb similarity index 79% rename from specs/riders_spec.rb rename to specs/rider_spec.rb index c27d8b1da..5a92ab6ca 100644 --- a/specs/riders_spec.rb +++ b/specs/rider_spec.rb @@ -36,10 +36,15 @@ lines_from_csv.length.must_equal trips_number end - it "Returns 0 if driver is not found" do + it "Returns 0 if rider is not found" do bad_id = RideShare::Rider.new(901291029102, "Nope", "Nope") bad_id.trips.must_equal 0 end + + it "Returns 0 if rider has not had a trip" do + no_trips = RideShare::Rider.new(300, "Miss Isom Gleason", "791-114-8423 x70188") + no_trips.trips.must_equal 0 + end end describe "Rider#drivers" do @@ -49,11 +54,21 @@ my_rider.drivers[-1].must_be_instance_of RideShare::Driver end - #WIP FROM HERE!! it "Returns accurate drivers associated with trips" do drivers = my_rider.drivers - drivers[0].must_equal #find something this should equal? - #driver.id must equal trip.driver_id + first_driver_id = drivers[0].id + + trips = my_rider.trips + first_driver_id_trip = trips[0].driver_id + + first_driver_id.must_equal first_driver_id_trip + end + + it "Returns unique list of Drivers, even if Rider has driven with driver more than once" do + rider = RideShare::Rider.new(250, "Kylie Cartwright", "734.297.0789 x3288") + drivers = rider.drivers + driver_ids = drivers.map { |driver| driver.id } + driver_ids.uniq!.must_equal nil end end diff --git a/specs/trips_spec.rb b/specs/trip_spec.rb similarity index 100% rename from specs/trips_spec.rb rename to specs/trip_spec.rb From acdea67eeb31f244a01d58fa44163104de3e6be8 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Thu, 9 Mar 2017 09:29:20 -0800 Subject: [PATCH 30/33] cleaning up indents --- lib/trip.rb | 2 +- specs/rider_spec.rb | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 5efafc24a..432d77c6d 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -9,7 +9,7 @@ def initialize(id, driver_id, rider_id, date, rating) @date = date unless rating > 0 && rating < 6 - raise InvalidRatingError.new("Ride rating must be 1-5, your rating was #{ rating }") + raise InvalidRatingError.new("Rating must be 1-5, your rating was #{ rating }") end @rating = rating end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 5a92ab6ca..87494fc36 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -25,7 +25,7 @@ end end - it "trips.length matches number of trips from CSV file for that rider" do + it "trips.length matches number of trips from CSV file" do trips_number = my_rider.trips.length lines_from_csv = [] trips_csv.each do |line| @@ -48,7 +48,7 @@ end describe "Rider#drivers" do - it "Retrieve an array of all previous driver instances this rider has rode with" do + it "Retrieve an array of all previous driver instances for this rider" do my_rider.drivers.must_be_instance_of Array my_rider.drivers[0].must_be_instance_of RideShare::Driver my_rider.drivers[-1].must_be_instance_of RideShare::Driver @@ -64,11 +64,11 @@ first_driver_id.must_equal first_driver_id_trip end - it "Returns unique list of Drivers, even if Rider has driven with driver more than once" do + it "Returns unique list of Drivers" do rider = RideShare::Rider.new(250, "Kylie Cartwright", "734.297.0789 x3288") drivers = rider.drivers driver_ids = drivers.map { |driver| driver.id } - driver_ids.uniq!.must_equal nil + driver_ids.uniq!.must_be_nil end end @@ -96,8 +96,8 @@ describe "find_rider Rider class method" do it "should return one rider based on numeric ID" do - my_rider = RideShare::Rider.find_rider(1) - my_rider.must_be_instance_of RideShare::Rider + this_rider = RideShare::Rider.find_rider(1) + this_rider.must_be_instance_of RideShare::Rider end it "should return 0 if no driver found by that ID" do From f0bd43d1d6c8c09273d7d02a7a77ac8060f53853 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Sun, 12 Mar 2017 19:23:35 -0700 Subject: [PATCH 31/33] DRYd up some tests on Driver w/find_all instead of each. Added test case for average rating for driver who had no rides yet --- lib/driver.rb | 8 ++++++-- lib/rider.rb | 5 ++--- specs/driver_spec.rb | 14 +++++++++----- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 29f87c017..9a6edad9e 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -18,8 +18,12 @@ def trips def average_rating total = 0 - trips.each { |trip| total += trip.rating } - + if trips == 0 + return 0 + else + trips.each { |trip| total += trip.rating } + end + rating = (total.to_f / trips.length) return rating.round(1) end diff --git a/lib/rider.rb b/lib/rider.rb index b1dc60190..57b37c5b3 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -17,9 +17,8 @@ def trips end def drivers - drivers = trips.map do |trip| - trip.driver - end + drivers = trips.map { |trip| trip.driver } + return drivers.uniq { |driver| driver.id } end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 8860f7988..a202bfc0e 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -31,11 +31,8 @@ it "trips.length matches number of trips from CSV file for that driver" do trips_number = my_driver.trips.length - lines_from_csv = [] - trips_csv.each do |line| - if line[1].to_i == my_driver.id - lines_from_csv << line - end + lines_from_csv = trips_csv.find_all do |line| + line[1].to_i == my_driver.id end lines_from_csv.length.must_equal trips_number end @@ -52,6 +49,11 @@ my_driver.average_rating.must_equal 4.2 end + it "returns 0 if that driver has not yet had any trips" do + new_driver = RideShare::Driver.new(123456, "Ms. Lynn Trickey", "ZFLHHMKS402GD4P09") + new_driver.average_rating.must_equal 0 + end + end describe "find_all Driver class method" do @@ -84,7 +86,9 @@ it "should return 0 if no driver found by that ID" do bad_id = RideShare::Driver.find_driver("apple") + other_bad_id = RideShare::Driver.find_driver(98098098098) bad_id.must_equal 0 + other_bad_id.must_equal 0 end end From 24473bdde0880906d6697d1240c36642942cb1fb Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Sun, 12 Mar 2017 19:27:48 -0700 Subject: [PATCH 32/33] used find_all to DRY up test for Rider.trips --- specs/driver_spec.rb | 2 -- specs/rider_spec.rb | 7 ++----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index a202bfc0e..f46ed8d40 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -53,7 +53,6 @@ new_driver = RideShare::Driver.new(123456, "Ms. Lynn Trickey", "ZFLHHMKS402GD4P09") new_driver.average_rating.must_equal 0 end - end describe "find_all Driver class method" do @@ -91,5 +90,4 @@ other_bad_id.must_equal 0 end end - end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 87494fc36..fae210298 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -27,11 +27,8 @@ it "trips.length matches number of trips from CSV file" do trips_number = my_rider.trips.length - lines_from_csv = [] - trips_csv.each do |line| - if line[2].to_i == my_rider.id - lines_from_csv << line - end + lines_from_csv = trips_csv.find_all do |line| + line[2].to_i == my_rider.id end lines_from_csv.length.must_equal trips_number end From 279fb4dd4deaa5381e55c25c12ae04e8013fc859 Mon Sep 17 00:00:00 2001 From: Lynn Trickey Date: Sun, 12 Mar 2017 19:45:04 -0700 Subject: [PATCH 33/33] finished checking all code & trying to cover edge cases with testing. --- lib/trip.rb | 4 ++-- specs/rider_spec.rb | 2 -- specs/trip_spec.rb | 10 ++++++---- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 432d77c6d..dc11ae503 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -35,7 +35,7 @@ def self.find_all end def self.find_all_driver(driver_id) - all_trips = find_all + all_trips = RideShare::Trip.find_all driver_trips = all_trips.find_all do |trip| trip.driver_id == driver_id end @@ -44,7 +44,7 @@ def self.find_all_driver(driver_id) end def self.find_all_rider(rider_id) - all_trips = find_all + all_trips = RideShare::Trip.find_all rider_trips = all_trips.find_all do |trip| trip.rider_id == rider_id end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index fae210298..9cae9a531 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -101,7 +101,5 @@ bad_id = RideShare::Rider.find_rider("apple") bad_id.must_equal 0 end - end - end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 436901931..e6fcc26e7 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -1,7 +1,6 @@ require_relative 'spec_helper' require 'pry' - describe "Trip" do let(:my_trip) {RideShare::Trip.new(1, 1, 54, 2016-04-05, 3)} let(:all_trips) {RideShare::Trip.find_all} @@ -27,7 +26,6 @@ it "returns an a specific driver instance" do my_trip.driver.must_be_instance_of RideShare::Driver end - end describe "Testing #rider method" do @@ -71,7 +69,9 @@ it "Returns 0 if driver ID not found" do bad_id = RideShare::Trip.find_all_driver("bad driver ID") - bad_id.must_equal 0 + bad_id.must_equal 0 + other_bad_id = RideShare::Trip.find_all_rider(100000) + other_bad_id.must_equal 0 end end @@ -88,7 +88,9 @@ it "Returns 0 if ID not found" do bad_id = RideShare::Trip.find_all_rider("bad rider ID") - bad_id.must_equal 0 + bad_id.must_equal 0 + other_bad_id = RideShare::Trip.find_all_rider(100000) + other_bad_id.must_equal 0 end end end