diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..f80bd402f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +coverage +test.rb 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/lib/driver.rb b/lib/driver.rb new file mode 100644 index 000000000..fac0c6162 --- /dev/null +++ b/lib/driver.rb @@ -0,0 +1,39 @@ +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_length(driver_hash[:vin], "VIN", 17) + 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] + } ) + 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 + + 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_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/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/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 new file mode 100644 index 000000000..21553df56 --- /dev/null +++ b/lib/ride_share.rb @@ -0,0 +1,13 @@ +require 'csv' +require 'pry' + +module RideShare +end + +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/lib/rider.rb b/lib/rider.rb new file mode 100644 index 000000000..8522dcd4a --- /dev/null +++ b/lib/rider.rb @@ -0,0 +1,44 @@ +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 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 + + 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 + + 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/lib/trip.rb b/lib/trip.rb new file mode 100644 index 000000000..70b290ea9 --- /dev/null +++ b/lib/trip.rb @@ -0,0 +1,52 @@ +module RideShare + class Trip + include Validation + attr_reader :id, :driver_id, :rider_id, :date, :rating + + def initialize(trip_hash) + @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_range(trip_hash[:rating], "Rating", 1, 5) + 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 + } ) + end + 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 } + end + + def driver + begin + return Driver.find(driver_id) + rescue NoDriverError => e + 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/lib/validation.rb b/lib/validation.rb new file mode 100644 index 000000000..3bcf3a976 --- /dev/null +++ b/lib/validation.rb @@ -0,0 +1,29 @@ +module Validation + private + + 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_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.") + 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/.keep b/specs/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/specs/driver_spec.rb b/specs/driver_spec.rb new file mode 100644 index 000000000..263a2f99a --- /dev/null +++ b/specs/driver_spec.rb @@ -0,0 +1,144 @@ +require_relative 'spec_helper' + +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')[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 + 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 + + it "Only accepts non-negative integer IDs" do + driver_hash1 = { id: "id", 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 + end + + 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 + + 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 + + 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 + + describe "Driver.all" do + it "Driver.all returns an array" do + drivers_array.must_be_instance_of Array + 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 + 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[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 + + 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 + driver64.trips.length.must_equal 7 + end + + it "Returns an empty array if no trips are found" do + driver100.trips.must_equal [] + 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 + + 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 new file mode 100644 index 000000000..391755cd6 --- /dev/null +++ b/specs/rider_spec.rb @@ -0,0 +1,152 @@ +require_relative 'spec_helper' + +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')[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 + 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 + + it "Only accepts non-negative integer IDs" do + rider_hash1 = { id: "id", 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 + end + + 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 "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 + + describe "Rider.all" do + it "Rider.all returns an array" do + riders_array.must_be_instance_of Array + 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 + 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[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 + + 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 + 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 + 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 "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 +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 4d1e3fdc8..b902ca782 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,8 +1,9 @@ +require 'simplecov' +SimpleCov.start + require 'minitest' require 'minitest/autorun' require 'minitest/reporters' -# Add simplecov - Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -# Require_relative your lib files here! +require_relative '../lib/ride_share' diff --git a/specs/trip_spec.rb b/specs/trip_spec.rb new file mode 100644 index 000000000..ab0d53314 --- /dev/null +++ b/specs/trip_spec.rb @@ -0,0 +1,199 @@ +require_relative 'spec_helper' + +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')[1..-1] } + + 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 Date.parse("2014-07-12") + + trip.must_respond_to :rating + trip.rating.must_equal 5 + end + + 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: -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 + + 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 + proc { RideShare::Trip.new(trip_hash4) }.must_raise ArgumentError + + 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_hash5) }.must_raise ArgumentError + proc { RideShare::Trip.new(trip_hash6) }.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 "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 happened 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 + + 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 + + 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 + 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 + 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 + + it "Returns empty array if no trips are found" do + RideShare::Trip.find_driver_trips(10000).must_equal [] + end + 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 + + it "Returns empty array if no trips are found" do + RideShare::Trip.find_rider_trips(10000).must_equal [] + 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 "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 }) + + 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 + trip.rider.must_be_instance_of RideShare::Rider + end + + 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 }) + + proc { bad_trip.rider }.must_output (/.+/) + bad_trip.rider.must_equal nil + end + end +end