diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..251342cb --- /dev/null +++ b/.gitignore @@ -0,0 +1,51 @@ +*.gem +*.rbc +/.config +/coverage/ +/InstalledFiles +/pkg/ +/spec/reports/ +/spec/examples.txt +/test/tmp/ +/test/version_tmp/ +/tmp/ + +# Used by dotenv library to load environment variables. +# .env + +## Specific to RubyMotion: +.dat* +.repl_history +build/ +*.bridgesupport +build-iPhoneOS/ +build-iPhoneSimulator/ + +## Specific to RubyMotion (use of CocoaPods): +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# vendor/Pods/ + +## Documentation cache and generated files: +/.yardoc/ +/_yardoc/ +/doc/ +/rdoc/ + +## Environment normalization: +/.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 +.DS_Store \ No newline at end of file diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 00000000..73f861e5 --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +farmar diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..276cbf9e --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.3.0 diff --git a/Rakefile b/Rakefile new file mode 100644 index 00000000..9e53df88 --- /dev/null +++ b/Rakefile @@ -0,0 +1,15 @@ +# copied from Scrabble project + +require 'rake/testtask' + +Rake::TestTask.new do |t| + t.libs = ["lib", "specs"] + t.warning = false + t.verbose = false + t.test_files = FileList['specs/*_spec.rb'] + puts "Running TestTask" +end + +task default: :test do + puts "Running my Rakefile" +end \ No newline at end of file diff --git a/far_mar.rb b/far_mar.rb new file mode 100644 index 00000000..6e5635e2 --- /dev/null +++ b/far_mar.rb @@ -0,0 +1,52 @@ +# [x] gems your project needs +require 'csv' +require 'simplecov' +require 'minitest/reporters' + +# [x] our namespace module +module FarMar; end + +# all of our data classes that live in the module +#require 'lib/far_mar_market' + +# [x] ...require all needed classes +puts "Loading the project library." + +require './lib/Market' +require './lib/Product' +require './lib/Sale' +require './lib/Vendor' + +puts "Done Loading library!" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/Market.rb b/lib/Market.rb new file mode 100644 index 00000000..eaf800af --- /dev/null +++ b/lib/Market.rb @@ -0,0 +1,103 @@ +class FarMar::Market + + attr_reader :id, :name, :address, :city, :county, :state, :zip_code + + def initialize(id, name, address, city, county, state, zip_code) # could take an array + #initialize is NOT a constructor see the iterwebs + @id = id + @name = name + @address = address + @city = city + @county = county + @state = state + @zip_code = zip_code + + end + + # returns an array of market objects e.g [#] + def self.all # works as desired [] repeat for each class + + market_array = [] + + CSV.foreach('./support/markets.csv') do |line| + # maybe I do need to build an hash (dict) + # instance variables would be the key/ import csv data would be the value + id = line[0] + name = line[1] + address = line[2] + city = line[3] + county = line[4] + state = line[5] + zip_code = line[6] + + market_array << FarMar::Market.new(id, name, address, city, county, state, zip_code) + # market_array is an array of market objects e.g [#] + end + return market_array + end + + # returns the market object or nil associated with the Market id + + def self.find(id) # this parameter takes string + # find the market object with the id + found_market = nil # because there is no string + + all.each do |market| + if market.id == id + found_market = market + break + end + end + return found_market + end + + # returns an array of vendors whose market_id matches this markets id + # instance method running inside an instance of market + # because it is running *inside* of market it can refer to @id + def get_vendors #111 for the test case + + found_vendors = [] + + FarMar::Vendor.all.each do |vendor| + if vendor.market_id == @id + found_vendors.push(vendor) + end + end + return found_vendors + end +end + +# testing content moved to far_mar.rb +# instance method must call on the INSTANCE +# class methods call directly on the class + +# initialize during the instantiation of the instance variables +# a market is a line +# a row is column + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/lib/Product.rb b/lib/Product.rb new file mode 100644 index 00000000..54db0950 --- /dev/null +++ b/lib/Product.rb @@ -0,0 +1,88 @@ +class FarMar::Product + + attr_reader :id, :product_name, :vendor_id + + def initialize(id, product_name, vendor_id) + + @id = id + @product_name = product_name + @vendor_id = vendor_id + + end + + def self.all + + product_array = [] + + CSV.foreach('./support/products.csv') do |line| + + id = line[0] + product_name = line[1] + vendor_id = line[2] + + product_array << FarMar::Product.new(id, product_name, vendor_id) + + end + return product_array + end + + def self.find(id) + + found_product = nil + + all.each do |product| + if product.id == id + found_product = product + break + end + end + return found_product + end + + def get_vendor + found_vendor = FarMar::Vendor.find(@vendor_id) + return found_vendor + end + + def get_sales + + found_sales = [] + FarMar::Sale.all.each do |sale| + if sale.product_id == @id + found_sales.push(sale) + end + end + return found_sales + end + + def sale_count + return get_sales.length + end + + def self.by_vendor(vendor_id) + found_products = [] + + all.each do |product| + if product.vendor_id == vendor_id + found_products.push(product) + end + end + return found_products + end + + +end + + + + + + + + + + + + + + diff --git a/lib/Sale.rb b/lib/Sale.rb new file mode 100644 index 00000000..39ddaf5f --- /dev/null +++ b/lib/Sale.rb @@ -0,0 +1,92 @@ +class FarMar::Sale # can only chain one to another + + attr_reader :id, :sale_amount, :purchase_date, :vendor_id, :product_id + + def initialize(id, sale_amount, purchase_date, vendor_id, product_id) + + @id = id + @sale_amount = sale_amount # in cents + @purchase_date = purchase_date + #WRONG! should not have to require vendor_id or product_id + @vendor_id = vendor_id + @product_id = product_id + + end + # these can be re-used with a base class and + # a method to read in the files + # it's called inheritance + + def self.all + + sale_array = [] + + CSV.foreach('./support/sales.csv') do |line| + id = line[0] + sale_amount = line[1] # in cents + purchase_date = line[2] + vendor_id = line[3] + product_id = line[4] + + sale_array << FarMar::Sale.new(id, sale_amount, purchase_date, vendor_id,product_id) + + end + return sale_array + end + + def self.find(id) + + found_sale = nil + + all.each do |sale| + if sale.id == id + found_sale = sale + break + end + end + return found_sale + end + + def get_vendor + found_vendor = FarMar::Vendor.find(@vendor_id) + return found_vendor + end + + def get_product + found_product = FarMar::Product.find(@product_id) + return found_product + end + + # expects string, returns Sale objects + def self.between(begin_time, end_time) + + found_sales = [] + + start_time = Time.parse(begin_time) + finish_time = Time.parse(end_time) + + all.each do |sale| + + purchase_date = sale.purchase_date + sale_time = Time.parse(purchase_date) + + if sale_time >= start_time && + sale_time <= finish_time + + found_sales.push(sale) + end + end + return found_sales + end + +end + + + + + + + + + + + diff --git a/lib/Vendor.rb b/lib/Vendor.rb new file mode 100644 index 00000000..164c46a0 --- /dev/null +++ b/lib/Vendor.rb @@ -0,0 +1,94 @@ +class FarMar::Vendor + + attr_reader :id, :vendor_name, :employee_count, :market_id + + def initialize(id, vendor_name, employee_count, market_id) + + @id = id + @vendor_name = vendor_name + @employee_count = employee_count + @market_id = market_id + #WRONG! # should not have to enter market_id here + + end + # returns an array of Vendors + def self.all + + vendor_array = [] + + CSV.foreach('./support/vendors.csv') do |line| + id = line[0] + vendor_name = line[1] + employee_count = line[2] + market_id = line[3] + + vendor_array << FarMar::Vendor.new(id, vendor_name, employee_count, market_id) + end + return vendor_array + end + + def self.find(id) + + found_vendor = nil + + all.each do |vendor| + if vendor.id == id + found_vendor = vendor + break + end + end + return found_vendor + end + + #this method returns an instance of market that matches this vendors market_id + def get_market + found_market = FarMar::Market.find(@market_id) + return found_market + end + + def get_products + + found_products = [] + + FarMar::Product.all.each do |product| + if product.vendor_id == @id + found_products.push(product) + end + end + return found_products + end + + def get_sales + + found_sales = [] + FarMar::Sale.all.each do |sale| + if sale.vendor_id == @id + found_sales.push(sale) + end + end + return found_sales + end + + def get_revenue + found_sales = get_sales + sum_revenue = 0 + found_sales.each do |sale| + sum_revenue += sale.sale_amount.to_f + end + return sum_revenue + end + + def self.by_market(market_id) + found_vendors = [] + + all.each do |vendor| + if vendor.market_id == market_id + found_vendors.push(vendor) + end + end + return found_vendors + end +end + + + diff --git a/project_plan.txt b/project_plan.txt new file mode 100644 index 00000000..352a1be1 --- /dev/null +++ b/project_plan.txt @@ -0,0 +1,121 @@ +Project Structure + +Create a Ruby class to represent each kind of data in the support/ directory. In the Product Data section, there are detailed descriptions of each csv file contents and its relation to other objects in the system. Your implementation will have class methods to handle finding, sorting, and collecting the data into instances representing individual rows of data. Each of those instances will have instance methods to provide details about the object. + +Encapsulation + +[ ] file named /far_mar.rb. It will manage our data classes. It should look something like: + +# gems your project needs +require 'csv' + +# our namespace module +module FarMar; end + +# all of our data classes that live in the module +require 'lib/farmar_market' +# ...require all needed classes +Each class you build will live in the /lib directory, and belong to the FarMar module: + +# lib/farmar_market.rb +class FarMar::Market + # Your code goes here +end + +The module provides a namespace for the application. A namespace ensures the classes we create will not 'collide' or 'overlap' with a class that could exist elsewhere in a codebase (like in a gem). + +For example, Sale is a very generic class name that could very realistically exist in many codebases. Creating a module called FarMar allows us to specify which Sale we're talking about; FarMar::Sale is much more explicit and likely to be unique. + +Specs & Testing + +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 + +Each individual market has many vendors associated with it. The FarMar::Market data, in order in the CSV, consists of: + +ID - (Fixnum) a unique identifier for that market +Name - (String) the name of the market (not guaranteed unique) +Address - (String) street address of the market +City - (String) city in which the market is located +County - (String) county in which the market is located +State - (String) state in which the market is located +Zip - (String) zipcode in which the market is located +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: + +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 +FarMar::Product + +Each product belongs to a vendor. The vendor_id field refers to the FarMar::Vendor ID field. The FarMar::Product data, in order in the CSV, consists of: + +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 +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: + +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 +Requirements +############################## +Baseline +############################## +Project Setup +############################### +You'll be working as an individual on this project. - but it's ok to get help from collleagues +################################### + +[x] Fork the Ada-C6 repo to your Github account. +[x] Clone your fork to your projects directory, and cd into the cloned repo. +Create a gemset for your project +$ echo 2.3.0 > .ruby-version +$ echo farmar > .ruby-gemset +$ cd . +Install necessary gems via Terminal: +$ gem install minitest-reporters +$ gem install simplecov +Baseline Requirements + +Create a class for each of the data types listed above. Each class should be a part of the FarMar module. +You should be able to create instances of these classes that know about their associated data file. +Create your far_mar.rb file which will bring together all classes created in the previous step. +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 + +For each of the data classes build the following methods: + +self.all: returns a collection of instances, representing all of the objects described in the CSV +self.find(id): returns an instance of the object where the value of the id field in the CSV matches the passed parameter. +Additional FarMar::Market Methods + +#vendors: returns a collection of FarMar::Vendor instances that are associated with the market by the market_id field. +Additional FarMar::Vendor Methods + +#market: returns the FarMar::Market instance that is associated with this vendor using the FarMar::Vendor market_id field +#products: returns a collection of FarMar::Product instances that are associated by the FarMar::Product vendor_id field. +#sales: returns a collection of FarMar::Sale instances that are associated by the vendor_id field. +#revenue: returns the the sum of all of the vendor's sales (in cents) +self.by_market(market_id): returns all of the vendors with the given market_id +Additional FarMar::Product Methods + +#vendor: returns the FarMar::Vendor instance that is associated with this vendor using the FarMar::Product vendor_id field +#sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. +#number_of_sales: returns the number of times this product has been sold. +self.by_vendor(vendor_id): returns all of the products with the given vendor_id +Additional FarMar::Sale Methods + +#vendor: returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field +#product: returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field +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 \ No newline at end of file diff --git a/specs/Market_spec.rb b/specs/Market_spec.rb new file mode 100644 index 00000000..bc838b88 --- /dev/null +++ b/specs/Market_spec.rb @@ -0,0 +1,73 @@ +require_relative 'spec_helper.rb' +require_relative '../far_mar.rb' + + +# copied shamelessy and without remorse from Scrabble + +describe "Testing market" do + # test for the self.all method + it "creates a new instance of Market" do + + id = 4 + name = "Nada Whole Fudz" + address = "1215 Harry Hines Blvd" + city = "Portland" + county = "King" + state = "Washington" + zip_code = 98101 + + new_market1 = FarMar::Market.new(id, name, address, city, county, state, zip_code) + + new_market1.id.must_equal id + new_market1.name.must_equal name + new_market1.address.must_equal address + new_market1.city.must_equal city + new_market1.county.must_equal county + new_market1.state.must_equal state + new_market1.zip_code.must_equal 98101 + + end + + it "Tests that we can call self.all " do + new_market_array = FarMar::Market.all + new_market_array.must_be_kind_of Array + new_market_array[0].must_be_kind_of FarMar::Market + + end + + it "Tests that the object number == the CSV array length" do + # returns an array of an array of strings + new_market_array = FarMar::Market.all + csv_array_array = CSV.read('./support/markets.csv') + csv_array_array.length.must_equal new_market_array.length + end + + # testing the self.find methods + + it "Tests that the id parameter returns a matching market id" do + new_market = FarMar::Market.find('474') + new_market.must_be_kind_of FarMar::Market + new_market.id.must_equal '474' + end + + it "Tests that the method returns nil if no match for market id found" do + new_market = FarMar::Market.find('501') + new_market.must_equal nil + + end + + it "Tests that a market id will return its associated vendor(s)" do + new_market = FarMar::Market.find('111') + found_vendors = new_market.get_vendors + found_vendors.must_be_kind_of Array + found_vendors.length.must_equal 2 + + found_vendors.each do |vendor| + vendor.market_id.must_equal "111" + end + + end + + + +end \ No newline at end of file diff --git a/specs/Product_spec.rb b/specs/Product_spec.rb new file mode 100644 index 00000000..b3c362cc --- /dev/null +++ b/specs/Product_spec.rb @@ -0,0 +1,85 @@ +require_relative 'spec_helper.rb' +require_relative '../far_mar.rb' + +describe "Testing product" do + # tests for the self.all method + it "creates a new instance of Vendor" do + id = 6 + product_name = "Smooth Mushrooms" + vendor_id = 4 + + quotidian_product = FarMar::Product.new(id, product_name, vendor_id) + quotidian_product.id.must_equal id + quotidian_product.product_name.must_equal product_name + end + + it "Tests that we can call self.all" do + quotidian_product_array = FarMar::Product.all + quotidian_product_array.must_be_kind_of Array + quotidian_product_array[0].must_be_kind_of FarMar::Product + end + + it "Test that the object number == the CSV array length" do + quotidian_product_array = FarMar::Product.all + csv_array_array = CSV.read('./support/products.csv') + csv_array_array.length.must_equal quotidian_product_array.length + end + # testing the self.find methods + + it "Tests that the id parameter returns a matching product id" do + quotidian_product = FarMar::Product.find('30') + quotidian_product.must_be_kind_of FarMar::Product + quotidian_product.id.must_equal '30' + end + + it "Tests that the method returns nil if no match is found" do + quotidian_product = FarMar::Product.find('8195') + quotidian_product.must_equal nil + end + + it "Tests that get_vendor returns an instance of FarMar::Vendor using the FarMar::Product vendor_id field" do + new_product = FarMar::Product.find('30') + found_vendor = new_product.get_vendor + #vendor 11, depressed beets + found_vendor.id.must_equal '11' + end + + #sales: returns a collection of FarMar::Sale instances that are associated using the FarMar::Sale product_id field. + + # 64,8362,2013-11-07 05:30:08 -0800,11,30 + it "Tests that it returns an array of FarMar::Sale instance that use the FarMar::Sale product_id field" do + new_product = FarMar::Product.find('30') + found_sales = new_product.get_sales + found_sales.length.must_equal 1 + found_sales.first.product_id.must_equal '30' + end + + it "Tests that sale_count returns the count of sales for the instance of a product" do + new_product = FarMar::Product.find('30') + sale_count_of_product = new_product.sale_count + sale_count_of_product.must_equal 1 + end + + it "Test that it self.by_vendor(vendor_id): returns all of the products with the given vendor_id" do + found_products = FarMar::Product.by_vendor('474') + found_products.length.must_equal 5 + + end + +end + + + + + + + + + + + + + + + + diff --git a/specs/Sale_spec.rb b/specs/Sale_spec.rb new file mode 100644 index 00000000..08f394e1 --- /dev/null +++ b/specs/Sale_spec.rb @@ -0,0 +1,111 @@ +require_relative 'spec_helper.rb' +require_relative '../far_mar.rb' +require 'date' + +describe "Testing sale" do + + it "creates a new instance of Sale" do + id = 5 + sale_amount = 4440 + purchase_date = "2013-11-10 05:19:05 -0800" + vendor_id = 1 + product_id = 1 + + # instantiates new sale for testing + quotidian_sale = FarMar::Sale.new(id, sale_amount, purchase_date, vendor_id, product_id) + quotidian_sale.id.must_equal id + quotidian_sale.sale_amount.must_equal sale_amount + quotidian_sale.purchase_date.must_equal purchase_date + quotidian_sale.vendor_id.must_equal vendor_id + quotidian_sale.product_id.must_equal product_id + + end + + it "Tests that we can call self.all" do + quotidian_sale = FarMar::Sale.all + quotidian_sale.must_be_kind_of Array + quotidian_sale[0].must_be_kind_of FarMar::Sale + end + + it "Tests that the object number == the CSV array length" do + quotidian_sale_array = FarMar::Sale.all + csv_sale_array = CSV.read('./support/sales.csv') + csv_sale_array.length.must_equal quotidian_sale_array.length + end + + # testing the self.find methods + + it "Tests that the id parameter returns a matching sale id" do + quotidian_sale = FarMar::Sale.find('32') + quotidian_sale.must_be_kind_of FarMar::Sale + quotidian_sale.id.must_equal '32' + end + + it "Tests that the method returns nil if no match for the sale id is found" do + quotidian_sale = FarMar::Sale.find('13000') + quotidian_sale.must_equal nil + end + + it "Tests that the get_vendor method returns the FarMar::Vendor instance that is associated with this sale using the FarMar::Sale vendor_id field" do + new_sale = FarMar::Sale.find('32') + found_vendor = new_sale.get_vendor + #vendor_id 7 + found_vendor.id.must_equal '7' + end + + it "Tests that get_product returns the FarMar::Product instance that is associated with this sale using the FarMar::Sale product_id field" do + new_sale = FarMar::Sale.find('32') + found_product = new_sale.get_product + #product_id 14 + found_product.id.must_equal '14' + end + + # "2013-11-13 04:14:40 -0800" + it "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" do + + # tests for a single instance + start_time = "2013-11-13 04:14:40 -0800" + end_time = "2013-11-13 04:14:40 -0800" + + found_sales = FarMar::Sale.between(start_time, end_time) + found_sales.length.must_equal 1 + + # tests for many instances + start_time = "2013-11-13 00:00:00 -0800" + end_time = "2013-11-13 23:59:59 -0800" + + found_sales = FarMar::Sale.between(start_time, end_time) + found_sales.length.must_equal 669 + + # tests for instances where no sales are made + start_time = "2013-11-13 23:59:59 -0800" + end_time = "2013-11-13 00:00:00 -0800" + + found_sales = FarMar::Sale.between(start_time, end_time) + found_sales.length.must_equal 0 + + + # dr. who test because wiggly wobbly timey wimey + start_time = "1900-01-01 00:00:01 -0800" + end_time = "3000-12-31 23:59:59 -0800" + + found_sales = FarMar::Sale.between(start_time, end_time) + found_sales.length.must_equal 12798 + end + + +end + + + + + + + + + + + + + + diff --git a/specs/Vendor_spec.rb b/specs/Vendor_spec.rb new file mode 100644 index 00000000..061fbe04 --- /dev/null +++ b/specs/Vendor_spec.rb @@ -0,0 +1,103 @@ +require_relative 'spec_helper.rb' +require_relative '../far_mar.rb' + +describe "Testing vendor" do + # test for the self.all method + it "creates a new instance of Vendor" do + # dummy data + id = 4 + vendor_name = "Nada && Sons" + employee_count = 10 + market_id = 5 + # instantiates new vendor for testing + new_vendor1 = FarMar::Vendor.new(id, vendor_name, employee_count, market_id) + + new_vendor1.id.must_equal id + new_vendor1.vendor_name.must_equal vendor_name + new_vendor1.employee_count.must_equal employee_count + new_vendor1.market_id.must_equal market_id + + end + + it "Tests that we can call self.all " do + new_vendor_array = FarMar::Vendor.all + new_vendor_array.must_be_kind_of Array + new_vendor_array[0].must_be_kind_of FarMar::Vendor + + end + + it "Tests that the object number == the CSV array length" do + # # returns an array of an array of strings + new_vendor_array = FarMar::Vendor.all + csv_array_array = CSV.read('./support/vendors.csv') + csv_array_array.length.must_equal new_vendor_array.length + end + + # testing the self.find methods + + it "Tests that the id parameter returns a matching market id" do + new_vendor = FarMar::Vendor.find('474') + new_vendor.must_be_kind_of FarMar::Vendor + new_vendor.id.must_equal '474' + end + + it "Tests that the method returns nil if no match for market id found" do + new_vendor = FarMar::Vendor.find('2691') + new_vendor.must_equal nil + end + + # testing the additional FarMar::Vendor methods + it "Tests that the instance method named get_market returns the FarMar::Market instance associated with the vendor using FarMar::Vendor.market_id field" do + new_vendor = FarMar::Vendor.find('474') + found_market = new_vendor.get_market + found_market.id.must_equal '89' + end + + it "Tests that the instance method named get_products returns a list of FarMar::Product instances associated by vendor id" do + new_vendor = FarMar::Vendor.find('474') + found_products = new_vendor.get_products + found_products.must_be_kind_of Array + found_products.length.must_equal 5 + + found_products.each do |product| + product.vendor_id.must_equal "474" + end + end + + it "Tests that the instance method named get_sales returns a list of FarMar::Sale instances associated by vendor_id field" do + new_vendor = FarMar::Vendor.find('474') + found_sales = new_vendor.get_sales + found_sales.length.must_equal 2 + + found_sales.each do |sale| + sale.vendor_id.must_equal '474' + end + end + + it "Tests that the instance method get_revenue returns the sum of sales for the instance of vendor in cents." do + new_vendor = FarMar::Vendor.find('474') + revenue_earned = new_vendor.get_revenue + revenue_earned.must_equal 11176 + end + + it "Tests that self.by_market returns all vendors associated with a market_id" do + found_vendors = FarMar::Vendor.by_market('2') + found_vendors.length.must_equal 3 + + found_vendors.each do |vendor| + vendor.market_id.must_equal '2' + end + end +end + + + + + + + + + + + + diff --git a/specs/spec_helper.rb b/specs/spec_helper.rb new file mode 100644 index 00000000..d743c6e2 --- /dev/null +++ b/specs/spec_helper.rb @@ -0,0 +1,10 @@ +require 'simplecov' +SimpleCov.start #class method + +require 'minitest' +require 'minitest/spec' +require 'minitest/autorun' +require 'minitest/reporters' +require 'minitest/pride' + +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new \ No newline at end of file