diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..756e59e4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/.config +coverage/ +.DS_Store diff --git a/FarMar.rb b/FarMar.rb new file mode 100644 index 00000000..0d971e9a --- /dev/null +++ b/FarMar.rb @@ -0,0 +1,13 @@ +# gems your project needs +require 'csv' + +# our namespace module +module FarMar; +end + +# all of our data classes that live in the module + +require_relative './lib/mkt' +require_relative './lib/vendor' +require_relative './lib/products' +require_relative './lib/sales' diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..842f8aba --- /dev/null +++ b/Rakefile @@ -0,0 +1,7 @@ +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.test_files = FileList['specs/*_spec.rb'] #this is the set of files that will be tested. + end + +task default: :test diff --git a/lib/mkt.rb b/lib/mkt.rb new file mode 100644 index 00000000..14fbcbd0 --- /dev/null +++ b/lib/mkt.rb @@ -0,0 +1,37 @@ +module FarMar + + class Market + @@all_markets = nil + attr_accessor :id, :name, :street, :city, :county, :state, :zip + def initialize(id, name, street, city, county, state, zip) + @id = id + @name= name + @street = street + @city = city + @county = county + @state = state + @zip = zip + @vendors = Vendor.by_market(id) + end + + def vendors + return @vendors + end + + def self.all + if @@all_markets == nil + mkt = {} + CSV.read('./support/markets.csv').each do |line| + mkt[line[0]] = Market.new(line[0], line[1], line[2], line[3], line[4], line[5], line[6]) + end + @@all_markets = mkt + end + return @@all_markets + end + + def self.find(id) + Market.all[id] + end + + end +end diff --git a/lib/products.rb b/lib/products.rb new file mode 100644 index 00000000..93d57cbc --- /dev/null +++ b/lib/products.rb @@ -0,0 +1,61 @@ +module FarMar + + class Product + @@all_products_by_vendor = nil + @@all_products = nil + attr_accessor :product_id, :name, :vendor_id + def initialize(product_id, name, vendor_id) + @product_id = product_id + @name = name + @vendor_id = vendor_id + end + + #return Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field + ##this is really confusing. + def vendor + return Vendor.find(@vendor_id) + end + + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + def sales + return Sales.by_product(@product_id) + end + #number_of_sales: returns the number of times this product has been sold. + #first of product, last of sales + def number_of_sales + return Sales.by_product(@product_id).size + end + + ##create all product instances from csv + def self.all + if @@all_products == nil + products = { } + CSV.read('./support/products.csv').each do |line| + products[line[0]] = Product.new(line[0], line[1], line [2]) + end + @@all_products = products + end + return @@all_products + end + + ##find product by its own id + def self.find(product_id) + Product.all[product_id] + end + + #self.by_vendor(vendor_id): returns all of the products with the given vendor_id + def self.by_vendor(vendor_id) + if @@all_products_by_vendor == nil + products = { } + Product.all.each do |product_id, current_product| + if products[current_product.product_id] == nil + products[current_product.product_id] = { } + end + products[current_product.vendor_id][current_product.product_id] = current_product + end + @@all_products_by_vendor = products + end + return @@all_products_by_vendor[vendor_id] + end + end +end diff --git a/lib/sales.rb b/lib/sales.rb new file mode 100644 index 00000000..4a317a76 --- /dev/null +++ b/lib/sales.rb @@ -0,0 +1,81 @@ +module FarMar + + class Sales + @@all_sales_by_vendor = nil + @@all_sales_for_each_product = nil + @@all_sales = nil + attr_accessor :sales_id, :transaction_total, :datetime, :vendor_id, :product_id + def initialize(sales_id, transaction_total, datetime, vendor_id, product_id) + @sales_id = sales_id + @transaction_total = transaction_total.to_i + @datetime = DateTime.parse(datetime) + @vendor_id = vendor_id + @product_id = product_id + end + + def vendor + return Vendor.find(@vendor_id) + end + + def product + return Product.find(@product_id) + end + + ##create instances of sales for each element in csv + def self.all + if @@all_sales == nil + sales = { } + CSV.read('./support/sales.csv').each do |line| + sales[line[0]] = Sales.new(line[0],line[1],line[2],line[3],line[4]) + end + @@all_sales = sales + end + return @@all_sales + end + + def self.find(sales_id) + return Sales.all[sales_id] + end + + def self.between(beginning_time, end_time) + b = DateTime.parse(beginning_time) + e = DateTime.parse(end_time) + sales = { } + Sales.all.each do | sales_id, sale | + if b < sale.datetime && e > sale.date_time + sales[line[0]] = Sales.new(line[0],line[1],line[2],line[3],line[4]) + end + return sales + end + end + + + def self.by_product(product_id) + if @@all_sales_for_each_product == nil + sale = { } + Sales.all.each do |sales_id, sales| + if sale[sales.product_id] == nil + sale[sales.product_id] = { } + end + sale[sales.product_id][sales_id] = vendor_id + end + @@all_sales_for_each_product = sale + end + return @@all_sales_for_each_product[product_id] + end + + def self.by_vendor(vendor_id) + if @@all_sales_by_vendor == nil + transactions = { } + Sales.all.each do |sales_id, sales| + if transactions[sales.vendor_id] == nil + transactions[sales.vendor_id] = { } + end + transactions[sales.vendor_id][sales_id] = vendor_id + end + @@all_sales_by_vendor = transactions + end + return @@all_sales_by_vendor[vendor_id] + end + end +end diff --git a/lib/vendor.rb b/lib/vendor.rb new file mode 100644 index 00000000..d2d711c8 --- /dev/null +++ b/lib/vendor.rb @@ -0,0 +1,67 @@ +module FarMar + + class Vendor + @@all_vendors_by_market = nil + @@all_vendors = nil + attr_accessor :market_id, :vendor_id, :name, :employees + def initialize(market_id, vendor_id, name, employees) + @market_id = market_id + @vendor_id = vendor_id + @name = name + @employees = employees + end + + ##returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. + def products + return Product.by_vendor(@vendor_id) + end + + #sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. + def sales + return Sales.by_vendor(@vendor_id) + end + + #revenue: returns the the sum of all of the vendor's sales (in cents) + ##add for.each method to total all transaction_total(s) in hash + def revenue + total = 0 + Sales.by_vendor(@vendor_id).each do | sale_id, sale | + total += sale.transaction_total + end + return total + end + + def market + return Market.find(@market_id) + end + + def self.all + if @@all_vendors == nil + vendors = { } + CSV.read('./support/vendors.csv').each do |line| + vendors[line[0]] = Vendor.new(line[3],line[0],line[1],line[2]) + end + @@all_vendors = vendors + end + return @@all_vendors + end + + def self.find(vendor_id) + Vendor.all[vendor_id] + end + + def self.by_market(market_id) + if @@all_vendors_by_market == nil + vendors = { } + Vendor.all.each do |vendor_id, vendor| + if vendors[vendor.market_id] == nil + vendors[vendor.market_id] = { } + end + vendors[vendor.market_id][vendor.vendor_id] = vendor + end + @@all_vendors_by_market = vendors + end + return @@all_vendors_by_market[market_id] + end + end +end diff --git a/specs/mkt_spec.rb b/specs/mkt_spec.rb new file mode 100644 index 00000000..b085d77e --- /dev/null +++ b/specs/mkt_spec.rb @@ -0,0 +1,66 @@ +require_relative 'spec_helper' #get all the stuff we need for testing. + +module FarMar + describe Market do + #This allows us to just use a single instance ("magnolia") as a new Market throughout the tests. + describe "#initialize" do + + it "1 should create return instances of self" do + ballard = Market.new("85636", "Ballard", "737 Olive Way", "Seattle", "King", "WA", "98101") + magnolia = Market.new("1352", "Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199") + ballard.must_be_instance_of(Market) + magnolia.must_be_instance_of(Market) + end + + it "2 should reflect instance variables" do + magnolia = Market.new("11", "Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199") + magnolia.id.must_equal "11" + end + + it "3 should return instance variables" do + ballard = Market.new("85636", "Ballard", "737 Olive Way", "Seattle", "King", "WA", "98101") + ballard.name.must_equal("Ballard") + end + + it "4 should raise an error when entry invalid" do + magnolia = Market.new("1352", "Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199") + #I want to check that if we pass something that isn't a string in, that it doesn't try to score it. + proc {magnolia.name(21)}.must_raise(ArgumentError) + end + + it "5 should raise Argument Errors when needed" do + proc{ Market.new("Magnolia", "3510 W Commodore Way", "Seattle", "King", "WA", "98199") }.must_raise(ArgumentError) + end + end + + + it "6 should return a hash of objects" do + Market.all.must_be_kind_of(Hash) + end + + it "7 should return all objects of self" do + Market.all.length.must_equal 500 + end + end + + describe "8 Market.find" do + + it "9 should return object with self id" do + id = "1" + Market.find(id).name.must_equal("People's Co-op Farmers Market") + end + it "10 should return a hash of Market(id)" do + Market.find("333").must_be_kind_of(FarMar::Market) + end + it "11 should raise error if the id is invalid" do + Market.find(891).must_be_nil + end + + describe "12 Vendor" do + + it "13 should return a hash of instances of Vendors associated with each market" do + Market.find("333").vendors.must_be_kind_of(Hash) + end + end + end +end diff --git a/specs/product_spec.rb b/specs/product_spec.rb new file mode 100644 index 00000000..4fc02dd1 --- /dev/null +++ b/specs/product_spec.rb @@ -0,0 +1,84 @@ +require_relative 'spec_helper' #get all the stuff we need for testing. +module FarMar + class Product + + describe "#initialize" do + + it "1 should return instances of self" do + rachels = Product.new("id", "name", "vendor_id") + sebastian = Product.new("1000", "kitty_town", "CatCafé") + rachels.must_be_instance_of(Product) + sebastian.must_be_instance_of(Product) + end + + it "2 should return instance variables" do + downtown = Product.new("2234", "Emmanuel", "Natasha") + downtown.name.must_equal("Emmanuel") + end + + it "3 should return vendor_id" do + ballard = Product.new("Ballard", "1123", "Natasha") + ballard.vendor_id.must_equal("Natasha") + end + + it "4 should raise an error when entry invalid" do + magnolia = Product.new("1352", "Gina", "Sam") + #I want to check that if we pass something that isn't a string in, that it doesn't try to score it. + proc {magnolia.name(nifty)}.must_raise(Exception) + end + + it "5 should raise Argument Errors when needed" do + proc{Product.new("Magnolia")}.must_raise(Exception) + end + end + + describe "Product.all" do + + it "6 should return a hash of objects" do + Product.all.must_be_kind_of(Hash) + end + + it "7 should return all objects" do + vendor_id = ["1", "444", "10", "476", "343", "11"].sample + Product.all.must_include(vendor_id) + end + end + end + describe "vendor" do + ##16,Obedient Fish,8 + + it "8 should return vendor name" do + test_product = Product.new("16", "Obedient Fish", "8") + test_product.vendor.name.must_equal("Stamm Inc") + end + + it " 9 produces appropriate vendor by id" do + + test_product = Product.new("18","Yellow Bread","8") + test_product.vendor_id.must_equal("8") + end + end + + + + describe "find" do + + it "10 should return product objects with all its vendors" do + id = "15" + Product.find(id).name.must_equal("Comfortable Pretzel") + end + + it "11 should return a hash of Product(id)" do + ##56,Nom nom Beef,19 + Product.find("56").must_be_kind_of(FarMar::Product) + end + + it "12 should raise error if the id is invalid" do + Product.find(891).must_be_nil + end + + it "13 should return a hash of instances of Vendors associated with each market" do + Product.find("333").vendor.must_be_kind_of(FarMar::Vendor) + end + end +end diff --git a/specs/sale_spec.rb b/specs/sale_spec.rb new file mode 100644 index 00000000..830f8fe3 --- /dev/null +++ b/specs/sale_spec.rb @@ -0,0 +1,31 @@ +module FarMar + + class Sales + describe "vendor" do + it "1 should return the vendor associated with the sale instance by using its @vendor_id" do + test_sale = Sales.new("100","4573","2013-11-08 15:18:32 -0800","19","56") + test_sale.vendor.vendor_id.must_equal("19") + end + end + end + + describe "product" do + it "2 should return the product instance of the sale" do + test_sale = Sales.new("100",4573,"2013-11-08 15:18:32 -0800","19","56") + test_sale.product.name.must_equal("Nom nom Beef") + end + end + + describe "find" do + it "3 should find an instance of Sales by its sales_id" do + #("100","4573","2013-11-08 15:18:32 -0800","19","56") + Sales.find("100").transaction_total.must_be_instance_of(Fixnum) + end + end + + describe "self.between" do + it "should return all sales between arguments" do + Sales.between("2013-11-10 12:26:30 -0800","2013-11-09 13:30:15 -0800" ).must_be_instance_of(Hash) + end + end +end diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..3a01cf57 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,12 @@ +require 'simplecov' +require 'csv' + +SimpleCov.start +require_relative '../FarMar' +require 'minitest' +require 'minitest/spec' +require "minitest/autorun" +require "minitest/reporters" +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new diff --git a/specs/vendor_spec.rb b/specs/vendor_spec.rb new file mode 100644 index 00000000..4bf5d02d --- /dev/null +++ b/specs/vendor_spec.rb @@ -0,0 +1,89 @@ +require_relative 'spec_helper' #get all the stuff we need for testing. + +module FarMar + describe Vendor do + #This allows us to just use a single instance ("magnolia") as a new Market throughout the tests. + describe "#initialize" do + + it "1 should return instances of self" do + rachels = Vendor.new("market_id", "vendor_id", "name of vendor", "num_employees") + sebastian = Vendor.new("1000", "kitty_town", "CatCafé", "N&E") + rachels.must_be_instance_of(Vendor) + sebastian.must_be_instance_of(Vendor) + end + + it "2 should return instance variables" do + downtown = Vendor.new("2234", "1123", "Emmanuel", "Natasha") + downtown.name.must_equal("Emmanuel") + end + + it "3 should return market_id" do + ballard = Vendor.new("Ballard", "1123", "Emmanuel", "Natasha") + ballard.market_id.must_equal("Ballard") + end + + it "4 should raise an error when entry invalid" do + magnolia = Vendor.new("1352", "Magnolia", "Gina", "Sam") + #I want to check that if we pass something that isn't a string in, that it doesn't try to score it. + proc {magnolia.name(nifty)}.must_raise(Exception) + end + + it "5 should raise Argument Errors when needed" do + proc{ Vendor.new("Magnolia") }.must_raise(ArgumentError) + end + end + + describe "Vendor.all" do + + it "6 should return a hash of objects" do + Vendor.all.must_be_kind_of(Hash) + end + + it "7 should return all objects" do + vendor_id = ["1", "444", "10", "476", "343", "11"].sample + Vendor.all.must_include(vendor_id) + end + end + + describe "Vendor #products" do + + it "8 should return all products by vendor_id" do + test_vendor = Vendor.all["11"] + test_vendor.products.must_include("29") + end + + it "9 find vendor" do + test_vendor = Vendor.new("2","8","Stamm Inc","2") + test_vendor.products.must_include("16") + end + end + + describe "#find" do + + it "10 should return market objects with all its vendors" do + id = "1" + Market.find(id).name.must_equal("People's Co-op Farmers Market") + end + + it "11 should return a hash of Market(id)" do + Market.find("333").must_be_kind_of(FarMar::Market) + end + + it "12 should raise error if the id is invalid" do + Market.find(891).must_be_nil + end + + it "13 should return a hash of instances of Vendors associated with each market" do + Market.find("333").vendors.must_be_kind_of(Hash) + end + end + + # describe "#sales" do + # + # it "14 should return sales by vendor" do + # test_vendor = Vendor.new("9","Quigley, Breitenberg and Schuster","2","2") + # test_vendor.sales.must_include("49")#3697,2013-11-11 18:43:56 -0800,9,20 + # end + # end +end +end