Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
fb6b0c6
created stubs of tests, classes, and errors. requirements in comments
bcmdarroch Mar 7, 2017
5029e56
driver pseudocode complete
bcmdarroch Mar 7, 2017
3e8a785
finished pseudocode for rider and trip, ready to write first test
bcmdarroch Mar 7, 2017
79508fd
created rideshare.rb to hold module and its setup
bcmdarroch Mar 7, 2017
16dc9d7
set up Rakefile, added first driver test, failed
bcmdarroch Mar 7, 2017
af9af24
started driver initialize verify tests, most failed
bcmdarroch Mar 7, 2017
37c5349
driver passes initalize tests, added stubs of remaining tests
bcmdarroch Mar 7, 2017
f6ea6c6
driver passes initialize tests, created all and find tests & methods,…
bcmdarroch Mar 8, 2017
37a2409
driver passes driver.find tests
bcmdarroch Mar 8, 2017
c631ac2
made test stubs for trips and average rating
bcmdarroch Mar 8, 2017
9c4a8a8
wrote tests for trips and av_rating
bcmdarroch Mar 8, 2017
a6a98ec
wrote test class and methods, fails driver .trips method tests
bcmdarroch Mar 8, 2017
2e467d6
wrote trip tests, failing most of them
bcmdarroch Mar 8, 2017
c17e499
passes trip tests, wrote rider tests, fails all rider tests
bcmdarroch Mar 8, 2017
6dcc775
passes all baseline tests! :) need to work on coverage
bcmdarroch Mar 9, 2017
d2319a5
added method and test for trip.find_driver, passes basic test, need m…
bcmdarroch Mar 9, 2017
05a753f
fixed find_by_driver and find_by_rider tests to check for array of tr…
bcmdarroch Mar 12, 2017
cc70a20
passes tests for find_driver and find_rider, 100% coverage aww yeeah
bcmdarroch Mar 13, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
test/
10 changes: 9 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
# Fill me in!
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs = ["lib"]
t.warning = false
t.test_files = FileList['specs/*_spec.rb']
end

task default: :test
41 changes: 41 additions & 0 deletions lib/driver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require_relative 'rideshare'

module RideShare

class Driver
attr_reader :id, :name, :vin

def initialize(id, name, vin)
raise ArgumentError.new("The ID is invalid.") if id.class != Integer
raise ArgumentError.new("The name is invalid.") if name.class != String
raise InvalidVinError.new("The VIN number is invalid.") if vin.length != 17 || !vin.upcase.match(/^[0-9A-Z]+$/)

@id = id.to_i
@name = name
@vin = vin.upcase
end


def self.all
CSV.read("support/drivers.csv", headers: true).map do | line |
RideShare::Driver.new(line[0].to_i, line[1], line[2])
end
end

def self.find(driver_id)
raise ArgumentError.new("The driver ID is invalid.") if driver_id.class != Integer
self.all.find { |driver| driver.id == driver_id }
end

def trips
RideShare::Trip.find_by_driver(@id)
end

def average_rating
all_ratings = trips.map { |trip| trip.rating }
all_ratings.inject(:+).to_f / all_ratings.length
end

end

end
2 changes: 2 additions & 0 deletions lib/invalidrating.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class InvalidRatingError < StandardError
end
2 changes: 2 additions & 0 deletions lib/invalidvin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class InvalidVinError < StandardError
end
44 changes: 44 additions & 0 deletions lib/rider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative 'rideshare'

module RideShare

class Rider

attr_reader :id, :name, :phone

def initialize(id, name, phone)
raise ArgumentError.new("The ID is invalid.") if id.class != Integer
raise ArgumentError.new("The name is invalid.") if name.class != String
raise ArgumentError.new("The phone number is invalid.") if phone.class != String

@id = id
@name = name
@phone = phone
end

def self.all
CSV.read("support/riders.csv", headers: true).map do | line |
RideShare::Rider.new(line[0].to_i, line[1], line[2])
end
end

def self.find(rider_id)
raise ArgumentError.new("The driver ID is invalid.") if rider_id.class != Integer
self.all.find { |rider| rider.id == rider_id }
end

def trips
RideShare::Trip.find_by_rider(@id)
end

def drivers
rider_driver_ids = trips.map { |trip| trip.driver_id }
rider_drivers = rider_driver_ids.map do |driver_id|
RideShare::Trip.find_by_driver(driver_id)
end
rider_drivers.uniq
end

end

end
9 changes: 9 additions & 0 deletions lib/rideshare.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require 'csv'

module RideShare; end

require_relative 'invalidvin'
require_relative 'invalidrating'
require_relative 'trip'
require_relative 'driver'
require_relative 'rider'
51 changes: 51 additions & 0 deletions lib/trip.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
require_relative 'rideshare'

