From d05b68a395660c672654b220fd0b03251966f43f Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Thu, 22 Aug 2019 15:07:19 -0700 Subject: [PATCH 1/4] Finished Wave 1 w/tests passing --- lib/customer.rb | 13 +++++++++++++ lib/order.rb | 34 ++++++++++++++++++++++++++++++++++ test/customer_test.rb | 12 ++++++------ test/order_test.rb | 6 +++--- 4 files changed, 56 insertions(+), 9 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index e69de29b..2024ecc9 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -0,0 +1,13 @@ + +class Customer + + attr_reader :id + attr_accessor :email, :address + + def initialize(id, email, address) + @id = id + @email = email + @address = address + end + +end \ No newline at end of file diff --git a/lib/order.rb b/lib/order.rb index e69de29b..6a35a706 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,34 @@ +class Order + + attr_reader :id + attr_accessor :products, :customer, :fulfillment_status + + def initialize(id, products, customer, fulfillment_status = :pending) + @id = id + @products = products + @customer = customer + @fulfillment_status = fulfillment_status + + if [:pending, :paid, :processing, :shipped, :complete].include?(fulfillment_status) == false + raise ArgumentError.new('Invalid fulfillment status.') + end + end + + def add_product(product_name, product_price) + if @products.include?(product_name) == true + raise ArgumentError.new('Error. Product already exists in the list') + else + @products.store(product_name,product_price) + end + end + + def total + total = 0 + @products.each do |key, value| + price = value*(1.075) + total += price.round(2) + end + return total + end +end + diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..254affc2 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -35,7 +35,7 @@ # TODO: remove the 'x' in front of this block when you start wave 2 xdescribe "Customer Wave 2" do describe "Customer.all" do - it "Returns an array of all customers" do + xit "Returns an array of all customers" do customers = Customer.all expect(customers.length).must_equal 35 @@ -48,7 +48,7 @@ end end - it "Returns accurate information about the first customer" do + xit "Returns accurate information about the first customer" do first = Customer.all.first expect(first.id).must_equal 1 @@ -59,7 +59,7 @@ expect(first.address[:zip]).must_equal "98872-9105" end - it "Returns accurate information about the last customer" do + xit "Returns accurate information about the last customer" do last = Customer.all.last expect(last.id).must_equal 35 @@ -72,21 +72,21 @@ end describe "Customer.find" do - it "Can find the first customer from the CSV" do + xit "Can find the first customer from the CSV" do first = Customer.find(1) expect(first).must_be_kind_of Customer expect(first.id).must_equal 1 end - it "Can find the last customer from the CSV" do + xit "Can find the last customer from the CSV" do last = Customer.find(35) expect(last).must_be_kind_of Customer expect(last.id).must_equal 35 end - it "Returns nil for a customer that doesn't exist" do + xit "Returns nil for a customer that doesn't exist" do expect(Customer.find(53145)).must_be_nil end end diff --git a/test/order_test.rb b/test/order_test.rb index cdb2aec7..f91888d2 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -19,7 +19,7 @@ end describe "#initialize" do - it "Takes an ID, collection of products, customer, and fulfillment_status" do + it "Takes an ID, collection of products, customer, and fulfillment_status" do id = 1337 fulfillment_status = :shipped order = Order.new(id, {}, customer, fulfillment_status) @@ -62,7 +62,7 @@ end describe "#total" do - it "Returns the total from the collection of products" do + it "Returns the total from the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Order.new(1337, products, customer) @@ -79,7 +79,7 @@ end describe "#add_product" do - it "Increases the number of products" do + it "Increases the number of products" do products = { "banana" => 1.99, "cracker" => 3.00 } before_count = products.count order = Order.new(1337, products, customer) From 8f8a26181424cefe3eb3e80c5ff0fef0257e9ce4 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Sun, 25 Aug 2019 17:47:52 -0700 Subject: [PATCH 2/4] Finished Order Class, created class methods, completed given wave 2 tests --- data/customers.csv | 1 + data/orders.csv | 1 + lib/customer.rb | 32 +++++++++++++++++++++++++++++++- lib/order.rb | 30 ++++++++++++++++++++++++++++-- test/customer_test.rb | 14 +++++++------- test/order_test.rb | 32 ++++++++++++++++++++++++++------ 6 files changed, 94 insertions(+), 16 deletions(-) diff --git a/data/customers.csv b/data/customers.csv index 90580a89..68ce78d0 100644 --- a/data/customers.csv +++ b/data/customers.csv @@ -1,3 +1,4 @@ +id,email,street,city,state,zip 1,leonard.rogahn@hagenes.org,71596 Eden Route,Connellymouth,LA,98872-9105 2,ruben_nikolaus@kreiger.com,876 Kemmer Cove,East Luellatown,AL,21362 3,edison.mclaughlin@hyattjohns.co,96807 Cartwright Points,North Casper,MT,29547 diff --git a/data/orders.csv b/data/orders.csv index 597a5ce1..96309d2e 100644 --- a/data/orders.csv +++ b/data/orders.csv @@ -1,3 +1,4 @@ +id,products,customer,fulfillment_status 1,Lobster:17.18;Annatto seed:58.38;Camomile:83.21,25,complete 2,Sun dried tomatoes:90.16;Mastic:52.69;Nori:63.09;Cabbage:5.35,10,paid 3,Vegetable spaghetti:37.83;Dates:90.88;WhiteFlour:3.24;Caraway Seed:54.29,5,processing diff --git a/lib/customer.rb b/lib/customer.rb index 2024ecc9..731f45ca 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -1,3 +1,6 @@ +require 'csv' +require 'awesome_print' +require 'pry' class Customer @@ -9,5 +12,32 @@ def initialize(id, email, address) @email = email @address = address end + + def self.all + #CSV is an array of Hashes were each hash is a customer. + csv = CSV.read('/Users/dnsanche/ada/week3/grocery-store/data/customers.csv', headers: true).map(&:to_h) + customers = [] + + csv.each do |customer| + id = customer["id"].to_i + email = customer["email"] + address = { + street: customer["street"], + city: customer["city"], + state: customer["state"], + zip: customer["zip"], + } + customer = Customer.new(id,email,address) + customers << customer + end + return customers + end -end \ No newline at end of file + def self.find(id) + if self.all[id-1] != nil + return self.all[id-1] + else + nil + end + end +end diff --git a/lib/order.rb b/lib/order.rb index 6a35a706..e20abb6d 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,3 +1,7 @@ +require_relative 'customer' +require 'csv' +require 'awesome_print' + class Order attr_reader :id @@ -29,6 +33,28 @@ def total total += price.round(2) end return total - end -end + end + + def self.all + csv = CSV.read("/Users/dnsanche/ada/week3/grocery-store/data/orders.csv", headers: true).map(&:to_h) + orders = [] + csv.each do |customer_order| + id = customer_order["id"].to_i + products = {} + customer_order["products"].split(";").each do |product| + name_and_price = product.split(":") + products.store(name_and_price[0], name_and_price[1].to_f) + end + customer_id = customer_order["customer"].to_i + customer = Customer.find(customer_id) + fulfillment_status = customer_order["fulfillment_status"].to_sym + orders << Order.new(id,products,customer,fulfillment_status) + end + return orders + end + + def self.find(id) + #eturns an instance of Order where the value of the id field in the CSV matches the passed parameter + end +end diff --git a/test/customer_test.rb b/test/customer_test.rb index 254affc2..85b985e4 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -33,9 +33,9 @@ end # TODO: remove the 'x' in front of this block when you start wave 2 -xdescribe "Customer Wave 2" do +describe "Customer Wave 2" do describe "Customer.all" do - xit "Returns an array of all customers" do + it "Returns an array of all customers" do customers = Customer.all expect(customers.length).must_equal 35 @@ -48,7 +48,7 @@ end end - xit "Returns accurate information about the first customer" do + it "Returns accurate information about the first customer" do first = Customer.all.first expect(first.id).must_equal 1 @@ -59,7 +59,7 @@ expect(first.address[:zip]).must_equal "98872-9105" end - xit "Returns accurate information about the last customer" do + it "Returns accurate information about the last customer" do last = Customer.all.last expect(last.id).must_equal 35 @@ -72,21 +72,21 @@ end describe "Customer.find" do - xit "Can find the first customer from the CSV" do + it "Can find the first customer from the CSV" do first = Customer.find(1) expect(first).must_be_kind_of Customer expect(first.id).must_equal 1 end - xit "Can find the last customer from the CSV" do + it "Can find the last customer from the CSV" do last = Customer.find(35) expect(last).must_be_kind_of Customer expect(last.id).must_equal 35 end - xit "Returns nil for a customer that doesn't exist" do + it "Returns nil for a customer that doesn't exist" do expect(Customer.find(53145)).must_be_nil end end diff --git a/test/order_test.rb b/test/order_test.rb index f91888d2..6f3d47ff 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -1,7 +1,7 @@ require 'minitest/autorun' require 'minitest/reporters' require 'minitest/skip_dsl' - +require 'pry' require_relative '../lib/customer' require_relative '../lib/order' @@ -114,10 +114,19 @@ end # TODO: change 'xdescribe' to 'describe' to run these tests -xdescribe "Order Wave 2" do +describe "Order Wave 2" do describe "Order.all" do it "Returns an array of all orders" do - # TODO: Your test code here! + orders = Order.all + expect(orders.length).must_equal 100 + + orders.each do |o| + expect(o).must_be_kind_of Order + expect(o.id).must_be_kind_of Integer + expect(o.customer).must_be_kind_of Customer + expect(o.fulfillment_status).must_be_kind_of Symbol + end + end it "Returns accurate information about the first order" do @@ -142,19 +151,30 @@ it "Returns accurate information about the last order" do # TODO: Your test code here! + id = 100 + products = { + "Amaranth" => 83.81, + "Smoked Trout" => 70.6, + "Cheddar" => 5.63 + } + customer_id = 20 + fulfillment_status = :pending + + order = Order.all.last + end end describe "Order.find" do - it "Can find the first order from the CSV" do + xit "Can find the first order from the CSV" do # TODO: Your test code here! end - it "Can find the last order from the CSV" do + xit "Can find the last order from the CSV" do # TODO: Your test code here! end - it "Returns nil for an order that doesn't exist" do + xit "Returns nil for an order that doesn't exist" do # TODO: Your test code here! end end From 51c80531a295dc3dee0ec03989f27b21301132dc Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Sun, 25 Aug 2019 18:04:46 -0700 Subject: [PATCH 3/4] Finished class methods for order, finished test for find class method. It is passing all tests --- lib/order.rb | 6 +++++- test/order_test.rb | 30 +++++++++++++++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index e20abb6d..57fd7cc6 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -54,7 +54,11 @@ def self.all end def self.find(id) - #eturns an instance of Order where the value of the id field in the CSV matches the passed parameter + if Order.all[id-1] != nil + return Order.all[id-1] + else + nil + end end end diff --git a/test/order_test.rb b/test/order_test.rb index 6f3d47ff..509df34f 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -160,18 +160,38 @@ customer_id = 20 fulfillment_status = :pending - order = Order.all.last + last_order = Order.all.last + + # Check that all data was loaded as expected + expect(last_order.id).must_equal id + expect(last_order.products).must_equal products + expect(last_order.customer).must_be_kind_of Customer + expect(last_order.customer.id).must_equal customer_id + expect(last_order.fulfillment_status).must_equal fulfillment_status end end describe "Order.find" do - xit "Can find the first order from the CSV" do - # TODO: Your test code here! + it "Can find the first order from the CSV" do + first = Order.all.first + + expect(first).must_be_kind_of Order + expect(first.id).must_equal 1 + expect(first.products).must_be_kind_of Hash + expect(first.customer).must_be_kind_of Customer + expect(first.fulfillment_status).must_be_kind_of Symbol + end - xit "Can find the last order from the CSV" do - # TODO: Your test code here! + it "Can find the last order from the CSV" do + last = Order.all.last + + expect(first).must_be_kind_of Order + expect(first.id).must_equal 1 + expect(first.products).must_be_kind_of Hash + expect(first.customer).must_be_kind_of Customer + expect(first.fulfillment_status).must_be_kind_of Symbol end xit "Returns nil for an order that doesn't exist" do From a2ec81aea724a0e85e31812a428d8b114afe8fd3 Mon Sep 17 00:00:00 2001 From: daniela sanchez Date: Sun, 25 Aug 2019 18:14:34 -0700 Subject: [PATCH 4/4] Finished wave 2. Finished personal tests and all tests are still passing --- test/order_test.rb | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/test/order_test.rb b/test/order_test.rb index 509df34f..cb8b6426 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -174,28 +174,17 @@ describe "Order.find" do it "Can find the first order from the CSV" do - first = Order.all.first - - expect(first).must_be_kind_of Order + first = Customer.find(1) expect(first.id).must_equal 1 - expect(first.products).must_be_kind_of Hash - expect(first.customer).must_be_kind_of Customer - expect(first.fulfillment_status).must_be_kind_of Symbol - end it "Can find the last order from the CSV" do - last = Order.all.last - - expect(first).must_be_kind_of Order - expect(first.id).must_equal 1 - expect(first.products).must_be_kind_of Hash - expect(first.customer).must_be_kind_of Customer - expect(first.fulfillment_status).must_be_kind_of Symbol + last = Order.find(100) + expect(last.id).must_equal 100 end - xit "Returns nil for an order that doesn't exist" do - # TODO: Your test code here! + it "Returns nil for an order that doesn't exist" do + expect(Order.find(101)).must_be_nil end end end