From 80348150cf45b9c64fd10396e617cc569f93c266 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Tue, 6 Sep 2016 10:54:59 -0700 Subject: [PATCH 01/17] Initial file structure (and minor change to README --- README.md | 6 ++++-- lib/market.rb | 0 lib/product.rb | 0 lib/sale.rb | 0 lib/vendor.rb | 0 specs/market_spec.rb | 0 specs/product_spec.rb | 0 specs/sale_spec.rb | 0 specs/vendor_spec.rb | 0 9 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 lib/market.rb create mode 100644 lib/product.rb create mode 100644 lib/sale.rb create mode 100644 lib/vendor.rb create mode 100644 specs/market_spec.rb create mode 100644 specs/product_spec.rb create mode 100644 specs/sale_spec.rb create mode 100644 specs/vendor_spec.rb diff --git a/README.md b/README.md index 576e36ae..d74403da 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ # FarMar: The Farmers' Market Finder In this assignment we will be creating an application to look up farmers markets and their related vendors, products, and sales. We will use __CSV__ files as our _database_. + [Baseline](#baseline) + ## Learning Goals - Reinforce and practice all of the Ruby and programming concepts we've covered in class - Practice writing specs and using TDD @@ -81,7 +83,7 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id 5. Product_id - (Fixnum) a reference to which product was sold ## Requirements -### Baseline +### Baseline #### Project Setup 1. You'll be working as an individual on this project. 1. Fork the Ada-C6 repo to your Github account. @@ -154,4 +156,4 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id #### Try some inheritance or some composition - __Inheritance:__ Create a new _class_ that defines the shared/duplicated methods (i.e., `find`, `all`). Update your data classes to _inherit_ this _class_ . -- __Composition with a Mixin:__ Create a new _module_ that defines the duplicated methods (i.e., `find`, `all`). Update your data classes to _mixin_ this _module_. +- __Composition with a Mixin:__ Create a new _module_ that defines the duplicated methods (i.e., `find`, `all`). Update your data classes to _mixin_ this _module_. diff --git a/lib/market.rb b/lib/market.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/product.rb b/lib/product.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/sale.rb b/lib/sale.rb new file mode 100644 index 00000000..e69de29b diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/market_spec.rb b/specs/market_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..e69de29b diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..e69de29b From ca5bbf003c2c06500217a42abb605886909425f8 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Tue, 6 Sep 2016 11:32:37 -0700 Subject: [PATCH 02/17] Filestructure to baseline reqs --- .gitignore | 35 +++++++++++++++++++++++++++++++++++ README.md | 5 +++-- Rakefile | 7 +++++++ far_mar.rb | 6 ++++++ lib/market.rb | 8 ++++++++ lib/product.rb | 7 +++++++ lib/sale.rb | 7 +++++++ lib/vendor.rb | 7 +++++++ specs/market_spec.rb | 21 +++++++++++++++++++++ specs/spec_helper.rb | 9 +++++++++ 10 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Rakefile create mode 100644 far_mar.rb create mode 100644 specs/spec_helper.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..28f48498 --- /dev/null +++ b/.gitignore @@ -0,0 +1,35 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/test/tmp/ +/test/version_tmp/ +/tmp/ + +## Specific to RubyMotion: +.dat* +.repl_history +build/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalisation: +/.bundle/ +/vendor/bundle +/lib/bundler/man/ + +# for a library or gem, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# Gemfile.lock +# .ruby-version +# .ruby-gemset + +# unless supporting rvm < 1.11.0 or doing something fancy, ignore this: +.rvmrc diff --git a/README.md b/README.md index d74403da..eee8c893 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ In this assignment we will be creating an application to look up farmers markets and their related vendors, products, and sales. We will use __CSV__ files as our _database_. [Baseline](#baseline) + [Primary](#primary) ## Learning Goals - Reinforce and practice all of the Ruby and programming concepts we've covered in class @@ -83,7 +84,7 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id 5. Product_id - (Fixnum) a reference to which product was sold ## Requirements -### Baseline +### Baseline #### Project Setup 1. You'll be working as an individual on this project. 1. Fork the Ada-C6 repo to your Github account. @@ -103,7 +104,7 @@ Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id - Complete the boilerplate necessary for testing. You should be able to `$ rake` from the project root to run your specs. Have at least one spec to verify this setup before submitting your baseline. - **Once you have completed your baseline, you must submit a pull-request and get it approved by an instructor.** -## Primary Requirements +## Primary Requirements ### For each of the data classes build the following methods: 1. `self.all`: returns a collection of instances, representing all of the objects described in the CSV 1. `self.find(id)`: returns an instance of the object where the value of the `id` field in the CSV matches the passed parameter. diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..82e5a661 --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] + end + +task default: :test diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..0a4989b1 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,6 @@ +require_relative "lib/market.rb" +require_relative "lib/vendor.rb" +require_relative "lib/product.rb" +require_relative "lib/sale.rb" +module FarMar +end diff --git a/lib/market.rb b/lib/market.rb index e69de29b..93dcd688 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -0,0 +1,8 @@ +require 'csv' +#require_relative "../support/markets.csv" #new to me, @todo test it works without, does not work incl +module FarMar + class Market + end + + +end diff --git a/lib/product.rb b/lib/product.rb index e69de29b..cd1a38b8 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -0,0 +1,7 @@ +require 'csv' +module FarMar + class Product + end + + +end diff --git a/lib/sale.rb b/lib/sale.rb index e69de29b..031e0bf1 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -0,0 +1,7 @@ +require 'csv' +module FarMar + class Sale + end + + +end diff --git a/lib/vendor.rb b/lib/vendor.rb index e69de29b..74189272 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -0,0 +1,7 @@ +require 'csv' +module FarMar + class Vendor + end + + +end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index e69de29b..b3f6aec4 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -0,0 +1,21 @@ +require_relative "../lib/market.rb" +require_relative 'spec_helper' +module FarMar + + describe Market do + # describe "#firstmethodprobably init" do + # let(:market) { Market.new(:hash_maybe) } + # + # it "jskdsjdksd" do + # + # + # end + + + # end + + + end + + +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..3f5ec15b --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,9 @@ +require 'simplecov' +SimpleCov.start +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From a7e76e673eb4e62ba1439d16f3f83522f2e307a7 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Tue, 6 Sep 2016 11:49:13 -0700 Subject: [PATCH 03/17] Correction of baseline reqs --- specs/product_spec.rb | 21 +++++++++++++++++++++ specs/sale_spec.rb | 21 +++++++++++++++++++++ specs/vendor_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/specs/product_spec.rb b/specs/product_spec.rb index e69de29b..68314498 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -0,0 +1,21 @@ +require_relative "../lib/product.rb" +require_relative 'spec_helper' +module FarMar + + describe Product do + # describe "#firstmethodprobably init" do + # let(:market) { Market.new(:hash_maybe) } + # + # it "jskdsjdksd" do + # + # + # end + + + # end + + + end + + +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index e69de29b..c8f518dc 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -0,0 +1,21 @@ +require_relative "../lib/sale.rb" +require_relative 'spec_helper' +module FarMar + + describe Sale do + # describe "#firstmethodprobably init" do + # let(:market) { Market.new(:hash_maybe) } + # + # it "jskdsjdksd" do + # + # + # end + + + # end + + + end + + +end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index e69de29b..ad726fb8 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -0,0 +1,21 @@ +require_relative "../lib/vendor.rb" +require_relative 'spec_helper' +module FarMar + + describe Vendor do + # describe "#firstmethodprobably init" do + # let(:market) { Market.new(:hash_maybe) } + # + # it "jskdsjdksd" do + # + # + # end + + + # end + + + end + + +end From e8862c69e6c755708502aedb80d50aa3143feeca Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Tue, 6 Sep 2016 16:07:52 -0700 Subject: [PATCH 04/17] self.all and .find tested and passed for Market class --- README.md | 17 ++++++--- far_mar.rb | 1 + lib/market.rb | 37 ++++++++++++++++-- lib/product.rb | 1 - lib/sale.rb | 1 - lib/vendor.rb | 1 - specs/market_spec.rb | 56 ++++++++++++++++++++++----- specs/product_spec.rb | 88 ++++++++++++++++++++++++++++++++----------- specs/sale_spec.rb | 1 - specs/spec_helper.rb | 1 + specs/vendor_spec.rb | 1 - 11 files changed, 161 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index eee8c893..233407ed 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,13 @@ # FarMar: The Farmers' Market Finder In this assignment we will be creating an application to look up farmers markets and their related vendors, products, and sales. We will use __CSV__ files as our _database_. - [Baseline](#baseline) - [Primary](#primary) + Project Data + [Markets](#markets) + [Vendors](#vendors) + [Products](#products) + [Sales](#sales) + [Baseline](#baseline) + [Primary](#primary) ## Learning Goals - Reinforce and practice all of the Ruby and programming concepts we've covered in class @@ -47,7 +52,7 @@ For example, `Sale` is a very generic _class_ name that could very realistically You must have __90% test coverage__ from `simplecov`. The HTML files that are generated from `simplecov` should _not_ be included in your git repository. Tests should be in the form of __minitest specs__. Complete the necessary boilerplate to create a `Rakefile` and `spec_helper.rb` so that all of your tests run when you run `$ rake` from the project root. ### Project Data -#### FarMar::Market +#### FarMar::Market Each individual market has many vendors associated with it. The `FarMar::Market` data, in order in the CSV, consists of: 1. ID - (Fixnum) a unique identifier for that market @@ -58,7 +63,7 @@ Each individual market has many vendors associated with it. The `FarMar::Market` 6. State - (String) state in which the market is located 7. Zip - (String) zipcode in which the market is located -#### FarMar::Vendor +#### FarMar::Vendor Each vendor belongs to a market, the `market_id` field refers to the `FarMar::Market` ID field. Each vendor has many products for sell. The `FarMar::Vendor` data, in order in the CSV, consists of: @@ -67,14 +72,14 @@ Each vendor has many products for sell. The `FarMar::Vendor` data, in order in t 3. No. of Employees - (Fixnum) How many employees the vendor has at the market 4. Market_id - (Fixnum) a reference to which market the vendor attends -#### FarMar::Product +#### FarMar::Sale Each sale belongs to a vendor __AND__ a product. The `vendor_id` and `product_id` fields refer to the `FarMar::Vendor` and `FarMar::Product` ID fields, respectively. The `FarMar::Sale` data, in order in the CSV, consists of: 1. ID - (Fixnum) uniquely identifies the sale diff --git a/far_mar.rb b/far_mar.rb index 0a4989b1..cb35342d 100644 --- a/far_mar.rb +++ b/far_mar.rb @@ -2,5 +2,6 @@ require_relative "lib/vendor.rb" require_relative "lib/product.rb" require_relative "lib/sale.rb" +require 'csv' module FarMar end diff --git a/lib/market.rb b/lib/market.rb index 93dcd688..1ecc864b 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -1,8 +1,39 @@ -require 'csv' -#require_relative "../support/markets.csv" #new to me, @todo test it works without, does not work incl module FarMar class Market - end + attr_reader :id, :name, :address, :city, :county, :state, :zipcode + + def initialize(id, name, address, city, county, state, zipcode) + @id = id + @name = name + @address = address + @city = city + @county = county + @state = state + @zipcode = zipcode + end + + def self.all + markets = {} + CSV.read('support/markets.csv').each do |line| + market = self.new(line[0],line[1],line[2],line[3],line[4],line[5],line[6]) + markets[market.id] = market + end + return markets + end + def self.find(id) + markets = self.all + return markets[id] + end + + end end + +# # From BANK ACCOUNT PROJECT +# +# # Method returns an instance of Account where the value of the id field in the CSV matches the passed parameter +# def self.find(id) +# accounts = self.all +# return accounts[id] +# end diff --git a/lib/product.rb b/lib/product.rb index cd1a38b8..4de1ad3b 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -1,4 +1,3 @@ -require 'csv' module FarMar class Product end diff --git a/lib/sale.rb b/lib/sale.rb index 031e0bf1..31eb1000 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,4 +1,3 @@ -require 'csv' module FarMar class Sale end diff --git a/lib/vendor.rb b/lib/vendor.rb index 74189272..759325e0 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -1,4 +1,3 @@ -require 'csv' module FarMar class Vendor end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index b3f6aec4..d943dbc4 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -1,18 +1,56 @@ -require_relative "../lib/market.rb" require_relative 'spec_helper' module FarMar describe Market do - # describe "#firstmethodprobably init" do - # let(:market) { Market.new(:hash_maybe) } - # - # it "jskdsjdksd" do - # - # - # end + describe "#initialize" do + let(:market) { Market.new(:id, :name, :address, :city, :county, :state, :zipcode) } + it "can create an instance of Market" do + market.must_be_instance_of(Market) + end + it "must respond to (have parameters of) of market information" do + market.must_respond_to(:id) + market.must_respond_to(:name) + market.must_respond_to(:address) + market.must_respond_to(:city) + market.must_respond_to(:county) + market.must_respond_to(:state) + market.must_respond_to(:zipcode) + end + end - # end + describe "#self.all" do + + it "should return a hash" do + Market.all.must_be_instance_of(Hash) + end + it "should return information about markets" do + # @todo unsure if this is the way to test reading off csv files, or if even needed + # first listed market + Market.all["1"].id.must_equal("1") + Market.all["1"].name.must_equal("People's Co-op Farmers Market") + Market.all["1"].address.must_equal("30th and Burnside") + Market.all["1"].city.must_equal("Portland") + Market.all["1"].county.must_equal("Multnomah") + Market.all["1"].state.must_equal("Oregon") + Market.all["1"].zipcode.must_equal("97202") + # last listed market + Market.all["500"].id.must_equal("500") + Market.all["500"].name.must_equal("Montefiore Medical Center Farmers Market_Thursday") + Market.all["500"].address.must_equal("111 E. 210th Street") + Market.all["500"].city.must_equal("Bronx") + Market.all["500"].county.must_equal("Bronx") + Market.all["500"].state.must_equal("New York") + Market.all["500"].zipcode.must_equal("10467") + end + end #self.all + describe "#self.find(id)" do + it "should return an instance of a Market object of a certain id" do + random_market_id = rand(1..500).to_s + Market.find(random_market_id).must_be_instance_of(Market) + Market.find(random_market_id).id.must_equal(random_market_id) + end + end #self.find(id) end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 68314498..e61fb4a9 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -1,21 +1,67 @@ -require_relative "../lib/product.rb" -require_relative 'spec_helper' -module FarMar - - describe Product do - # describe "#firstmethodprobably init" do - # let(:market) { Market.new(:hash_maybe) } - # - # it "jskdsjdksd" do - # - # - # end - - - # end - - - end - - -end +# require_relative 'spec_helper' +# module FarMar +# +# describe Product do +# +# describe "#initialize" do +# let(:product) { Product.new(:id, :name, :no_of_employees, :market_id) } +# +# # ID - (Fixnum) uniquely identifies the vendor +# # Name - (String) the name of the vendor (not guaranteed unique) +# # No. of Employees - (Fixnum) How many employees the vendor has at the market +# # Market_id - (Fixnum) a reference to which market the vendor attends +# it "can create an instance of Market" do +# market.must_be_instance_of(Market) +# end +# it "must respond to (have parameters of) of market information" do +# market.must_respond_to(:id) +# market.must_respond_to(:name) +# market.must_respond_to(:address) +# market.must_respond_to(:city) +# market.must_respond_to(:county) +# market.must_respond_to(:state) +# market.must_respond_to(:zipcode) +# end +# end +# +# describe "#self.all" do +# +# it "should return a hash" do +# Market.all.must_be_instance_of(Hash) +# end +# it "should return information about markets" do +# # @todo unsure if this is the way to test reading off csv files, or if even needed +# # first listed market +# Market.all["1"].id.must_equal("1") +# Market.all["1"].name.must_equal("People's Co-op Farmers Market") +# Market.all["1"].address.must_equal("30th and Burnside") +# Market.all["1"].city.must_equal("Portland") +# Market.all["1"].county.must_equal("Multnomah") +# Market.all["1"].state.must_equal("Oregon") +# Market.all["1"].zipcode.must_equal("97202") +# # last listed market +# Market.all["500"].id.must_equal("500") +# Market.all["500"].name.must_equal("Montefiore Medical Center Farmers Market_Thursday") +# Market.all["500"].address.must_equal("111 E. 210th Street") +# Market.all["500"].city.must_equal("Bronx") +# Market.all["500"].county.must_equal("Bronx") +# Market.all["500"].state.must_equal("New York") +# Market.all["500"].zipcode.must_equal("10467") +# end +# end #self.all +# describe "#self.find(id)" do +# it "should return an instance of a Market object of a certain id" do +# random_market_id = rand(1..500).to_s +# Market.find(random_market_id).must_be_instance_of(Market) +# Market.find(random_market_id).id.must_equal(random_market_id) +# end +# end #self.find(id) +# +# +# +# +# end +# end +# +# +# diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index c8f518dc..162c6525 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -1,4 +1,3 @@ -require_relative "../lib/sale.rb" require_relative 'spec_helper' module FarMar diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 3f5ec15b..26ecbe4c 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,3 +1,4 @@ +require_relative '../far_mar' require 'simplecov' SimpleCov.start require 'minitest' diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index ad726fb8..07e1ff9b 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -1,4 +1,3 @@ -require_relative "../lib/vendor.rb" require_relative 'spec_helper' module FarMar From 9d61c53265f54f1350d689586bb1a448e68816d9 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Tue, 6 Sep 2016 17:27:54 -0700 Subject: [PATCH 05/17] minor changes, .all and .find(id) in classes Vendor/Product, not completed --- lib/market.rb | 2 +- lib/product.rb | 27 ++++++++++ lib/vendor.rb | 22 ++++++++ specs/market_spec.rb | 31 ++++++----- specs/product_spec.rb | 119 ++++++++++++++++++------------------------ specs/vendor_spec.rb | 54 ++++++++++++++++--- 6 files changed, 163 insertions(+), 92 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index 1ecc864b..9f83722e 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -16,7 +16,7 @@ def initialize(id, name, address, city, county, state, zipcode) def self.all markets = {} CSV.read('support/markets.csv').each do |line| - market = self.new(line[0],line[1],line[2],line[3],line[4],line[5],line[6]) + market = self.new(line[0].to_i,line[1],line[2],line[3],line[4],line[5],line[6]) markets[market.id] = market end return markets diff --git a/lib/product.rb b/lib/product.rb index 4de1ad3b..14001de5 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -1,5 +1,32 @@ module FarMar class Product + attr_reader :id, :name, :vendor_id + def initialize(id,name,vendor_id) + @id = id + @name = name + @vendor_id = vendor_id + end + def self.all + products = {} + CSV.read('support/products.csv').each do |line| + product = self.new(line[0].to_i,line[1],line[2].to_i) + products[product.id] = product + end + return products + end + def self.find(id) + products = self.all + return products[id] + end + + + + + + + + + end diff --git a/lib/vendor.rb b/lib/vendor.rb index 759325e0..dbb325bd 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -1,5 +1,27 @@ module FarMar class Vendor + attr_reader :id, :name, :number_of_employees, :market_id + def initialize(id,name,number_of_employees,market_id) + @id = id + @name = name + @number_of_employees = number_of_employees + @market_id = market_id + end + def self.all + vendors = {} + CSV.read('support/vendors.csv').each do |line| + vendor = self.new(line[0].to_i,line[1],line[2].to_i,line[3].to_i) + vendors[vendor.id] = vendor + end + return vendors + end + def self.find(id) + vendors = self.all + return vendors[id] + end + + + end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index d943dbc4..31130d0b 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -25,28 +25,27 @@ module FarMar Market.all.must_be_instance_of(Hash) end it "should return information about markets" do - # @todo unsure if this is the way to test reading off csv files, or if even needed # first listed market - Market.all["1"].id.must_equal("1") - Market.all["1"].name.must_equal("People's Co-op Farmers Market") - Market.all["1"].address.must_equal("30th and Burnside") - Market.all["1"].city.must_equal("Portland") - Market.all["1"].county.must_equal("Multnomah") - Market.all["1"].state.must_equal("Oregon") - Market.all["1"].zipcode.must_equal("97202") + Market.all[1].id.must_equal(1) + Market.all[1].name.must_equal("People's Co-op Farmers Market") + Market.all[1].address.must_equal("30th and Burnside") + Market.all[1].city.must_equal("Portland") + Market.all[1].county.must_equal("Multnomah") + Market.all[1].state.must_equal("Oregon") + Market.all[1].zipcode.must_equal("97202") # last listed market - Market.all["500"].id.must_equal("500") - Market.all["500"].name.must_equal("Montefiore Medical Center Farmers Market_Thursday") - Market.all["500"].address.must_equal("111 E. 210th Street") - Market.all["500"].city.must_equal("Bronx") - Market.all["500"].county.must_equal("Bronx") - Market.all["500"].state.must_equal("New York") - Market.all["500"].zipcode.must_equal("10467") + Market.all[500].id.must_equal(500) + Market.all[500].name.must_equal("Montefiore Medical Center Farmers Market_Thursday") + Market.all[500].address.must_equal("111 E. 210th Street") + Market.all[500].city.must_equal("Bronx") + Market.all[500].county.must_equal("Bronx") + Market.all[500].state.must_equal("New York") + Market.all[500].zipcode.must_equal("10467") end end #self.all describe "#self.find(id)" do it "should return an instance of a Market object of a certain id" do - random_market_id = rand(1..500).to_s + random_market_id = rand(1..500) Market.find(random_market_id).must_be_instance_of(Market) Market.find(random_market_id).id.must_equal(random_market_id) end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index e61fb4a9..98acc0bf 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -1,67 +1,52 @@ -# require_relative 'spec_helper' -# module FarMar -# -# describe Product do -# -# describe "#initialize" do -# let(:product) { Product.new(:id, :name, :no_of_employees, :market_id) } -# -# # ID - (Fixnum) uniquely identifies the vendor -# # Name - (String) the name of the vendor (not guaranteed unique) -# # No. of Employees - (Fixnum) How many employees the vendor has at the market -# # Market_id - (Fixnum) a reference to which market the vendor attends -# it "can create an instance of Market" do -# market.must_be_instance_of(Market) -# end -# it "must respond to (have parameters of) of market information" do -# market.must_respond_to(:id) -# market.must_respond_to(:name) -# market.must_respond_to(:address) -# market.must_respond_to(:city) -# market.must_respond_to(:county) -# market.must_respond_to(:state) -# market.must_respond_to(:zipcode) -# end -# end -# -# describe "#self.all" do -# -# it "should return a hash" do -# Market.all.must_be_instance_of(Hash) -# end -# it "should return information about markets" do -# # @todo unsure if this is the way to test reading off csv files, or if even needed -# # first listed market -# Market.all["1"].id.must_equal("1") -# Market.all["1"].name.must_equal("People's Co-op Farmers Market") -# Market.all["1"].address.must_equal("30th and Burnside") -# Market.all["1"].city.must_equal("Portland") -# Market.all["1"].county.must_equal("Multnomah") -# Market.all["1"].state.must_equal("Oregon") -# Market.all["1"].zipcode.must_equal("97202") -# # last listed market -# Market.all["500"].id.must_equal("500") -# Market.all["500"].name.must_equal("Montefiore Medical Center Farmers Market_Thursday") -# Market.all["500"].address.must_equal("111 E. 210th Street") -# Market.all["500"].city.must_equal("Bronx") -# Market.all["500"].county.must_equal("Bronx") -# Market.all["500"].state.must_equal("New York") -# Market.all["500"].zipcode.must_equal("10467") -# end -# end #self.all -# describe "#self.find(id)" do -# it "should return an instance of a Market object of a certain id" do -# random_market_id = rand(1..500).to_s -# Market.find(random_market_id).must_be_instance_of(Market) -# Market.find(random_market_id).id.must_equal(random_market_id) -# end -# end #self.find(id) -# -# -# -# -# end -# end -# -# -# +require_relative 'spec_helper' +module FarMar + + describe Product do + + describe "#initialize" do + let(:product) { Product.new(:id, :name, :vendor_id) } + + # ID - (Fixnum) uniquely identifies the product + # Name - (String) the name of the product (not guaranteed unique) + # Vendor_id - (Fixnum) a reference to which vendor sells this product + + it "can create an instance of Product" do + product.must_be_instance_of(Product) + end + it "must respond to (have parameters of) of Product information" do + product.must_respond_to(:id) + product.must_respond_to(:name) + product.must_respond_to(:vendor_id) + end + end + + describe "#self.all" do + + it "should return a hash" do + Product.all.must_be_instance_of(Hash) + end + it "should return information about markets" do + # first listed product + Product.all[1].id.must_equal(1) + Product.all[1].name.must_equal("Dry Beets") + Product.all[1].vendor_id.must_equal(1) + # last listed product + Product.all[8193].id.must_equal(8193) + Product.all[8193].name.must_equal("Cruel Beef") + Product.all[8193].vendor_id.must_equal(2690) + end + end #self.all +#@todo START HERE START HERE START HERE START HERE + # describe "#self.find(id)" do + # it "should return an instance of a Product object of a certain id" do + # random_product_id = rand(1..8193).to_s + # Product.find(random_product_id).must_be_instance_of(Product) + # Product.find(random_product_id).id.must_equal(random_product_id) + # end + # end #self.find(id) + + + + + end +end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 07e1ff9b..c22b97b3 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -2,16 +2,54 @@ module FarMar describe Vendor do - # describe "#firstmethodprobably init" do - # let(:market) { Market.new(:hash_maybe) } - # - # it "jskdsjdksd" do - # - # - # end + describe "#initialize" do + let(:vendor) { Vendor.new(:id, :name, :number_of_employees, :market_id) } + # Remember the datatypes! + # ID - (Fixnum) uniquely identifies the vendor + # Name - (String) the name of the vendor (not guaranteed unique) + # No. of Employees - (Fixnum) How many employees the vendor has at the market + # Market_id - (Fixnum) a reference to which market the vendor attends - # end + it "can create an instance of Vendor" do + vendor.must_be_instance_of(Vendor) + end + # test below passed once there was attr_readers, instance variables not needed + it "must respond to (have parameters of) of vendor information" do + vendor.must_respond_to(:id) + vendor.must_respond_to(:name) + vendor.must_respond_to(:number_of_employees) + vendor.must_respond_to(:market_id) + end + end + + describe "#self.all" do + + it "should return a hash" do + Vendor.all.must_be_instance_of(Hash) + end + it "should return information about vendors" do + # first listed vendor + puts Vendor.all[1] + Vendor.all[1].id.must_equal(1) + Vendor.all[1].name.must_equal("Feil-Farrell") + Vendor.all[1].number_of_employees.must_equal(8) + Vendor.all[1].market_id.must_equal(1) + + # last listed vendor + Vendor.all[2690].id.must_equal(2690) + Vendor.all[2690].name.must_equal("Mann-Lueilwitz") + Vendor.all[2690].number_of_employees.must_equal(4) + Vendor.all[2690].market_id.must_equal(500) + end + end #self.all + describe "#self.find(id)" do + it "should return an instance of a Vendor object of a certain id" do + random_vendor_id = rand(1..2690) + Vendor.find(random_vendor_id).must_be_instance_of(Vendor) + Vendor.find(random_vendor_id).id.must_equal(random_vendor_id) + end + end #self.find(id) end From b16954ca6bfe203b48eeb47dcafd6af0a2dd0976 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Tue, 6 Sep 2016 22:58:54 -0700 Subject: [PATCH 06/17] Fixed trouble Product class. All classes have .all and .self that pass tests --- specs/product_spec.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 98acc0bf..72598b1b 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -36,14 +36,13 @@ module FarMar Product.all[8193].vendor_id.must_equal(2690) end end #self.all -#@todo START HERE START HERE START HERE START HERE - # describe "#self.find(id)" do - # it "should return an instance of a Product object of a certain id" do - # random_product_id = rand(1..8193).to_s - # Product.find(random_product_id).must_be_instance_of(Product) - # Product.find(random_product_id).id.must_equal(random_product_id) - # end - # end #self.find(id) + describe "#self.find(id)" do + it "should return an instance of a Product object of a certain id" do + random_product_id = rand(1..8193) + Product.find(random_product_id).must_be_instance_of(Product) + Product.find(random_product_id).id.must_equal(random_product_id) + end + end #self.find(id) From 09568fd6c8dcf1024ec43ffd867705b987c2180a Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Wed, 7 Sep 2016 15:51:05 -0700 Subject: [PATCH 07/17] struggling with #vendor --- README.md | 14 +++++++------- lib/market.rb | 17 +++++++---------- specs/market_spec.rb | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 233407ed..d9a302d6 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # FarMar: The Farmers' Market Finder In this assignment we will be creating an application to look up farmers markets and their related vendors, products, and sales. We will use __CSV__ files as our _database_. - Project Data - [Markets](#markets) - [Vendors](#vendors) - [Products](#products) - [Sales](#sales) - [Baseline](#baseline) - [Primary](#primary) + Project Data + [Markets](#markets) + [Vendors](#vendors) + [Products](#products) + [Sales](#sales) + [Baseline](#baseline) + [Primary](#primary) ## Learning Goals - Reinforce and practice all of the Ruby and programming concepts we've covered in class diff --git a/lib/market.rb b/lib/market.rb index 9f83722e..f35b1a18 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -16,6 +16,7 @@ def initialize(id, name, address, city, county, state, zipcode) def self.all markets = {} CSV.read('support/markets.csv').each do |line| + #these are the arguments fed into the class instance -don't confuse with an array market = self.new(line[0].to_i,line[1],line[2],line[3],line[4],line[5],line[6]) markets[market.id] = market end @@ -26,14 +27,10 @@ def self.find(id) return markets[id] end + def vendors + hash_of_vendors = Vendor.all + return hash_of_vendors + end - end -end - -# # From BANK ACCOUNT PROJECT -# -# # Method returns an instance of Account where the value of the id field in the CSV matches the passed parameter -# def self.find(id) -# accounts = self.all -# return accounts[id] -# end + end #class +end #module diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 31130d0b..2d0b30be 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -51,6 +51,27 @@ module FarMar end end #self.find(id) + describe "#vendors" do + before(:each) do + @market = Market.new(:id, :name, :address, :city, :county, :state, :zipcode) + end + it "should return a collection of hash" do + skip + @market.vendors.each do |vendor| + vendor.must_be_instance_of(Hash) + end + # it "should return a collection of instances of Vendor" do + # @market.vendors.each do |vendor| + # puts "#{vendor} and more" #@todo help help + # vendor.must_be_instance_of(Vendor) + # end + + + end + it "should request vendors associated with itself (its instance of Market)" do + skip + end + end end From 6d729298671ee79dda3b086b23fbc7a4f49e9f04 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Wed, 7 Sep 2016 16:18:08 -0700 Subject: [PATCH 08/17] now understand to test in the method that need testing rather than method that refers to the method that needed testing --- specs/market_spec.rb | 23 +++++++---------------- specs/vendor_spec.rb | 16 ++++++---------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 2d0b30be..865b7f06 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -55,25 +55,16 @@ module FarMar before(:each) do @market = Market.new(:id, :name, :address, :city, :county, :state, :zipcode) end - it "should return a collection of hash" do - skip - @market.vendors.each do |vendor| - vendor.must_be_instance_of(Hash) + it "should request vendors associated with itself (its instance of Market)" do + @market.vendors.each do |key, vendor| + puts @market.id # @todo testputs + vendor.market_id.must_equal(@market.id) end - # it "should return a collection of instances of Vendor" do - # @market.vendors.each do |vendor| - # puts "#{vendor} and more" #@todo help help - # vendor.must_be_instance_of(Vendor) - # end - end - it "should request vendors associated with itself (its instance of Market)" do - skip - end - end + end #vendors - end + end #market -end +end #module diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index c22b97b3..3929031a 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -4,13 +4,6 @@ module FarMar describe Vendor do describe "#initialize" do let(:vendor) { Vendor.new(:id, :name, :number_of_employees, :market_id) } - - # Remember the datatypes! - # ID - (Fixnum) uniquely identifies the vendor - # Name - (String) the name of the vendor (not guaranteed unique) - # No. of Employees - (Fixnum) How many employees the vendor has at the market - # Market_id - (Fixnum) a reference to which market the vendor attends - it "can create an instance of Vendor" do vendor.must_be_instance_of(Vendor) end @@ -28,9 +21,14 @@ module FarMar it "should return a hash" do Vendor.all.must_be_instance_of(Hash) end + it "should be a collection of Vendor objects" do + Vendor.all.each do |vendor_id, vendor| + vendor_id.must_equal(vendor.id) + vendor.must_be_instance_of(Vendor) + end + end it "should return information about vendors" do # first listed vendor - puts Vendor.all[1] Vendor.all[1].id.must_equal(1) Vendor.all[1].name.must_equal("Feil-Farrell") Vendor.all[1].number_of_employees.must_equal(8) @@ -53,6 +51,4 @@ module FarMar end - - end From e5584c273cbf4d9f976df6a0adffc1bf9ec527fb Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Wed, 7 Sep 2016 16:58:35 -0700 Subject: [PATCH 09/17] getting Vendor methods done, failing #products test, @todo write Vendor method products --- lib/market.rb | 11 ++++++++--- lib/vendor.rb | 7 ++++++- specs/market_spec.rb | 5 +++-- specs/vendor_spec.rb | 22 ++++++++++++++++++++++ 4 files changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index f35b1a18..154f2710 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -28,9 +28,14 @@ def self.find(id) end def vendors - hash_of_vendors = Vendor.all - return hash_of_vendors - end + market_vendors = {} + Vendor.all.each { |vendor_id, vendor| + if vendor.market_id == id + market_vendors[vendor_id] = vendor + end + } + return market_vendors + end end #class end #module diff --git a/lib/vendor.rb b/lib/vendor.rb index dbb325bd..6c13a488 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -19,7 +19,12 @@ def self.find(id) vendors = self.all return vendors[id] end - + def market + return @market_id + end + def products + # @todo START HERE START HERE START HERE + end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 865b7f06..f2b0e967 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -56,8 +56,9 @@ module FarMar @market = Market.new(:id, :name, :address, :city, :county, :state, :zipcode) end it "should request vendors associated with itself (its instance of Market)" do - @market.vendors.each do |key, vendor| - puts @market.id # @todo testputs + #puts "VVVVVVV" # This puts fine + @market.vendors.each do |vendor_id, vendor| + # puts "#{vendor.market_id} is this market id" #this is not putsing?! vendor.market_id.must_equal(@market.id) end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 3929031a..8681db92 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -49,6 +49,28 @@ module FarMar end end #self.find(id) + describe "#market" do + before(:each) do + @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + end + it "should reutrn the Market instance that it is associated to" do + @vendor.market.must_equal(@vendor.market_id) + end + end + + describe "#products" do + before(:each) do + @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + end + #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. + it "should return a collection of Product instances associated to current vendor" do + @vendor.products.each do |product_id, product| + # puts "#{vendor.market_id} is this market id" #this is not putsing?! + product.vendor_id.must_equal(@vendor.id) + end + end + + end end end From 910a254ad1a3efd6f0c4160da85b04862cd838b4 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Thu, 8 Sep 2016 12:27:36 -0700 Subject: [PATCH 10/17] off-day yesterday causing problems today: working on Sale.all, probably have to rework all.all --- lib/product.rb | 10 ------ lib/sale.rb | 42 ++++++++++++++++++++++-- lib/vendor.rb | 22 ++++++++++++- specs/product_spec.rb | 6 ++-- specs/sale_spec.rb | 74 +++++++++++++++++++++++++++++++++++++++---- specs/vendor_spec.rb | 25 +++++++++++++-- 6 files changed, 154 insertions(+), 25 deletions(-) diff --git a/lib/product.rb b/lib/product.rb index 14001de5..ec9e5e41 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -19,15 +19,5 @@ def self.find(id) return products[id] end - - - - - - - - end - - end diff --git a/lib/sale.rb b/lib/sale.rb index 31eb1000..8a144bff 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,6 +1,44 @@ +# Remember: the datetime here loaded are not converted from string. Suggest require 'time' and Time.Parse(line[2]) + module FarMar class Sale - end +# ID - (Fixnum) uniquely identifies the sale +# Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) +# Purchase_time - (Datetime) when the sale was completed +# Vendor_id - (Fixnum) a reference to which vendor completed the sale +# Product_id - (Fixnum) a reference to which product was sold + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id + + def initialize(id, amount, purchase_time, vendor_id, product_id) + @id = id + @amount = amount # @todo program is not happy about this at the moment / 100 + @purchase_time = purchase_time + @vendor_id = vendor_id + @product_id = product_id + end + + # @todo it returns an array, not a hash + def self.all + sales = {} # @todo START HERE START HERE this is what all goes wrong + # CSV.read('support/sales.csv').each do |line| + # sale = self.new(line[0].to_i,line[1].to_f,line[2],line[3].to_i,line[4].to_i) + # sales[sale.id] = sale + # end + end + +# VENDOR + + + # def self.find(id) + # vendors = self.all + # return vendors[id] + # end + + + + + -end + end #class +end #module diff --git a/lib/vendor.rb b/lib/vendor.rb index 6c13a488..aa3bb44b 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -1,12 +1,14 @@ module FarMar class Vendor attr_reader :id, :name, :number_of_employees, :market_id + def initialize(id,name,number_of_employees,market_id) @id = id @name = name @number_of_employees = number_of_employees @market_id = market_id end + def self.all vendors = {} CSV.read('support/vendors.csv').each do |line| @@ -15,17 +17,35 @@ def self.all end return vendors end + def self.find(id) vendors = self.all return vendors[id] end + def market return @market_id end + def products - # @todo START HERE START HERE START HERE + vendor_products = {} + Product.all.each { |product_id, product| + if product.vendor_id == id + vendor_products[product_id] = product # creates a hash of products carried by specific vendor + end + } + return vendor_products end +# #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + def sales + vendor_sales = {} + Sale.all.each do |sale_id, sale| + if sale.vendor_id == id + vendor_sales[sale_id] = sale + end + end + end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 72598b1b..3ab330f3 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -1,3 +1,6 @@ +# ID - (Fixnum) uniquely identifies the product +# Name - (String) the name of the product (not guaranteed unique) +# Vendor_id - (Fixnum) a reference to which vendor sells this product require_relative 'spec_helper' module FarMar @@ -6,9 +9,6 @@ module FarMar describe "#initialize" do let(:product) { Product.new(:id, :name, :vendor_id) } - # ID - (Fixnum) uniquely identifies the product - # Name - (String) the name of the product (not guaranteed unique) - # Vendor_id - (Fixnum) a reference to which vendor sells this product it "can create an instance of Product" do product.must_be_instance_of(Product) diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 162c6525..12ea04f0 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -2,16 +2,78 @@ module FarMar describe Sale do - # describe "#firstmethodprobably init" do - # let(:market) { Market.new(:hash_maybe) } - # - # it "jskdsjdksd" do + describe "#initialize" do + let(:sale) { Sale.new(:id, :amount, :purchase_time, :vendor_id, :product_id)} + it "can create an instance of Sale" do + sale.must_be_instance_of(Sale) + end + it "must respond to vendor parameters" do + sale.must_respond_to(:id) + sale.must_respond_to(:amount) + sale.must_respond_to(:purchase_time) + sale.must_respond_to(:vendor_id) + sale.must_respond_to(:product_id) + end + end + + describe "#self.all" do + # before(:each) do + # Sale.new(:id, :amount, :purchase_time, :vendor_id, :product_id) + # end + it "should return a hash" do + Sale.all.must_be_instance_of(Hash) + end + + # @todo this is weird and I need to think about it + it "should be a collection of Sale objects" do + Sale.all.each do |sale_id, sale| + sale_id.must_equal(sale.id) + sale.must_be_instance_of(Sale) + end + end + it "should totally find a random Sale and decide it's an instance of Sale" do + + random_sale_id = rand(1..100) # 100 is an arbitrary low number + Sale.all[random_sale_id].must_be_instance_of(Sale) + end + end + + + + # describe Vendor do + # describe "#self.all" do # + # it "should be a collection of Vendor objects" do + # Vendor.all.each do |vendor_id, vendor| + # vendor_id.must_equal(vendor.id) + # vendor.must_be_instance_of(Vendor) + # end + # end + # it "should return information about vendors" do + # # first listed vendor + # Vendor.all[1].id.must_equal(1) + # Vendor.all[1].name.must_equal("Feil-Farrell") + # Vendor.all[1].number_of_employees.must_equal(8) + # Vendor.all[1].market_id.must_equal(1) # - # end + # # last listed vendor + # Vendor.all[2690].id.must_equal(2690) + # Vendor.all[2690].name.must_equal("Mann-Lueilwitz") + # Vendor.all[2690].number_of_employees.must_equal(4) + # Vendor.all[2690].market_id.must_equal(500) + # end + # end #self.all + # describe "#self.find(id)" do + # it "should return an instance of a Vendor object of a certain id" do + # random_vendor_id = rand(1..2690) + # Vendor.find(random_vendor_id).must_be_instance_of(Vendor) + # Vendor.find(random_vendor_id).id.must_equal(random_vendor_id) + # end + # end #self.find(id) + + - # end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 8681db92..245aab0e 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -21,12 +21,18 @@ module FarMar it "should return a hash" do Vendor.all.must_be_instance_of(Hash) end + it "should be a collection of Vendor objects" do Vendor.all.each do |vendor_id, vendor| vendor_id.must_equal(vendor.id) vendor.must_be_instance_of(Vendor) end end + # @todo this worked in Sale + # it "should totally find a random Sale and decide it's an instance of Sale" do + # random_sale_id = rand(1..100) # 100 is an arbitrary low number + # Sale.all[random_sale_id].must_be_instance_of(Sale) + # end it "should return information about vendors" do # first listed vendor Vendor.all[1].id.must_equal(1) @@ -62,15 +68,28 @@ module FarMar before(:each) do @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) end - #products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. - it "should return a collection of Product instances associated to current vendor" do + it "should return a collection of Product instances associated to specific vendor" do @vendor.products.each do |product_id, product| - # puts "#{vendor.market_id} is this market id" #this is not putsing?! + puts "#{vendor.market_id} is this market id" #this is not puts-ing?! product.vendor_id.must_equal(@vendor.id) end end + end + + describe "#sales" do + before(:each) do + @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + end + it "should return a collection of Sale instances that are associated to a specific vendor" do + skip # @todo error here because I forgot to code .sale and .all for Sale class + @vendor.sales.each do |sale_id, sale| + sale.vendor_id.must_equal(@vendor.id) + end + end end + + end end From b7a6fd229e3fc7632f09b0c4d34c22d8193c6250 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Thu, 8 Sep 2016 14:57:48 -0700 Subject: [PATCH 11/17] missing return caused problems, now solved. Currently working on: #sales in Vendor --- lib/market.rb | 6 ++-- lib/sale.rb | 39 ++++++++++++-------------- lib/vendor.rb | 16 +++++------ specs/market_spec.rb | 15 +++++----- specs/sale_spec.rb | 66 ++++++++++++++++++-------------------------- specs/vendor_spec.rb | 27 +++++++++--------- 6 files changed, 77 insertions(+), 92 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index 154f2710..3c285bbe 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -27,13 +27,13 @@ def self.find(id) return markets[id] end - def vendors + def vendors(id) market_vendors = {} - Vendor.all.each { |vendor_id, vendor| + Vendor.all.each do |vendor_id, vendor| if vendor.market_id == id market_vendors[vendor_id] = vendor end - } + end #each return market_vendors end diff --git a/lib/sale.rb b/lib/sale.rb index 8a144bff..bcb9b8b5 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,38 +1,35 @@ # Remember: the datetime here loaded are not converted from string. Suggest require 'time' and Time.Parse(line[2]) - -module FarMar - class Sale # ID - (Fixnum) uniquely identifies the sale # Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) # Purchase_time - (Datetime) when the sale was completed # Vendor_id - (Fixnum) a reference to which vendor completed the sale # Product_id - (Fixnum) a reference to which product was sold +module FarMar + class Sale + attr_reader :id, :amount, :purchase_time, :vendor_id, :product_id def initialize(id, amount, purchase_time, vendor_id, product_id) - @id = id - @amount = amount # @todo program is not happy about this at the moment / 100 - @purchase_time = purchase_time - @vendor_id = vendor_id - @product_id = product_id + @id = id + @amount = amount / 100 + @purchase_time = purchase_time + @vendor_id = vendor_id + @product_id = product_id end - # @todo it returns an array, not a hash def self.all - sales = {} # @todo START HERE START HERE this is what all goes wrong - # CSV.read('support/sales.csv').each do |line| - # sale = self.new(line[0].to_i,line[1].to_f,line[2],line[3].to_i,line[4].to_i) - # sales[sale.id] = sale - # end + sales = {} + CSV.read('support/sales.csv').each do |line| + sale = self.new(line[0].to_i,line[1].to_f,line[2],line[3].to_i,line[4].to_i) + sales[sale.id] = sale + end + return sales # Never, ever, ever, ever, ever (!!) forget this one again. end -# VENDOR - - - # def self.find(id) - # vendors = self.all - # return vendors[id] - # end + def self.find(id) + vendors = self.all + return vendors[id] + end diff --git a/lib/vendor.rb b/lib/vendor.rb index aa3bb44b..c21bdb87 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -38,14 +38,14 @@ def products end # #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. - def sales - vendor_sales = {} - Sale.all.each do |sale_id, sale| - if sale.vendor_id == id - vendor_sales[sale_id] = sale - end - end - end + # def sales + # vendor_sales = {} + # Sale.all.each do |sale_id, sale| + # if sale.vendor_id == id + # vendor_sales[sale_id] = sale + # end + # end + # end end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index f2b0e967..4b1dd1f8 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -52,17 +52,16 @@ module FarMar end #self.find(id) describe "#vendors" do - before(:each) do - @market = Market.new(:id, :name, :address, :city, :county, :state, :zipcode) - end it "should request vendors associated with itself (its instance of Market)" do - #puts "VVVVVVV" # This puts fine - @market.vendors.each do |vendor_id, vendor| - # puts "#{vendor.market_id} is this market id" #this is not putsing?! - vendor.market_id.must_equal(@market.id) + all_markets = Market.all + random_market_id = rand(1..500) + random_market = all_markets[random_market_id] + random_market.vendors(random_market_id).each do |vendor_id, vendor| + vendor.market_id.must_equal(random_market_id) end - end + + end #vendors end #market diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 12ea04f0..0fde014d 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -3,7 +3,7 @@ module FarMar describe Sale do describe "#initialize" do - let(:sale) { Sale.new(:id, :amount, :purchase_time, :vendor_id, :product_id)} + let(:sale) { Sale.new(:id, 10000, :purchase_time, :vendor_id, :product_id)} it "can create an instance of Sale" do sale.must_be_instance_of(Sale) end @@ -17,14 +17,28 @@ module FarMar end describe "#self.all" do - # before(:each) do - # Sale.new(:id, :amount, :purchase_time, :vendor_id, :product_id) - # end + it "should return a hash" do Sale.all.must_be_instance_of(Hash) end + it "should return information about sales" do + # first sale in csv + Sale.all[1].id.must_equal(1) # The sale_id hash has a key value 1, it's not an array call + Sale.all[1].amount.must_equal(92.90) + Sale.all[1].purchase_time.must_equal("2013-11-07 04:34:56 -0800") + Sale.all[1].vendor_id.must_equal(1) + Sale.all[1].product_id.must_equal(1) + # last sale in csv + Sale.all[12001].id.must_equal(12001) + Sale.all[12001].amount.must_equal(89.23) + Sale.all[12001].purchase_time.must_equal("2013-11-12 02:03:31 -0800") + Sale.all[12001].vendor_id.must_equal(2690) + Sale.all[12001].product_id.must_equal(8192) + end + # When I lost trust in my program I added these two it"shoulds" + # -and they failed until I fixed my problem + # @todo figure out if they are needed or if it is overcoverage - # @todo this is weird and I need to think about it it "should be a collection of Sale objects" do Sale.all.each do |sale_id, sale| sale_id.must_equal(sale.id) @@ -33,44 +47,18 @@ module FarMar end it "should totally find a random Sale and decide it's an instance of Sale" do - random_sale_id = rand(1..100) # 100 is an arbitrary low number + random_sale_id = rand(1..12001) Sale.all[random_sale_id].must_be_instance_of(Sale) end end - - - # describe Vendor do - # describe "#self.all" do - # - # it "should be a collection of Vendor objects" do - # Vendor.all.each do |vendor_id, vendor| - # vendor_id.must_equal(vendor.id) - # vendor.must_be_instance_of(Vendor) - # end - # end - # it "should return information about vendors" do - # # first listed vendor - # Vendor.all[1].id.must_equal(1) - # Vendor.all[1].name.must_equal("Feil-Farrell") - # Vendor.all[1].number_of_employees.must_equal(8) - # Vendor.all[1].market_id.must_equal(1) - # - # # last listed vendor - # Vendor.all[2690].id.must_equal(2690) - # Vendor.all[2690].name.must_equal("Mann-Lueilwitz") - # Vendor.all[2690].number_of_employees.must_equal(4) - # Vendor.all[2690].market_id.must_equal(500) - # end - # end #self.all - # describe "#self.find(id)" do - # it "should return an instance of a Vendor object of a certain id" do - # random_vendor_id = rand(1..2690) - # Vendor.find(random_vendor_id).must_be_instance_of(Vendor) - # Vendor.find(random_vendor_id).id.must_equal(random_vendor_id) - # end - # end #self.find(id) - + describe "#self.find(id)" do + it "should return an instance of a Sale of a certain id" do + random_sale_id = rand(1..12001) + Sale.find(random_sale_id).must_be_instance_of(Sale) + Sale.find(random_sale_id).id.must_equal(random_sale_id) + end + end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index 245aab0e..f3445ec6 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -22,13 +22,15 @@ module FarMar Vendor.all.must_be_instance_of(Hash) end + # This test not in Market or the rest + # Weird must think about this it "should be a collection of Vendor objects" do Vendor.all.each do |vendor_id, vendor| vendor_id.must_equal(vendor.id) vendor.must_be_instance_of(Vendor) end end - # @todo this worked in Sale + # @todo this worked in Sale, and then it didn't # it "should totally find a random Sale and decide it's an instance of Sale" do # random_sale_id = rand(1..100) # 100 is an arbitrary low number # Sale.all[random_sale_id].must_be_instance_of(Sale) @@ -76,18 +78,17 @@ module FarMar end end - describe "#sales" do - before(:each) do - @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) - end - it "should return a collection of Sale instances that are associated to a specific vendor" do - skip # @todo error here because I forgot to code .sale and .all for Sale class - @vendor.sales.each do |sale_id, sale| - sale.vendor_id.must_equal(@vendor.id) - end - end - - end + # describe "#sales" do + # before(:each) do + # @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + # end + # it "should return a collection of Sale instances that are associated to a specific vendor" do + # @vendor.sales.each do |sale_id, sale| + # sale.vendor_id.must_equal(@vendor.id) + # end + # end + # + # end From 77088b6e3e5eaa03d324e9f28741e718cf887d0e Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Thu, 8 Sep 2016 16:20:30 -0700 Subject: [PATCH 12/17] Working on: Vendor method self.by_market --- lib/market.rb | 6 ++--- lib/sale.rb | 2 +- lib/vendor.rb | 34 ++++++++++++++++---------- specs/market_spec.rb | 5 ++-- specs/vendor_spec.rb | 57 +++++++++++++++++++++++++++++++------------- 5 files changed, 69 insertions(+), 35 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index 3c285bbe..e4cff35e 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -27,15 +27,15 @@ def self.find(id) return markets[id] end - def vendors(id) + def vendors market_vendors = {} Vendor.all.each do |vendor_id, vendor| - if vendor.market_id == id + if vendor.market_id == id # id is attr_reader id, no need for argument market_vendors[vendor_id] = vendor end end #each return market_vendors - end + end #class end #module diff --git a/lib/sale.rb b/lib/sale.rb index bcb9b8b5..0822d2a2 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -11,7 +11,7 @@ class Sale def initialize(id, amount, purchase_time, vendor_id, product_id) @id = id - @amount = amount / 100 + @amount = amount / 100 # in dollars @purchase_time = purchase_time @vendor_id = vendor_id @product_id = product_id diff --git a/lib/vendor.rb b/lib/vendor.rb index c21bdb87..b5aadfd5 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -29,23 +29,33 @@ def market def products vendor_products = {} - Product.all.each { |product_id, product| - if product.vendor_id == id + Product.all.each do |product_id, product| + if product.vendor_id == id # finding id because it is an attr_reader vendor_products[product_id] = product # creates a hash of products carried by specific vendor end - } + end return vendor_products end -# #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. - # def sales - # vendor_sales = {} - # Sale.all.each do |sale_id, sale| - # if sale.vendor_id == id - # vendor_sales[sale_id] = sale - # end - # end - # end +#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + def sales + vendor_sales = {} + Sale.all.each do |sale_id, sale| + if sale.vendor_id == id + vendor_sales[sale_id] = sale + end + end #each + return vendor_sales + end #def + + def revenue + revenue = 0 + sales.each do |sale_id, sale| + revenue += (sale.amount * 100) + end + return revenue + end + end diff --git a/specs/market_spec.rb b/specs/market_spec.rb index 4b1dd1f8..ceab091d 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -56,13 +56,14 @@ module FarMar all_markets = Market.all random_market_id = rand(1..500) random_market = all_markets[random_market_id] - random_market.vendors(random_market_id).each do |vendor_id, vendor| + random_market.vendors.each do |vendor_id, vendor| vendor.market_id.must_equal(random_market_id) end end + end #vendors + - end #vendors end #market diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index f3445ec6..df064de3 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -67,29 +67,52 @@ module FarMar end describe "#products" do - before(:each) do - @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) - end + # before(:each) do + # @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + # end it "should return a collection of Product instances associated to specific vendor" do - @vendor.products.each do |product_id, product| - puts "#{vendor.market_id} is this market id" #this is not puts-ing?! - product.vendor_id.must_equal(@vendor.id) + all_vendors = Vendor.all + random_vendor_id = rand(1..2690) + random_vendor = all_vendors[random_vendor_id] + random_vendor.products.each do |product_id, product| + product.vendor_id.must_equal(random_vendor_id) end + # @vendor.products.each do |product_id, product| + # puts "#{vendor.market_id} is this market id" #this is not puts-ing?! + # product.vendor_id.must_equal(@vendor.id) + # end end end - # describe "#sales" do - # before(:each) do - # @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) - # end - # it "should return a collection of Sale instances that are associated to a specific vendor" do - # @vendor.sales.each do |sale_id, sale| - # sale.vendor_id.must_equal(@vendor.id) - # end - # end - # - # end + describe "#sales" do + it "should return the sales associated to the instance of Vendor" do + all_vendors = Vendor.all + random_vendor_id = rand(1..2690) + random_vendor = all_vendors[random_vendor_id] + random_vendor.sales.each do |sale_id, sale| + sale.vendor_id.must_equal(random_vendor_id) + end + end + end + describe "#revenue" do + # Here it seems to use the same code for the test and the method. + it "returns the sum of all of the vendor's sales (in cents)" do + random_vendor_id = rand(1..2690) + random_vendor = Vendor.all[random_vendor_id] + vendor_revenue = 0 + random_vendor.sales.each do |sale_id, sale| + vendor_revenue += (sale.amount * 100) # back to cents + end + random_vendor.revenue.must_equal(vendor_revenue) + end + # Here is the specific vendor revenue for vendor 1: 38259 + it "returns the sum of all of the vendor's sales (in cents)" do + all_vendors = Vendor.all + all_vendors[1].revenue.must_equal(38259) + end + + end end From d64faa0d15f2e1cdcd1f7a8bbbe88d8aeb285f6c Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Thu, 8 Sep 2016 16:59:53 -0700 Subject: [PATCH 13/17] Whittling away on Vendor by_market-method --- lib/vendor.rb | 39 +++++++++++++++++++++++---------------- specs/vendor_spec.rb | 16 +++++++++++++++- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/lib/vendor.rb b/lib/vendor.rb index b5aadfd5..c4d3da3f 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -37,25 +37,32 @@ def products return vendor_products end -#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. - def sales - vendor_sales = {} - Sale.all.each do |sale_id, sale| - if sale.vendor_id == id - vendor_sales[sale_id] = sale + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + def sales + vendor_sales = {} + Sale.all.each do |sale_id, sale| + if sale.vendor_id == id + vendor_sales[sale_id] = sale + end + end #each + return vendor_sales + end #def + + def revenue + revenue = 0 + sales.each do |sale_id, sale| + revenue += (sale.amount * 100) end - end #each - return vendor_sales - end #def - - def revenue - revenue = 0 - sales.each do |sale_id, sale| - revenue += (sale.amount * 100) + return revenue end - return revenue - end + # self.by_market(market_id): returns all of the vendors with the given market_id + def self.by_market(market_id) + # market_vendors = self.all.map { |vendor_id, vendor| + # vendor.market == market_id ? } + # @todo START HERE START HERE START HERE + return market_vendors + end end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index df064de3..f05eb79d 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -111,9 +111,23 @@ module FarMar all_vendors = Vendor.all all_vendors[1].revenue.must_equal(38259) end - end +# self.by_market(market_id): returns all of the vendors with the given market_id +# is this going to be just the method in another way?? + describe "#self.by_market" do + it "should return all the vendor's with a given market_id" do + random_market_id = rand(1..500) + market_vendors = {} + Vendor.all.each do |vendor_id, vendor| + if vendor.market == random_market_id + market_vendors[vendor_id] = vendor # gives us a hash of test ids + end + end + Vendor.by_market(random_market_id).must_equal(market_vendors) + end + + end end end From 61e0b457d1980388cf2267a1a839a427b08585ab Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Thu, 8 Sep 2016 23:00:13 -0700 Subject: [PATCH 14/17] Now understand how difficult it is to test self.-methods. Finished with Vendor by_market. --- .gitignore | 1 + lib/product.rb | 2 ++ lib/vendor.rb | 7 +++---- specs/vendor_spec.rb | 50 +++++++++++++------------------------------- 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 28f48498..2dc07618 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.rbc /.config /coverage/ +/specs/coverage /InstalledFiles /pkg/ /spec/reports/ diff --git a/lib/product.rb b/lib/product.rb index ec9e5e41..c3cafaf7 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -6,6 +6,8 @@ def initialize(id,name,vendor_id) @name = name @vendor_id = vendor_id end + + def self.all products = {} CSV.read('support/products.csv').each do |line| diff --git a/lib/vendor.rb b/lib/vendor.rb index c4d3da3f..f36be2c6 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -57,10 +57,9 @@ def revenue end # self.by_market(market_id): returns all of the vendors with the given market_id - def self.by_market(market_id) - # market_vendors = self.all.map { |vendor_id, vendor| - # vendor.market == market_id ? } - # @todo START HERE START HERE START HERE + def self.by_market(market_id) # nifty select method! returns only those that are true + market_vendors = self.all.select { |vendor_id, vendor| + vendor.market == market_id } return market_vendors end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index f05eb79d..ba0d5a13 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -58,18 +58,16 @@ module FarMar end #self.find(id) describe "#market" do - before(:each) do - @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) - end - it "should reutrn the Market instance that it is associated to" do - @vendor.market.must_equal(@vendor.market_id) - end + before(:each) do + @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) end + it "should reutrn the Market instance that it is associated to" do + @vendor.market.must_equal(@vendor.market_id) + end + end describe "#products" do - # before(:each) do - # @vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) - # end + it "should return a collection of Product instances associated to specific vendor" do all_vendors = Vendor.all random_vendor_id = rand(1..2690) @@ -77,10 +75,6 @@ module FarMar random_vendor.products.each do |product_id, product| product.vendor_id.must_equal(random_vendor_id) end - # @vendor.products.each do |product_id, product| - # puts "#{vendor.market_id} is this market id" #this is not puts-ing?! - # product.vendor_id.must_equal(@vendor.id) - # end end end @@ -96,38 +90,24 @@ module FarMar end describe "#revenue" do - # Here it seems to use the same code for the test and the method. - it "returns the sum of all of the vendor's sales (in cents)" do - random_vendor_id = rand(1..2690) - random_vendor = Vendor.all[random_vendor_id] - vendor_revenue = 0 - random_vendor.sales.each do |sale_id, sale| - vendor_revenue += (sale.amount * 100) # back to cents - end - random_vendor.revenue.must_equal(vendor_revenue) - end # Here is the specific vendor revenue for vendor 1: 38259 it "returns the sum of all of the vendor's sales (in cents)" do all_vendors = Vendor.all - all_vendors[1].revenue.must_equal(38259) + all_vendors[1].revenue.must_equal(38259) # [1] does not indicate array, instead hash key 1 end end -# self.by_market(market_id): returns all of the vendors with the given market_id -# is this going to be just the method in another way?? + # self.by_market(market_id): returns all of the vendors with the given market_id + # is this going to be just the method in another way?? describe "#self.by_market" do - it "should return all the vendor's with a given market_id" do + # actually my self.code is untestable because I have no way to inject a test file + # @todo test something else + it "should return instances of Vendor" do random_market_id = rand(1..500) - market_vendors = {} - Vendor.all.each do |vendor_id, vendor| - if vendor.market == random_market_id - market_vendors[vendor_id] = vendor # gives us a hash of test ids - end + Vendor.by_market(random_market_id).each do |vendor_id, vendor| + vendor.must_be_instance_of(Vendor) end - Vendor.by_market(random_market_id).must_equal(market_vendors) end - end - end end From a0664ac34a1490ec10212ffbe3be8c00b15dfa48 Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Fri, 9 Sep 2016 12:21:38 -0700 Subject: [PATCH 15/17] FarMar reqs near complete except handling datetime (and the method related to it) --- README.md | 8 +-- lib/product.rb | 39 +++++++++++++- lib/sale.rb | 28 +++++++--- lib/vendor.rb | 6 +-- specs/product_spec.rb | 115 +++++++++++++++++++++++++++++------------- specs/sale_spec.rb | 21 ++++++++ 6 files changed, 168 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index d9a302d6..af331f39 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,10 @@ In this assignment we will be creating an application to look up farmers markets and their related vendors, products, and sales. We will use __CSV__ files as our _database_. Project Data - [Markets](#markets) - [Vendors](#vendors) - [Products](#products) - [Sales](#sales) + * [Markets](#markets) + * [Vendors](#vendors) + * [Products](#products) + * [Sales](#sales) [Baseline](#baseline) [Primary](#primary) diff --git a/lib/product.rb b/lib/product.rb index c3cafaf7..fb68e479 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -7,7 +7,6 @@ def initialize(id,name,vendor_id) @vendor_id = vendor_id end - def self.all products = {} CSV.read('support/products.csv').each do |line| @@ -21,5 +20,43 @@ def self.find(id) return products[id] end + def vendor + product_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + Vendor.all.each do |ven_id, ven| + if ven_id == vendor_id + product_vendor = ven + break + end + end + return product_vendor + end + # Returns a collection of Sale instances, associated using the Sale product_id field. + def sales + product_sales = {} + Sale.all.each do |sale_id, sale| + if sale.product_id == id + product_sales[sale_id] = sale + end + end + return product_sales + end + + #Returns the number of times this product has been sold. + def number_of_sales + return sales.length + end + + #Returns all of the products with the given vendor_id + def self.by_vendor(ven_id) + vendor_products = {} + self.all.each do |pro_id, pro| + if ven_id == pro.vendor_id + vendor_products[pro_id] = pro + end + end + return vendor_products + end + + end end diff --git a/lib/sale.rb b/lib/sale.rb index 0822d2a2..f7694c91 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,9 +1,4 @@ # Remember: the datetime here loaded are not converted from string. Suggest require 'time' and Time.Parse(line[2]) -# ID - (Fixnum) uniquely identifies the sale -# Amount - (Fixnum) the amount of the transaction, in cents (i.e., 150 would be $1.50) -# Purchase_time - (Datetime) when the sale was completed -# Vendor_id - (Fixnum) a reference to which vendor completed the sale -# Product_id - (Fixnum) a reference to which product was sold module FarMar class Sale @@ -11,7 +6,7 @@ class Sale def initialize(id, amount, purchase_time, vendor_id, product_id) @id = id - @amount = amount / 100 # in dollars + @amount = amount / 100 # in dollars, never used! @purchase_time = purchase_time @vendor_id = vendor_id @product_id = product_id @@ -31,8 +26,29 @@ def self.find(id) return vendors[id] end + def vendor + sale_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + Vendor.all.each do |ven_id, ven| + if ven_id == vendor_id + sale_vendor = ven + end + end + return sale_vendor + end +#Returns the Product instance, associated with this sale using the Sale product_id field + def product + sale_product = Product.new(:id, :name, :vendor_id) + Product.all.each do |pro_id, pro| + if pro_id == product_id + sale_product = pro + break + end + end + return sale_product + end + #self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments diff --git a/lib/vendor.rb b/lib/vendor.rb index f36be2c6..748a96f3 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -37,7 +37,7 @@ def products return vendor_products end - #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + #Returns a collection of Sale instances, associated by the vendor_id field. def sales vendor_sales = {} Sale.all.each do |sale_id, sale| @@ -56,11 +56,11 @@ def revenue return revenue end - # self.by_market(market_id): returns all of the vendors with the given market_id + # Returns all of the vendors with the given market_id def self.by_market(market_id) # nifty select method! returns only those that are true market_vendors = self.all.select { |vendor_id, vendor| vendor.market == market_id } - return market_vendors + return market_vendors # it is an array of hash end end diff --git a/specs/product_spec.rb b/specs/product_spec.rb index 3ab330f3..efb7776b 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -6,44 +6,89 @@ module FarMar describe Product do - describe "#initialize" do - let(:product) { Product.new(:id, :name, :vendor_id) } - - - it "can create an instance of Product" do - product.must_be_instance_of(Product) - end - it "must respond to (have parameters of) of Product information" do - product.must_respond_to(:id) - product.must_respond_to(:name) - product.must_respond_to(:vendor_id) - end + describe "#initialize" do + let(:product) { Product.new(:id, :name, :vendor_id) } + + + it "can create an instance of Product" do + product.must_be_instance_of(Product) + end + it "must respond to (have parameters of) of Product information" do + product.must_respond_to(:id) + product.must_respond_to(:name) + product.must_respond_to(:vendor_id) + end + end + + describe "#self.all" do + + it "should return a hash" do + Product.all.must_be_instance_of(Hash) + end + it "should return information about markets" do + # first listed product + Product.all[1].id.must_equal(1) + Product.all[1].name.must_equal("Dry Beets") + Product.all[1].vendor_id.must_equal(1) + # last listed product + Product.all[8193].id.must_equal(8193) + Product.all[8193].name.must_equal("Cruel Beef") + Product.all[8193].vendor_id.must_equal(2690) + end + end #self.all + describe "#self.find(id)" do + it "should return an instance of a Product object of a certain id" do + random_product_id = rand(1..8193) + Product.find(random_product_id).must_be_instance_of(Product) + Product.find(random_product_id).id.must_equal(random_product_id) + end + end #self.find(id) + + #vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field + describe "#vendor" do + let(:product_instance) { Product.new(298,"Curly Fruit",96) } + it "should return an instance of Vendor" do + product_instance.vendor.must_be_instance_of(Vendor) + end + + it "should be associated with the vendor_id in its instance of Product" do + product_instance.vendor.id.must_equal(product_instance.vendor_id) + end + end + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + describe "#sales" do + let(:product_instance) { Product.new(298,"Curly Fruit",96) } + it "should return a hash" do + product_instance.sales.must_be_instance_of(Hash) + end + + it "should return a collection of Sale instances associated by product_id" do + product_instance.sales.each do |sale_id, sale| + sale.must_be_instance_of(Sale) + product_instance.id.must_equal(sale.product_id) end + end + end - describe "#self.all" do - - it "should return a hash" do - Product.all.must_be_instance_of(Hash) - end - it "should return information about markets" do - # first listed product - Product.all[1].id.must_equal(1) - Product.all[1].name.must_equal("Dry Beets") - Product.all[1].vendor_id.must_equal(1) - # last listed product - Product.all[8193].id.must_equal(8193) - Product.all[8193].name.must_equal("Cruel Beef") - Product.all[8193].vendor_id.must_equal(2690) - end - end #self.all - describe "#self.find(id)" do - it "should return an instance of a Product object of a certain id" do - random_product_id = rand(1..8193) - Product.find(random_product_id).must_be_instance_of(Product) - Product.find(random_product_id).id.must_equal(random_product_id) - end - end #self.find(id) + #number_of_sales: returns the number of times this product has been sold. + describe "#number_of_sales" do + before(:each) do + @product_instance = Product.new(298,"Curly Fruit",96) + end + it "should return the number of times a product was sold" do + @product_instance.number_of_sales.must_equal(7) + end + end + + describe "#self.by_vendor()" do + it "should return a collection of instances of Product" do + random_vendor_id = rand(1..2690) + Product.by_vendor(random_vendor_id).each do |pro_id, pro| + pro.must_be_instance_of(Product) + end + end + end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index 0fde014d..a96a78c3 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -60,8 +60,29 @@ module FarMar end end + #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field + describe "#vendor" do + let(:sale_instance) { Sale.new(96,2289,"2013-11-10 17:23:48 -0800",19,57)} + it "should return an instance of Vendor" do + sale_instance.vendor.must_be_instance_of(Vendor) + end + it "should be associated with vendor_id" do + sale_instance.vendor.id.must_equal(sale_instance.vendor_id) + end + end +#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field + describe "#product" do + let(:sale_instance) { Sale.new(96,2289,"2013-11-10 17:23:48 -0800",19,57)} + it "should return an instance of Product" do + sale_instance.product.must_be_instance_of(Product) + end + it "should be associated by the sale using the product_id field" do + sale_instance.product.id.must_equal(sale_instance.product_id) + end + end + #self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments end From 1f5361d36ba8ed0d43630d40a611166ff88c518f Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Fri, 9 Sep 2016 14:37:19 -0700 Subject: [PATCH 16/17] Primary Reqs complete and tested. Working on refactoring --- far_mar.rb | 4 ++++ lib/product.rb | 2 +- lib/sale.rb | 19 ++++++++++++++----- lib/vendor.rb | 2 +- specs/sale_spec.rb | 34 +++++++++++++++++++++------------- specs/spec_helper.rb | 2 +- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/far_mar.rb b/far_mar.rb index cb35342d..304ea712 100644 --- a/far_mar.rb +++ b/far_mar.rb @@ -3,5 +3,9 @@ require_relative "lib/product.rb" require_relative "lib/sale.rb" require 'csv' +require 'date' module FarMar + # string_date = "2013-11-09 23:42:41 -0800" + # date = DateTime.parse(string_date) + # puts date end diff --git a/lib/product.rb b/lib/product.rb index fb68e479..4b9302d1 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -38,7 +38,7 @@ def sales product_sales[sale_id] = sale end end - return product_sales + return product_sales end #Returns the number of times this product has been sold. diff --git a/lib/sale.rb b/lib/sale.rb index f7694c91..821e929a 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -1,4 +1,3 @@ -# Remember: the datetime here loaded are not converted from string. Suggest require 'time' and Time.Parse(line[2]) module FarMar class Sale @@ -6,7 +5,7 @@ class Sale def initialize(id, amount, purchase_time, vendor_id, product_id) @id = id - @amount = amount / 100 # in dollars, never used! + @amount = amount #in cents @purchase_time = purchase_time @vendor_id = vendor_id @product_id = product_id @@ -15,7 +14,7 @@ def initialize(id, amount, purchase_time, vendor_id, product_id) def self.all sales = {} CSV.read('support/sales.csv').each do |line| - sale = self.new(line[0].to_i,line[1].to_f,line[2],line[3].to_i,line[4].to_i) + sale = self.new(line[0].to_i,line[1].to_f,DateTime.parse(line[2]),line[3].to_i,line[4].to_i) sales[sale.id] = sale end return sales # Never, ever, ever, ever, ever (!!) forget this one again. @@ -48,8 +47,18 @@ def product return sale_product end - #self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments - + # Returns a collection of Sale objects where the purchase time is between the two times given as arguments + def self.between(beginning_time, end_time) + sales_within_times = {} + self.all.each do |sal_id, sal| + if beginning_time <= sal.purchase_time + if end_time >= sal.purchase_time + sales_within_times[sal_id] = sal + end + end + end + return sales_within_times + end diff --git a/lib/vendor.rb b/lib/vendor.rb index 748a96f3..fc1e6ca3 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -51,7 +51,7 @@ def sales def revenue revenue = 0 sales.each do |sale_id, sale| - revenue += (sale.amount * 100) + revenue += (sale.amount) end return revenue end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index a96a78c3..bd1dad04 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -3,7 +3,7 @@ module FarMar describe Sale do describe "#initialize" do - let(:sale) { Sale.new(:id, 10000, :purchase_time, :vendor_id, :product_id)} + let(:sale) { Sale.new(:id, :amount, :purchase_time, :vendor_id, :product_id)} it "can create an instance of Sale" do sale.must_be_instance_of(Sale) end @@ -24,20 +24,17 @@ module FarMar it "should return information about sales" do # first sale in csv Sale.all[1].id.must_equal(1) # The sale_id hash has a key value 1, it's not an array call - Sale.all[1].amount.must_equal(92.90) - Sale.all[1].purchase_time.must_equal("2013-11-07 04:34:56 -0800") + Sale.all[1].amount.must_equal(9290) + Sale.all[1].purchase_time.must_equal(DateTime.parse("2013-11-07 04:34:56 -0800")) Sale.all[1].vendor_id.must_equal(1) Sale.all[1].product_id.must_equal(1) # last sale in csv Sale.all[12001].id.must_equal(12001) - Sale.all[12001].amount.must_equal(89.23) - Sale.all[12001].purchase_time.must_equal("2013-11-12 02:03:31 -0800") + Sale.all[12001].amount.must_equal(8923) + Sale.all[12001].purchase_time.must_equal(DateTime.parse("2013-11-12 02:03:31 -0800")) Sale.all[12001].vendor_id.must_equal(2690) Sale.all[12001].product_id.must_equal(8192) end - # When I lost trust in my program I added these two it"shoulds" - # -and they failed until I fixed my problem - # @todo figure out if they are needed or if it is overcoverage it "should be a collection of Sale objects" do Sale.all.each do |sale_id, sale| @@ -45,8 +42,7 @@ module FarMar sale.must_be_instance_of(Sale) end end - it "should totally find a random Sale and decide it's an instance of Sale" do - + it "should find a random Sale and decide it's an instance of Sale" do random_sale_id = rand(1..12001) Sale.all[random_sale_id].must_be_instance_of(Sale) end @@ -60,7 +56,6 @@ module FarMar end end - #vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field describe "#vendor" do let(:sale_instance) { Sale.new(96,2289,"2013-11-10 17:23:48 -0800",19,57)} it "should return an instance of Vendor" do @@ -71,7 +66,6 @@ module FarMar end end -#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field describe "#product" do let(:sale_instance) { Sale.new(96,2289,"2013-11-10 17:23:48 -0800",19,57)} it "should return an instance of Product" do @@ -82,7 +76,21 @@ module FarMar end end - #self.between(beginning_time, end_time): returns a collection of FarMar::Sale objects where the purchase time is between the two times given as arguments + describe "#self.between(beginning_time, end_time)" do + before(:each) do + beginning_time = DateTime.parse("2013-11-11 03:36:15 -0800") + end_time = DateTime.parse("2013-11-11 03:38:15 -0800") + @sales = Sale.between(beginning_time, end_time) + end + + it "should return a hash" do + @sales.must_be_instance_of(Hash) + end + + it "should find sales objects within the test timeframe" do + @sales.length.must_be(:>,0) + end + end end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb index 26ecbe4c..09d9efc0 100644 --- a/specs/spec_helper.rb +++ b/specs/spec_helper.rb @@ -1,4 +1,3 @@ -require_relative '../far_mar' require 'simplecov' SimpleCov.start require 'minitest' @@ -7,4 +6,5 @@ require "minitest/reporters" require 'minitest/pride' +require_relative '../far_mar' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new From eed49599a398ef91c33b1c88607b5c51511a0a0a Mon Sep 17 00:00:00 2001 From: Alma Maizels Date: Fri, 9 Sep 2016 15:52:17 -0700 Subject: [PATCH 17/17] Refactored methods replacing .each with Enumerables --- lib/market.rb | 20 ++++++++++++------ lib/product.rb | 47 ++++++++++++++++++++++++++++--------------- lib/sale.rb | 9 +++++---- lib/vendor.rb | 42 ++++++++++++++++++++++++-------------- specs/market_spec.rb | 10 ++++++--- specs/product_spec.rb | 12 +++++++---- specs/sale_spec.rb | 11 ++++------ specs/vendor_spec.rb | 31 +++++++++------------------- 8 files changed, 105 insertions(+), 77 deletions(-) diff --git a/lib/market.rb b/lib/market.rb index e4cff35e..8502c093 100644 --- a/lib/market.rb +++ b/lib/market.rb @@ -13,6 +13,7 @@ def initialize(id, name, address, city, county, state, zipcode) @zipcode = zipcode end + # Returns a collection of instances, representing all of the objects described in the CSV def self.all markets = {} CSV.read('support/markets.csv').each do |line| @@ -22,19 +23,26 @@ def self.all end return markets end + + # Returns an instance of the object where the value of the id field in the CSV matches the passed parameter. def self.find(id) markets = self.all return markets[id] end + #Returns a collection of Vendor instances, associated with the market by the market_id field. def vendors - market_vendors = {} - Vendor.all.each do |vendor_id, vendor| - if vendor.market_id == id # id is attr_reader id, no need for argument - market_vendors[vendor_id] = vendor - end - end #each + market_vendors = Vendor.all.select { |vendor_id, vendor| + vendor.market_id == id } # id is attr_reader id, no need for argument return market_vendors + # Refactoring from: + # market_vendors = {} + # Vendor.all.each do |vendor_id, vendor| + # if vendor.market_id == id # id is attr_reader id, no need for argument + # market_vendors[vendor_id] = vendor + # end + # end #each + # return market_vendors end end #class diff --git a/lib/product.rb b/lib/product.rb index 4b9302d1..01ddf5cc 100644 --- a/lib/product.rb +++ b/lib/product.rb @@ -7,6 +7,7 @@ def initialize(id,name,vendor_id) @vendor_id = vendor_id end + # Returns a collection of instances, representing all of the objects described in the CSV def self.all products = {} CSV.read('support/products.csv').each do |line| @@ -15,30 +16,45 @@ def self.all end return products end + + # Returns an instance of the object where the value of the id field in the CSV matches the passed parameter. def self.find(id) products = self.all return products[id] end + # Returns the Vendor instance, associated with this vendor using the Product vendor_id field def vendor - product_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) - Vendor.all.each do |ven_id, ven| - if ven_id == vendor_id - product_vendor = ven - break - end - end - return product_vendor + product_vendor_array = Vendor.all.find { |ven_id, ven| + ven_id == vendor_id } + product_vendor = product_vendor_array[1] # second element in find array + return product_vendor # vendor object + + # Refactoring: + # product_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) + # Vendor.all.each do |ven_id, ven| + # if ven_id == vendor_id + # product_vendor = ven + # break + # end + # end + # return product_vendor # vendor object, not hash end + # Returns a collection of Sale instances, associated using the Sale product_id field. def sales - product_sales = {} - Sale.all.each do |sale_id, sale| - if sale.product_id == id - product_sales[sale_id] = sale - end - end - return product_sales + product_sales = Sale.all.select { |sale_id, sale| + sale.product_id == id } + return product_sales + + # Refactoring: + # product_sales = {} + # Sale.all.each do |sale_id, sale| + # if sale.product_id == id + # product_sales[sale_id] = sale + # end + # end + # return product_sales end #Returns the number of times this product has been sold. @@ -57,6 +73,5 @@ def self.by_vendor(ven_id) return vendor_products end - end end diff --git a/lib/sale.rb b/lib/sale.rb index 821e929a..c860d7d0 100644 --- a/lib/sale.rb +++ b/lib/sale.rb @@ -11,20 +11,23 @@ def initialize(id, amount, purchase_time, vendor_id, product_id) @product_id = product_id end + # Returns a collection of instances, representing all of the objects described in the CSV def self.all sales = {} CSV.read('support/sales.csv').each do |line| - sale = self.new(line[0].to_i,line[1].to_f,DateTime.parse(line[2]),line[3].to_i,line[4].to_i) + sale = self.new(line[0].to_i,line[1].to_i,DateTime.parse(line[2]),line[3].to_i,line[4].to_i) sales[sale.id] = sale end return sales # Never, ever, ever, ever, ever (!!) forget this one again. end + # Returns an instance of the object where the value of the id field in the CSV matches the passed parameter. def self.find(id) vendors = self.all return vendors[id] end + # Returns the Vendor instance, associated with this sale, using the Sale vendor_id field def vendor sale_vendor = Vendor.new(:id, :name, :number_of_employees, :market_id) Vendor.all.each do |ven_id, ven| @@ -35,7 +38,7 @@ def vendor return sale_vendor end -#Returns the Product instance, associated with this sale using the Sale product_id field + #Returns the Product instance, associated with this sale using the Sale product_id field def product sale_product = Product.new(:id, :name, :vendor_id) Product.all.each do |pro_id, pro| @@ -60,7 +63,5 @@ def self.between(beginning_time, end_time) return sales_within_times end - - end #class end #module diff --git a/lib/vendor.rb b/lib/vendor.rb index fc1e6ca3..48eade81 100644 --- a/lib/vendor.rb +++ b/lib/vendor.rb @@ -9,6 +9,7 @@ def initialize(id,name,number_of_employees,market_id) @market_id = market_id end + # Returns a collection of instances, representing all of the objects described in the CSV def self.all vendors = {} CSV.read('support/vendors.csv').each do |line| @@ -18,46 +19,57 @@ def self.all return vendors end + # Returns an instance of the object where the value of the id field in the CSV matches the passed parameter. def self.find(id) vendors = self.all return vendors[id] end + # Returns the Market instance, associated with this vendor using the Vendor market_id field def market return @market_id end + # Returns a collection of Product instances, associated by the Product vendor_id field. def products - vendor_products = {} - Product.all.each do |product_id, product| - if product.vendor_id == id # finding id because it is an attr_reader - vendor_products[product_id] = product # creates a hash of products carried by specific vendor - end - end + vendor_products = Product.all.select { |product_id, product| product.vendor_id == id} return vendor_products + # Refactor from the old method below (turns out select does return a hash when used on a hash): + # vendor_products = {} + # Product.all.each do |product_id, product| + # if product.vendor_id == id # finding id because it is an attr_reader + # vendor_products[product_id] = product # creates a hash of products carried by specific vendor + # end + # end + # return vendor_products end #Returns a collection of Sale instances, associated by the vendor_id field. def sales - vendor_sales = {} - Sale.all.each do |sale_id, sale| - if sale.vendor_id == id - vendor_sales[sale_id] = sale - end - end #each + vendor_sales = Sale.all.select { |sale_id, sale| + sale.vendor_id == id } return vendor_sales + # Before refactoring: + # vendor_sales = {} + # Sale.all.each do |sale_id, sale| + # if sale.vendor_id == id + # vendor_sales[sale_id] = sale + # end + # end #each + # return vendor_sales end #def + # Returns the the sum of all of the vendor's sales (in cents) def revenue revenue = 0 sales.each do |sale_id, sale| - revenue += (sale.amount) + revenue += (sale.amount) #in cents end return revenue end - # Returns all of the vendors with the given market_id - def self.by_market(market_id) # nifty select method! returns only those that are true + # Returns all of the vendors with the given market_ids + def self.by_market(market_id) # nifty select method! returns only those that are true as an array though market_vendors = self.all.select { |vendor_id, vendor| vendor.market == market_id } return market_vendors # it is an array of hash diff --git a/specs/market_spec.rb b/specs/market_spec.rb index ceab091d..68700a6f 100644 --- a/specs/market_spec.rb +++ b/specs/market_spec.rb @@ -42,6 +42,13 @@ module FarMar Market.all[500].state.must_equal("New York") Market.all[500].zipcode.must_equal("10467") end + + it "should be a collection of Market objects" do + Market.all.each do |market_id, market| + market_id.must_equal(market.id) # shows it's pairing up correctly + market.must_be_instance_of(Market) # shows the value objects are market instances + end + end end #self.all describe "#self.find(id)" do it "should return an instance of a Market object of a certain id" do @@ -62,9 +69,6 @@ module FarMar end end #vendors - - - end #market diff --git a/specs/product_spec.rb b/specs/product_spec.rb index efb7776b..0656cc1d 100644 --- a/specs/product_spec.rb +++ b/specs/product_spec.rb @@ -1,6 +1,3 @@ -# ID - (Fixnum) uniquely identifies the product -# Name - (String) the name of the product (not guaranteed unique) -# Vendor_id - (Fixnum) a reference to which vendor sells this product require_relative 'spec_helper' module FarMar @@ -25,7 +22,7 @@ module FarMar it "should return a hash" do Product.all.must_be_instance_of(Hash) end - it "should return information about markets" do + it "should return information about products" do # first listed product Product.all[1].id.must_equal(1) Product.all[1].name.must_equal("Dry Beets") @@ -35,6 +32,13 @@ module FarMar Product.all[8193].name.must_equal("Cruel Beef") Product.all[8193].vendor_id.must_equal(2690) end + + it "should be a collection of Product objects" do + Product.all.each do |product_id, product| + product_id.must_equal(product.id) + product.must_be_instance_of(Product) + end + end end #self.all describe "#self.find(id)" do it "should return an instance of a Product object of a certain id" do diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb index bd1dad04..95432df6 100644 --- a/specs/sale_spec.rb +++ b/specs/sale_spec.rb @@ -23,7 +23,7 @@ module FarMar end it "should return information about sales" do # first sale in csv - Sale.all[1].id.must_equal(1) # The sale_id hash has a key value 1, it's not an array call + Sale.all[1].id.must_equal(1) # The sale_id hash has a key: 1, it's not an array call Sale.all[1].amount.must_equal(9290) Sale.all[1].purchase_time.must_equal(DateTime.parse("2013-11-07 04:34:56 -0800")) Sale.all[1].vendor_id.must_equal(1) @@ -57,7 +57,7 @@ module FarMar end describe "#vendor" do - let(:sale_instance) { Sale.new(96,2289,"2013-11-10 17:23:48 -0800",19,57)} + let(:sale_instance) { Sale.new(96,2289,DateTime.parse("2013-11-10 17:23:48 -0800"),19,57)} it "should return an instance of Vendor" do sale_instance.vendor.must_be_instance_of(Vendor) end @@ -67,7 +67,7 @@ module FarMar end describe "#product" do - let(:sale_instance) { Sale.new(96,2289,"2013-11-10 17:23:48 -0800",19,57)} + let(:sale_instance) { Sale.new(96,2289,DateTime.parse("2013-11-10 17:23:48 -0800"),19,57)} it "should return an instance of Product" do sale_instance.product.must_be_instance_of(Product) end @@ -76,24 +76,21 @@ module FarMar end end + # using a known timestamp: "2013-11-11 03:37:15 -0800" describe "#self.between(beginning_time, end_time)" do before(:each) do beginning_time = DateTime.parse("2013-11-11 03:36:15 -0800") end_time = DateTime.parse("2013-11-11 03:38:15 -0800") @sales = Sale.between(beginning_time, end_time) end - it "should return a hash" do @sales.must_be_instance_of(Hash) end - it "should find sales objects within the test timeframe" do @sales.length.must_be(:>,0) end end - end - end diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb index ba0d5a13..6619dbb5 100644 --- a/specs/vendor_spec.rb +++ b/specs/vendor_spec.rb @@ -21,34 +21,26 @@ module FarMar it "should return a hash" do Vendor.all.must_be_instance_of(Hash) end - - # This test not in Market or the rest - # Weird must think about this - it "should be a collection of Vendor objects" do - Vendor.all.each do |vendor_id, vendor| - vendor_id.must_equal(vendor.id) - vendor.must_be_instance_of(Vendor) - end - end - # @todo this worked in Sale, and then it didn't - # it "should totally find a random Sale and decide it's an instance of Sale" do - # random_sale_id = rand(1..100) # 100 is an arbitrary low number - # Sale.all[random_sale_id].must_be_instance_of(Sale) - # end it "should return information about vendors" do # first listed vendor Vendor.all[1].id.must_equal(1) Vendor.all[1].name.must_equal("Feil-Farrell") Vendor.all[1].number_of_employees.must_equal(8) Vendor.all[1].market_id.must_equal(1) - # last listed vendor Vendor.all[2690].id.must_equal(2690) Vendor.all[2690].name.must_equal("Mann-Lueilwitz") Vendor.all[2690].number_of_employees.must_equal(4) Vendor.all[2690].market_id.must_equal(500) end + it "should be a collection of Vendor objects" do + Vendor.all.each do |vendor_id, vendor| + vendor_id.must_equal(vendor.id) + vendor.must_be_instance_of(Vendor) + end + end end #self.all + describe "#self.find(id)" do it "should return an instance of a Vendor object of a certain id" do random_vendor_id = rand(1..2690) @@ -67,7 +59,6 @@ module FarMar end describe "#products" do - it "should return a collection of Product instances associated to specific vendor" do all_vendors = Vendor.all random_vendor_id = rand(1..2690) @@ -89,19 +80,15 @@ module FarMar end end + # Here is the specific vendor revenue for vendor 1: 38259 cents describe "#revenue" do - # Here is the specific vendor revenue for vendor 1: 38259 it "returns the sum of all of the vendor's sales (in cents)" do all_vendors = Vendor.all - all_vendors[1].revenue.must_equal(38259) # [1] does not indicate array, instead hash key 1 + all_vendors[1].revenue.must_equal(38259) # [1] does not indicate array loc, instead hash key: 1 end end - # self.by_market(market_id): returns all of the vendors with the given market_id - # is this going to be just the method in another way?? describe "#self.by_market" do - # actually my self.code is untestable because I have no way to inject a test file - # @todo test something else it "should return instances of Vendor" do random_market_id = rand(1..500) Vendor.by_market(random_market_id).each do |vendor_id, vendor|