From f5b00819c5cec0c5f28f09a027dd8a54bfd637dd Mon Sep 17 00:00:00 2001 From: emaust Date: Thu, 22 Aug 2019 08:42:19 -0700 Subject: [PATCH 1/8] Wave 1 Order, Customer classes intialized --- lib/customer.rb | 14 ++++++++ lib/order.rb | 74 +++++++++++++++++++++++++++++++++++++++++++ test/customer_test.rb | 34 ++++++++++---------- test/order_test.rb | 66 +++++++++++++++++++------------------- 4 files changed, 138 insertions(+), 50 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index e69de29b..01ccd4e5 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -0,0 +1,14 @@ + + +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..8390e265 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -0,0 +1,74 @@ + +class Order + attr_reader :id, :products, :customer, :fulfillment_status + + # instance of Customer, the person who placed the order + # fulfillment_status, a symbol of :pending, :paid, :processing, :shipped, :complete + # if there is not fulfilment_status, default to :pending + # otherwise, ArgumentError should be raised + + def initialize(id, products, customer, fulfillment_status) + @id = id + @products = products + @customer = customer + @fulfillment_status = fulfillment_status + end + + # collection of products and their cost + # will be in a hash like { "banana" => 1.99, "cracker" => 3.00 } + # should have an add_product method + # two parameters: product name and price + # add data to product collection + # assume there is only one of each product + # if a product with the same name has already been added to the order, an ArgumentError should be raised + # zero products is allowed (empty hash) + + # xdescribe "#add_product" 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) + + product_count = 0 + product_name = () + + def add_product(product_name, price) + + puts "Enter product" + product_name = gets.chomp.to_s + if @product.include?(product_name) + raise ArgumentError + end + + puts "Enter product cost" + price = gets.chomp.to_s + + @products = { + product_name => price + } + + product_count += 1 + + end + + # should have method called total + # summing up the products + # adding a 7.5% tax + # rounding the result to two decimal places + + def total(products) + total_ + + end + + + + + + + + + + + + \ No newline at end of file diff --git a/test/customer_test.rb b/test/customer_test.rb index 54889400..defa3492 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -6,7 +6,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -describe "Customer Wave 1" do +xdescribe "Customer Wave 1" do ID = 123 EMAIL = "a@a.co" ADDRESS = { @@ -15,17 +15,17 @@ state: "WA", zip: "98101" }.freeze - - describe "#initialize" do + + xdescribe "#initialize" do it "Takes an ID, email and address info" do cust = Customer.new(ID, EMAIL, ADDRESS) - + expect(cust).must_respond_to :id expect(cust.id).must_equal ID - + expect(cust).must_respond_to :email expect(cust.email).must_equal EMAIL - + expect(cust).must_respond_to :address expect(cust.address).must_equal ADDRESS end @@ -37,20 +37,20 @@ describe "Customer.all" do it "Returns an array of all customers" do customers = Customer.all - + expect(customers.length).must_equal 35 customers.each do |c| expect(c).must_be_kind_of Customer - + expect(c.id).must_be_kind_of Integer expect(c.email).must_be_kind_of String expect(c.address).must_be_kind_of Hash end end - + it "Returns accurate information about the first customer" do first = Customer.all.first - + expect(first.id).must_equal 1 expect(first.email).must_equal "leonard.rogahn@hagenes.org" expect(first.address[:street]).must_equal "71596 Eden Route" @@ -58,10 +58,10 @@ expect(first.address[:state]).must_equal "LA" expect(first.address[:zip]).must_equal "98872-9105" end - + it "Returns accurate information about the last customer" do last = Customer.all.last - + expect(last.id).must_equal 35 expect(last.email).must_equal "rogers_koelpin@oconnell.org" expect(last.address[:street]).must_equal '7513 Kaylee Summit' @@ -70,22 +70,22 @@ expect(last.address[:zip]).must_equal '64529-2614' end end - + describe "Customer.find" 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 - + 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 - + it "Returns nil for a customer that doesn't exist" do expect(Customer.find(53145)).must_be_nil end diff --git a/test/order_test.rb b/test/order_test.rb index cdb2aec7..161f8ceb 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -17,41 +17,41 @@ } Customer.new(123, "a@a.co", address) end - + describe "#initialize" 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) - + expect(order).must_respond_to :id expect(order.id).must_equal id - + expect(order).must_respond_to :products expect(order.products.length).must_equal 0 - + expect(order).must_respond_to :customer expect(order.customer).must_equal customer - + expect(order).must_respond_to :fulfillment_status expect(order.fulfillment_status).must_equal fulfillment_status end - - it "Accepts all legal statuses" do + + xit "Accepts all legal statuses" do valid_statuses = %i[pending paid processing shipped complete] - + valid_statuses.each do |fulfillment_status| order = Order.new(1, {}, customer, fulfillment_status) expect(order.fulfillment_status).must_equal fulfillment_status end end - - it "Uses pending if no fulfillment_status is supplied" do + + xit "Uses pending if no fulfillment_status is supplied" do order = Order.new(1, {}, customer) expect(order.fulfillment_status).must_equal :pending end - - it "Raises an ArgumentError for bogus statuses" do + + xit "Raises an ArgumentError for bogus statuses" do bogus_statuses = [3, :bogus, 'pending', nil] bogus_statuses.each do |fulfillment_status| expect { @@ -60,53 +60,53 @@ end end end - - describe "#total" do + + xdescribe "#total" do it "Returns the total from the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Order.new(1337, products, customer) - + expected_total = 5.36 - + expect(order.total).must_equal expected_total end - + it "Returns a total of zero if there are no products" do order = Order.new(1337, {}, customer) - + expect(order.total).must_equal 0 end end - + describe "#add_product" 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) - + order.add_product("salad", 4.25) expected_count = before_count + 1 expect(order.products.count).must_equal expected_count end - + it "Is added to the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Order.new(1337, products, customer) - + order.add_product("sandwich", 4.25) expect(order.products.include?("sandwich")).must_equal true end - + it "Raises an ArgumentError if the product is already present" do products = { "banana" => 1.99, "cracker" => 3.00 } - + order = Order.new(1337, products, customer) before_total = order.total - + expect { order.add_product("banana", 4.25) }.must_raise ArgumentError - + # The list of products should not have been modified expect(order.total).must_equal before_total end @@ -119,7 +119,7 @@ it "Returns an array of all orders" do # TODO: Your test code here! end - + it "Returns accurate information about the first order" do id = 1 products = { @@ -129,9 +129,9 @@ } customer_id = 25 fulfillment_status = :complete - + order = Order.all.first - + # Check that all data was loaded as expected expect(order.id).must_equal id expect(order.products).must_equal products @@ -139,21 +139,21 @@ expect(order.customer.id).must_equal customer_id expect(order.fulfillment_status).must_equal fulfillment_status end - + it "Returns accurate information about the last order" do # TODO: Your test code here! end end - + describe "Order.find" do it "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 # TODO: Your test code here! end - + it "Returns nil for an order that doesn't exist" do # TODO: Your test code here! end From aa6a2acaacd82124f6ca7ff3bbbf825bfb947bf1 Mon Sep 17 00:00:00 2001 From: emaust Date: Thu, 22 Aug 2019 17:32:21 -0700 Subject: [PATCH 2/8] Wave 1 completed --- lib/order.rb | 105 ++++++++++++++++++++++----------------------- test/order_test.rb | 9 ++-- 2 files changed, 56 insertions(+), 58 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index 8390e265..2477d90a 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,74 +1,71 @@ - +require 'pry' class Order - attr_reader :id, :products, :customer, :fulfillment_status + attr_reader :id + attr_accessor :products, :customer, :fulfillment_status # instance of Customer, the person who placed the order # fulfillment_status, a symbol of :pending, :paid, :processing, :shipped, :complete # if there is not fulfilment_status, default to :pending # otherwise, ArgumentError should be raised - def initialize(id, products, customer, fulfillment_status) + def initialize(id, products, customer, fulfillment_status = :pending) @id = id @products = products @customer = customer @fulfillment_status = fulfillment_status - end - - # collection of products and their cost - # will be in a hash like { "banana" => 1.99, "cracker" => 3.00 } - # should have an add_product method - # two parameters: product name and price - # add data to product collection - # assume there is only one of each product - # if a product with the same name has already been added to the order, an ArgumentError should be raised - # zero products is allowed (empty hash) - - # xdescribe "#add_product" 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) - - product_count = 0 - product_name = () - - def add_product(product_name, price) - puts "Enter product" - product_name = gets.chomp.to_s - if @product.include?(product_name) + status_array = [:pending, :paid, :processing, :shipped, :complete] + + if id.is_a?(Integer) == false + raise ArgumentError + elsif + products.is_a?(Hash) == false + raise ArgumentError + elsif + customer.is_a?(Customer) == false raise ArgumentError + elsif + !(status_array.include?(fulfillment_status)) + raise ArgumentError end - puts "Enter product cost" - price = gets.chomp.to_s - - @products = { - product_name => price - } - - product_count += 1 - end - # should have method called total - # summing up the products - # adding a 7.5% tax - # rounding the result to two decimal places + def add_product(product_name, price) + if @products.keys.include?(product_name) + raise ArgumentError + else + @products[product_name] = price + end + return @products + end - def total(products) - total_ + def total + cost_array = [] + @products.each do |product_name, cost| + cost_array << cost + end + sub_total = cost_array.sum + taxed = sub_total.to_f * 1.075 + return taxed.round(2) end - - - - - - - - - - - - \ No newline at end of file +end + +# should have method called total +# summing up the products +# adding a 7.5% tax +# rounding the result to two decimal places + + + + + + + + + + + + + diff --git a/test/order_test.rb b/test/order_test.rb index 161f8ceb..f0c3297d 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -5,6 +5,7 @@ require_relative '../lib/customer' require_relative '../lib/order' + Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe "Order Wave 1" do @@ -37,7 +38,7 @@ expect(order.fulfillment_status).must_equal fulfillment_status end - xit "Accepts all legal statuses" do + it "Accepts all legal statuses" do valid_statuses = %i[pending paid processing shipped complete] valid_statuses.each do |fulfillment_status| @@ -46,12 +47,12 @@ end end - xit "Uses pending if no fulfillment_status is supplied" do + it "Uses pending if no fulfillment_status is supplied" do order = Order.new(1, {}, customer) expect(order.fulfillment_status).must_equal :pending end - xit "Raises an ArgumentError for bogus statuses" do + it "Raises an ArgumentError for bogus statuses" do bogus_statuses = [3, :bogus, 'pending', nil] bogus_statuses.each do |fulfillment_status| expect { @@ -61,7 +62,7 @@ end end - xdescribe "#total" do + describe "#total" do it "Returns the total from the collection of products" do products = { "banana" => 1.99, "cracker" => 3.00 } order = Order.new(1337, products, customer) From e895e121278d42f177744c537fdbad7d123bb55e Mon Sep 17 00:00:00 2001 From: emaust Date: Sat, 24 Aug 2019 18:56:50 -0700 Subject: [PATCH 3/8] customer array initialized --- lib/customer.rb | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index 01ccd4e5..fb9273e2 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -1,9 +1,15 @@ +require 'csv' +require 'awesome_print' +require 'pry' + class Customer - attr_reader :id - attr_accessor :email, :address + attr_reader :id, :customer + attr_accessor :email, :address, :customers + + def initialize(id, email, address) @id = id @@ -11,4 +17,29 @@ def initialize(id, email, address) @address = address end -end \ No newline at end of file + + def self.all + @customers = [] + customer_array = CSV.read("../data/customers.csv").map(&:to_a) + binding.pry + customer_array.each do |id, email, address| + @id = customer_array.shift.shift + @customers << @id + email = customer_array.shift.shift + @email = email.to_s + @customers << @email + address = {street: customer_array[0], + city: customer_array[1], + state: customer_array[2], + zip: customer_array[3]} + @address = address + @customers << @address + end + return @customers + end +end + + + + +Customer.all From f3741f8184823e58e6b2f61856c391a65eb6def1 Mon Sep 17 00:00:00 2001 From: emaust Date: Sat, 24 Aug 2019 20:34:56 -0700 Subject: [PATCH 4/8] customer array with appropriate data types --- lib/customer.rb | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/lib/customer.rb b/lib/customer.rb index fb9273e2..789ebd15 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -19,27 +19,28 @@ def initialize(id, email, address) def self.all - @customers = [] customer_array = CSV.read("../data/customers.csv").map(&:to_a) - binding.pry - customer_array.each do |id, email, address| - @id = customer_array.shift.shift - @customers << @id - email = customer_array.shift.shift - @email = email.to_s - @customers << @email - address = {street: customer_array[0], - city: customer_array[1], - state: customer_array[2], - zip: customer_array[3]} - @address = address - @customers << @address - end - return @customers - end + @customers = customer_array.map do |index| + customer_info = [] + @id = index[0].to_i + customer_info << @id + @email = index[1] + customer_info << @email + @address = { + street: index[2], + city: index[3], + state: index[4], + zip: index[5] + } + customer_info << @address + end + + binding.pry + end - +end Customer.all + From 1915f837e24d324436ef4ba68a3bc3ce70d7c199 Mon Sep 17 00:00:00 2001 From: emaust Date: Sun, 25 Aug 2019 18:54:24 -0700 Subject: [PATCH 5/8] Wave 2: products hash working --- lib/order.rb | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/lib/order.rb b/lib/order.rb index 2477d90a..514437f4 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -1,4 +1,7 @@ require 'pry' +require 'csv' +require 'awesome_print' + class Order attr_reader :id attr_accessor :products, :customer, :fulfillment_status @@ -50,8 +53,56 @@ def total taxed = sub_total.to_f * 1.075 return taxed.round(2) end + + # def self.all(id, products, customer, fulfillment_status) + + + + + # orders = csv_data.map do |order| + + def self.all + + csv_data = CSV.read("data/orders.csv").map(&:to_a) + csv_data.each do |order| + + isolate_order_products = [] + organized_products = [] + + id = order[0].to_i + isolate_order_products = order.slice(1) + customer_id = order[2].to_i + fulfillment_status = order[3].to_sym + + single_product = isolate_order_products.split(";") + + single_product.each do |index| + isolate_value = index.split(":") + item_hash = { + isolate_value[0] => isolate_value[1].to_f + } + organized_products << item_hash + end + + single_order = Order.new(id, organized_products, customer_id, fulfillment_status) + + total_orders << single_order + + end + return total_orders end +# id = order[0].to_i +# products = {} +# products = order[1] +# customer = order[2].to_i +# fulfullment_status = order[3] +# end + +# end +end + +# instance = Order.all # should have method called total # summing up the products # adding a 7.5% tax @@ -69,3 +120,4 @@ def total + From a66cd1adb8758e65bdacb2d22e1a25230df8f411 Mon Sep 17 00:00:00 2001 From: emaust Date: Sun, 25 Aug 2019 20:36:11 -0700 Subject: [PATCH 6/8] Restored after apocalypse - wave 1 complete, wave 2 self.all complete --- lib/customer.rb | 29 ++--- lib/order.rb | 19 +--- lib/order_prying.rb | 123 ++++++++++++++++++++ test/customer_test.rb | 42 +++---- test/order_test.rb | 258 +++++++++++++++++++++--------------------- 5 files changed, 290 insertions(+), 181 deletions(-) create mode 100644 lib/order_prying.rb diff --git a/lib/customer.rb b/lib/customer.rb index 789ebd15..e9d00a6d 100644 --- a/lib/customer.rb +++ b/lib/customer.rb @@ -19,28 +19,29 @@ def initialize(id, email, address) def self.all - customer_array = CSV.read("../data/customers.csv").map(&:to_a) - @customers = customer_array.map do |index| - customer_info = [] - @id = index[0].to_i - customer_info << @id - @email = index[1] - customer_info << @email - @address = { + csv_data = CSV.read("data/customers.csv") + customer_data = [] + + csv_data.each do |index| + id = index[0].to_i + email = index[1] + address = { street: index[2], city: index[3], state: index[4], zip: index[5] } - customer_info << @address + individual_customer = Customer.new(id, email, address) + customer_data << individual_customer end - binding.pry - + return customer_data end +def self.find(id) + + customer_database = self.all + return customer_database.find { |customer| customer.id == id } +end end - -Customer.all - diff --git a/lib/order.rb b/lib/order.rb index 514437f4..de39b101 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -6,10 +6,6 @@ class Order attr_reader :id attr_accessor :products, :customer, :fulfillment_status - # instance of Customer, the person who placed the order - # fulfillment_status, a symbol of :pending, :paid, :processing, :shipped, :complete - # if there is not fulfilment_status, default to :pending - # otherwise, ArgumentError should be raised def initialize(id, products, customer, fulfillment_status = :pending) @id = id @@ -92,22 +88,11 @@ def self.all return total_orders end -# id = order[0].to_i -# products = {} -# products = order[1] -# customer = order[2].to_i -# fulfullment_status = order[3] -# end +# def self.find -# end -end -# instance = Order.all -# should have method called total -# summing up the products -# adding a 7.5% tax -# rounding the result to two decimal places +end diff --git a/lib/order_prying.rb b/lib/order_prying.rb new file mode 100644 index 00000000..514437f4 --- /dev/null +++ b/lib/order_prying.rb @@ -0,0 +1,123 @@ +require 'pry' +require 'csv' +require 'awesome_print' + +class Order + attr_reader :id + attr_accessor :products, :customer, :fulfillment_status + + # instance of Customer, the person who placed the order + # fulfillment_status, a symbol of :pending, :paid, :processing, :shipped, :complete + # if there is not fulfilment_status, default to :pending + # otherwise, ArgumentError should be raised + + def initialize(id, products, customer, fulfillment_status = :pending) + @id = id + @products = products + @customer = customer + @fulfillment_status = fulfillment_status + + status_array = [:pending, :paid, :processing, :shipped, :complete] + + if id.is_a?(Integer) == false + raise ArgumentError + elsif + products.is_a?(Hash) == false + raise ArgumentError + elsif + customer.is_a?(Customer) == false + raise ArgumentError + elsif + !(status_array.include?(fulfillment_status)) + raise ArgumentError + end + + end + + def add_product(product_name, price) + if @products.keys.include?(product_name) + raise ArgumentError + else + @products[product_name] = price + end + return @products + end + + def total + cost_array = [] + @products.each do |product_name, cost| + cost_array << cost + end + + sub_total = cost_array.sum + taxed = sub_total.to_f * 1.075 + return taxed.round(2) + end + + # def self.all(id, products, customer, fulfillment_status) + + + + + # orders = csv_data.map do |order| + + def self.all + + csv_data = CSV.read("data/orders.csv").map(&:to_a) + csv_data.each do |order| + + isolate_order_products = [] + organized_products = [] + + id = order[0].to_i + isolate_order_products = order.slice(1) + customer_id = order[2].to_i + fulfillment_status = order[3].to_sym + + single_product = isolate_order_products.split(";") + + single_product.each do |index| + isolate_value = index.split(":") + item_hash = { + isolate_value[0] => isolate_value[1].to_f + } + organized_products << item_hash + end + + single_order = Order.new(id, organized_products, customer_id, fulfillment_status) + + total_orders << single_order + + end + return total_orders +end + +# id = order[0].to_i +# products = {} +# products = order[1] +# customer = order[2].to_i +# fulfullment_status = order[3] +# end + +# end +end + +# instance = Order.all +# should have method called total +# summing up the products +# adding a 7.5% tax +# rounding the result to two decimal places + + + + + + + + + + + + + + diff --git a/test/customer_test.rb b/test/customer_test.rb index defa3492..90b3d8ce 100644 --- a/test/customer_test.rb +++ b/test/customer_test.rb @@ -6,34 +6,34 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -xdescribe "Customer Wave 1" do +describe "Customer Wave 1" do ID = 123 EMAIL = "a@a.co" ADDRESS = { - street: "123 Main", - city: "Seattle", - state: "WA", - zip: "98101" - }.freeze + street: "123 Main", + city: "Seattle", + state: "WA", + zip: "98101" +}.freeze + +describe "#initialize" do +it "Takes an ID, email and address info" do + cust = Customer.new(ID, EMAIL, ADDRESS) - xdescribe "#initialize" do - it "Takes an ID, email and address info" do - cust = Customer.new(ID, EMAIL, ADDRESS) - - expect(cust).must_respond_to :id - expect(cust.id).must_equal ID - - expect(cust).must_respond_to :email - expect(cust.email).must_equal EMAIL - - expect(cust).must_respond_to :address - expect(cust.address).must_equal ADDRESS - end - end + expect(cust).must_respond_to :id + expect(cust.id).must_equal ID + + expect(cust).must_respond_to :email + expect(cust.email).must_equal EMAIL + + expect(cust).must_respond_to :address + expect(cust.address).must_equal ADDRESS +end +end 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 it "Returns an array of all customers" do customers = Customer.all diff --git a/test/order_test.rb b/test/order_test.rb index f0c3297d..682c210d 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -11,111 +11,111 @@ describe "Order Wave 1" do let(:customer) do address = { - street: "123 Main", - city: "Seattle", - state: "WA", - zip: "98101" - } - Customer.new(123, "a@a.co", address) - end + street: "123 Main", + city: "Seattle", + state: "WA", + zip: "98101" + } + Customer.new(123, "a@a.co", address) +end + +describe "#initialize" 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) - describe "#initialize" 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) - - expect(order).must_respond_to :id - expect(order.id).must_equal id - - expect(order).must_respond_to :products - expect(order.products.length).must_equal 0 - - expect(order).must_respond_to :customer - expect(order.customer).must_equal customer - - expect(order).must_respond_to :fulfillment_status - expect(order.fulfillment_status).must_equal fulfillment_status - end - - it "Accepts all legal statuses" do - valid_statuses = %i[pending paid processing shipped complete] - - valid_statuses.each do |fulfillment_status| - order = Order.new(1, {}, customer, fulfillment_status) - expect(order.fulfillment_status).must_equal fulfillment_status - end - end - - it "Uses pending if no fulfillment_status is supplied" do - order = Order.new(1, {}, customer) - expect(order.fulfillment_status).must_equal :pending - end - - it "Raises an ArgumentError for bogus statuses" do - bogus_statuses = [3, :bogus, 'pending', nil] - bogus_statuses.each do |fulfillment_status| - expect { - Order.new(1, {}, customer, fulfillment_status) - }.must_raise ArgumentError - end - end - end + expect(order).must_respond_to :id + expect(order.id).must_equal id - describe "#total" do - it "Returns the total from the collection of products" do - products = { "banana" => 1.99, "cracker" => 3.00 } - order = Order.new(1337, products, customer) - - expected_total = 5.36 - - expect(order.total).must_equal expected_total - end - - it "Returns a total of zero if there are no products" do - order = Order.new(1337, {}, customer) - - expect(order.total).must_equal 0 - end - end + expect(order).must_respond_to :products + expect(order.products.length).must_equal 0 - describe "#add_product" 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) - - order.add_product("salad", 4.25) - expected_count = before_count + 1 - expect(order.products.count).must_equal expected_count - end - - it "Is added to the collection of products" do - products = { "banana" => 1.99, "cracker" => 3.00 } - order = Order.new(1337, products, customer) - - order.add_product("sandwich", 4.25) - expect(order.products.include?("sandwich")).must_equal true - end - - it "Raises an ArgumentError if the product is already present" do - products = { "banana" => 1.99, "cracker" => 3.00 } - - order = Order.new(1337, products, customer) - before_total = order.total - - expect { - order.add_product("banana", 4.25) - }.must_raise ArgumentError - - # The list of products should not have been modified - expect(order.total).must_equal before_total - end + expect(order).must_respond_to :customer + expect(order.customer).must_equal customer + + expect(order).must_respond_to :fulfillment_status + expect(order.fulfillment_status).must_equal fulfillment_status +end + +it "Accepts all legal statuses" do + valid_statuses = %i[pending paid processing shipped complete] + + valid_statuses.each do |fulfillment_status| + order = Order.new(1, {}, customer, fulfillment_status) + expect(order.fulfillment_status).must_equal fulfillment_status end end +it "Uses pending if no fulfillment_status is supplied" do + order = Order.new(1, {}, customer) + expect(order.fulfillment_status).must_equal :pending +end + +it "Raises an ArgumentError for bogus statuses" do + bogus_statuses = [3, :bogus, 'pending', nil] + bogus_statuses.each do |fulfillment_status| + expect { + Order.new(1, {}, customer, fulfillment_status) + }.must_raise ArgumentError +end +end +end + +describe "#total" do +it "Returns the total from the collection of products" do + products = { "banana" => 1.99, "cracker" => 3.00 } + order = Order.new(1337, products, customer) + + expected_total = 5.36 + + expect(order.total).must_equal expected_total +end + +it "Returns a total of zero if there are no products" do + order = Order.new(1337, {}, customer) + + expect(order.total).must_equal 0 +end +end + +describe "#add_product" 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) + + order.add_product("salad", 4.25) + expected_count = before_count + 1 + expect(order.products.count).must_equal expected_count +end + +it "Is added to the collection of products" do + products = { "banana" => 1.99, "cracker" => 3.00 } + order = Order.new(1337, products, customer) + + order.add_product("sandwich", 4.25) + expect(order.products.include?("sandwich")).must_equal true +end + +it "Raises an ArgumentError if the product is already present" do + products = { "banana" => 1.99, "cracker" => 3.00 } + + order = Order.new(1337, products, customer) + before_total = order.total + + expect { + order.add_product("banana", 4.25) +}.must_raise ArgumentError + +# The list of products should not have been modified +expect(order.total).must_equal before_total +end +end +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! @@ -124,39 +124,39 @@ it "Returns accurate information about the first order" do id = 1 products = { - "Lobster" => 17.18, - "Annatto seed" => 58.38, - "Camomile" => 83.21 - } - customer_id = 25 - fulfillment_status = :complete - - order = Order.all.first - - # Check that all data was loaded as expected - expect(order.id).must_equal id - expect(order.products).must_equal products - expect(order.customer).must_be_kind_of Customer - expect(order.customer.id).must_equal customer_id - expect(order.fulfillment_status).must_equal fulfillment_status - end + "Lobster" => 17.18, + "Annatto seed" => 58.38, + "Camomile" => 83.21 + } + customer_id = 25 + fulfillment_status = :complete - it "Returns accurate information about the last order" do - # TODO: Your test code here! - end + order = Order.all.first + + # Check that all data was loaded as expected + expect(order.id).must_equal id + expect(order.products).must_equal products + expect(order.customer).must_be_kind_of Customer + expect(order.customer.id).must_equal customer_id + expect(order.fulfillment_status).must_equal fulfillment_status end - describe "Order.find" do - it "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 - # TODO: Your test code here! - end - - it "Returns nil for an order that doesn't exist" do - # TODO: Your test code here! - end + xit "Returns accurate information about the last order" do + # TODO: Your test code here! end end + +describe "Order.find" do + it "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 + # TODO: Your test code here! + end + + it "Returns nil for an order that doesn't exist" do + # TODO: Your test code here! + end +end +end From c60d7ddd4aab5aa3418263a50f1ab10f70d58e70 Mon Sep 17 00:00:00 2001 From: emaust Date: Sun, 25 Aug 2019 23:34:33 -0700 Subject: [PATCH 7/8] Both waves passing, need to write last tests --- lib/order.rb | 52 +++++++++++++++++++++------------------------- test/order_test.rb | 25 ++++++++++++++++++---- 2 files changed, 45 insertions(+), 32 deletions(-) diff --git a/lib/order.rb b/lib/order.rb index de39b101..bef3bc81 100644 --- a/lib/order.rb +++ b/lib/order.rb @@ -20,9 +20,9 @@ def initialize(id, products, customer, fulfillment_status = :pending) elsif products.is_a?(Hash) == false raise ArgumentError - elsif - customer.is_a?(Customer) == false - raise ArgumentError + # elsif + # customer.is_a?(Integer) == false + # raise ArgumentError elsif !(status_array.include?(fulfillment_status)) raise ArgumentError @@ -58,40 +58,40 @@ def total # orders = csv_data.map do |order| def self.all + total_orders = [] - csv_data = CSV.read("data/orders.csv").map(&:to_a) + csv_data = CSV.read("data/orders.csv") csv_data.each do |order| - isolate_order_products = [] - organized_products = [] - + product_hash = {} id = order[0].to_i isolate_order_products = order.slice(1) customer_id = order[2].to_i fulfillment_status = order[3].to_sym single_product = isolate_order_products.split(";") - single_product.each do |index| isolate_value = index.split(":") - item_hash = { - isolate_value[0] => isolate_value[1].to_f - } - organized_products << item_hash + product_hash.store( + isolate_value[0], isolate_value[1].to_f + ) + end + customer = Customer.find(customer_id) + + single_order = Order.new(id, product_hash, customer, fulfillment_status) + total_orders << single_order + + end - - single_order = Order.new(id, organized_products, customer_id, fulfillment_status) - - total_orders << single_order - + return total_orders end - return total_orders -end - -# def self.find - - - + + + def self.find(id) + order_database = self.all + return order_database.find { |order| order.id == id } + end + end @@ -102,7 +102,3 @@ def self.all - - - - diff --git a/test/order_test.rb b/test/order_test.rb index 682c210d..cfe86467 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -119,6 +119,8 @@ 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 end it "Returns accurate information about the first order" do @@ -141,12 +143,27 @@ expect(order.fulfillment_status).must_equal fulfillment_status end - xit "Returns accurate information about the last order" do - # TODO: Your test code here! - end + it "Returns accurate information about the last order" do + id = 100 + products = { + "Amaranth" => 83.81, + "Smoked Trout" => 70.6, + "Cheddar" => 5.63 + } + customer_id = 20 + fulfillment_status = :pending + + order = Order.all.last + + expect(order.id).must_equal id + expect(order.products).must_equal products + expect(order.customer).must_be_kind_of Customer + expect(order.customer.id).must_equal customer_id + expect(order.fulfillment_status).must_equal fulfillment_status +end end -describe "Order.find" do +xdescribe "Order.find" do it "Can find the first order from the CSV" do # TODO: Your test code here! end From bcce2ac639857d363e9d6a1307435d619e8b6458 Mon Sep 17 00:00:00 2001 From: emaust Date: Sun, 25 Aug 2019 23:41:46 -0700 Subject: [PATCH 8/8] Wave 1 and 2 completed - all tests pass --- test/order_test.rb | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/test/order_test.rb b/test/order_test.rb index cfe86467..fdbb5b4c 100644 --- a/test/order_test.rb +++ b/test/order_test.rb @@ -163,17 +163,22 @@ end end -xdescribe "Order.find" do +describe "Order.find" do it "Can find the first order from the CSV" do - # TODO: Your test code here! + first = Order.find(1) + + expect(first).must_be_kind_of Order + expect(first.id).must_equal 1 end - it "Can find the last order from the CSV" do - # TODO: Your test code here! + last = Order.find(100) + + expect(last).must_be_kind_of Order + expect(last.id).must_equal 100 end it "Returns nil for an order that doesn't exist" do - # TODO: Your test code here! + expect(Order.find(64087)).must_be_nil end end end