module RideShare

class Trip
attr_reader :id, :driver_id, :rider_id, :date, :rating

# could try to implement hash argument instead
def initialize(id, driver_id, rider_id, date, rating)
raise ArgumentError.new("The ID is invalid.") if id.class != Integer
raise ArgumentError.new("The driver ID is invalid.") if driver_id.class != Integer
raise ArgumentError.new("The rider ID is invalid.") if rider_id.class != Integer
raise ArgumentError.new("The date is invalid.") if date.class != String
raise InvalidRatingError.new("The rating is invalid.") if !(1..5).include?(rating)


@id = id.to_i
@driver_id = driver_id
@rider_id = rider_id
@date = date
@rating = rating.to_i
end

# could implement with hash
def self.all
CSV.read("support/trips.csv", headers: true).map do | line |
RideShare::Trip.new(line[0].to_i, line[1].to_i, line[2].to_i, line[3], line[4].to_i)
end
end

def self.find_by_driver(driver_id)
raise ArgumentError.new("The driver ID is invalid.") if driver_id.class != Integer
self.all.find_all { |trip| trip.driver_id == driver_id }
end

def self.find_by_rider(rider_id)
raise ArgumentError.new("The rider ID is invalid.") if rider_id.class != Integer
self.all.find_all { |trip| trip.rider_id == rider_id }
end

def find_driver
RideShare::Driver.find(@driver_id)
end

def find_rider
RideShare::Rider.find(@rider_id)
end

end

end
117 changes: 117 additions & 0 deletions specs/driver_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
require_relative 'spec_helper'


describe "Driver" do
let(:drivers) { RideShare::Driver.all }

describe "Driver#initialize" do
let(:vindiesel) { RideShare::Driver.new(777, "Vin Diesel", "FASTNFURIOUS00007") }

it "Driver has a name, ID, and VIN" do
vindiesel.id.must_equal 777
vindiesel.name.must_equal "Vin Diesel"
vindiesel.vin.must_equal "FASTNFURIOUS00007"
end

it "raises error if Driver's ID or name are invalid input" do
proc {
RideShare::Driver.new("VD", "Vin Diesel", "fastnfurious00007")
}.must_raise ArgumentError
proc {
RideShare::Driver.new(777, ["Vin Diesel"], "FASTNFURIOUS00007")
}.must_raise ArgumentError
end

it "raises error if VIN has invalid characters or wrong length" do
proc {
RideShare::Driver.new(777, "Vin Diesel", "FASTNFURIOUS@@!!7")
}.must_raise InvalidVinError
proc {
RideShare::Driver.new(777, "Vin Diesel", "FASTNFURIOUS007")
}.must_raise InvalidVinError
end

it "accepts lowercase letters in VIN" do
vindiesel = RideShare::Driver.new(777, "Vin Diesel", "fastnfurious00007")
vindiesel.vin.must_equal "FASTNFURIOUS00007"
end

end


describe "Driver#all" do

it "returns an array of Driver instances" do
drivers.must_be_kind_of Array
drivers.all? do | driver |
driver.must_be_instance_of RideShare::Driver
end
end

it "returns array with the correct number of Drivers from csv" do
drivers.length.must_equal 100
end

it "Driver instances match what's in the csv file" do
index = 0
CSV.read("support/drivers.csv", headers: true) do |line|
drivers[index].id.must_equal line[0].to_i
drivers[index].name.must_equal line[1]
drivers[index].vin.must_equal line[2]
index += 1
end
end

end


describe "Driver#find" do

it "raises an error if a non-integer is provided" do
proc {
RideShare::Driver.find("first driver")
}.must_raise ArgumentError
end

it "returns an instance of Driver" do
RideShare::Driver.find(1).must_be_instance_of RideShare::Driver
end

it "can find any driver based on a randomly generated number" do
10.times do
random_id = rand(1..100)
driver_check = drivers[random_id - 1].id
driver = RideShare::Driver.find(random_id)
expect(driver.id).must_equal driver_check
end
end

it "returns nil if account doesn't exist" do
RideShare::Driver.find(383).must_be_nil
end

end


describe "Driver#trips and #ratings" do
let(:shakira) { RideShare::Driver.new(16, "Shakira Stamm", "SALUVSAL3WA67SBPZ") }

it "returns an array whose length matches the number of Driver's trips" do
shakira.trips.must_be_kind_of Array
shakira.trips.length.must_equal RideShare::Trip.find_by_driver(16).length
end

it "returns an array of all Trip instances" do
shakira.trips.all? do | trip |
trip.must_be_instance_of RideShare::Trip
end
end

it "returns correct average rating for a Driver" do
# (2 + 5 + 1 + 2 + 4 + 1) / 6
shakira.average_rating.must_equal 2.5
end

end

end
Loading