From 0448660fba5f4e49855477f60777616daa9297f5 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Mon, 6 Mar 2017 16:26:27 -0800 Subject: [PATCH 01/61] created files in lib and specs, populated Rakefile and spec_helper --- Rakefile | 9 +++++++++ lib/driver.rb | 0 lib/rider.rb | 0 lib/trip.rb | 0 specs/driver_spec.rb | 0 specs/rider_spec.rb | 0 specs/spec_helper.rb | 8 ++++++-- specs/trip_spec.rb | 0 8 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 lib/driver.rb create mode 100644 lib/rider.rb create mode 100644 lib/trip.rb create mode 100644 specs/driver_spec.rb create mode 100644 specs/rider_spec.rb create mode 100644 specs/trip_spec.rb diff --git a/Rakefile b/Rakefile index c556a763c..f5b131443 100644 --- a/Rakefile +++ b/Rakefile @@ -1 +1,10 @@ # 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/driver.rb b/lib/driver.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/rider.rb b/lib/rider.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/trip.rb b/lib/trip.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb new file mode 100644 index 000000000..e69de29bb diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 4d1e3fdc8..5a43c775d 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,8 +1,12 @@ +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 '../lib/driver' +require_relative '../lib/rider' +require_relative '../lib/trip' diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb new file mode 100644 index 000000000..e69de29bb From 085b25afc73c99b182a8fc81108c15042d29e59b Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Mon, 6 Mar 2017 16:34:22 -0800 Subject: [PATCH 02/61] added bare testing structure --- specs/driver_spec.rb | 9 +++++++++ specs/trip_spec.rb | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index e69de29bb..82040a8a2 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -0,0 +1,9 @@ +require_relative 'spec_helper' +require_relative '../lib/driver' + +describe "Driver" do + + describe "#initialize" do + end + +end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index e69de29bb..ee528ad31 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -0,0 +1,9 @@ +require_relative 'spec_helper' +require_relative '../lib/trip' + +describe "Trip" do + + describe "#initialize" do + end + +end From 575067b2e41d4fa72b26a77cf4cc9d7ac7d87181 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Mon, 6 Mar 2017 16:57:50 -0800 Subject: [PATCH 03/61] pseudocode for Driver class --- lib/driver.rb | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/lib/driver.rb b/lib/driver.rb index e69de29bb..27d6b9084 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -0,0 +1,33 @@ +require 'csv' + +class Driver + attr_reader :id + + def initialize(id, name, vin) + @id = id + @name = name + @vin = vin + end + +def self.all + # read SCV + # returns a list of driver instances +end + +def self.find(driver_id) + # searches .all for all driver matching parameter + # returns an instance of a driver +end + +def lists_trips + # passes driver ID to find_trips_per_driver + # returns a list of trip instances only this driver has taken +end + +def avg_rating + # accesses rating form each trip instance + # calcualtes average + # returns an average rating for that driver based on all trips taken +end + +end From 57572f4277c1cfe6cd0d7d360abd664aa1af07f8 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Mon, 6 Mar 2017 16:59:39 -0800 Subject: [PATCH 04/61] created Rider class and initialize method --- lib/rider.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index e69de29bb..2ff59641a 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -0,0 +1,11 @@ +require 'csv' + +class Rider + attr_reader :id + + def initialize(id, name, phone_num) + @id = id + @name = name + @phone_num = phone_num + end +end From 3a1941f92388081e57a93d1f31a6e3cabf784dd4 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Mon, 6 Mar 2017 17:00:10 -0800 Subject: [PATCH 05/61] created Trip class and initialize method --- lib/trip.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index e69de29bb..653167068 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -0,0 +1,13 @@ +require 'csv' + +class Trip + attr_reader :id, :driver_id, :rider_id + + def initialize(id, rider_id, driver_id, date, rating) + @id = id + @rider_id = rider_id + @driver_id = driver_id + @date = date + @rating = rating + end +end From 6d4f6b56b195ed70e7559b4777909e5121bdbb8c Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 10:32:21 -0800 Subject: [PATCH 06/61] added pseudocode and methods for rider.rb and trip.rb --- lib/rider.rb | 21 +++++++++++++++++++++ lib/trip.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index 2ff59641a..37196475d 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -8,4 +8,25 @@ def initialize(id, name, phone_num) @name = name @phone_num = phone_num end + + def self.all + # reads CSV + # returns a list of all driver instances + end + + def self.find(rider_id) + # searches .all for rider matching the parameter + # returns a driver instance + end + + def lists_trips + # passes rider_id to find_trips_per_rider + # returns a list of trip instances only this rider has taken + end + + def lists_drivers + # accesses list of trip instances from lists_trips + # returns lists of all previous driver instances assoicated with this rider + end + end diff --git a/lib/trip.rb b/lib/trip.rb index 653167068..ba9a34d07 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -10,4 +10,30 @@ def initialize(id, rider_id, driver_id, date, rating) @date = date @rating = rating end + + def self.all + # reads CSV + # returns a list of all trip instances + end + + def self.find_trips_per_driver(driver_id) + # searches .all for trips matching the driver_id + # returns a list of trip instances associated with one driver + end + + def self.find_trips_per_rider + # searches .all for trips matching the rider_id + # returns a list of trip instances associated with one rider + end + + def driver + # passes driver_id to find + # returns a driver instance + end + + def rider + # passes rider_id to find + # returns a rider instance + end + end From 5f65d7dc157a92a522ec71b6d47bb0961a01a5b1 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 12:46:09 -0800 Subject: [PATCH 07/61] added testing for initialize method for Driver class, Rider class and Trip class --- specs/driver_spec.rb | 25 ++++++++++++++++++++++++- specs/rider_spec.rb | 33 +++++++++++++++++++++++++++++++++ specs/trip_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 82040a8a2..16d507874 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -4,6 +4,29 @@ describe "Driver" do describe "#initialize" do - end + it "Takes an ID, name, and vin" do + name = "Bob Belcher" + id = 12345 + vin = 98765 + driver = Driver.new(id, name, vin) + + driver.must_respond_to :id + driver.id.must_equal id + + driver.must_respond_to :name + driver.name.must_equal name + driver.must_respond_to :vin + driver.vin.must_equal vin + end + + it "Is a kind of Driver" do + name = "Bob Belcher" + id = 12345 + vin = 98765 + driver = Driver.new(id, name, vin) + + driver.must_be_kind_of Driver + end + end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index e69de29bb..03db11310 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -0,0 +1,33 @@ +require 'pry' +require_relative 'spec_helper' +require_relative '../lib/rider' + +describe "Rider" do + + describe "#initialize" do + it "Takes an ID, name, and phone number" do + name = "Louise Belcher" + id = 12345 + phone_num = "(206) 222 222" + rider = Rider.new(id, name, phone_num) + + rider.must_respond_to :id + rider.id.must_equal id + + rider.must_respond_to :name + rider.name.must_equal name + + rider.must_respond_to :phone_num + rider.phone_num.must_equal phone_num + end + + it "Is a kind of Driver" do + name = "Louise Belcher" + id = 12345 + phone_num = "(206) 222 222" + rider = Rider.new(id, name, phone_num) + + rider.must_be_kind_of Rider + end + end +end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index ee528ad31..6b3f84667 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -4,6 +4,40 @@ describe "Trip" do describe "#initialize" do + it "Takes an ID, driver ID, raider ID, date, and rating" do + id = 12345 + driver_id = 45678 + rider_id = 9876 + date = "2016-04-05" + rating = 4 + trip = Trip.new(id, driver_id, rider_id, date, rating) + + trip.must_respond_to :id + trip.id.must_equal id + + trip.must_respond_to :driver_id + trip.driver_id.must_equal driver_id + + trip.must_respond_to :rider_id + trip.rider_id.must_equal rider_id + + trip.must_respond_to :date + trip.date.must_equal date + + trip.must_respond_to :rating + trip.rating.must_equal rating + end + + it "Is a kind of Driver" do + id = 12345 + driver_id = 45678 + rider_id = 9876 + date = "2016-04-05" + rating = 4 + trip = Trip.new(id, driver_id, rider_id, date, rating) + + trip.must_be_kind_of Trip + end end end From d54917f5ff4db7c0a61d47c38d585373e43478d8 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 12:47:24 -0800 Subject: [PATCH 08/61] improved initialize methods in Classes Rider, Trip, and Driver and added attr_readers --- lib/driver.rb | 2 +- lib/rider.rb | 6 +++--- lib/trip.rb | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 27d6b9084..7eaa203cc 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,7 +1,7 @@ require 'csv' class Driver - attr_reader :id + attr_reader :id, :name, :vin def initialize(id, name, vin) @id = id diff --git a/lib/rider.rb b/lib/rider.rb index 37196475d..debe5742f 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,7 +1,7 @@ require 'csv' class Rider - attr_reader :id + attr_reader :id, :name, :phone_num def initialize(id, name, phone_num) @id = id @@ -19,12 +19,12 @@ def self.find(rider_id) # returns a driver instance end - def lists_trips + def list_trips # passes rider_id to find_trips_per_rider # returns a list of trip instances only this rider has taken end - def lists_drivers + def list_drivers # accesses list of trip instances from lists_trips # returns lists of all previous driver instances assoicated with this rider end diff --git a/lib/trip.rb b/lib/trip.rb index ba9a34d07..c10ceb7c0 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,12 +1,12 @@ require 'csv' class Trip - attr_reader :id, :driver_id, :rider_id + attr_reader :id, :driver_id, :rider_id, :date, :rating - def initialize(id, rider_id, driver_id, date, rating) + def initialize(id, driver_id, rider_id, date, rating) @id = id - @rider_id = rider_id @driver_id = driver_id + @rider_id = rider_id @date = date @rating = rating end From 628f2fd7e9950de129a81998f69f1e96989596f1 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 13:17:50 -0800 Subject: [PATCH 09/61] added test for Driver.all method --- specs/driver_spec.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 16d507874..78d17c5f0 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -29,4 +29,34 @@ driver.must_be_kind_of Driver end end + + describe "Driver#all" do + + before do + @drivers = Driver.all + end + + it "Returns an array of all drivers" do + @drivers.class.must_equal Array + @drivers.each { |driver| driver.must_be_instance_of Driver } + @drivers.length.must_equal 100 + + @drivers.first.id.must_equal 1 + @drivers[0].name.must_equal "Bernardo Prosacco" + @drivers.first.vin.must_equal "WBWSS52P9NEYLVDE9" + + @drivers.last.id.must_equal 100 + @drivers[-1].name.must_equal "Minnie Dach" + @drivers.last.vin.must_equal "XF9Z0ST7X18WD41HT" + + index = 0 + CSV.read("support/drivers.csv") do |line| + + @drivers[index].id.must_equal line[0].to_i + @drivers[index].name.must_equal line[1].to_s + @drivers[index].vin.must_equal line[2] + index += 1 + end + end + end end From 6476bd4fc405b9801b9026aa1173fbc2bf15faba Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 13:18:16 -0800 Subject: [PATCH 10/61] added code for Driver.all method --- lib/driver.rb | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 7eaa203cc..e0406d51b 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -9,25 +9,34 @@ def initialize(id, name, vin) @vin = vin end -def self.all - # read SCV - # returns a list of driver instances -end + def self.all -def self.find(driver_id) - # searches .all for all driver matching parameter - # returns an instance of a driver -end + drivers = CSV.read("support/drivers.csv", { :headers => true }) + drivers_array = [] -def lists_trips - # passes driver ID to find_trips_per_driver - # returns a list of trip instances only this driver has taken -end + drivers.each do |line| + drivers_array << Driver.new(line[0].to_i, line[1], line[2]) + end -def avg_rating - # accesses rating form each trip instance - # calcualtes average - # returns an average rating for that driver based on all trips taken -end + return drivers_array + # read SCV + # returns a list of driver instances + end + + def self.find(driver_id) + # searches .all for all driver matching parameter + # returns an instance of a driver + end + + def lists_trips + # passes driver ID to find_trips_per_driver + # returns a list of trip instances only this driver has taken + end + + def avg_rating + # accesses rating form each trip instance + # calcualtes average + # returns an average rating for that driver based on all trips taken + end end From db999e924a653665e331399ea5ce1454367a679a Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 13:38:17 -0800 Subject: [PATCH 11/61] added testing for Driver.find method --- specs/driver_spec.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 78d17c5f0..3424ca357 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -58,5 +58,35 @@ index += 1 end end + + end + + describe "Driver#find" do + + before do + @drivers = Driver.all + end + + it "Returns a driver that exists" do + driver = Driver.find(54) + driver.must_be_instance_of Driver + end + + it "Can find the first driver from the CSV" do + driver = Driver.find(1) + driver.name.must_equal @drivers.first.name + end + + it "Can find the last driver from the CSV" do + driver = Driver.find(100) + driver.name.must_equal @drivers.last.name + end + + it "Raises an error for a driver that doesn't exist" do + proc { + Driver.find(101) + }.must_raise ArgumentError + end + end end From 313f01a9f24c55d35be96c99db90b4cfe4c867bb Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 13:39:00 -0800 Subject: [PATCH 12/61] added code to the Driver.find method --- lib/driver.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index e0406d51b..7eccdb3db 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -10,7 +10,8 @@ def initialize(id, name, vin) end def self.all - + # read SCV + # returns a list of driver instances drivers = CSV.read("support/drivers.csv", { :headers => true }) drivers_array = [] @@ -19,13 +20,18 @@ def self.all end return drivers_array - # read SCV - # returns a list of driver instances end def self.find(driver_id) # searches .all for all driver matching parameter # returns an instance of a driver + Driver.all.each do |driver| + if driver.id == driver_id + return driver + end + end + # create a special NoDriver error, maybe rescue it? + raise ArgumentError.new("driver #{driver_id} does not exist") end def lists_trips From 76c6e87ad03e40336bb96ba17c94918bcf57c4e0 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 14:01:05 -0800 Subject: [PATCH 13/61] added tests for Rider.all --- specs/rider_spec.rb | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 03db11310..1fedc3972 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -10,7 +10,7 @@ id = 12345 phone_num = "(206) 222 222" rider = Rider.new(id, name, phone_num) - + rider.must_respond_to :id rider.id.must_equal id @@ -30,4 +30,36 @@ rider.must_be_kind_of Rider end end + + describe "Rider#all" do + + before do + @riders = Rider.all + end + + it "Returns an array of all riders" do + @riders.class.must_equal Array + @riders.each { |rider| rider.must_be_instance_of Rider } + @riders.length.must_equal 300 + + @riders.first.id.must_equal 1 + @riders[0].name.must_equal "Nina Hintz Sr." + @riders.first.phone_num.must_equal "560.815.3059" + + @riders.last.id.must_equal 300 + @riders[-1].name.must_equal "Miss Isom Gleason" + @riders.last.phone_num.must_equal "791-114-8423 x70188" + + index = 0 + CSV.read("support/riders.csv") do |line| + + @riders[index].id.must_equal line[0].to_i + @riders[index].name.must_equal line[1] + @riders[index].phone_num.must_equal line[2] + index += 1 + end + end + + end + end From 700c545918e97210977d39c3433641cef45d0acd Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 14:01:30 -0800 Subject: [PATCH 14/61] Added code for Rider.all method --- lib/rider.rb | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/rider.rb b/lib/rider.rb index debe5742f..cd1ff4130 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -11,7 +11,15 @@ def initialize(id, name, phone_num) def self.all # reads CSV - # returns a list of all driver instances + # returns a list of all rider instances + riders = CSV.read("support/riders.csv", { :headers => true }) + riders_array = [] + + riders.each do |line| + riders_array << Rider.new(line[0].to_i, line[1], line[2]) + end + + return riders_array end def self.find(rider_id) From 3b15d9112ef8e9494c0f91a2e3eaae94073f4d23 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 14:05:17 -0800 Subject: [PATCH 15/61] added tests for Driver.find method --- specs/rider_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 1fedc3972..02d4d29bd 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -59,7 +59,34 @@ index += 1 end end + end + + describe "Driver#find" do + before do + @riders = Rider.all + end + + it "Returns a rider that exists" do + rider = Rider.find(150) + rider.must_be_instance_of Rider + end + + it "Can find the first rider from the CSV" do + rider = Rider.find(1) + rider.name.must_equal @riders.first.name + end + + it "Can find the last rider from the CSV" do + rider = Rider.find(300) + rider.name.must_equal @riders.last.name + end + + it "Raises an error for a rider that doesn't exist" do + proc { + Rider.find(301) + }.must_raise ArgumentError + end end end From 599692b086538cdd62ebb3833b55db69a3506baa Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 14:06:32 -0800 Subject: [PATCH 16/61] added code for Rider.find method --- lib/rider.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index cd1ff4130..c40df9790 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -25,6 +25,13 @@ def self.all def self.find(rider_id) # searches .all for rider matching the parameter # returns a driver instance + Rider.all.each do |rider| + if rider.id == rider_id + return rider + end + end + # create a special NoDriver error, maybe rescue it? + raise ArgumentError.new("rider #{rider_id} does not exist") end def list_trips From c211ca7db27280bbd14c81115d137e2f2b4e3d55 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 14:27:21 -0800 Subject: [PATCH 17/61] added tests for Trip.all method --- specs/trip_spec.rb | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 6b3f84667..d3bb28dd4 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -28,7 +28,7 @@ trip.rating.must_equal rating end - it "Is a kind of Driver" do + it "Is a kind of Trip" do id = 12345 driver_id = 45678 rider_id = 9876 @@ -40,4 +40,41 @@ end end + describe "Trip#all" do + + before do + @trips = Trip.all + end + + it "Returns an array of all trips" do + @trips.class.must_equal Array + @trips.each { |trip| trip.must_be_instance_of Trip } + @trips.length.must_equal 600 + + @trips.first.id.must_equal 1 + @trips[0].driver_id.must_equal 1 + @trips.first.rider_id.must_equal 54 + @trips.first.date.must_equal "2016-04-05" + @trips.first.rating.must_equal 3 + + @trips.last.id.must_equal 600 + @trips[-1].driver_id.must_equal 61 + @trips.last.rider_id.must_equal 168 + @trips.last.date.must_equal "2016-04-25" + @trips.last.rating.must_equal 3 + + index = 0 + CSV.read("support/trips.csv") do |line| + + @trips[index].id.must_equal line[0].to_i + @trips[index].driver_id.must_equal line[1].to_i + @trips[index].rider_id.must_equal line[2].to_i + @trips[index].date.must_equal line[3] + @trips[index].rating.must_equal line[4].to_i + index += 1 + end + end + end + + end From dac4ff46f7a9f79602bf1b9f05a177e735dcf823 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 14:27:47 -0800 Subject: [PATCH 18/61] added code for Test.all method --- lib/trip.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index c10ceb7c0..21a5c8e93 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -14,6 +14,14 @@ def initialize(id, driver_id, rider_id, date, rating) def self.all # reads CSV # returns a list of all trip instances + trips = CSV.read("support/trips.csv", { :headers => true }) + trips_array = [] + + trips.each do |line| + trips_array << Trip.new(line[0].to_i, line[1].to_i, line[2].to_i, line[3], line[4].to_i) + end + + return trips_array end def self.find_trips_per_driver(driver_id) From 1f8388c31f6fc052030802be85fc643196697269 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:17:19 -0800 Subject: [PATCH 19/61] added code for Trip.finds_trips_driver --- lib/trip.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 21a5c8e93..057b8458f 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -24,12 +24,20 @@ def self.all return trips_array end - def self.find_trips_per_driver(driver_id) + def self.find_trips_driver(driver_id) # searches .all for trips matching the driver_id # returns a list of trip instances associated with one driver + trips = [] + Trip.all.each do |trip| + if trip.driver_id == driver_id + trips << trip + end + end + + return trips end - def self.find_trips_per_rider + def self.find_trips_rider(rider_id) # searches .all for trips matching the rider_id # returns a list of trip instances associated with one rider end From 6625cf1693db798a5140cd1f5df07aad2c16eeab Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:17:59 -0800 Subject: [PATCH 20/61] added testing for Trip.find_trips_driver --- specs/trip_spec.rb | 48 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index d3bb28dd4..a8828fe3d 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -1,5 +1,6 @@ + require_relative 'spec_helper' -require_relative '../lib/trip' + describe "Trip" do @@ -63,18 +64,43 @@ @trips.last.date.must_equal "2016-04-25" @trips.last.rating.must_equal 3 - index = 0 - CSV.read("support/trips.csv") do |line| - - @trips[index].id.must_equal line[0].to_i - @trips[index].driver_id.must_equal line[1].to_i - @trips[index].rider_id.must_equal line[2].to_i - @trips[index].date.must_equal line[3] - @trips[index].rating.must_equal line[4].to_i - index += 1 - end + # not working, CSV file is not being read + # index = 0 + # CSV.read("support/trips.csv", { :headers => true }) do |line| + # @trips[index].id.must_equal line[0].to_i + # puts @trips + # @trips[index].driver_id.must_equal line[1].to_i + # @trips[index].rider_id.must_equal line[2].to_i + # @trips[index].date.must_equal line[3] + # @trips[index].rating.must_equal line[4].to_i + # index += 1 + # end end end + describe "trip#find_trips_driver" do + + before do + @driver_trips = Trip.find_trips_driver(1) + end + + it "Returns a list of trips" do + @driver_trips.must_be_kind_of Array + @driver_trips.each { |trip| trip.must_be_instance_of Trip } + + # not working, CSV file is not being read + # trips = 0 + # CSV.read("support/trips.csv", { :headers => true }) do |line| + # puts line + # trips += 1 if line[1].to_i == 1 + # end + + @driver_trips.length.must_equal 9 + end + + it "Returns and empty array for a driver ID that doesn't exist" do + Trip.find_trips_driver(101).must_equal [] + end + end end From 1b45f1ceb41396ba9a9b4acbcafa3ffe55e0533e Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:18:22 -0800 Subject: [PATCH 21/61] addred require csv --- specs/spec_helper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 5a43c775d..2c29b1f64 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -7,6 +7,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require 'csv' require_relative '../lib/driver' require_relative '../lib/rider' require_relative '../lib/trip' From 45365a8cfaa52f32631a0b19926b717f73cead0a Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:24:38 -0800 Subject: [PATCH 22/61] added tests for Trip.find_trips_rider method --- specs/trip_spec.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index a8828fe3d..b23326cad 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -103,4 +103,22 @@ end end + describe "trip#find_trips_rider" do + + before do + @rider_trips = Rider.find_trips_rider(1) + end + + it "Returns a list of trips" do + @rider_trips.must_be_kind_of Array + @rider_trips.each { |trip| trip.must_be_instance_of Trip } + + @rider_trips.length.must_equal 2 + end + + it "Returns and empty array for a rider ID that doesn't exist" do + Trip.find_trips_rider(301).must_equal [] + end + end + end From f5b894d757f65ac8ad67edb7cd968ea474dbf90d Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:27:24 -0800 Subject: [PATCH 23/61] fixed class instance in before do block in trip#find_trips_rider --- specs/trip_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index b23326cad..bce64832b 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -106,7 +106,7 @@ describe "trip#find_trips_rider" do before do - @rider_trips = Rider.find_trips_rider(1) + @rider_trips = Trip.find_trips_rider(1) end it "Returns a list of trips" do From 1b8a73795ac904dc2f93ccacc3119712693eb2b6 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:28:07 -0800 Subject: [PATCH 24/61] added code to Trip.find_trips_rider --- lib/trip.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index 057b8458f..ad34ac1c6 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -40,6 +40,14 @@ def self.find_trips_driver(driver_id) def self.find_trips_rider(rider_id) # searches .all for trips matching the rider_id # returns a list of trip instances associated with one rider + trips = [] + Trip.all.each do |trip| + if trip.rider_id == rider_id + trips << trip + end + end + + return trips end def driver From a5bee22cba3a7e4fd7096dd45aaf51e680402840 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Tue, 7 Mar 2017 16:39:26 -0800 Subject: [PATCH 25/61] created .gitignore --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..2aacaa234 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/coverage/ +/specs/coverage/ From fddd513416e0dc180c1d19dcf07dad03d9cbdd08 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 10:51:47 -0800 Subject: [PATCH 26/61] refactored class methods using enumerables --- lib/driver.rb | 25 +++++++++++++++---------- lib/rider.rb | 6 +----- specs/driver_spec.rb | 19 ++++++++++--------- specs/rider_spec.rb | 18 +++++++++--------- specs/trip_spec.rb | 1 - 5 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 7eccdb3db..151535fdd 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -13,25 +13,30 @@ def self.all # read SCV # returns a list of driver instances drivers = CSV.read("support/drivers.csv", { :headers => true }) - drivers_array = [] + # drivers_array = [] - drivers.each do |line| - drivers_array << Driver.new(line[0].to_i, line[1], line[2]) - end + # drivers.each do |line| + # drivers_array << Driver.new(line[0].to_i, line[1], line[2]) + # end - return drivers_array + drivers.map { |line| Driver.new(line[0].to_i, line[1], line[2]) } + + #return drivers_array end def self.find(driver_id) # searches .all for all driver matching parameter # returns an instance of a driver - Driver.all.each do |driver| - if driver.id == driver_id - return driver - end - end + # Driver.all.each do |driver| + # if driver.id == driver_id + # return driver + # end + # end + found_driver = Driver.all.find { |driver| driver.id == driver_id } + return found_driver if found_driver # create a special NoDriver error, maybe rescue it? raise ArgumentError.new("driver #{driver_id} does not exist") + end def lists_trips diff --git a/lib/rider.rb b/lib/rider.rb index c40df9790..74787972d 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -13,13 +13,9 @@ def self.all # reads CSV # returns a list of all rider instances riders = CSV.read("support/riders.csv", { :headers => true }) - riders_array = [] - riders.each do |line| - riders_array << Rider.new(line[0].to_i, line[1], line[2]) - end + riders.map { |line| Rider.new(line[0].to_i, line[1], line[2]) } - return riders_array end def self.find(rider_id) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 3424ca357..6bbf553b3 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -1,5 +1,4 @@ require_relative 'spec_helper' -require_relative '../lib/driver' describe "Driver" do @@ -49,14 +48,16 @@ @drivers[-1].name.must_equal "Minnie Dach" @drivers.last.vin.must_equal "XF9Z0ST7X18WD41HT" - index = 0 - CSV.read("support/drivers.csv") do |line| - - @drivers[index].id.must_equal line[0].to_i - @drivers[index].name.must_equal line[1].to_s - @drivers[index].vin.must_equal line[2] - index += 1 - end + # not working, not reading the CSV + # index = 0 + # CSV.read("support/drivers.csv") do |line| + # + # @drivers[index].id.must_equal line[0].to_i + # puts line + # @drivers[index].name.must_equal line[1].to_s + # @drivers[index].vin.must_equal line[2] + # index += 1 + # end end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 02d4d29bd..c84ddf2ed 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -1,6 +1,5 @@ require 'pry' require_relative 'spec_helper' -require_relative '../lib/rider' describe "Rider" do @@ -50,14 +49,15 @@ @riders[-1].name.must_equal "Miss Isom Gleason" @riders.last.phone_num.must_equal "791-114-8423 x70188" - index = 0 - CSV.read("support/riders.csv") do |line| - - @riders[index].id.must_equal line[0].to_i - @riders[index].name.must_equal line[1] - @riders[index].phone_num.must_equal line[2] - index += 1 - end + # not working, not reading the CSV file + # index = 0 + # CSV.read("support/riders.csv") do |line| + # + # @riders[index].id.must_equal line[0].to_i + # @riders[index].name.must_equal line[1] + # @riders[index].phone_num.must_equal line[2] + # index += 1 + # end end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index bce64832b..059e11af2 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -1,4 +1,3 @@ - require_relative 'spec_helper' From 8f3c7d8b705587bbed51ab4e313a315cc5cd8739 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 10:52:08 -0800 Subject: [PATCH 27/61] refactored class methods using enumerables --- lib/trip.rb | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index ad34ac1c6..fa0e0b72d 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -3,6 +3,8 @@ class Trip attr_reader :id, :driver_id, :rider_id, :date, :rating + @@all_trips = nil + def initialize(id, driver_id, rider_id, date, rating) @id = id @driver_id = driver_id @@ -14,40 +16,42 @@ def initialize(id, driver_id, rider_id, date, rating) def self.all # reads CSV # returns a list of all trip instances + return @@all_trips if @@all_trips trips = CSV.read("support/trips.csv", { :headers => true }) - trips_array = [] - trips.each do |line| - trips_array << Trip.new(line[0].to_i, line[1].to_i, line[2].to_i, line[3], line[4].to_i) + @@all_trips = trips.map do |line| + Trip.new(line[0].to_i, line[1].to_i, line[2].to_i, line[3], line[4].to_i) end - return trips_array + return @@all_trips end def self.find_trips_driver(driver_id) # searches .all for trips matching the driver_id # returns a list of trip instances associated with one driver - trips = [] - Trip.all.each do |trip| - if trip.driver_id == driver_id - trips << trip - end - end + # trips = [] + # Trip.all.each do |trip| + # if trip.driver_id == driver_id + # trips << trip + # end + # end + + Trip.all.select { |trip| trip.driver_id == driver_id } - return trips + # return trips end def self.find_trips_rider(rider_id) # searches .all for trips matching the rider_id # returns a list of trip instances associated with one rider - trips = [] - Trip.all.each do |trip| - if trip.rider_id == rider_id - trips << trip - end - end - - return trips + # trips = [] + # Trip.all.each do |trip| + # if trip.rider_id == rider_id + # trips << trip + # end + # end + Trip.all.select { |trip| trip.rider_id == rider_id } + # return trips end def driver From 8c5df575d45bf8ab108a9cc34b3a45134823f91f Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 10:53:11 -0800 Subject: [PATCH 28/61] fixed tests that are reading from a csv file --- specs/driver_spec.rb | 18 ++++++++---------- specs/rider_spec.rb | 17 ++++++++--------- specs/trip_spec.rb | 8 +++----- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 6bbf553b3..7d06470aa 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -48,16 +48,14 @@ @drivers[-1].name.must_equal "Minnie Dach" @drivers.last.vin.must_equal "XF9Z0ST7X18WD41HT" - # not working, not reading the CSV - # index = 0 - # CSV.read("support/drivers.csv") do |line| - # - # @drivers[index].id.must_equal line[0].to_i - # puts line - # @drivers[index].name.must_equal line[1].to_s - # @drivers[index].vin.must_equal line[2] - # index += 1 - # end + index = 0 + CSV.read("support/drivers.csv", { :headers => true }).each do |line| + + @drivers[index].id.must_equal line[0].to_i + @drivers[index].name.must_equal line[1].to_s + @drivers[index].vin.must_equal line[2] + index += 1 + end end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index c84ddf2ed..c1537024f 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -49,15 +49,14 @@ @riders[-1].name.must_equal "Miss Isom Gleason" @riders.last.phone_num.must_equal "791-114-8423 x70188" - # not working, not reading the CSV file - # index = 0 - # CSV.read("support/riders.csv") do |line| - # - # @riders[index].id.must_equal line[0].to_i - # @riders[index].name.must_equal line[1] - # @riders[index].phone_num.must_equal line[2] - # index += 1 - # end + index = 0 + CSV.read("support/riders.csv", { :headers => true }).each do |line| + + @riders[index].id.must_equal line[0].to_i + @riders[index].name.must_equal line[1] + @riders[index].phone_num.must_equal line[2] + index += 1 + end end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 059e11af2..badc9a680 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -87,17 +87,15 @@ @driver_trips.must_be_kind_of Array @driver_trips.each { |trip| trip.must_be_instance_of Trip } - # not working, CSV file is not being read # trips = 0 - # CSV.read("support/trips.csv", { :headers => true }) do |line| - # puts line + # CSV.read("support/trips.csv", { :headers => true }).each do |line| # trips += 1 if line[1].to_i == 1 # end @driver_trips.length.must_equal 9 end - it "Returns and empty array for a driver ID that doesn't exist" do + it "Returns an empty array for a driver ID that doesn't exist" do Trip.find_trips_driver(101).must_equal [] end end @@ -115,7 +113,7 @@ @rider_trips.length.must_equal 2 end - it "Returns and empty array for a rider ID that doesn't exist" do + it "Returns an empty array for a rider ID that doesn't exist" do Trip.find_trips_rider(301).must_equal [] end end From 66286eaadaf8fb3dbdd4a8d7ee2c8e45ce8ece10 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 10:55:41 -0800 Subject: [PATCH 29/61] improved Rider.find method using enumerables --- lib/rider.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/rider.rb b/lib/rider.rb index 74787972d..773682d11 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -21,11 +21,8 @@ def self.all def self.find(rider_id) # searches .all for rider matching the parameter # returns a driver instance - Rider.all.each do |rider| - if rider.id == rider_id - return rider - end - end + found_rider = Rider.all.find { |rider| rider.id == rider_id } + return found_rider if found_rider # create a special NoDriver error, maybe rescue it? raise ArgumentError.new("rider #{rider_id} does not exist") end From 1aced0a534a5a8170272548b722666b0bc1b2c06 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 11:30:17 -0800 Subject: [PATCH 30/61] added testing for driver method in class Trip --- specs/trip_spec.rb | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index badc9a680..fdc3e9d72 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -40,7 +40,7 @@ end end - describe "Trip#all" do + describe "Trip.all" do before do @trips = Trip.all @@ -77,7 +77,7 @@ end end - describe "trip#find_trips_driver" do + describe "Trip.find_trips_driver" do before do @driver_trips = Trip.find_trips_driver(1) @@ -100,7 +100,7 @@ end end - describe "trip#find_trips_rider" do + describe "Trip.find_trips_rider" do before do @rider_trips = Trip.find_trips_rider(1) @@ -118,4 +118,25 @@ end end + describe "trip#driver" do + it "Returns a driver that exists" do + trip = Trip.new(100, 29, 138, "2016-09-04", 2) + trip.driver.must_be_kind_of Driver + end + + it "Returns the correct driver for a specific trip" do + trip = Trip.new(100, 29, 138, "2016-09-04", 2) + name = "Miss Gustave Erdman" + trip.driver.name.must_equal name + end + + # trip exists, but driver does not - improve!!! + it "Raises an error if driver doesn't exist" do + trip = Trip.new(83, 0, 103, "2015-12-25", 2) + proc { + trip.driver + }.must_raise ArgumentError + end + end + end From 8771577c8469c146ed9b8c94d6e955f3be59923e Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 11:31:27 -0800 Subject: [PATCH 31/61] changed method descriptions in describe do block --- specs/driver_spec.rb | 4 ++-- specs/rider_spec.rb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 7d06470aa..9b6d4dd09 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -29,7 +29,7 @@ end end - describe "Driver#all" do + describe "Driver.all" do before do @drivers = Driver.all @@ -60,7 +60,7 @@ end - describe "Driver#find" do + describe "Driver.find" do before do @drivers = Driver.all diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index c1537024f..b43d8ab3d 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -20,7 +20,7 @@ rider.phone_num.must_equal phone_num end - it "Is a kind of Driver" do + it "Is a kind of Rider" do name = "Louise Belcher" id = 12345 phone_num = "(206) 222 222" @@ -30,7 +30,7 @@ end end - describe "Rider#all" do + describe "Rider.all" do before do @riders = Rider.all @@ -60,7 +60,7 @@ end end - describe "Driver#find" do + describe "Rider.find" do before do @riders = Rider.all From cfcae1664b257b5dec6f843073d3116f86891739 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 11:32:06 -0800 Subject: [PATCH 32/61] added code for driver method in class Trip --- lib/trip.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index fa0e0b72d..220039067 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -55,11 +55,16 @@ def self.find_trips_rider(rider_id) end def driver + # looks for a specific driver of a specific trip # passes driver_id to find # returns a driver instance + Driver.find(@driver_id) + # if I look driver and cannot driver (driver 0) + # it should give me a driver back and I should do something end def rider + # looks for a specific rider of a specific trip # passes rider_id to find # returns a rider instance end From 2af117eb27fdaceb12e44e4c121289724cd030c0 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 11:33:02 -0800 Subject: [PATCH 33/61] added tests for rider method in class Trip --- specs/trip_spec.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index fdc3e9d72..27dc816f3 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -139,4 +139,25 @@ end end + describe "trip#rider" do + it "Returns a rider that exists" do + trip = Trip.new(100, 29, 138, "2016-09-04", 2) + trip.rider.must_be_kind_of Rider + end + + it "Returns the correct rider for a specific trip" do + trip = Trip.new(100, 29, 138, "2016-09-04", 2) + name = "Miss Frida Abshire" + trip.rider.name.must_equal name + end + + # trip exists, but rider does not - improve!!! + it "Raises an error if rider doesn't exist" do + trip = Trip.new(267, 14, 0, "2015-04-23", 4) + proc { + trip.rider + }.must_raise ArgumentError + end + end + end From 644fed025e81acab2ee00dbe9236ffd8243381cc Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:23:22 -0800 Subject: [PATCH 34/61] added tests to methods driver and rider in Trip class --- specs/trip_spec.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 27dc816f3..d09de9ae2 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -113,6 +113,10 @@ @rider_trips.length.must_equal 2 end + it "Returns an empty array if there are no trips for this rider ID" do + Trip.find_trips_rider(100).must_equal [] + end + it "Returns an empty array for a rider ID that doesn't exist" do Trip.find_trips_rider(301).must_equal [] end @@ -130,6 +134,10 @@ trip.driver.name.must_equal name end + it "Returns an empty array if there are no trips for this driver ID" do + Trip.find_trips_driver(100).must_equal [] + end + # trip exists, but driver does not - improve!!! it "Raises an error if driver doesn't exist" do trip = Trip.new(83, 0, 103, "2015-12-25", 2) From bbe37c143b6db0c0b6f2e581ac4a9715d111e1e4 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:37:50 -0800 Subject: [PATCH 35/61] added tests for list-trips method in class Rider --- specs/rider_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index b43d8ab3d..33fcca735 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -88,4 +88,21 @@ end end + describe "Rider#list_trips" do + it "Returns a list of trips for a specific rider" do + rider = Rider.new(210, "Rhea Zieme", "940-838-2968 x4910") + rider.list_trips.must_be_kind_of Array + end + + it "Returns a correct number of trips for a specific rider" do + rider = Rider.new(1, "Nina Hintz Sr.", "560.815.3059") + rider.list_trips.length.must_equal 9 + end + + it "Returns an empty array if there are no trips for that rider" do + rider = Rider.new(100, "Hipolito Rogahn", "944.179.4883") + rider.list_trips.must_equal [] + end + end + end From 5f9d0ebdd845e8b78603e07e33bd6b6d70bc0b12 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:40:50 -0800 Subject: [PATCH 36/61] fixed test for list_trips --- specs/rider_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 33fcca735..d232d9812 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -96,7 +96,7 @@ it "Returns a correct number of trips for a specific rider" do rider = Rider.new(1, "Nina Hintz Sr.", "560.815.3059") - rider.list_trips.length.must_equal 9 + rider.list_trips.length.must_equal 2 end it "Returns an empty array if there are no trips for that rider" do From 21166e5dee382b93d357bd87d2b8562098629459 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:46:25 -0800 Subject: [PATCH 37/61] added tests for list_drivers method in Rider class --- specs/rider_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index d232d9812..e616e1337 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -105,4 +105,21 @@ end end + describe "Rider#list_drivers" do + it "Returns a list of drivers for a specific rider" do + rider_trips = Rider.new(93, "Mrs. Rickey Dickens", "5FS0Y47Z59YGGSXS0") + rider_trips.list_drivers.must_be_kind_of Array + end + + it "Returns a correct number of drivers for a specific driver" do + rider = Rider.new(1, "Bernardo Prosacco", "WBWSS52P9NEYLVDE9") + rider.list_drivers.length.must_equal 9 + end + + it "Returns an empty array if there are no drivers for that driver" do + rider = Rider.new(100, "Hipolito Rogahn", "944.179.4883") + rider.list_drivers.must_equal [] + end + end + end From 3ca74fb43a855e030d340bd98edb89d314af987a Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:47:04 -0800 Subject: [PATCH 38/61] Added code for list_drivers method --- lib/rider.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index 773682d11..b4dada2ce 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -28,13 +28,17 @@ def self.find(rider_id) end def list_trips + # looks for all trips associated with just one rider # passes rider_id to find_trips_per_rider # returns a list of trip instances only this rider has taken + Trip.find_trips_rider(@id) end def list_drivers + # looks for all drivers associated with just one rider # accesses list of trip instances from lists_trips # returns lists of all previous driver instances assoicated with this rider + Trip.find_trips_driver(@id) end end From fde8237e8f34b8b4b9d005795dd45a39dcb4cd45 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:59:23 -0800 Subject: [PATCH 39/61] added tests for list_trips method --- specs/driver_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 9b6d4dd09..b3d91172a 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -86,6 +86,23 @@ Driver.find(101) }.must_raise ArgumentError end + end + + describe "Driver#list_trips" do + it "Returns a list of trips for a specific driver" do + driver = Driver.new(36, "Mr. Marcelina Jenkins", "WD3VLLK2X04HF50PL") + driver.list_trips.must_be_kind_of Array + end + it "Returns a correct number of trips for a specific driver" do + driver = Driver.new(1, "Bernardo Prosacco", "WBWSS52P9NEYLVDE9") + driver.list_trips.length.must_equal 9 + end + + it "Returns an empty array if there are no trips for that driver" do + driver = Driver.new(100, "Minnie Dach", "XF9Z0ST7X18WD41HT") + driver.list_trips.must_equal [] + end end + end From 1af4f72481131a504d19353d5a84e35f48fc3421 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 13:59:49 -0800 Subject: [PATCH 40/61] added code to list_trips method --- lib/driver.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index 151535fdd..e20aca14f 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -39,9 +39,10 @@ def self.find(driver_id) end - def lists_trips + def list_trips # passes driver ID to find_trips_per_driver # returns a list of trip instances only this driver has taken + Trip.find_trips_driver(@id) end def avg_rating From d04bc978360e4db9f6a9aa60f62286e8b719cd55 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Wed, 8 Mar 2017 15:41:04 -0800 Subject: [PATCH 41/61] added test for list_drivers method --- specs/rider_spec.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index e616e1337..172c27d8c 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -111,12 +111,14 @@ rider_trips.list_drivers.must_be_kind_of Array end - it "Returns a correct number of drivers for a specific driver" do - rider = Rider.new(1, "Bernardo Prosacco", "WBWSS52P9NEYLVDE9") - rider.list_drivers.length.must_equal 9 + it "Returns a correct number of drivers for a specific rider" do + rider = Rider.new(41, "Ms. Westley Pouros", "133.000.1809 x9028") + rider.list_drivers.length.must_equal 2 + rider.list_drivers.length.wont_equal rider.list_trips.length end - it "Returns an empty array if there are no drivers for that driver" do + # Hipolito exists as a Rider, but didn't take any trips yet + it "Returns an empty array if there are no drivers for that rider" do rider = Rider.new(100, "Hipolito Rogahn", "944.179.4883") rider.list_drivers.must_equal [] end From f9fdf5c83b33e815793998a2adcacd6c98cab881 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Thu, 9 Mar 2017 12:29:20 -0800 Subject: [PATCH 42/61] fixed list_drivers method using enumerables --- lib/rider.rb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/rider.rb b/lib/rider.rb index b4dada2ce..c401e8b61 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,4 +1,5 @@ require 'csv' +require_relative 'trip' class Rider attr_reader :id, :name, :phone_num @@ -38,7 +39,17 @@ def list_drivers # looks for all drivers associated with just one rider # accesses list of trip instances from lists_trips # returns lists of all previous driver instances assoicated with this rider - Trip.find_trips_driver(@id) + drivers = list_trips.map { |trip| Driver.find(trip.driver_id) } + # drivers = [] + # list_trips.each do |trip| + # drivers << Driver.find(trip.driver_id) + # end + drivers.uniq do |driver| + driver.id + end end end + +variable = Rider.new(93, "Mrs. Rickey Dickens", "5FS0Y47Z59YGGSXS0") +puts variable.list_trips From 5408feff197af21a17b15abc15325087b0177926 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 20:10:19 -0800 Subject: [PATCH 43/61] Added code to avg_method --- lib/driver.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/driver.rb b/lib/driver.rb index e20aca14f..3d06d0c38 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,4 +1,5 @@ require 'csv' +require_relative 'trip' class Driver attr_reader :id, :name, :vin @@ -49,6 +50,10 @@ def avg_rating # accesses rating form each trip instance # calcualtes average # returns an average rating for that driver based on all trips taken + ratings = list_trips.map { |trip| trip.rating.to_f } + return 0 if ratings == [] + rating = (ratings.reduce(:+)) / ratings.length + rating.round(2) end end From 71cb078c2a68d39ef963d70fe2d8183124605def Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 20:44:24 -0800 Subject: [PATCH 44/61] added error handling for an invalid id in Driver.find method and returning nil for no results --- lib/driver.rb | 22 +++++----------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 3d06d0c38..a0a5c9832 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -14,30 +14,18 @@ def self.all # read SCV # returns a list of driver instances drivers = CSV.read("support/drivers.csv", { :headers => true }) - # drivers_array = [] - - # drivers.each do |line| - # drivers_array << Driver.new(line[0].to_i, line[1], line[2]) - # end drivers.map { |line| Driver.new(line[0].to_i, line[1], line[2]) } - - #return drivers_array end def self.find(driver_id) + unless (driver_id.is_a? Integer) && driver_id > 0 + raise ArgumentError.new("driver ID:#{driver_id} is not valid") + end # searches .all for all driver matching parameter # returns an instance of a driver - # Driver.all.each do |driver| - # if driver.id == driver_id - # return driver - # end - # end - found_driver = Driver.all.find { |driver| driver.id == driver_id } - return found_driver if found_driver - # create a special NoDriver error, maybe rescue it? - raise ArgumentError.new("driver #{driver_id} does not exist") - + Driver.all.find { |driver| driver.id == driver_id } + # return found_driver if found_driver end def list_trips From 953916846d87b3b7fe67e3062a035d2f2455cbd8 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 20:45:06 -0800 Subject: [PATCH 45/61] changed tests for Driver.find adding testing for errors and returning nil --- specs/driver_spec.rb | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index b3d91172a..445638aaf 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -81,9 +81,14 @@ driver.name.must_equal @drivers.last.name end - it "Raises an error for a driver that doesn't exist" do + it "Returns nil for a driver that doesn't exist" do + driver = Driver.find(101) + driver.must_equal nil + end + + it "Raises an error for an invalid driver ID" do proc { - Driver.find(101) + Driver.find("a") }.must_raise ArgumentError end end @@ -105,4 +110,21 @@ end end + describe "Driver#avg_rating" do + it "Returns a number" do + driver = Driver.new(60, "Oma Swift DDS", "TAMCBRPM7EN5GD88L") + driver.avg_rating.must_be_kind_of Float + end + + it "Returns correct average for a specific driver" do + driver = Driver.new(1, "Bernardo Prosacco", "WBWSS52P9NEYLVDE9") + driver.avg_rating.must_equal 2.33 + end + + it "Returns (something) if there are no trips for this driver" do + driver = Driver.new(100, "Minnie Dach", "XF9Z0ST7X18WD41HT") + driver.avg_rating.must_equal 0 + end + end + end From 503d102f8596e9615af16e0c6ceeee12e89d7ea5 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 20:48:31 -0800 Subject: [PATCH 46/61] added error handling and returning nil for Rider.find method --- lib/rider.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/rider.rb b/lib/rider.rb index c401e8b61..7f4d26f88 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,5 +1,6 @@ require 'csv' require_relative 'trip' +require_relative 'driver' class Rider attr_reader :id, :name, :phone_num @@ -20,12 +21,13 @@ def self.all end def self.find(rider_id) + unless (rider_id.is_a? Integer) && rider_id > 0 + raise ArgumentError.new("rider ID:#{rider_id} is not valid") + end # searches .all for rider matching the parameter # returns a driver instance - found_rider = Rider.all.find { |rider| rider.id == rider_id } - return found_rider if found_rider + Rider.all.find { |rider| rider.id == rider_id } # create a special NoDriver error, maybe rescue it? - raise ArgumentError.new("rider #{rider_id} does not exist") end def list_trips @@ -52,4 +54,4 @@ def list_drivers end variable = Rider.new(93, "Mrs. Rickey Dickens", "5FS0Y47Z59YGGSXS0") -puts variable.list_trips +puts variable.list_drivers From 7f970ae3432bf2ef202c5f2955533b9cbf365ba4 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 20:49:05 -0800 Subject: [PATCH 47/61] added tests for error hadling and returning nil for Rider.find method --- specs/rider_spec.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 172c27d8c..09cefe236 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -81,9 +81,14 @@ rider.name.must_equal @riders.last.name end - it "Raises an error for a rider that doesn't exist" do + it "Returns nil for a rider that doesn't exist" do + rider = Rider.find(301) + rider.must_equal nil + end + + it "Raises an error for an invalid rider ID" do proc { - Rider.find(301) + Rider.find("a") }.must_raise ArgumentError end end @@ -92,6 +97,7 @@ it "Returns a list of trips for a specific rider" do rider = Rider.new(210, "Rhea Zieme", "940-838-2968 x4910") rider.list_trips.must_be_kind_of Array + rider.list_trips.each { |trip| trip.must_be_kind_of Trip } end it "Returns a correct number of trips for a specific rider" do @@ -107,8 +113,10 @@ describe "Rider#list_drivers" do it "Returns a list of drivers for a specific rider" do - rider_trips = Rider.new(93, "Mrs. Rickey Dickens", "5FS0Y47Z59YGGSXS0") - rider_trips.list_drivers.must_be_kind_of Array + rider = Rider.new(93, "Mrs. Rickey Dickens", "5FS0Y47Z59YGGSXS0") + rider_drivers = rider.list_drivers + rider_drivers.must_be_kind_of Array + rider_drivers.each { |driver| driver.must_be_kind_of Driver } end it "Returns a correct number of drivers for a specific rider" do From 5e73105ee5011365cae332f273f5c46b65f41813 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 20:56:41 -0800 Subject: [PATCH 48/61] change tests for Trip.rider and Trip.driver methods to return nil if driver or rider doesn't exist --- specs/trip_spec.rb | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index d09de9ae2..bdce3bd17 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -138,12 +138,9 @@ Trip.find_trips_driver(100).must_equal [] end - # trip exists, but driver does not - improve!!! - it "Raises an error if driver doesn't exist" do + it "Returns nil if driver doesn't exist" do trip = Trip.new(83, 0, 103, "2015-12-25", 2) - proc { - trip.driver - }.must_raise ArgumentError + trip.driver.must_equal nil end end @@ -160,11 +157,9 @@ end # trip exists, but rider does not - improve!!! - it "Raises an error if rider doesn't exist" do + it "Returns nil if rider doesn't exist" do trip = Trip.new(267, 14, 0, "2015-04-23", 4) - proc { - trip.rider - }.must_raise ArgumentError + trip.rider.must_equal nil end end From 97f9ebe8679200fdd8ac2c78f6596e0810ebdc5a Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 21:14:39 -0800 Subject: [PATCH 49/61] added check for vin being 17 characters long --- lib/driver.rb | 4 ++-- specs/trip_spec.rb | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index a0a5c9832..242edc149 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -7,7 +7,7 @@ class Driver def initialize(id, name, vin) @id = id @name = name - @vin = vin + @vin = vin if vin.length == 17 end def self.all @@ -19,7 +19,7 @@ def self.all end def self.find(driver_id) - unless (driver_id.is_a? Integer) && driver_id > 0 + unless (driver_id.is_a? Integer) && driver_id >= 0 raise ArgumentError.new("driver ID:#{driver_id} is not valid") end # searches .all for all driver matching parameter diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index bdce3bd17..de5131001 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -156,7 +156,6 @@ trip.rider.name.must_equal name end - # trip exists, but rider does not - improve!!! it "Returns nil if rider doesn't exist" do trip = Trip.new(267, 14, 0, "2015-04-23", 4) trip.rider.must_equal nil From cb1466f38420c7a7fbaa15604f96bc2ca054d3df Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 21:24:08 -0800 Subject: [PATCH 50/61] added error raising for vin of wrong length --- lib/driver.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index 242edc149..fa8a76e5b 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -7,7 +7,12 @@ class Driver def initialize(id, name, vin) @id = id @name = name - @vin = vin if vin.length == 17 + + if vin.length == 17 + @vin = vin + else + raise ArgumentError.new("Vin must be 17 characters long.") + end end def self.all From bbd75091b08094ec43cc69b228eb616c1c0d840d Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 21:24:50 -0800 Subject: [PATCH 51/61] added testing of an error raining for wrong length of vin --- specs/driver_spec.rb | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 445638aaf..a3a1fe7f6 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -6,7 +6,7 @@ it "Takes an ID, name, and vin" do name = "Bob Belcher" id = 12345 - vin = 98765 + vin = "9e87e65r98302e984" driver = Driver.new(id, name, vin) driver.must_respond_to :id @@ -22,11 +22,17 @@ it "Is a kind of Driver" do name = "Bob Belcher" id = 12345 - vin = 98765 + vin = "9e87e65r98302e984" driver = Driver.new(id, name, vin) driver.must_be_kind_of Driver end + + it "Doesn't create a driver if vin is not 17 characters long" do + proc { + Driver.new(123, "Tina Belcher", "w3475rs78t") + }.must_raise ArgumentError + end end describe "Driver.all" do @@ -83,7 +89,7 @@ it "Returns nil for a driver that doesn't exist" do driver = Driver.find(101) - driver.must_equal nil + driver.must_be_nil end it "Raises an error for an invalid driver ID" do From ca1fc935dfc674ce695bbaab67e312a18780d793 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 22:01:20 -0800 Subject: [PATCH 52/61] added tests for valid rating --- specs/trip_spec.rb | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index de5131001..3945f1353 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -38,6 +38,30 @@ trip.must_be_kind_of Trip end + + it "Accepts rating" do + trip = Trip.new(12, 21, 382, "2016-01-04", 1) + trip.rating.must_equal 1 + end + + it "Accepts rating only as integers" do + proc { + Trip.new(12, 21, 382, "2016-01-04", 1.3) + }.must_raise ArgumentError + end + + it "Allows rating only within acceptable range (1 - 5)" do + proc { + Trip.new(12, 21, 382, "2016-01-04", 0) + }.must_raise ArgumentError + end + + it "Raises an error if invalid rating is given" do + proc { + Trip.new(12, 21, 382, "2016-01-04", "a") + }.must_raise ArgumentError + end + end describe "Trip.all" do @@ -63,7 +87,6 @@ @trips.last.date.must_equal "2016-04-25" @trips.last.rating.must_equal 3 - # not working, CSV file is not being read # index = 0 # CSV.read("support/trips.csv", { :headers => true }) do |line| # @trips[index].id.must_equal line[0].to_i @@ -140,7 +163,7 @@ it "Returns nil if driver doesn't exist" do trip = Trip.new(83, 0, 103, "2015-12-25", 2) - trip.driver.must_equal nil + trip.driver.must_be_nil end end @@ -158,7 +181,7 @@ it "Returns nil if rider doesn't exist" do trip = Trip.new(267, 14, 0, "2015-04-23", 4) - trip.rider.must_equal nil + trip.rider.must_be_nil end end From 877c17db341dfc75d2d375a0a3db1ef47e3584a3 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 22:02:01 -0800 Subject: [PATCH 53/61] added code that checks for valid rating --- lib/trip.rb | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 220039067..5c728585c 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -10,7 +10,12 @@ def initialize(id, driver_id, rider_id, date, rating) @driver_id = driver_id @rider_id = rider_id @date = date - @rating = rating + + unless (rating.is_a? Integer) && (1..5).include?(rating) + raise ArgumentError.new("Rating must be an integer within range 1-5") + else + @rating = rating + end end def self.all @@ -29,29 +34,13 @@ def self.all def self.find_trips_driver(driver_id) # searches .all for trips matching the driver_id # returns a list of trip instances associated with one driver - # trips = [] - # Trip.all.each do |trip| - # if trip.driver_id == driver_id - # trips << trip - # end - # end - Trip.all.select { |trip| trip.driver_id == driver_id } - - # return trips end def self.find_trips_rider(rider_id) # searches .all for trips matching the rider_id # returns a list of trip instances associated with one rider - # trips = [] - # Trip.all.each do |trip| - # if trip.rider_id == rider_id - # trips << trip - # end - # end Trip.all.select { |trip| trip.rider_id == rider_id } - # return trips end def driver @@ -59,7 +48,7 @@ def driver # passes driver_id to find # returns a driver instance Driver.find(@driver_id) - # if I look driver and cannot driver (driver 0) + # if I look driver and cannot find driver (driver 0) # it should give me a driver back and I should do something end @@ -67,6 +56,7 @@ def rider # looks for a specific rider of a specific trip # passes rider_id to find # returns a rider instance + Rider.find(@rider_id) end end From 7e3d4530eca56cc7ce9f9779a08985c989d2d5fd Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 22:41:46 -0800 Subject: [PATCH 54/61] added check for return trips for a driver, it returns instances of Trips --- lib/driver.rb | 8 ++++++-- lib/rider.rb | 21 ++++++++------------- specs/driver_spec.rb | 29 +++++++++++++++-------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index fa8a76e5b..f591e636a 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -4,6 +4,8 @@ class Driver attr_reader :id, :name, :vin + @@all_drivers = nil + def initialize(id, name, vin) @id = id @name = name @@ -16,11 +18,13 @@ def initialize(id, name, vin) end def self.all - # read SCV + # read CSV # returns a list of driver instances + return @@all_drivers if @@all_drivers drivers = CSV.read("support/drivers.csv", { :headers => true }) - drivers.map { |line| Driver.new(line[0].to_i, line[1], line[2]) } + @@all_drivers = drivers.map { |line| Driver.new(line[0].to_i, line[1], line[2]) } + return @@all_drivers end def self.find(driver_id) diff --git a/lib/rider.rb b/lib/rider.rb index 7f4d26f88..0aef2cf43 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -5,6 +5,8 @@ class Rider attr_reader :id, :name, :phone_num + @@all_riders = nil + def initialize(id, name, phone_num) @id = id @name = name @@ -14,20 +16,21 @@ def initialize(id, name, phone_num) def self.all # reads CSV # returns a list of all rider instances + return @@all_riders if @@all_riders riders = CSV.read("support/riders.csv", { :headers => true }) - riders.map { |line| Rider.new(line[0].to_i, line[1], line[2]) } + @@all_riders = riders.map { |line| Rider.new(line[0].to_i, line[1], line[2]) } + return @@all_riders end def self.find(rider_id) - unless (rider_id.is_a? Integer) && rider_id > 0 + unless (rider_id.is_a? Integer) && rider_id >= 0 raise ArgumentError.new("rider ID:#{rider_id} is not valid") end # searches .all for rider matching the parameter # returns a driver instance Rider.all.find { |rider| rider.id == rider_id } - # create a special NoDriver error, maybe rescue it? end def list_trips @@ -42,16 +45,8 @@ def list_drivers # accesses list of trip instances from lists_trips # returns lists of all previous driver instances assoicated with this rider drivers = list_trips.map { |trip| Driver.find(trip.driver_id) } - # drivers = [] - # list_trips.each do |trip| - # drivers << Driver.find(trip.driver_id) - # end - drivers.uniq do |driver| - driver.id - end + + drivers.uniq { |driver| driver.id } end end - -variable = Rider.new(93, "Mrs. Rickey Dickens", "5FS0Y47Z59YGGSXS0") -puts variable.list_drivers diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index a3a1fe7f6..ab13864cc 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -28,11 +28,11 @@ driver.must_be_kind_of Driver end - it "Doesn't create a driver if vin is not 17 characters long" do - proc { - Driver.new(123, "Tina Belcher", "w3475rs78t") - }.must_raise ArgumentError - end + it "Doesn't create a driver if vin is not 17 characters long" do + proc { + Driver.new(123, "Tina Belcher", "w3475rs78t") + }.must_raise ArgumentError + end end describe "Driver.all" do @@ -54,14 +54,14 @@ @drivers[-1].name.must_equal "Minnie Dach" @drivers.last.vin.must_equal "XF9Z0ST7X18WD41HT" - index = 0 - CSV.read("support/drivers.csv", { :headers => true }).each do |line| - - @drivers[index].id.must_equal line[0].to_i - @drivers[index].name.must_equal line[1].to_s - @drivers[index].vin.must_equal line[2] - index += 1 - end + # index = 0 + # CSV.read("support/drivers.csv", { :headers => true }).each do |line| + # + # @drivers[index].id.must_equal line[0].to_i + # @drivers[index].name.must_equal line[1].to_s + # @drivers[index].vin.must_equal line[2] + # index += 1 + # end end end @@ -103,6 +103,7 @@ it "Returns a list of trips for a specific driver" do driver = Driver.new(36, "Mr. Marcelina Jenkins", "WD3VLLK2X04HF50PL") driver.list_trips.must_be_kind_of Array + driver.list_trips.each { |trip| trip.must_be_kind_of Trip } end it "Returns a correct number of trips for a specific driver" do @@ -127,7 +128,7 @@ driver.avg_rating.must_equal 2.33 end - it "Returns (something) if there are no trips for this driver" do + it "Returns 0 if there are no trips for this driver" do driver = Driver.new(100, "Minnie Dach", "XF9Z0ST7X18WD41HT") driver.avg_rating.must_equal 0 end From 055f9e7ddc53cecb62ccba079e476be1de0add19 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 22:42:51 -0800 Subject: [PATCH 55/61] changed checking for returning nil values --- specs/rider_spec.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 09cefe236..afc8ddc16 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -49,14 +49,14 @@ @riders[-1].name.must_equal "Miss Isom Gleason" @riders.last.phone_num.must_equal "791-114-8423 x70188" - index = 0 - CSV.read("support/riders.csv", { :headers => true }).each do |line| - - @riders[index].id.must_equal line[0].to_i - @riders[index].name.must_equal line[1] - @riders[index].phone_num.must_equal line[2] - index += 1 - end + # index = 0 + # CSV.read("support/riders.csv", { :headers => true }).each do |line| + # + # @riders[index].id.must_equal line[0].to_i + # @riders[index].name.must_equal line[1] + # @riders[index].phone_num.must_equal line[2] + # index += 1 + # end end end @@ -83,7 +83,7 @@ it "Returns nil for a rider that doesn't exist" do rider = Rider.find(301) - rider.must_equal nil + rider.must_be_nil end it "Raises an error for an invalid rider ID" do From a72d6c2d05ba44dea1e1569a637ae58bfd694f7a Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 22:43:37 -0800 Subject: [PATCH 56/61] removed comments --- lib/driver.rb | 13 ++----------- lib/rider.rb | 11 +---------- lib/trip.rb | 16 +--------------- 3 files changed, 4 insertions(+), 36 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index f591e636a..b6960c24c 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -13,13 +13,11 @@ def initialize(id, name, vin) if vin.length == 17 @vin = vin else - raise ArgumentError.new("Vin must be 17 characters long.") + raise ArgumentError.new("vin must be 17 characters long") end end def self.all - # read CSV - # returns a list of driver instances return @@all_drivers if @@all_drivers drivers = CSV.read("support/drivers.csv", { :headers => true }) @@ -31,22 +29,15 @@ def self.find(driver_id) unless (driver_id.is_a? Integer) && driver_id >= 0 raise ArgumentError.new("driver ID:#{driver_id} is not valid") end - # searches .all for all driver matching parameter - # returns an instance of a driver + Driver.all.find { |driver| driver.id == driver_id } - # return found_driver if found_driver end def list_trips - # passes driver ID to find_trips_per_driver - # returns a list of trip instances only this driver has taken Trip.find_trips_driver(@id) end def avg_rating - # accesses rating form each trip instance - # calcualtes average - # returns an average rating for that driver based on all trips taken ratings = list_trips.map { |trip| trip.rating.to_f } return 0 if ratings == [] rating = (ratings.reduce(:+)) / ratings.length diff --git a/lib/rider.rb b/lib/rider.rb index 0aef2cf43..fb2e0994f 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -14,8 +14,6 @@ def initialize(id, name, phone_num) end def self.all - # reads CSV - # returns a list of all rider instances return @@all_riders if @@all_riders riders = CSV.read("support/riders.csv", { :headers => true }) @@ -28,22 +26,15 @@ def self.find(rider_id) unless (rider_id.is_a? Integer) && rider_id >= 0 raise ArgumentError.new("rider ID:#{rider_id} is not valid") end - # searches .all for rider matching the parameter - # returns a driver instance + Rider.all.find { |rider| rider.id == rider_id } end def list_trips - # looks for all trips associated with just one rider - # passes rider_id to find_trips_per_rider - # returns a list of trip instances only this rider has taken Trip.find_trips_rider(@id) end def list_drivers - # looks for all drivers associated with just one rider - # accesses list of trip instances from lists_trips - # returns lists of all previous driver instances assoicated with this rider drivers = list_trips.map { |trip| Driver.find(trip.driver_id) } drivers.uniq { |driver| driver.id } diff --git a/lib/trip.rb b/lib/trip.rb index 5c728585c..e43e753c2 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -12,15 +12,13 @@ def initialize(id, driver_id, rider_id, date, rating) @date = date unless (rating.is_a? Integer) && (1..5).include?(rating) - raise ArgumentError.new("Rating must be an integer within range 1-5") + raise ArgumentError.new("rating must be an integer within range 1-5") else @rating = rating end end def self.all - # reads CSV - # returns a list of all trip instances return @@all_trips if @@all_trips trips = CSV.read("support/trips.csv", { :headers => true }) @@ -32,30 +30,18 @@ def self.all end def self.find_trips_driver(driver_id) - # searches .all for trips matching the driver_id - # returns a list of trip instances associated with one driver Trip.all.select { |trip| trip.driver_id == driver_id } end def self.find_trips_rider(rider_id) - # searches .all for trips matching the rider_id - # returns a list of trip instances associated with one rider Trip.all.select { |trip| trip.rider_id == rider_id } end def driver - # looks for a specific driver of a specific trip - # passes driver_id to find - # returns a driver instance Driver.find(@driver_id) - # if I look driver and cannot find driver (driver 0) - # it should give me a driver back and I should do something end def rider - # looks for a specific rider of a specific trip - # passes rider_id to find - # returns a rider instance Rider.find(@rider_id) end From 598d5211734ef445c2b60c294dee2e35ade7c4ec Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 23:47:12 -0800 Subject: [PATCH 57/61] added files for specific error --- lib/InvalidIDError.rb | 2 ++ lib/InvalidRatingError.rb | 2 ++ lib/InvalidVinError.rb | 2 ++ 3 files changed, 6 insertions(+) create mode 100644 lib/InvalidIDError.rb create mode 100644 lib/InvalidRatingError.rb create mode 100644 lib/InvalidVinError.rb diff --git a/lib/InvalidIDError.rb b/lib/InvalidIDError.rb new file mode 100644 index 000000000..cbb960b9a --- /dev/null +++ b/lib/InvalidIDError.rb @@ -0,0 +1,2 @@ +class InvalidIDError < StandardError +end diff --git a/lib/InvalidRatingError.rb b/lib/InvalidRatingError.rb new file mode 100644 index 000000000..7094ff07d --- /dev/null +++ b/lib/InvalidRatingError.rb @@ -0,0 +1,2 @@ +class InvalidRatingError < StandardError +end diff --git a/lib/InvalidVinError.rb b/lib/InvalidVinError.rb new file mode 100644 index 000000000..36de16ee6 --- /dev/null +++ b/lib/InvalidVinError.rb @@ -0,0 +1,2 @@ +class InvalidVinError < StandardError +end From b4d32da725b029acb62da3e7bdc6abb0b675d5b7 Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sat, 11 Mar 2017 23:47:58 -0800 Subject: [PATCH 58/61] specified errors I am raising --- lib/driver.rb | 6 ++++-- lib/rider.rb | 3 ++- lib/trip.rb | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index b6960c24c..af578936b 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,5 +1,7 @@ require 'csv' require_relative 'trip' +require_relative 'InvalidVinError' +require_relative 'InvalidIDError' class Driver attr_reader :id, :name, :vin @@ -13,7 +15,7 @@ def initialize(id, name, vin) if vin.length == 17 @vin = vin else - raise ArgumentError.new("vin must be 17 characters long") + raise InvalidVinError.new("vin must be 17 characters long") end end @@ -27,7 +29,7 @@ def self.all def self.find(driver_id) unless (driver_id.is_a? Integer) && driver_id >= 0 - raise ArgumentError.new("driver ID:#{driver_id} is not valid") + raise InvalidIDError.new("driver ID:#{driver_id} is not valid") end Driver.all.find { |driver| driver.id == driver_id } diff --git a/lib/rider.rb b/lib/rider.rb index fb2e0994f..ea4a8c84d 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,6 +1,7 @@ require 'csv' require_relative 'trip' require_relative 'driver' +require_relative 'InvalidIDError' class Rider attr_reader :id, :name, :phone_num @@ -24,7 +25,7 @@ def self.all def self.find(rider_id) unless (rider_id.is_a? Integer) && rider_id >= 0 - raise ArgumentError.new("rider ID:#{rider_id} is not valid") + raise InvalidIDError.new("rider ID:#{rider_id} is not valid") end Rider.all.find { |rider| rider.id == rider_id } diff --git a/lib/trip.rb b/lib/trip.rb index e43e753c2..2137e2728 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,4 +1,5 @@ require 'csv' +require_relative 'InvalidRatingError' class Trip attr_reader :id, :driver_id, :rider_id, :date, :rating @@ -12,7 +13,7 @@ def initialize(id, driver_id, rider_id, date, rating) @date = date unless (rating.is_a? Integer) && (1..5).include?(rating) - raise ArgumentError.new("rating must be an integer within range 1-5") + raise InvalidRatingError.new("rating must be an integer within range 1-5") else @rating = rating end From 663d7c03c14e2803f5baa96c6d096b07b7cc594f Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sun, 12 Mar 2017 12:22:53 -0700 Subject: [PATCH 59/61] changed tests involving errors to include more specific errors I created --- specs/driver_spec.rb | 4 ++-- specs/rider_spec.rb | 2 +- specs/trip_spec.rb | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index ab13864cc..2b49d835d 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -31,7 +31,7 @@ it "Doesn't create a driver if vin is not 17 characters long" do proc { Driver.new(123, "Tina Belcher", "w3475rs78t") - }.must_raise ArgumentError + }.must_raise InvalidVinError end end @@ -95,7 +95,7 @@ it "Raises an error for an invalid driver ID" do proc { Driver.find("a") - }.must_raise ArgumentError + }.must_raise InvalidIDError end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index afc8ddc16..90a81206b 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -89,7 +89,7 @@ it "Raises an error for an invalid rider ID" do proc { Rider.find("a") - }.must_raise ArgumentError + }.must_raise InvalidIDError end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 3945f1353..9ec47ce1e 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -47,19 +47,19 @@ it "Accepts rating only as integers" do proc { Trip.new(12, 21, 382, "2016-01-04", 1.3) - }.must_raise ArgumentError + }.must_raise InvalidRatingError end it "Allows rating only within acceptable range (1 - 5)" do proc { Trip.new(12, 21, 382, "2016-01-04", 0) - }.must_raise ArgumentError + }.must_raise InvalidRatingError end it "Raises an error if invalid rating is given" do proc { Trip.new(12, 21, 382, "2016-01-04", "a") - }.must_raise ArgumentError + }.must_raise InvalidRatingError end end From 41bf9363ada0fac4b156605cb973cf9eb40ba71b Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sun, 12 Mar 2017 12:39:11 -0700 Subject: [PATCH 60/61] moved one test into correct location --- specs/trip_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 9ec47ce1e..8fb96dcc3 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -121,6 +121,10 @@ it "Returns an empty array for a driver ID that doesn't exist" do Trip.find_trips_driver(101).must_equal [] end + + it "Returns an empty array if there are no trips for this driver ID" do + Trip.find_trips_driver(100).must_equal [] + end end describe "Trip.find_trips_rider" do @@ -157,10 +161,6 @@ trip.driver.name.must_equal name end - it "Returns an empty array if there are no trips for this driver ID" do - Trip.find_trips_driver(100).must_equal [] - end - it "Returns nil if driver doesn't exist" do trip = Trip.new(83, 0, 103, "2015-12-25", 2) trip.driver.must_be_nil From 64a2502246454e02c7687ac94600e437d0d678ca Mon Sep 17 00:00:00 2001 From: Spatterjaaay Date: Sun, 12 Mar 2017 20:41:28 -0700 Subject: [PATCH 61/61] removed unnecessary return --- lib/driver.rb | 1 - lib/rider.rb | 2 -- lib/trip.rb | 2 -- 3 files changed, 5 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index af578936b..3732750d7 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -24,7 +24,6 @@ def self.all drivers = CSV.read("support/drivers.csv", { :headers => true }) @@all_drivers = drivers.map { |line| Driver.new(line[0].to_i, line[1], line[2]) } - return @@all_drivers end def self.find(driver_id) diff --git a/lib/rider.rb b/lib/rider.rb index ea4a8c84d..df167931f 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -19,8 +19,6 @@ def self.all riders = CSV.read("support/riders.csv", { :headers => true }) @@all_riders = riders.map { |line| Rider.new(line[0].to_i, line[1], line[2]) } - - return @@all_riders end def self.find(rider_id) diff --git a/lib/trip.rb b/lib/trip.rb index 2137e2728..8aad1ed92 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -26,8 +26,6 @@ def self.all @@all_trips = trips.map do |line| Trip.new(line[0].to_i, line[1].to_i, line[2].to_i, line[3], line[4].to_i) end - - return @@all_trips end def self.find_trips_driver(driver_id)