From 67480f36f8f77c872ee43c8c4f4d8825e1e91e1d Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 15:31:39 -0800 Subject: [PATCH 01/35] set up Rakefile and spec_helper --- Rakefile | 10 +++++++++- lib/.keep | 0 specs/.keep | 0 specs/spec_helper.rb | 4 +++- 4 files changed, 12 insertions(+), 2 deletions(-) delete mode 100644 lib/.keep delete mode 100644 specs/.keep 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/.keep b/lib/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/specs/.keep b/specs/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 4d1e3fdc8..573e2cfa2 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,7 +1,9 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From fc428348d4100af86b6385837dde07fbca39d307 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 15:37:01 -0800 Subject: [PATCH 02/35] set up initial file structure --- lib/driver.rb | 0 lib/rider.rb | 0 lib/trip.rb | 0 specs/driver_spec.rb | 1 + specs/rider_spec.rb | 1 + specs/spec_helper.rb | 3 +++ specs/trip_spec.rb | 1 + 7 files changed, 6 insertions(+) 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/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..ae9c220ea --- /dev/null +++ b/specs/driver_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/rider_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 573e2cfa2..b5950c52e 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -8,3 +8,6 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new # Require_relative your lib files here! +require_relative '../lib/driver.rb' +require_relative '../lib/rider.rb' +require_relative '..lib/trip.rb' diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb new file mode 100644 index 000000000..ae9c220ea --- /dev/null +++ b/specs/trip_spec.rb @@ -0,0 +1 @@ +require_relative 'spec_helper' From 5c4a9358d09eca87b392d9f0f4e72c9156791a9c Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 15:56:01 -0800 Subject: [PATCH 03/35] sets up and tests initialize --- lib/driver.rb | 12 ++++++++++++ specs/driver_spec.rb | 24 ++++++++++++++++++++++++ specs/spec_helper.rb | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/driver.rb b/lib/driver.rb index e69de29bb..bf4467253 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -0,0 +1,12 @@ +require 'csv' +require_relative 'trip' + +class Driver + attr_reader :id, :name, :vin + + def initialize(driver_hash) + @id = driver_hash[:id] + @name = driver_hash[:name] + @vin = driver_hash[:vin] + end +end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index ae9c220ea..9f812325b 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -1 +1,25 @@ require_relative 'spec_helper' + +describe "Driver tests" do + before do + @driver = Driver.new({ + id: 4, + name: "Ada", + vin: "1XKAD49X2DJ395724" + }) + end + + describe "Driver#initialize" do + it "Takes an ID, name, and VIN" do + @driver.must_respond_to :id + @driver.id.must_equal 4 + + @driver.must_respond_to :name + @driver.name.must_equal "Ada" + + @driver.must_respond_to :vin + @driver.vin.must_equal "1XKAD49X2DJ395724" + end + end + +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index b5950c52e..baeb2ee8d 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -10,4 +10,4 @@ # Require_relative your lib files here! require_relative '../lib/driver.rb' require_relative '../lib/rider.rb' -require_relative '..lib/trip.rb' +require_relative '../lib/trip.rb' From ef9dfccd3d3b1471238e16cf4fa80723daa678b3 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 16:23:14 -0800 Subject: [PATCH 04/35] Sets up and tests initialize for Rider --- lib/rider.rb | 12 ++++++++++++ specs/rider_spec.rb | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index e69de29bb..ee80504a5 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -0,0 +1,12 @@ +require 'csv' +require_relative 'trip' + +class Rider + attr_reader :id, :name, :phone + + def initialize(rider_hash) + @id = rider_hash[:id] + @name = rider_hash[:name] + @phone = rider_hash[:phone] + end +end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index ae9c220ea..3168f3a25 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -1 +1,25 @@ require_relative 'spec_helper' + +describe "Rider tests" do + before do + @rider = Rider.new({ + id: 8, + name: "Galois", + phone: "206-555-2468" + }) + end + + describe "Rider#initialize" do + it "Takes an ID, name, and phone number" do + @rider.must_respond_to :id + @rider.id.must_equal 8 + + @rider.must_respond_to :name + @rider.name.must_equal "Galois" + + @rider.must_respond_to :phone + @rider.phone.must_equal "206-555-2468" + end + end + +end From 1296d000cd0403d66357178bfff4f885cc8b2b21 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 16:38:53 -0800 Subject: [PATCH 05/35] Added .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..4ebc8aea5 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +coverage From 3a81cc6a23a6bbbecf042f121d6fffbb7c80882a Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 16:54:57 -0800 Subject: [PATCH 06/35] adds and tests initialize for Trip --- lib/trip.rb | 16 ++++++++++++++++ specs/trip_spec.rb | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index e69de29bb..f0c447ba8 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -0,0 +1,16 @@ +# require 'csv' +# require_relative 'driver' +# require_relative 'rider' + +class Trip + attr_reader :id, :driver_id, :rider_id, :date, :rating + + def initialize(trip_hash) + @id = trip_hash[:id] + @driver_id = trip_hash[:driver_id] + @rider_id = trip_hash[:rider_id] + @date = trip_hash[:date] + @rating = trip_hash[:rating] + end + +end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index ae9c220ea..3c369af7c 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -1 +1,33 @@ require_relative 'spec_helper' + +describe "Trip tests" do + before do + @trip = Trip.new({ + id: 2, + driver_id: 4, + rider_id: 8, + date: "2014-07-12", + rating: 5 + }) + end + + describe "Trip#initialize" do + it "Takes an ID, driver_id, rider_id, date, and rating" do + @trip.must_respond_to :id + @trip.id.must_equal 2 + + @trip.must_respond_to :driver_id + @trip.driver_id.must_equal 4 + + @trip.must_respond_to :rider_id + @trip.rider_id.must_equal 8 + + @trip.must_respond_to :date + @trip.date.must_equal "2014-07-12" + + @trip.must_respond_to :rating + @trip.rating.must_equal 5 + end + end + +end From 8206e3305ab6a2d583ec0a4ddafe6e9df49e3ce3 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Mon, 6 Mar 2017 16:55:29 -0800 Subject: [PATCH 07/35] tinkering with require stuff --- lib/driver.rb | 4 ++-- lib/rider.rb | 4 ++-- specs/spec_helper.rb | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index bf4467253..1074d3862 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,5 +1,5 @@ -require 'csv' -require_relative 'trip' +# require 'csv' +# require_relative 'trip' class Driver attr_reader :id, :name, :vin diff --git a/lib/rider.rb b/lib/rider.rb index ee80504a5..870d83a96 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,5 +1,5 @@ -require 'csv' -require_relative 'trip' +# require 'csv' +# require_relative 'trip' class Rider attr_reader :id, :name, :phone diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index baeb2ee8d..a837b2aa6 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -4,9 +4,10 @@ require 'minitest' require 'minitest/autorun' require 'minitest/reporters' - Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new +require 'csv' + # Require_relative your lib files here! require_relative '../lib/driver.rb' require_relative '../lib/rider.rb' From 1a4ecf942bedcd8fa211ee760606fe6665f1ce26 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 10:16:46 -0800 Subject: [PATCH 08/35] changed my befores to lets in tests and took out some comments in class files --- lib/driver.rb | 3 --- lib/rider.rb | 3 --- lib/trip.rb | 4 ---- specs/driver_spec.rb | 21 +++++++-------------- specs/rider_spec.rb | 20 +++++++------------- specs/trip_spec.rb | 31 +++++++++++-------------------- 6 files changed, 25 insertions(+), 57 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 1074d3862..bc417f8cb 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,6 +1,3 @@ -# require 'csv' -# require_relative 'trip' - class Driver attr_reader :id, :name, :vin diff --git a/lib/rider.rb b/lib/rider.rb index 870d83a96..6d1f2d77a 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,6 +1,3 @@ -# require 'csv' -# require_relative 'trip' - class Rider attr_reader :id, :name, :phone diff --git a/lib/trip.rb b/lib/trip.rb index f0c447ba8..44a5fb9be 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,7 +1,3 @@ -# require 'csv' -# require_relative 'driver' -# require_relative 'rider' - class Trip attr_reader :id, :driver_id, :rider_id, :date, :rating diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 9f812325b..d0aa53b82 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -1,25 +1,18 @@ require_relative 'spec_helper' describe "Driver tests" do - before do - @driver = Driver.new({ - id: 4, - name: "Ada", - vin: "1XKAD49X2DJ395724" - }) - end + let(:driver) { Driver.new({ id: 4, name: "Ada", vin: "1XKAD49X2DJ395724" }) } describe "Driver#initialize" do it "Takes an ID, name, and VIN" do - @driver.must_respond_to :id - @driver.id.must_equal 4 + driver.must_respond_to :id + driver.id.must_equal 4 - @driver.must_respond_to :name - @driver.name.must_equal "Ada" + driver.must_respond_to :name + driver.name.must_equal "Ada" - @driver.must_respond_to :vin - @driver.vin.must_equal "1XKAD49X2DJ395724" + driver.must_respond_to :vin + driver.vin.must_equal "1XKAD49X2DJ395724" end end - end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 3168f3a25..07b809b43 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -1,24 +1,18 @@ require_relative 'spec_helper' describe "Rider tests" do - before do - @rider = Rider.new({ - id: 8, - name: "Galois", - phone: "206-555-2468" - }) - end + let(:rider) { Rider.new({ id: 8, name: "Galois", phone: "206-555-2468" }) } describe "Rider#initialize" do it "Takes an ID, name, and phone number" do - @rider.must_respond_to :id - @rider.id.must_equal 8 + rider.must_respond_to :id + rider.id.must_equal 8 - @rider.must_respond_to :name - @rider.name.must_equal "Galois" + rider.must_respond_to :name + rider.name.must_equal "Galois" - @rider.must_respond_to :phone - @rider.phone.must_equal "206-555-2468" + rider.must_respond_to :phone + rider.phone.must_equal "206-555-2468" end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 3c369af7c..5dc265ebd 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -1,33 +1,24 @@ require_relative 'spec_helper' describe "Trip tests" do - before do - @trip = Trip.new({ - id: 2, - driver_id: 4, - rider_id: 8, - date: "2014-07-12", - rating: 5 - }) - end + let(:trip) { Trip.new({ id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 }) } describe "Trip#initialize" do it "Takes an ID, driver_id, rider_id, date, and rating" do - @trip.must_respond_to :id - @trip.id.must_equal 2 + trip.must_respond_to :id + trip.id.must_equal 2 - @trip.must_respond_to :driver_id - @trip.driver_id.must_equal 4 + trip.must_respond_to :driver_id + trip.driver_id.must_equal 4 - @trip.must_respond_to :rider_id - @trip.rider_id.must_equal 8 + trip.must_respond_to :rider_id + trip.rider_id.must_equal 8 - @trip.must_respond_to :date - @trip.date.must_equal "2014-07-12" + trip.must_respond_to :date + trip.date.must_equal "2014-07-12" - @trip.must_respond_to :rating - @trip.rating.must_equal 5 + trip.must_respond_to :rating + trip.rating.must_equal 5 end end - end From 1b336636fbba90f7b9437e379024307ab53e3798 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 10:39:09 -0800 Subject: [PATCH 09/35] raise argument errors for invalid information in initialize (and test that) --- lib/driver.rb | 4 ++++ specs/driver_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/driver.rb b/lib/driver.rb index bc417f8cb..2b4591e02 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -2,6 +2,10 @@ class Driver attr_reader :id, :name, :vin def initialize(driver_hash) + raise ArgumentError.new("Driver id must be an integer") if driver_hash[:id].class != Integer + raise ArgumentError.new("Name must be a non-empty string") if driver_hash[:name].class != String || driver_hash[:name] == "" + raise ArgumentError.new("VIN must be a non-empty string") if driver_hash[:vin].class != String || driver_hash[:vin] == "" + @id = driver_hash[:id] @name = driver_hash[:name] @vin = driver_hash[:vin] diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index d0aa53b82..8ad1891bf 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -14,5 +14,26 @@ driver.must_respond_to :vin driver.vin.must_equal "1XKAD49X2DJ395724" end + + it "only accepts integer IDs" do + driver_hash = { id: "id", name: "Ada", vin: "1XKAD49X2DJ395724" } + proc { Driver.new(driver_hash) }.must_raise ArgumentError + end + + it "only accepts non-empty strings for names" do + driver_hash1 = { id: 4, name: "", vin: "1XKAD49X2DJ395724" } + driver_hash2 = { id: 4, name: 45, vin: "1XKAD49X2DJ395724" } + + proc { Driver.new(driver_hash1) }.must_raise ArgumentError + proc { Driver.new(driver_hash2) }.must_raise ArgumentError + end + + it "only accepts non-empty strings for VINs" do + driver_hash1 = { id: 4, name: "Ada", vin: "" } + driver_hash2 = { id: 4, name: "Ada", vin: [] } + + proc { Driver.new(driver_hash1) }.must_raise ArgumentError + proc { Driver.new(driver_hash2) }.must_raise ArgumentError + end end end From fc163867a4323a9bcb3f614327de4f4968f64072 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 10:48:12 -0800 Subject: [PATCH 10/35] put everything in a RideShare module --- lib/driver.rb | 2 +- lib/ride_share.rb | 7 +++++++ lib/rider.rb | 2 +- lib/trip.rb | 2 +- specs/driver_spec.rb | 12 ++++++------ specs/rider_spec.rb | 2 +- specs/spec_helper.rb | 7 ++++--- specs/trip_spec.rb | 2 +- 8 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 lib/ride_share.rb diff --git a/lib/driver.rb b/lib/driver.rb index 2b4591e02..7d502fafa 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,4 +1,4 @@ -class Driver +class RideShare::Driver attr_reader :id, :name, :vin def initialize(driver_hash) diff --git a/lib/ride_share.rb b/lib/ride_share.rb new file mode 100644 index 000000000..75d9ce395 --- /dev/null +++ b/lib/ride_share.rb @@ -0,0 +1,7 @@ +require 'csv' + +module RideShare;end + +require_relative 'driver' +require_relative 'rider' +require_relative 'trip' diff --git a/lib/rider.rb b/lib/rider.rb index 6d1f2d77a..99c3ddf6b 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,4 +1,4 @@ -class Rider +class RideShare::Rider attr_reader :id, :name, :phone def initialize(rider_hash) diff --git a/lib/trip.rb b/lib/trip.rb index 44a5fb9be..aeed2d026 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,4 +1,4 @@ -class Trip +class RideShare::Trip attr_reader :id, :driver_id, :rider_id, :date, :rating def initialize(trip_hash) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 8ad1891bf..2d6203cf7 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -1,7 +1,7 @@ require_relative 'spec_helper' describe "Driver tests" do - let(:driver) { Driver.new({ id: 4, name: "Ada", vin: "1XKAD49X2DJ395724" }) } + let(:driver) { RideShare::Driver.new({ id: 4, name: "Ada", vin: "1XKAD49X2DJ395724" }) } describe "Driver#initialize" do it "Takes an ID, name, and VIN" do @@ -17,23 +17,23 @@ it "only accepts integer IDs" do driver_hash = { id: "id", name: "Ada", vin: "1XKAD49X2DJ395724" } - proc { Driver.new(driver_hash) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash) }.must_raise ArgumentError end it "only accepts non-empty strings for names" do driver_hash1 = { id: 4, name: "", vin: "1XKAD49X2DJ395724" } driver_hash2 = { id: 4, name: 45, vin: "1XKAD49X2DJ395724" } - proc { Driver.new(driver_hash1) }.must_raise ArgumentError - proc { Driver.new(driver_hash2) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError end it "only accepts non-empty strings for VINs" do driver_hash1 = { id: 4, name: "Ada", vin: "" } driver_hash2 = { id: 4, name: "Ada", vin: [] } - proc { Driver.new(driver_hash1) }.must_raise ArgumentError - proc { Driver.new(driver_hash2) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError end end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 07b809b43..c1a5dab46 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -1,7 +1,7 @@ require_relative 'spec_helper' describe "Rider tests" do - let(:rider) { Rider.new({ id: 8, name: "Galois", phone: "206-555-2468" }) } + let(:rider) { RideShare::Rider.new({ id: 8, name: "Galois", phone: "206-555-2468" }) } describe "Rider#initialize" do it "Takes an ID, name, and phone number" do diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index a837b2aa6..26976a350 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -9,6 +9,7 @@ require 'csv' # Require_relative your lib files here! -require_relative '../lib/driver.rb' -require_relative '../lib/rider.rb' -require_relative '../lib/trip.rb' +# require_relative '../lib/driver.rb' +# require_relative '../lib/rider.rb' +# require_relative '../lib/trip.rb' +require_relative '../lib/ride_share' diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 5dc265ebd..a31a1f43c 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -1,7 +1,7 @@ require_relative 'spec_helper' describe "Trip tests" do - let(:trip) { Trip.new({ id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 }) } + let(:trip) { RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 }) } describe "Trip#initialize" do it "Takes an ID, driver_id, rider_id, date, and rating" do From 2103dad06a280234750abcee3317e328028b0760 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 11:15:14 -0800 Subject: [PATCH 11/35] Adds and tests Driver.all method --- lib/driver.rb | 12 ++++++++++++ specs/driver_spec.rb | 28 ++++++++++++++++++++++++++++ specs/spec_helper.rb | 6 ------ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 7d502fafa..773569e0b 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -10,4 +10,16 @@ def initialize(driver_hash) @name = driver_hash[:name] @vin = driver_hash[:vin] end + + def self.all + drivers = [] + CSV.read("support/drivers.csv")[1..-1].each do |line| + drivers << RideShare::Driver.new( { + id: line[0].to_i, + name: line[1], + vin: line[2] + } ) + end + return drivers + end end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 2d6203cf7..bf2d49aaa 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -2,6 +2,8 @@ describe "Driver tests" do let(:driver) { RideShare::Driver.new({ id: 4, name: "Ada", vin: "1XKAD49X2DJ395724" }) } + let(:drivers_array) { RideShare::Driver.all } + let(:csv_info) { CSV.read('support/drivers.csv') } describe "Driver#initialize" do it "Takes an ID, name, and VIN" do @@ -36,4 +38,30 @@ proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError end end + + describe "Driver.all" do + it "Driver.all returns an array" do + drivers_array.must_be_instance_of Array + end + + it "Everything in the array is a Driver" do + drivers_array.each do |driver| + driver.must_be_instance_of RideShare::Driver + end + end + + it "The number of drivers is correct" do + drivers_array.length.must_equal csv_info.count - 1 + end + + it "The information for the first & last driver is correct" do + drivers_array[0].id.must_equal csv_info[1][0].to_i + drivers_array[0].name.must_equal csv_info[1][1] + drivers_array[0].vin.must_equal csv_info[1][2] + + drivers_array[-1].id.must_equal csv_info[-1][0].to_i + drivers_array[-1].name.must_equal csv_info[-1][1] + drivers_array[-1].vin.must_equal csv_info[-1][2] + end + end end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 26976a350..b902ca782 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -6,10 +6,4 @@ require 'minitest/reporters' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -require 'csv' - -# Require_relative your lib files here! -# require_relative '../lib/driver.rb' -# require_relative '../lib/rider.rb' -# require_relative '../lib/trip.rb' require_relative '../lib/ride_share' From 1c1304d16c9ee7532183f92a9ccbdd0422f7314b Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 11:21:26 -0800 Subject: [PATCH 12/35] Adds and tests Rider.all --- lib/rider.rb | 12 ++++++++++++ specs/rider_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index 99c3ddf6b..bd81e8013 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -6,4 +6,16 @@ def initialize(rider_hash) @name = rider_hash[:name] @phone = rider_hash[:phone] end + + def self.all + riders = [] + CSV.read("support/riders.csv")[1..-1].each do |line| + riders << RideShare::Rider.new( { + id: line[0].to_i, + name: line[1], + phone: line[2] + } ) + end + return riders + end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index c1a5dab46..f3986629a 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -2,6 +2,8 @@ describe "Rider tests" do let(:rider) { RideShare::Rider.new({ id: 8, name: "Galois", phone: "206-555-2468" }) } + let(:riders_array) { RideShare::Rider.all } + let(:csv_info) { CSV.read('support/riders.csv') } describe "Rider#initialize" do it "Takes an ID, name, and phone number" do @@ -16,4 +18,29 @@ end end + describe "Rider.all" do + it "Rider.all returns an array" do + riders_array.must_be_instance_of Array + end + + it "Everything in the array is a Rider" do + riders_array.each do |rider| + rider.must_be_instance_of RideShare::Rider + end + end + + it "The number of riders is correct" do + riders_array.length.must_equal csv_info.count - 1 + end + + it "The information for the first & last rider is correct" do + riders_array[0].id.must_equal csv_info[1][0].to_i + riders_array[0].name.must_equal csv_info[1][1] + riders_array[0].phone.must_equal csv_info[1][2] + + riders_array[-1].id.must_equal csv_info[-1][0].to_i + riders_array[-1].name.must_equal csv_info[-1][1] + riders_array[-1].phone.must_equal csv_info[-1][2] + end + end end From ab077a6f7c46136ea287ae95261cbf5dd2f4e2b3 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 11:25:42 -0800 Subject: [PATCH 13/35] Validates info when creating a new Rider (and tests that) --- lib/rider.rb | 4 ++++ specs/rider_spec.rb | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index bd81e8013..537f6cb40 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -2,6 +2,10 @@ class RideShare::Rider attr_reader :id, :name, :phone def initialize(rider_hash) + raise ArgumentError.new("Rider ID must be an integer") if rider_hash[:id].class != Integer + raise ArgumentError.new("Name must be a non-empty string") if rider_hash[:name].class != String || rider_hash[:name] == "" + raise ArgumentError.new("Phone must be a non-empty string") if rider_hash[:phone].class != String || rider_hash[:phone] == "" + @id = rider_hash[:id] @name = rider_hash[:name] @phone = rider_hash[:phone] diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index f3986629a..03fdfa2e1 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -16,6 +16,27 @@ rider.must_respond_to :phone rider.phone.must_equal "206-555-2468" end + + it "only accepts integer IDs" do + rider_hash = { id: "id", name: "Ada", phone: "2065552468" } + proc { RideShare::Rider.new(rider_hash) }.must_raise ArgumentError + end + + it "only accepts non-empty strings for names" do + rider_hash1 = { id: 4, name: "", phone: "2065552468" } + rider_hash2 = { id: 4, name: 45, phone: "2065552468" } + + proc { RideShare::Rider.new(rider_hash1) }.must_raise ArgumentError + proc { RideShare::Rider.new(rider_hash2) }.must_raise ArgumentError + end + + it "only accepts non-empty strings for VINs" do + rider_hash1 = { id: 4, name: "Ada", phone: "" } + rider_hash2 = { id: 4, name: "Ada", phone: [] } + + proc { RideShare::Rider.new(rider_hash1) }.must_raise ArgumentError + proc { RideShare::Rider.new(rider_hash2) }.must_raise ArgumentError + end end describe "Rider.all" do From f050ae8ef0c1c802b2795e7ab9367b6ef8443a46 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 14:23:41 -0800 Subject: [PATCH 14/35] Put validation methods in RideShare module and cleaned up some code --- lib/driver.rb | 10 ++----- lib/ride_share.rb | 18 +++++++++++- lib/rider.rb | 10 ++----- lib/trip.rb | 15 ++++++---- specs/driver_spec.rb | 34 +++++++++++++++------- specs/rider_spec.rb | 34 +++++++++++++++------- specs/trip_spec.rb | 69 ++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 148 insertions(+), 42 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index 773569e0b..c8f144fd2 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -2,13 +2,9 @@ class RideShare::Driver attr_reader :id, :name, :vin def initialize(driver_hash) - raise ArgumentError.new("Driver id must be an integer") if driver_hash[:id].class != Integer - raise ArgumentError.new("Name must be a non-empty string") if driver_hash[:name].class != String || driver_hash[:name] == "" - raise ArgumentError.new("VIN must be a non-empty string") if driver_hash[:vin].class != String || driver_hash[:vin] == "" - - @id = driver_hash[:id] - @name = driver_hash[:name] - @vin = driver_hash[:vin] + @id = RideShare::validate_int(driver_hash[:id], "Driver ID") + @name = RideShare::validate_string(driver_hash[:name], "Name") + @vin = RideShare::validate_string(driver_hash[:vin], "VIN") end def self.all diff --git a/lib/ride_share.rb b/lib/ride_share.rb index 75d9ce395..9d39107ac 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -1,6 +1,22 @@ require 'csv' -module RideShare;end +module RideShare + def self.validate_int(field, field_name) + if field.class != Integer || field <= 0 + raise ArgumentError.new("Required field #{field_name} must be a positive integer.") + end + + return field + end + + def self.validate_string(field, field_name) + if field.class != String || field == "" + raise ArgumentError.new("Required field #{field_name} must be a non-empty string.") + end + + return field + end +end require_relative 'driver' require_relative 'rider' diff --git a/lib/rider.rb b/lib/rider.rb index 537f6cb40..5b946f0bf 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -2,13 +2,9 @@ class RideShare::Rider attr_reader :id, :name, :phone def initialize(rider_hash) - raise ArgumentError.new("Rider ID must be an integer") if rider_hash[:id].class != Integer - raise ArgumentError.new("Name must be a non-empty string") if rider_hash[:name].class != String || rider_hash[:name] == "" - raise ArgumentError.new("Phone must be a non-empty string") if rider_hash[:phone].class != String || rider_hash[:phone] == "" - - @id = rider_hash[:id] - @name = rider_hash[:name] - @phone = rider_hash[:phone] + @id = RideShare::validate_int(rider_hash[:id], "Rider ID") + @name = RideShare::validate_string(rider_hash[:name], "Name") + @phone = RideShare::validate_string(rider_hash[:phone], "Phone Number") end def self.all diff --git a/lib/trip.rb b/lib/trip.rb index aeed2d026..45f098b0b 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -2,11 +2,16 @@ class RideShare::Trip attr_reader :id, :driver_id, :rider_id, :date, :rating def initialize(trip_hash) - @id = trip_hash[:id] - @driver_id = trip_hash[:driver_id] - @rider_id = trip_hash[:rider_id] - @date = trip_hash[:date] - @rating = trip_hash[:rating] + raise ArgumentError.new("Rating must be between 1-5") if !(trip_hash[:rating].between?(1,5)) + + @id = RideShare::validate_int(trip_hash[:id], "Trip ID") + @driver_id = RideShare::validate_int(trip_hash[:driver_id], "Driver ID") + @rider_id = RideShare::validate_int(trip_hash[:rider_id], "Rider ID") + @date = RideShare::validate_string(trip_hash[:date], "Date") + @rating = RideShare::validate_int(trip_hash[:rating], "Rating") end + def self.all + + end end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index bf2d49aaa..90d62b02e 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -17,25 +17,38 @@ driver.vin.must_equal "1XKAD49X2DJ395724" end - it "only accepts integer IDs" do - driver_hash = { id: "id", name: "Ada", vin: "1XKAD49X2DJ395724" } - proc { RideShare::Driver.new(driver_hash) }.must_raise ArgumentError + it "Only accepts positive integer IDs" do + driver_hash1 = { id: "id", name: "Ada", vin: "1XKAD49X2DJ395724" } + driver_hash2 = { id: 0, name: "Ada", vin: "1XKAD49X2DJ395724" } + driver_hash3 = { id: -5, name: "Ada", vin: "1XKAD49X2DJ395724" } + + proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash3) }.must_raise ArgumentError end - it "only accepts non-empty strings for names" do + it "Only accepts non-empty strings for name and VIN" do driver_hash1 = { id: 4, name: "", vin: "1XKAD49X2DJ395724" } driver_hash2 = { id: 4, name: 45, vin: "1XKAD49X2DJ395724" } proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError + + driver_hash3 = { id: 4, name: "Ada", vin: "" } + driver_hash4 = { id: 4, name: "Ada", vin: [] } + + proc { RideShare::Driver.new(driver_hash3) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash4) }.must_raise ArgumentError end - it "only accepts non-empty strings for VINs" do - driver_hash1 = { id: 4, name: "Ada", vin: "" } - driver_hash2 = { id: 4, name: "Ada", vin: [] } + it "All fields are required" do + driver_hash1 = { name: "Ada", vin: "1XKAD49X2DJ395724" } + driver_hash2 = { id: 4, vin: "1XKAD49X2DJ395724" } + driver_hash3 = { name: "Ada", id: 4} proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash3) }.must_raise ArgumentError end end @@ -44,10 +57,9 @@ drivers_array.must_be_instance_of Array end - it "Everything in the array is a Driver" do - drivers_array.each do |driver| - driver.must_be_instance_of RideShare::Driver - end + it "First and last element in array is a Driver" do + drivers_array[0].must_be_instance_of RideShare::Driver + drivers_array[-1].must_be_instance_of RideShare::Driver end it "The number of drivers is correct" do diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 03fdfa2e1..cecc90b25 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -17,25 +17,38 @@ rider.phone.must_equal "206-555-2468" end - it "only accepts integer IDs" do - rider_hash = { id: "id", name: "Ada", phone: "2065552468" } - proc { RideShare::Rider.new(rider_hash) }.must_raise ArgumentError + it "Only accepts positive integer IDs" do + rider_hash1 = { id: "id", name: "Ada", phone: "2065552468" } + rider_hash2 = { id: 0, name: "Ada", phone: "2065552468" } + rider_hash3 = { id: -4, name: "Ada", phone: "2065552468" } + + proc { RideShare::Rider.new(rider_hash1) }.must_raise ArgumentError + proc { RideShare::Rider.new(rider_hash2) }.must_raise ArgumentError + proc { RideShare::Rider.new(rider_hash3) }.must_raise ArgumentError end - it "only accepts non-empty strings for names" do + it "Only accepts non-empty strings for Name and Phone Number" do rider_hash1 = { id: 4, name: "", phone: "2065552468" } rider_hash2 = { id: 4, name: 45, phone: "2065552468" } proc { RideShare::Rider.new(rider_hash1) }.must_raise ArgumentError proc { RideShare::Rider.new(rider_hash2) }.must_raise ArgumentError + + rider_hash3 = { id: 4, name: "Ada", phone: "" } + rider_hash4 = { id: 4, name: "Ada", phone: [] } + + proc { RideShare::Rider.new(rider_hash3) }.must_raise ArgumentError + proc { RideShare::Rider.new(rider_hash4) }.must_raise ArgumentError end - it "only accepts non-empty strings for VINs" do - rider_hash1 = { id: 4, name: "Ada", phone: "" } - rider_hash2 = { id: 4, name: "Ada", phone: [] } + it "All fields are required" do + rider_hash1 = { name: "Galois", phone: "206-555-2468" } + rider_hash2 = { id: 8, phone: "206-555-2468" } + rider_hash3 = { id: 8, name: "Galois" } proc { RideShare::Rider.new(rider_hash1) }.must_raise ArgumentError proc { RideShare::Rider.new(rider_hash2) }.must_raise ArgumentError + proc { RideShare::Rider.new(rider_hash3) }.must_raise ArgumentError end end @@ -44,10 +57,9 @@ riders_array.must_be_instance_of Array end - it "Everything in the array is a Rider" do - riders_array.each do |rider| - rider.must_be_instance_of RideShare::Rider - end + it "The first and last element of the array is a Rider" do + riders_array[0].must_be_instance_of RideShare::Rider + riders_array[-1].must_be_instance_of RideShare::Rider end it "The number of riders is correct" do diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index a31a1f43c..4dddc9e96 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -20,5 +20,74 @@ trip.must_respond_to :rating trip.rating.must_equal 5 end + + it "Only accepts positive integer IDs for all ID fields" do + trip_hash1 = { id: "id", driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash2 = { id: 0, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash3 = { id: -2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } + + proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError + + trip_hash4 = { id: 2, driver_id: "four", rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash5 = { id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash6 = { id: 2, driver_id: -4, rider_id: 8, date: "2014-07-12", rating: 5 } + + proc { RideShare::Trip.new(trip_hash4) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash5) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash6) }.must_raise ArgumentError + + trip_hash7 = { id: 2, driver_id: 4, rider_id: [8], date: "2014-07-12", rating: 5 } + trip_hash8 = { id: 2, driver_id: 4, rider_id: 0, date: "2014-07-12", rating: 5 } + trip_hash9 = { id: 2, driver_id: 4, rider_id: -8, date: "2014-07-12", rating: 5 } + + proc { RideShare::Trip.new(trip_hash7) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash8) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash9) }.must_raise ArgumentError + end + + it "Only accepts non-empty strings for Date" do + trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: 87, rating: 5 } + trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "", rating: 5 } + + proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError + end + + it "Rating must be an integer 1-5" do + trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 0 } + trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: "rating!" } + trip_hash3 = { id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 6 } + + proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError + end end + + # describe "Trip.all" do + # it "Trip.all returns an array" do + # trips_array.must_be_instance_of Array + # end + # + # it "The first and last element of the array is a Trip" do + # trips_array[0].must_be_instance_of RideShare::Trip + # trips_array[-1].must_be_instance_of RideShare::Trip + # end + # + # it "The number of trips is correct" do + # trips_array.length.must_equal csv_info.count - 1 + # end + # + # it "The information for the first & last trip is correct" do + # trips_array[0].id.must_equal csv_info[1][0].to_i + # trips_array[0].name.must_equal csv_info[1][1] + # trips_array[0].phone.must_equal csv_info[1][2] + # + # trips_array[-1].id.must_equal csv_info[-1][0].to_i + # trips_array[-1].name.must_equal csv_info[-1][1] + # trips_array[-1].phone.must_equal csv_info[-1][2] + # end + # end end From 70eaa874dfca31c9d72acafd3a980c8fed04f454 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 16:00:15 -0800 Subject: [PATCH 15/35] Worked on Trip.all method and allowed IDs to be 0 --- lib/ride_share.rb | 6 ++-- lib/trip.rb | 14 ++++++-- specs/driver_spec.rb | 6 ++-- specs/rider_spec.rb | 6 ++-- specs/trip_spec.rb | 86 +++++++++++++++++++++++--------------------- 5 files changed, 64 insertions(+), 54 deletions(-) diff --git a/lib/ride_share.rb b/lib/ride_share.rb index 9d39107ac..bc1c292ca 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -2,8 +2,8 @@ module RideShare def self.validate_int(field, field_name) - if field.class != Integer || field <= 0 - raise ArgumentError.new("Required field #{field_name} must be a positive integer.") + if field.class != Integer || field < 0 + raise ArgumentError.new("Required field #{field_name} must be a non-negative integer.") end return field @@ -13,7 +13,7 @@ def self.validate_string(field, field_name) if field.class != String || field == "" raise ArgumentError.new("Required field #{field_name} must be a non-empty string.") end - + return field end end diff --git a/lib/trip.rb b/lib/trip.rb index 45f098b0b..98aa6d693 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -7,11 +7,21 @@ def initialize(trip_hash) @id = RideShare::validate_int(trip_hash[:id], "Trip ID") @driver_id = RideShare::validate_int(trip_hash[:driver_id], "Driver ID") @rider_id = RideShare::validate_int(trip_hash[:rider_id], "Rider ID") - @date = RideShare::validate_string(trip_hash[:date], "Date") + @date = Date.parse(RideShare::validate_string(trip_hash[:date], "Date")) @rating = RideShare::validate_int(trip_hash[:rating], "Rating") end def self.all - + trips = [] + CSV.read("support/trips.csv")[1..-1].each do |line| + trips << RideShare::Trip.new( { + id: line[0].to_i, + driver_id: line[1].to_i, + rider_id: line[2].to_i, + date: line[3], + rating: line[4].to_i + } ) + end + return trips end end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 90d62b02e..3561863f8 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -17,14 +17,12 @@ driver.vin.must_equal "1XKAD49X2DJ395724" end - it "Only accepts positive integer IDs" do + it "Only accepts non-negative integer IDs" do driver_hash1 = { id: "id", name: "Ada", vin: "1XKAD49X2DJ395724" } - driver_hash2 = { id: 0, name: "Ada", vin: "1XKAD49X2DJ395724" } - driver_hash3 = { id: -5, name: "Ada", vin: "1XKAD49X2DJ395724" } + driver_hash2 = { id: -5, name: "Ada", vin: "1XKAD49X2DJ395724" } proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError - proc { RideShare::Driver.new(driver_hash3) }.must_raise ArgumentError end it "Only accepts non-empty strings for name and VIN" do diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index cecc90b25..4f8662fb9 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -17,14 +17,12 @@ rider.phone.must_equal "206-555-2468" end - it "Only accepts positive integer IDs" do + it "Only accepts non-negative integer IDs" do rider_hash1 = { id: "id", name: "Ada", phone: "2065552468" } - rider_hash2 = { id: 0, name: "Ada", phone: "2065552468" } - rider_hash3 = { id: -4, name: "Ada", phone: "2065552468" } + rider_hash2 = { id: -4, name: "Ada", phone: "2065552468" } proc { RideShare::Rider.new(rider_hash1) }.must_raise ArgumentError proc { RideShare::Rider.new(rider_hash2) }.must_raise ArgumentError - proc { RideShare::Rider.new(rider_hash3) }.must_raise ArgumentError end it "Only accepts non-empty strings for Name and Phone Number" do diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 4dddc9e96..599ddd7a5 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -2,6 +2,8 @@ describe "Trip tests" do let(:trip) { RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 }) } + let(:trips_array) { RideShare::Trip.all } + let(:csv_info) { CSV.read('support/trips.csv') } describe "Trip#initialize" do it "Takes an ID, driver_id, rider_id, date, and rating" do @@ -15,39 +17,33 @@ trip.rider_id.must_equal 8 trip.must_respond_to :date - trip.date.must_equal "2014-07-12" + trip.date.must_equal Date.parse("2014-07-12") trip.must_respond_to :rating trip.rating.must_equal 5 end - it "Only accepts positive integer IDs for all ID fields" do + it "Only accepts non-negative integer IDs for all ID fields" do trip_hash1 = { id: "id", driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } - trip_hash2 = { id: 0, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } - trip_hash3 = { id: -2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash2 = { id: -2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 } proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError - proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError - trip_hash4 = { id: 2, driver_id: "four", rider_id: 8, date: "2014-07-12", rating: 5 } - trip_hash5 = { id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 } - trip_hash6 = { id: 2, driver_id: -4, rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash3 = { id: 2, driver_id: "four", rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash4 = { id: 2, driver_id: -4, rider_id: 8, date: "2014-07-12", rating: 5 } + proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError proc { RideShare::Trip.new(trip_hash4) }.must_raise ArgumentError - proc { RideShare::Trip.new(trip_hash5) }.must_raise ArgumentError - proc { RideShare::Trip.new(trip_hash6) }.must_raise ArgumentError - trip_hash7 = { id: 2, driver_id: 4, rider_id: [8], date: "2014-07-12", rating: 5 } - trip_hash8 = { id: 2, driver_id: 4, rider_id: 0, date: "2014-07-12", rating: 5 } - trip_hash9 = { id: 2, driver_id: 4, rider_id: -8, date: "2014-07-12", rating: 5 } + trip_hash5 = { id: 2, driver_id: 4, rider_id: [8], date: "2014-07-12", rating: 5 } + trip_hash6 = { id: 2, driver_id: 4, rider_id: -8, date: "2014-07-12", rating: 5 } - proc { RideShare::Trip.new(trip_hash7) }.must_raise ArgumentError - proc { RideShare::Trip.new(trip_hash8) }.must_raise ArgumentError - proc { RideShare::Trip.new(trip_hash9) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash5) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash6) }.must_raise ArgumentError end - it "Only accepts non-empty strings for Date" do + it "Only accepts strings for Date" do trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: 87, rating: 5 } trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "", rating: 5 } @@ -55,6 +51,10 @@ proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError end + it "Once initialized, date must be a Date" do + trip.date.must_be_instance_of Date + end + it "Rating must be an integer 1-5" do trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 0 } trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: "rating!" } @@ -66,28 +66,32 @@ end end - # describe "Trip.all" do - # it "Trip.all returns an array" do - # trips_array.must_be_instance_of Array - # end - # - # it "The first and last element of the array is a Trip" do - # trips_array[0].must_be_instance_of RideShare::Trip - # trips_array[-1].must_be_instance_of RideShare::Trip - # end - # - # it "The number of trips is correct" do - # trips_array.length.must_equal csv_info.count - 1 - # end - # - # it "The information for the first & last trip is correct" do - # trips_array[0].id.must_equal csv_info[1][0].to_i - # trips_array[0].name.must_equal csv_info[1][1] - # trips_array[0].phone.must_equal csv_info[1][2] - # - # trips_array[-1].id.must_equal csv_info[-1][0].to_i - # trips_array[-1].name.must_equal csv_info[-1][1] - # trips_array[-1].phone.must_equal csv_info[-1][2] - # end - # end + describe "Trip.all" do + it "Trip.all returns an array" do + trips_array.must_be_instance_of Array + end + + # it "The first and last element of the array is a Trip" do + # trips_array[0].must_be_instance_of RideShare::Trip + # trips_array[-1].must_be_instance_of RideShare::Trip + # end + # + # it "The number of trips is correct" do + # trips_array.length.must_equal csv_info.count - 1 + # end + # + # it "The information for the first & last trip is correct" do + # trips_array[0].id.must_equal csv_info[1][0].to_i + # trips_array[0].driver_id.must_equal csv_info[1][1].to_i + # trips_array[0].rider_id.must_equal csv_info[1][2].to_i + # trips_array[0].date.must_equal Date.parse(csv_info[1][3]) + # trips_array[0].rating.must_equal csv_info[1][4].to_i + # + # trips_array[-1].id.must_equal csv_info[-1][0].to_i + # trips_array[-1].driver_id.must_equal csv_info[-1][1].to_i + # trips_array[-1].rider_id.must_equal csv_info[-1][2].to_i + # trips_array[-1].date.must_equal Date.parse(csv_info[-1][3]) + # trips_array[-1].rating.must_equal csv_info[-1][4].to_i + # end + end end From 739f8914a2c4730e84475c7ca943ce65d3b5045a Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 16:15:53 -0800 Subject: [PATCH 16/35] Adds and tests Driver.find --- lib/driver.rb | 8 ++++++++ lib/no_driver_error.rb | 2 ++ lib/ride_share.rb | 1 + specs/driver_spec.rb | 31 ++++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 lib/no_driver_error.rb diff --git a/lib/driver.rb b/lib/driver.rb index c8f144fd2..894b18f02 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -18,4 +18,12 @@ def self.all end return drivers end + + def self.find(id) + drivers = RideShare::Driver.all + drivers.each do |driver| + return driver if id == driver.id + end + raise NoDriverError.new("Driver with that ID does not exist") + end end diff --git a/lib/no_driver_error.rb b/lib/no_driver_error.rb new file mode 100644 index 000000000..85d28f9cd --- /dev/null +++ b/lib/no_driver_error.rb @@ -0,0 +1,2 @@ +class NoDriverError < StandardError +end diff --git a/lib/ride_share.rb b/lib/ride_share.rb index bc1c292ca..8babdd187 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -18,6 +18,7 @@ def self.validate_string(field, field_name) end end +require_relative 'no_driver_error' require_relative 'driver' require_relative 'rider' require_relative 'trip' diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 3561863f8..5a2fc5447 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -3,7 +3,7 @@ describe "Driver tests" do let(:driver) { RideShare::Driver.new({ id: 4, name: "Ada", vin: "1XKAD49X2DJ395724" }) } let(:drivers_array) { RideShare::Driver.all } - let(:csv_info) { CSV.read('support/drivers.csv') } + let(:csv_info) { CSV.read('support/drivers.csv')[1..-1] } describe "Driver#initialize" do it "Takes an ID, name, and VIN" do @@ -61,17 +61,38 @@ end it "The number of drivers is correct" do - drivers_array.length.must_equal csv_info.count - 1 + drivers_array.length.must_equal csv_info.count end it "The information for the first & last driver is correct" do - drivers_array[0].id.must_equal csv_info[1][0].to_i - drivers_array[0].name.must_equal csv_info[1][1] - drivers_array[0].vin.must_equal csv_info[1][2] + drivers_array[0].id.must_equal csv_info[0][0].to_i + drivers_array[0].name.must_equal csv_info[0][1] + drivers_array[0].vin.must_equal csv_info[0][2] drivers_array[-1].id.must_equal csv_info[-1][0].to_i drivers_array[-1].name.must_equal csv_info[-1][1] drivers_array[-1].vin.must_equal csv_info[-1][2] end end + + describe "Driver.find" do + it "Returns a driver that exists" do + RideShare::Driver.find(64).must_be_instance_of RideShare::Driver + RideShare::Driver.find(64).vin.must_equal "SUA9K8KA35CZ8X2FT" + end + + it "Can find the first driver from the CSV" do + RideShare::Driver.find(csv_info[0][0].to_i).must_be_instance_of RideShare::Driver + RideShare::Driver.find(csv_info[0][0].to_i).name.must_equal csv_info[0][1] + end + + it "Can find the last driver from the CSV" do + RideShare::Driver.find(csv_info[-1][0].to_i).must_be_instance_of RideShare::Driver + RideShare::Driver.find(csv_info[-1][0].to_i).vin.must_equal csv_info[-1][2] + end + + it "Raises an error for a driver that doesn't exist" do + proc { RideShare::Driver.find(789078) }.must_raise NoDriverError + end + end end From bb68fdd01d81eef1fdef9b06b91a57031ca5a284 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 16:24:53 -0800 Subject: [PATCH 17/35] Adds and tests Rider.find method --- lib/no_rider_error.rb | 2 ++ lib/ride_share.rb | 1 + lib/rider.rb | 8 ++++++++ specs/rider_spec.rb | 31 ++++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 lib/no_rider_error.rb diff --git a/lib/no_rider_error.rb b/lib/no_rider_error.rb new file mode 100644 index 000000000..fb049ef5f --- /dev/null +++ b/lib/no_rider_error.rb @@ -0,0 +1,2 @@ +class NoRiderError < StandardError +end diff --git a/lib/ride_share.rb b/lib/ride_share.rb index 8babdd187..e926cbc16 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -18,6 +18,7 @@ def self.validate_string(field, field_name) end end +require_relative 'no_rider_error' require_relative 'no_driver_error' require_relative 'driver' require_relative 'rider' diff --git a/lib/rider.rb b/lib/rider.rb index 5b946f0bf..d77226c2c 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -18,4 +18,12 @@ def self.all end return riders end + + def self.find(id) + riders = RideShare::Rider.all + riders.each do |rider| + return rider if id == rider.id + end + raise NoRiderError.new("Rider with that ID does not exist") + end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 4f8662fb9..7d6d64134 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -3,7 +3,7 @@ describe "Rider tests" do let(:rider) { RideShare::Rider.new({ id: 8, name: "Galois", phone: "206-555-2468" }) } let(:riders_array) { RideShare::Rider.all } - let(:csv_info) { CSV.read('support/riders.csv') } + let(:csv_info) { CSV.read('support/riders.csv')[1..-1] } describe "Rider#initialize" do it "Takes an ID, name, and phone number" do @@ -61,17 +61,38 @@ end it "The number of riders is correct" do - riders_array.length.must_equal csv_info.count - 1 + riders_array.length.must_equal csv_info.count end it "The information for the first & last rider is correct" do - riders_array[0].id.must_equal csv_info[1][0].to_i - riders_array[0].name.must_equal csv_info[1][1] - riders_array[0].phone.must_equal csv_info[1][2] + riders_array[0].id.must_equal csv_info[0][0].to_i + riders_array[0].name.must_equal csv_info[0][1] + riders_array[0].phone.must_equal csv_info[0][2] riders_array[-1].id.must_equal csv_info[-1][0].to_i riders_array[-1].name.must_equal csv_info[-1][1] riders_array[-1].phone.must_equal csv_info[-1][2] end end + + describe "Rider.find" do + it "Returns a rider that exists" do + RideShare::Rider.find(16).must_be_instance_of RideShare::Rider + RideShare::Rider.find(16).name.must_equal "Mr. Onie Spinka" + end + + it "Can find the first rider from the CSV" do + RideShare::Rider.find(csv_info[0][0].to_i).must_be_instance_of RideShare::Rider + RideShare::Rider.find(csv_info[0][0].to_i).name.must_equal csv_info[0][1] + end + + it "Can find the last rider from the CSV" do + RideShare::Rider.find(csv_info[-1][0].to_i).must_be_instance_of RideShare::Rider + RideShare::Rider.find(csv_info[-1][0].to_i).phone.must_equal csv_info[-1][2] + end + + it "Raises an error for a rider that doesn't exist" do + proc { RideShare::Rider.find(0) }.must_raise NoRiderError + end + end end From 5836a3db3c5a233ea16bb42757e73d32def25b6c Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 16:57:25 -0800 Subject: [PATCH 18/35] working on the Trip.find_driver_trips --- lib/trip.rb | 13 ++++++++++ specs/trip_spec.rb | 65 ++++++++++++++++++++++++++++++---------------- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 98aa6d693..6b9ebfd44 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -24,4 +24,17 @@ def self.all end return trips end + + def self.find_driver_trips(driver_id) + trips = RideShare::Trip.all + drivers_trips = [] + + trips.each do |trip| + drivers_trips << trip if driver_id == trip.driver_id + end + + #deal with if no trips with that driver + + return drivers_trips + end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 599ddd7a5..696099699 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -3,7 +3,7 @@ describe "Trip tests" do let(:trip) { RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 5 }) } let(:trips_array) { RideShare::Trip.all } - let(:csv_info) { CSV.read('support/trips.csv') } + let(:csv_info) { CSV.read('support/trips.csv')[1..-1] } describe "Trip#initialize" do it "Takes an ID, driver_id, rider_id, date, and rating" do @@ -71,27 +71,46 @@ trips_array.must_be_instance_of Array end - # it "The first and last element of the array is a Trip" do - # trips_array[0].must_be_instance_of RideShare::Trip - # trips_array[-1].must_be_instance_of RideShare::Trip - # end - # - # it "The number of trips is correct" do - # trips_array.length.must_equal csv_info.count - 1 - # end - # - # it "The information for the first & last trip is correct" do - # trips_array[0].id.must_equal csv_info[1][0].to_i - # trips_array[0].driver_id.must_equal csv_info[1][1].to_i - # trips_array[0].rider_id.must_equal csv_info[1][2].to_i - # trips_array[0].date.must_equal Date.parse(csv_info[1][3]) - # trips_array[0].rating.must_equal csv_info[1][4].to_i - # - # trips_array[-1].id.must_equal csv_info[-1][0].to_i - # trips_array[-1].driver_id.must_equal csv_info[-1][1].to_i - # trips_array[-1].rider_id.must_equal csv_info[-1][2].to_i - # trips_array[-1].date.must_equal Date.parse(csv_info[-1][3]) - # trips_array[-1].rating.must_equal csv_info[-1][4].to_i - # end + it "The first and last element of the array is a Trip" do + trips_array[0].must_be_instance_of RideShare::Trip + trips_array[-1].must_be_instance_of RideShare::Trip + end + + it "The number of trips is correct" do + trips_array.length.must_equal csv_info.count + end + + it "The information for the first & last trip is correct" do + trips_array[0].id.must_equal csv_info[0][0].to_i + trips_array[0].driver_id.must_equal csv_info[0][1].to_i + trips_array[0].rider_id.must_equal csv_info[0][2].to_i + trips_array[0].date.must_equal Date.parse(csv_info[0][3]) + trips_array[0].rating.must_equal csv_info[0][4].to_i + + trips_array[-1].id.must_equal csv_info[-1][0].to_i + trips_array[-1].driver_id.must_equal csv_info[-1][1].to_i + trips_array[-1].rider_id.must_equal csv_info[-1][2].to_i + trips_array[-1].date.must_equal Date.parse(csv_info[-1][3]) + trips_array[-1].rating.must_equal csv_info[-1][4].to_i + end + end + + describe "Trip.find_driver_trips" do + it "Returns an Array" do + RideShare::Trip.find_driver_trips(2).must_be_instance_of Array + end + + it "The first and last element of the array is a Trip" do + drivers_trips = RideShare::Trip.find_driver_trips(2) + + drivers_trips[0].must_be_instance_of RideShare::Trip + drivers_trips[-1].must_be_instance_of RideShare::Trip + end + + it "The number of trips is correct" do + RideShare::Trip.find_driver_trips(2).length.must_equal 8 + end + + #do a test for what to do if no trips were taken by a driver end end From b7f3aa16053b6bc44fd51353a7fe645eee0f5596 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Tue, 7 Mar 2017 17:02:45 -0800 Subject: [PATCH 19/35] working on Trip.find_rider_trips method --- lib/trip.rb | 13 +++++++++++++ specs/trip_spec.rb | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index 6b9ebfd44..f6460375d 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -37,4 +37,17 @@ def self.find_driver_trips(driver_id) return drivers_trips end + + def self.find_rider_trips(rider_id) + trips = RideShare::Trip.all + riders_trips = [] + + trips.each do |trip| + riders_trips << trip if rider_id == trip.rider_id + end + + #deal with if no trips with that rider + + return riders_trips + end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 696099699..4a52bebd2 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -113,4 +113,23 @@ #do a test for what to do if no trips were taken by a driver end + + describe "Trip.find_rider_trips" do + it "Returns an Array" do + RideShare::Trip.find_rider_trips(2).must_be_instance_of Array + end + + it "The first and last element of the array is a Trip" do + riders_trips = RideShare::Trip.find_rider_trips(2) + + riders_trips[0].must_be_instance_of RideShare::Trip + riders_trips[-1].must_be_instance_of RideShare::Trip + end + + it "The number of trips is correct" do + RideShare::Trip.find_rider_trips(41).length.must_equal 3 + end + + #do a test for what to do if no trips were taken by a rider + end end From 4367649e5f6d119bc790aefe09b9d3c9ea996923 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 11:09:08 -0800 Subject: [PATCH 20/35] Moved validation methods to separate module, plus some refactoring --- lib/driver.rb | 21 +++++++++------------ lib/ride_share.rb | 16 +--------------- lib/rider.rb | 21 +++++++++------------ lib/trip.rb | 41 ++++++++++++----------------------------- lib/validation.rb | 18 ++++++++++++++++++ specs/trip_spec.rb | 8 ++++++-- 6 files changed, 55 insertions(+), 70 deletions(-) create mode 100644 lib/validation.rb diff --git a/lib/driver.rb b/lib/driver.rb index 894b18f02..ace605391 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,29 +1,26 @@ class RideShare::Driver + include Validation attr_reader :id, :name, :vin def initialize(driver_hash) - @id = RideShare::validate_int(driver_hash[:id], "Driver ID") - @name = RideShare::validate_string(driver_hash[:name], "Name") - @vin = RideShare::validate_string(driver_hash[:vin], "VIN") + @id = validate_int(driver_hash[:id], "Driver ID") + @name = validate_string(driver_hash[:name], "Name") + @vin = validate_string(driver_hash[:vin], "VIN") end def self.all - drivers = [] - CSV.read("support/drivers.csv")[1..-1].each do |line| - drivers << RideShare::Driver.new( { + return CSV.read("support/drivers.csv")[1..-1].map do |line| + new( { id: line[0].to_i, name: line[1], vin: line[2] } ) end - return drivers end def self.find(id) - drivers = RideShare::Driver.all - drivers.each do |driver| - return driver if id == driver.id - end - raise NoDriverError.new("Driver with that ID does not exist") + found_driver = all.find { |driver| driver.id == id } + raise NoDriverError.new("Driver with that ID does not exist") if found_driver == nil + return found_driver end end diff --git a/lib/ride_share.rb b/lib/ride_share.rb index e926cbc16..95627b279 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -1,23 +1,9 @@ require 'csv' module RideShare - def self.validate_int(field, field_name) - if field.class != Integer || field < 0 - raise ArgumentError.new("Required field #{field_name} must be a non-negative integer.") - end - - return field - end - - def self.validate_string(field, field_name) - if field.class != String || field == "" - raise ArgumentError.new("Required field #{field_name} must be a non-empty string.") - end - - return field - end end +require_relative 'validation' require_relative 'no_rider_error' require_relative 'no_driver_error' require_relative 'driver' diff --git a/lib/rider.rb b/lib/rider.rb index d77226c2c..33b4e5677 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,29 +1,26 @@ class RideShare::Rider + include Validation attr_reader :id, :name, :phone def initialize(rider_hash) - @id = RideShare::validate_int(rider_hash[:id], "Rider ID") - @name = RideShare::validate_string(rider_hash[:name], "Name") - @phone = RideShare::validate_string(rider_hash[:phone], "Phone Number") + @id = validate_int(rider_hash[:id], "Rider ID") + @name = validate_string(rider_hash[:name], "Name") + @phone = validate_string(rider_hash[:phone], "Phone Number") end def self.all - riders = [] - CSV.read("support/riders.csv")[1..-1].each do |line| - riders << RideShare::Rider.new( { + return CSV.read("support/riders.csv")[1..-1].map do |line| + new( { id: line[0].to_i, name: line[1], phone: line[2] } ) end - return riders end def self.find(id) - riders = RideShare::Rider.all - riders.each do |rider| - return rider if id == rider.id - end - raise NoRiderError.new("Rider with that ID does not exist") + found_rider = all.find { |rider| rider.id == id } + raise NoRiderError.new("Rider with that ID does not exist") if found_rider == nil + return found_rider end end diff --git a/lib/trip.rb b/lib/trip.rb index f6460375d..7670c76f2 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,20 +1,20 @@ class RideShare::Trip + include Validation attr_reader :id, :driver_id, :rider_id, :date, :rating def initialize(trip_hash) raise ArgumentError.new("Rating must be between 1-5") if !(trip_hash[:rating].between?(1,5)) - @id = RideShare::validate_int(trip_hash[:id], "Trip ID") - @driver_id = RideShare::validate_int(trip_hash[:driver_id], "Driver ID") - @rider_id = RideShare::validate_int(trip_hash[:rider_id], "Rider ID") - @date = Date.parse(RideShare::validate_string(trip_hash[:date], "Date")) - @rating = RideShare::validate_int(trip_hash[:rating], "Rating") + @id = validate_int(trip_hash[:id], "Trip ID") + @driver_id = validate_int(trip_hash[:driver_id], "Driver ID") + @rider_id = validate_int(trip_hash[:rider_id], "Rider ID") + @date = Date.parse(validate_string(trip_hash[:date], "Date")) + @rating = validate_int(trip_hash[:rating], "Rating") end def self.all - trips = [] - CSV.read("support/trips.csv")[1..-1].each do |line| - trips << RideShare::Trip.new( { + return CSV.read("support/trips.csv")[1..-1].map do |line| + new( { id: line[0].to_i, driver_id: line[1].to_i, rider_id: line[2].to_i, @@ -22,32 +22,15 @@ def self.all rating: line[4].to_i } ) end - return trips end def self.find_driver_trips(driver_id) - trips = RideShare::Trip.all - drivers_trips = [] - - trips.each do |trip| - drivers_trips << trip if driver_id == trip.driver_id - end - - #deal with if no trips with that driver - - return drivers_trips + trips = all + return trips.select { |trip| driver_id == trip.driver_id } end def self.find_rider_trips(rider_id) - trips = RideShare::Trip.all - riders_trips = [] - - trips.each do |trip| - riders_trips << trip if rider_id == trip.rider_id - end - - #deal with if no trips with that rider - - return riders_trips + trips = all + return trips.select { |trip| rider_id == trip.rider_id } end end diff --git a/lib/validation.rb b/lib/validation.rb new file mode 100644 index 000000000..c79f51659 --- /dev/null +++ b/lib/validation.rb @@ -0,0 +1,18 @@ +module Validation + + def validate_int(field, field_name) + if field.class != Integer || field < 0 + raise ArgumentError.new("Required field #{field_name} must be a non-negative integer.") + end + + return field + end + + def validate_string(field, field_name) + if field.class != String || field == "" + raise ArgumentError.new("Required field #{field_name} must be a non-empty string.") + end + + return field + end +end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 4a52bebd2..5f43d7197 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -111,7 +111,9 @@ RideShare::Trip.find_driver_trips(2).length.must_equal 8 end - #do a test for what to do if no trips were taken by a driver + it "Returns empty array if no trips are found" do + RideShare::Trip.find_driver_trips(10000).length.must_equal 0 + end end describe "Trip.find_rider_trips" do @@ -130,6 +132,8 @@ RideShare::Trip.find_rider_trips(41).length.must_equal 3 end - #do a test for what to do if no trips were taken by a rider + it "Returns empty array if no trips are found" do + RideShare::Trip.find_rider_trips(10000).length.must_equal 0 + end end end From cc7c8a9fc863e51656a0a5ec6a81b0453ebc3d8a Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 11:09:44 -0800 Subject: [PATCH 21/35] made validation methods private --- lib/validation.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/validation.rb b/lib/validation.rb index c79f51659..76b96796a 100644 --- a/lib/validation.rb +++ b/lib/validation.rb @@ -1,4 +1,5 @@ module Validation + private def validate_int(field, field_name) if field.class != Integer || field < 0 From bfe2a6ffe4a5a682226784c4ce71963cc4b2e79d Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 11:35:34 -0800 Subject: [PATCH 22/35] Adds and tests Driver.trips and messed with how classes are put in the modules --- lib/driver.rb | 50 +++++++++++++++++++++++--------------- lib/rider.rb | 42 +++++++++++++++++--------------- lib/trip.rb | 58 +++++++++++++++++++++++--------------------- specs/driver_spec.rb | 19 +++++++++++++++ 4 files changed, 101 insertions(+), 68 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index ace605391..7b7d58a09 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -1,26 +1,36 @@ -class RideShare::Driver - include Validation - attr_reader :id, :name, :vin +module RideShare + class Driver + include Validation + attr_reader :id, :name, :vin - def initialize(driver_hash) - @id = validate_int(driver_hash[:id], "Driver ID") - @name = validate_string(driver_hash[:name], "Name") - @vin = validate_string(driver_hash[:vin], "VIN") - end + def initialize(driver_hash) + @id = validate_int(driver_hash[:id], "Driver ID") + @name = validate_string(driver_hash[:name], "Name") + @vin = validate_string(driver_hash[:vin], "VIN") + end - def self.all - return CSV.read("support/drivers.csv")[1..-1].map do |line| - new( { - id: line[0].to_i, - name: line[1], - vin: line[2] - } ) + def self.all + return CSV.read("support/drivers.csv")[1..-1].map do |line| + new( { + id: line[0].to_i, + name: line[1], + vin: line[2] + } ) + end + end + + def self.find(id) + found_driver = all.find { |driver| driver.id == id } + raise NoDriverError.new("Driver with that ID does not exist") if found_driver == nil + return found_driver + end + + def trips + return Trip.find_driver_trips(id) end - end - def self.find(id) - found_driver = all.find { |driver| driver.id == id } - raise NoDriverError.new("Driver with that ID does not exist") if found_driver == nil - return found_driver + # def avg_rating + # + # end end end diff --git a/lib/rider.rb b/lib/rider.rb index 33b4e5677..924e354c7 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -1,26 +1,28 @@ -class RideShare::Rider - include Validation - attr_reader :id, :name, :phone +module RideShare + class Rider + include Validation + attr_reader :id, :name, :phone - def initialize(rider_hash) - @id = validate_int(rider_hash[:id], "Rider ID") - @name = validate_string(rider_hash[:name], "Name") - @phone = validate_string(rider_hash[:phone], "Phone Number") - end + def initialize(rider_hash) + @id = validate_int(rider_hash[:id], "Rider ID") + @name = validate_string(rider_hash[:name], "Name") + @phone = validate_string(rider_hash[:phone], "Phone Number") + end - def self.all - return CSV.read("support/riders.csv")[1..-1].map do |line| - new( { - id: line[0].to_i, - name: line[1], - phone: line[2] - } ) + def self.all + return CSV.read("support/riders.csv")[1..-1].map do |line| + new( { + id: line[0].to_i, + name: line[1], + phone: line[2] + } ) + end end - end - def self.find(id) - found_rider = all.find { |rider| rider.id == id } - raise NoRiderError.new("Rider with that ID does not exist") if found_rider == nil - return found_rider + def self.find(id) + found_rider = all.find { |rider| rider.id == id } + raise NoRiderError.new("Rider with that ID does not exist") if found_rider == nil + return found_rider + end end end diff --git a/lib/trip.rb b/lib/trip.rb index 7670c76f2..602a88d58 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -1,36 +1,38 @@ -class RideShare::Trip - include Validation - attr_reader :id, :driver_id, :rider_id, :date, :rating +module RideShare + class Trip + include Validation + attr_reader :id, :driver_id, :rider_id, :date, :rating - def initialize(trip_hash) - raise ArgumentError.new("Rating must be between 1-5") if !(trip_hash[:rating].between?(1,5)) + def initialize(trip_hash) + raise ArgumentError.new("Rating must be between 1-5") if !(trip_hash[:rating].between?(1,5)) - @id = validate_int(trip_hash[:id], "Trip ID") - @driver_id = validate_int(trip_hash[:driver_id], "Driver ID") - @rider_id = validate_int(trip_hash[:rider_id], "Rider ID") - @date = Date.parse(validate_string(trip_hash[:date], "Date")) - @rating = validate_int(trip_hash[:rating], "Rating") - end + @id = validate_int(trip_hash[:id], "Trip ID") + @driver_id = validate_int(trip_hash[:driver_id], "Driver ID") + @rider_id = validate_int(trip_hash[:rider_id], "Rider ID") + @date = Date.parse(validate_string(trip_hash[:date], "Date")) + @rating = validate_int(trip_hash[:rating], "Rating") + end - def self.all - return CSV.read("support/trips.csv")[1..-1].map do |line| - new( { - id: line[0].to_i, - driver_id: line[1].to_i, - rider_id: line[2].to_i, - date: line[3], - rating: line[4].to_i - } ) + def self.all + return CSV.read("support/trips.csv")[1..-1].map do |line| + new( { + id: line[0].to_i, + driver_id: line[1].to_i, + rider_id: line[2].to_i, + date: line[3], + rating: line[4].to_i + } ) + end end - end - def self.find_driver_trips(driver_id) - trips = all - return trips.select { |trip| driver_id == trip.driver_id } - end + def self.find_driver_trips(driver_id) + trips = all + return trips.select { |trip| driver_id == trip.driver_id } + end - def self.find_rider_trips(rider_id) - trips = all - return trips.select { |trip| rider_id == trip.rider_id } + def self.find_rider_trips(rider_id) + trips = all + return trips.select { |trip| rider_id == trip.rider_id } + end end end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 5a2fc5447..a48e3fc90 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -95,4 +95,23 @@ proc { RideShare::Driver.find(789078) }.must_raise NoDriverError end end + + describe "Driver#trips" do + it "Returns an array" do + driver.trips.must_be_instance_of Array + end + + it "First and last element of array are Trips" do + driver.trips[0].must_be_instance_of RideShare::Trip + driver.trips[-1].must_be_instance_of RideShare::Trip + end + + it "The number of trips is correct" do + RideShare::Driver.find(64).trips.length.must_equal 7 + end + + it "Returns an empty array if no trips are found" do + RideShare::Driver.find(100).trips.length.must_equal 0 + end + end end From b0df1834b89589c9f082c7409eca92f08e7aafed Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 13:33:57 -0800 Subject: [PATCH 23/35] Adds and tests Driver.avg_rating --- lib/driver.rb | 9 ++++++--- lib/no_rating_error.rb | 2 ++ lib/ride_share.rb | 1 + specs/driver_spec.rb | 20 ++++++++++++++++++-- 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 lib/no_rating_error.rb diff --git a/lib/driver.rb b/lib/driver.rb index 7b7d58a09..ccc4534dc 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -29,8 +29,11 @@ def trips return Trip.find_driver_trips(id) end - # def avg_rating - # - # end + def avg_rating + raise NoRatingError.new("This driver doesn't have any ratings yet") if trips.empty? + trip_ratings = trips.map { |trip| trip.rating } + average_rating = trip_ratings.reduce(:+) / trips.length.to_f + return average_rating.round(2) + end end end diff --git a/lib/no_rating_error.rb b/lib/no_rating_error.rb new file mode 100644 index 000000000..7c487bd0b --- /dev/null +++ b/lib/no_rating_error.rb @@ -0,0 +1,2 @@ +class NoRatingError < StandardError +end diff --git a/lib/ride_share.rb b/lib/ride_share.rb index 95627b279..ba3bae3e6 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -6,6 +6,7 @@ module RideShare require_relative 'validation' require_relative 'no_rider_error' require_relative 'no_driver_error' +require_relative 'no_rating_error' require_relative 'driver' require_relative 'rider' require_relative 'trip' diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index a48e3fc90..512fa3dcd 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -4,6 +4,8 @@ let(:driver) { RideShare::Driver.new({ id: 4, name: "Ada", vin: "1XKAD49X2DJ395724" }) } let(:drivers_array) { RideShare::Driver.all } let(:csv_info) { CSV.read('support/drivers.csv')[1..-1] } + let (:driver64) { RideShare::Driver.find(64) } + let (:driver100) { RideShare::Driver.find(100) } describe "Driver#initialize" do it "Takes an ID, name, and VIN" do @@ -107,11 +109,25 @@ end it "The number of trips is correct" do - RideShare::Driver.find(64).trips.length.must_equal 7 + driver64.trips.length.must_equal 7 end it "Returns an empty array if no trips are found" do - RideShare::Driver.find(100).trips.length.must_equal 0 + driver100.trips.length.must_equal 0 + end + end + + describe "Driver#avg_rating" do + it "Returns correct rating for a driver" do + driver64.avg_rating.must_be_within_epsilon (19/7.0), 0.01 + end + + it "Must return a Float" do + driver64.avg_rating.must_be_instance_of Float + end + + it "Raises error when there's no rating for a driver" do + proc { driver100.avg_rating }.must_raise NoRatingError end end end From 774261449a90581a806e12bd1b5fc34208f42dd5 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 13:44:42 -0800 Subject: [PATCH 24/35] Adds and tests Rider.trips method --- lib/rider.rb | 4 ++++ specs/rider_spec.rb | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/lib/rider.rb b/lib/rider.rb index 924e354c7..98eb53b1e 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -24,5 +24,9 @@ def self.find(id) raise NoRiderError.new("Rider with that ID does not exist") if found_rider == nil return found_rider end + + def trips + return Trip.find_rider_trips(id) + end end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index 7d6d64134..f6afec342 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -95,4 +95,23 @@ proc { RideShare::Rider.find(0) }.must_raise NoRiderError end end + + describe "Rider#trips" do + it "Returns an array" do + rider.trips.must_be_instance_of Array + end + + it "First and last element of array are Trips" do + rider.trips[0].must_be_instance_of RideShare::Trip + rider.trips[-1].must_be_instance_of RideShare::Trip + end + + it "The number of trips is correct for Rider 146" do + RideShare::Rider.find(146).trips.length.must_equal 4 + end + + it "Returns an empty array if no trips are found" do + RideShare::Rider.find(300).trips.must_equal [] + end + end end From 76184fbd43a18bde42d51318cfdb12d9df2bfaf6 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 14:58:47 -0800 Subject: [PATCH 25/35] require pry --- lib/ride_share.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/ride_share.rb b/lib/ride_share.rb index ba3bae3e6..21553df56 100644 --- a/lib/ride_share.rb +++ b/lib/ride_share.rb @@ -1,4 +1,5 @@ require 'csv' +require 'pry' module RideShare end From d10b6d69c289ce8a52b3e13676a0e9690dece43d Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 14:59:43 -0800 Subject: [PATCH 26/35] ignore a file I was playing around with --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 4ebc8aea5..f80bd402f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ coverage +test.rb From e2a21dd0a93a1db1e8b472f61eb703dceac69ac7 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 16:26:01 -0800 Subject: [PATCH 27/35] Adds and tests Rider.drivers method --- lib/rider.rb | 12 ++++++++++++ specs/rider_spec.rb | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/lib/rider.rb b/lib/rider.rb index 98eb53b1e..8522dcd4a 100644 --- a/lib/rider.rb +++ b/lib/rider.rb @@ -28,5 +28,17 @@ def self.find(id) def trips return Trip.find_rider_trips(id) end + + def drivers + drivers = trips.map do |trip| + begin + Driver.find(trip.driver_id) + rescue NoDriverError => e + puts "An error has occurred: #{e.message}" + end + end + drivers.delete(nil) + return drivers.uniq { |driver| driver.id } + end end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index f6afec342..d7a5843ef 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -4,6 +4,9 @@ let(:rider) { RideShare::Rider.new({ id: 8, name: "Galois", phone: "206-555-2468" }) } let(:riders_array) { RideShare::Rider.all } let(:csv_info) { CSV.read('support/riders.csv')[1..-1] } + let(:rider146) { RideShare::Rider.find(146) } + let(:rider300) { RideShare::Rider.find(300) } + let(:rider103) { RideShare::Rider.find(103) } describe "Rider#initialize" do it "Takes an ID, name, and phone number" do @@ -111,7 +114,41 @@ end it "Returns an empty array if no trips are found" do - RideShare::Rider.find(300).trips.must_equal [] + rider300.trips.must_equal [] + end + end + + describe "Rider#drivers" do + it "Returns an array" do + rider.drivers.must_be_instance_of Array + end + + it "First and last element of array are Drivers" do + rider.drivers[0].must_be_instance_of RideShare::Driver + rider.drivers[-1].must_be_instance_of RideShare::Driver + end + + it "The number of drivers is correct for Rider 146" do + rider146.drivers.length.must_equal 4 + end + + it "Drivers are not duplicated for a rider that's had a driver > 1 time" do + rider164_drivers = RideShare::Rider.find(164).drivers + + rider164_drivers.length.must_equal 5 + rider164_drivers.each { |driver| rider164_drivers.count(driver).must_equal 1 } + end + + it "Returns an empty array if no trips were taken" do + rider300.drivers.must_equal [] + end + + it "Outputs a message if it tries to find a driver with an ID that doesn't exist" do + proc { rider103.drivers }.must_output (/.+/) + end + + it "Doesn't include drivers with invalid driver ids for driver ID that doesn't exist" do + rider103.drivers.length.must_equal 1 end end end From 38a7b20f6ce212cc40d45c7e014f4e1686e78e5b Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 16:26:35 -0800 Subject: [PATCH 28/35] Added a comment for smthg to work on later --- specs/driver_spec.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 512fa3dcd..00a2797d3 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -129,5 +129,7 @@ it "Raises error when there's no rating for a driver" do proc { driver100.avg_rating }.must_raise NoRatingError end + + #make test for formatting?? end end From 90e49a61280fab3a05e2f278caf72d2bd8d0af7d Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 16:43:33 -0800 Subject: [PATCH 29/35] Adds and tests Trip.driver method --- lib/trip.rb | 8 ++++++++ specs/trip_spec.rb | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/trip.rb b/lib/trip.rb index 602a88d58..4b4db9004 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -34,5 +34,13 @@ def self.find_rider_trips(rider_id) trips = all return trips.select { |trip| rider_id == trip.rider_id } end + + def driver + begin + return Driver.find(driver_id) + rescue NoDriverError => e + puts "An error has occurred: #{e.message}" + end + end end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 5f43d7197..ae218a258 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -136,4 +136,15 @@ RideShare::Trip.find_rider_trips(10000).length.must_equal 0 end end + + describe "Trip#driver" do + it "returns a Driver object if the driver exists" do + trip.driver.must_be_instance_of RideShare::Driver + end + + it "outputs message if driver doesn't exist" do + bad_trip = RideShare::Trip.new({ id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 }) + proc { bad_trip.driver }.must_output (/.+/) + end + end end From bc80463eb0a7e1936309cba7aa65fd628756da37 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Wed, 8 Mar 2017 16:50:17 -0800 Subject: [PATCH 30/35] Adds and tests Trip#rider method --- lib/trip.rb | 8 ++++++++ specs/trip_spec.rb | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/trip.rb b/lib/trip.rb index 4b4db9004..830d3bc7e 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -42,5 +42,13 @@ def driver puts "An error has occurred: #{e.message}" end end + + def rider + begin + return Rider.find(rider_id) + rescue NoRiderError => e + puts "An error has occurred: #{e.message}" + end + end end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index ae218a258..3d58b9b2e 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -142,9 +142,22 @@ trip.driver.must_be_instance_of RideShare::Driver end - it "outputs message if driver doesn't exist" do + it "outputs message and returns nil if driver doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 }) proc { bad_trip.driver }.must_output (/.+/) + bad_trip.driver.must_equal nil + end + end + + describe "Trip#rider" do + it "returns a Rider object if the driver exists" do + trip.rider.must_be_instance_of RideShare::Rider + end + + it "outputs message and returns nil if rider doesn't exist" do + bad_trip = RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 0, date: "2014-07-12", rating: 5 }) + proc { bad_trip.rider }.must_output (/.+/) + bad_trip.rider.must_equal nil end end end From 475411c2a90f17f2fccff66f81499f66362eeec6 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Fri, 10 Mar 2017 20:20:06 -0800 Subject: [PATCH 31/35] put in some reminder comments --- specs/trip_spec.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index 3d58b9b2e..de6276b74 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -30,7 +30,7 @@ proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError - trip_hash3 = { id: 2, driver_id: "four", rider_id: 8, date: "2014-07-12", rating: 5 } + trip_hash3 = { id: 2, driver_id: 4.5, rider_id: 8, date: "2014-07-12", rating: 5 } trip_hash4 = { id: 2, driver_id: -4, rider_id: 8, date: "2014-07-12", rating: 5 } proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError @@ -142,6 +142,8 @@ trip.driver.must_be_instance_of RideShare::Driver end + #test if info on driver is correct + it "outputs message and returns nil if driver doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 }) proc { bad_trip.driver }.must_output (/.+/) @@ -150,10 +152,12 @@ end describe "Trip#rider" do - it "returns a Rider object if the driver exists" do + it "returns a Rider object if the rider exists" do trip.rider.must_be_instance_of RideShare::Rider end + #test if info on rider is correct + it "outputs message and returns nil if rider doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 0, date: "2014-07-12", rating: 5 }) proc { bad_trip.rider }.must_output (/.+/) From be92b358b3c77e2920bf586a0a4473ea4b5233c0 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Fri, 10 Mar 2017 20:35:22 -0800 Subject: [PATCH 32/35] Forces VINs to be 17 characters --- lib/driver.rb | 2 +- lib/validation.rb | 8 ++++++-- specs/driver_spec.rb | 12 +++++++++--- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/lib/driver.rb b/lib/driver.rb index ccc4534dc..fac0c6162 100644 --- a/lib/driver.rb +++ b/lib/driver.rb @@ -6,7 +6,7 @@ class Driver def initialize(driver_hash) @id = validate_int(driver_hash[:id], "Driver ID") @name = validate_string(driver_hash[:name], "Name") - @vin = validate_string(driver_hash[:vin], "VIN") + @vin = validate_string_length(driver_hash[:vin], "VIN", 17) end def self.all diff --git a/lib/validation.rb b/lib/validation.rb index 76b96796a..974b46dc9 100644 --- a/lib/validation.rb +++ b/lib/validation.rb @@ -5,7 +5,6 @@ def validate_int(field, field_name) if field.class != Integer || field < 0 raise ArgumentError.new("Required field #{field_name} must be a non-negative integer.") end - return field end @@ -13,7 +12,12 @@ def validate_string(field, field_name) if field.class != String || field == "" raise ArgumentError.new("Required field #{field_name} must be a non-empty string.") end - return field end + + def validate_string_length(field, field_name, length) + valid_string = validate_string(field, field_name) + raise ArgumentError.new("#{field_name} is invalid length") if valid_string.length != length + return valid_string + end end diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index 00a2797d3..bf53fe53c 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -27,16 +27,22 @@ proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError end - it "Only accepts non-empty strings for name and VIN" do + it "Only accepts non-empty strings for name" do driver_hash1 = { id: 4, name: "", vin: "1XKAD49X2DJ395724" } driver_hash2 = { id: 4, name: 45, vin: "1XKAD49X2DJ395724" } proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError + end - driver_hash3 = { id: 4, name: "Ada", vin: "" } - driver_hash4 = { id: 4, name: "Ada", vin: [] } + it "Only accepts VINs that are strings of length 17" do + driver_hash1 = { id: 4, name: "Ada", vin: [] } + driver_hash2 = { id: 4, name: "Ada", vin: "" } + driver_hash3 = { id: 4, name: "Ada", vin: "1XKAD49X2DJ39572" } + driver_hash4 = { id: 4, name: "Ada", vin: "1XKAD49X2DJ3957246" } + proc { RideShare::Driver.new(driver_hash1) }.must_raise ArgumentError + proc { RideShare::Driver.new(driver_hash2) }.must_raise ArgumentError proc { RideShare::Driver.new(driver_hash3) }.must_raise ArgumentError proc { RideShare::Driver.new(driver_hash4) }.must_raise ArgumentError end From 817091b40e0eff9a2234a22dd754c2502c2c51c9 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Sun, 12 Mar 2017 16:55:44 -0700 Subject: [PATCH 33/35] changed validation of trip rating --- lib/trip.rb | 4 +--- lib/validation.rb | 6 ++++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/trip.rb b/lib/trip.rb index 830d3bc7e..70b290ea9 100644 --- a/lib/trip.rb +++ b/lib/trip.rb @@ -4,13 +4,11 @@ class Trip attr_reader :id, :driver_id, :rider_id, :date, :rating def initialize(trip_hash) - raise ArgumentError.new("Rating must be between 1-5") if !(trip_hash[:rating].between?(1,5)) - @id = validate_int(trip_hash[:id], "Trip ID") @driver_id = validate_int(trip_hash[:driver_id], "Driver ID") @rider_id = validate_int(trip_hash[:rider_id], "Rider ID") @date = Date.parse(validate_string(trip_hash[:date], "Date")) - @rating = validate_int(trip_hash[:rating], "Rating") + @rating = validate_int_range(trip_hash[:rating], "Rating", 1, 5) end def self.all diff --git a/lib/validation.rb b/lib/validation.rb index 974b46dc9..3bcf3a976 100644 --- a/lib/validation.rb +++ b/lib/validation.rb @@ -8,6 +8,12 @@ def validate_int(field, field_name) return field end + def validate_int_range(field, field_name, min, max) + valid_int = validate_int(field, field_name) + raise ArgumentError.new("#{field_name} must be between #{min} and #{max}") if !(valid_int.between?(min, max)) + return valid_int + end + def validate_string(field, field_name) if field.class != String || field == "" raise ArgumentError.new("Required field #{field_name} must be a non-empty string.") From a57f2bca7d852e39369915ecae49b24d66c51190 Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Sun, 12 Mar 2017 17:35:06 -0700 Subject: [PATCH 34/35] Added test for date input --- specs/trip_spec.rb | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index de6276b74..ec1903f4f 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -43,7 +43,7 @@ proc { RideShare::Trip.new(trip_hash6) }.must_raise ArgumentError end - it "Only accepts strings for Date" do + it "Only accepts non-empty strings for Date" do trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: 87, rating: 5 } trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "", rating: 5 } @@ -51,6 +51,16 @@ proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError end + it "raises ArgumentError if the date string cannot be parsed to a Date" do + trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: "jsad;flk", rating: 5 } + trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "Two weeks ago", rating: 5 } + trip_hash3 = { id: 2, driver_id: 4, rider_id: 8, date: "a trip happend at some point", rating: 5 } + + proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError + end + it "Once initialized, date must be a Date" do trip.date.must_be_instance_of Date end @@ -64,6 +74,20 @@ proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError end + + it "All fields are required" do + trip_hash1 = { driver_id: 4, rider_id: 8, date: "2014-07-12", rating: 1 } + trip_hash2 = { id: 2, rider_id: 8, date: "2014-07-12", rating: 1 } + trip_hash3 = { id: 2, driver_id: 4, date: "2014-07-12", rating: 1 } + trip_hash4 = { id: 2, driver_id: 4, rider_id: 8, rating: 1 } + trip_hash5 = { id: 2, driver_id: 4, rider_id: 8, date: "2014-07-12" } + + proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash3) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash4) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash5) }.must_raise ArgumentError + end end describe "Trip.all" do @@ -142,7 +166,10 @@ trip.driver.must_be_instance_of RideShare::Driver end - #test if info on driver is correct + it "returns the correct Driver for a driver that exists" do + driver = trip.driver + driver.name.must_equal "Jeromy O'Keefe DVM" + end it "outputs message and returns nil if driver doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 }) @@ -156,7 +183,10 @@ trip.rider.must_be_instance_of RideShare::Rider end - #test if info on rider is correct + it "returns the correct Rider for a rider that exists" do + rider = trip.rider + rider.phone.must_equal "1-904-093-5211 x9183" + end it "outputs message and returns nil if rider doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 0, date: "2014-07-12", rating: 5 }) From 11b6bfe68d73974b9d453f1b08ff9e1cc80dff7b Mon Sep 17 00:00:00 2001 From: add2point71dots Date: Sun, 12 Mar 2017 17:56:36 -0700 Subject: [PATCH 35/35] Minor formatting changes in test files --- specs/driver_spec.rb | 7 +++++-- specs/rider_spec.rb | 4 +--- specs/trip_spec.rb | 20 +++++++++++--------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb index bf53fe53c..263a2f99a 100644 --- a/specs/driver_spec.rb +++ b/specs/driver_spec.rb @@ -119,7 +119,7 @@ end it "Returns an empty array if no trips are found" do - driver100.trips.length.must_equal 0 + driver100.trips.must_equal [] end end @@ -136,6 +136,9 @@ proc { driver100.avg_rating }.must_raise NoRatingError end - #make test for formatting?? + it "Returns rating with no more than two decimal places" do + rating_string = driver64.avg_rating.to_s + rating_string.length.must_be :<=, 4 + end end end diff --git a/specs/rider_spec.rb b/specs/rider_spec.rb index d7a5843ef..391755cd6 100644 --- a/specs/rider_spec.rb +++ b/specs/rider_spec.rb @@ -134,9 +134,7 @@ it "Drivers are not duplicated for a rider that's had a driver > 1 time" do rider164_drivers = RideShare::Rider.find(164).drivers - rider164_drivers.length.must_equal 5 - rider164_drivers.each { |driver| rider164_drivers.count(driver).must_equal 1 } end it "Returns an empty array if no trips were taken" do @@ -147,7 +145,7 @@ proc { rider103.drivers }.must_output (/.+/) end - it "Doesn't include drivers with invalid driver ids for driver ID that doesn't exist" do + it "Trips taken with driver IDs that don't return a Driver won't add to the array" do rider103.drivers.length.must_equal 1 end end diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb index ec1903f4f..ab0d53314 100644 --- a/specs/trip_spec.rb +++ b/specs/trip_spec.rb @@ -54,7 +54,7 @@ it "raises ArgumentError if the date string cannot be parsed to a Date" do trip_hash1 = { id: 2, driver_id: 4, rider_id: 8, date: "jsad;flk", rating: 5 } trip_hash2 = { id: 2, driver_id: 4, rider_id: 8, date: "Two weeks ago", rating: 5 } - trip_hash3 = { id: 2, driver_id: 4, rider_id: 8, date: "a trip happend at some point", rating: 5 } + trip_hash3 = { id: 2, driver_id: 4, rider_id: 8, date: "a trip happened at some point", rating: 5 } proc { RideShare::Trip.new(trip_hash1) }.must_raise ArgumentError proc { RideShare::Trip.new(trip_hash2) }.must_raise ArgumentError @@ -136,7 +136,7 @@ end it "Returns empty array if no trips are found" do - RideShare::Trip.find_driver_trips(10000).length.must_equal 0 + RideShare::Trip.find_driver_trips(10000).must_equal [] end end @@ -157,39 +157,41 @@ end it "Returns empty array if no trips are found" do - RideShare::Trip.find_rider_trips(10000).length.must_equal 0 + RideShare::Trip.find_rider_trips(10000).must_equal [] end end describe "Trip#driver" do - it "returns a Driver object if the driver exists" do + it "Returns a Driver object if the driver exists" do trip.driver.must_be_instance_of RideShare::Driver end - it "returns the correct Driver for a driver that exists" do + it "Returns the correct Driver for a driver that exists" do driver = trip.driver driver.name.must_equal "Jeromy O'Keefe DVM" end - it "outputs message and returns nil if driver doesn't exist" do + it "Outputs message and returns nil if driver doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 0, rider_id: 8, date: "2014-07-12", rating: 5 }) + proc { bad_trip.driver }.must_output (/.+/) bad_trip.driver.must_equal nil end end describe "Trip#rider" do - it "returns a Rider object if the rider exists" do + it "Returns a Rider object if the rider exists" do trip.rider.must_be_instance_of RideShare::Rider end - it "returns the correct Rider for a rider that exists" do + it "Returns the correct Rider for a rider that exists" do rider = trip.rider rider.phone.must_equal "1-904-093-5211 x9183" end - it "outputs message and returns nil if rider doesn't exist" do + it "Outputs message and returns nil if rider doesn't exist" do bad_trip = RideShare::Trip.new({ id: 2, driver_id: 4, rider_id: 0, date: "2014-07-12", rating: 5 }) + proc { bad_trip.rider }.must_output (/.+/) bad_trip.rider.must_equal nil end