From ce311d77e56df22abcda20cdaa4b601d197931c3 Mon Sep 17 00:00:00 2001 From: Tehut Getahun Date: Thu, 20 Apr 2017 14:04:43 -0700 Subject: [PATCH 1/2] added relationship tests for product-merchant on product model --- test/fixtures/products.yml | 3 ++- test/models/product_test.rb | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index e36800e24c..4388620738 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -6,6 +6,7 @@ one: quantity: 3 price: 1.5 category: doll + merchant: :one aliens_two: name: aliens @@ -75,4 +76,4 @@ no_category: description: she's a tour guide, she's a barbie quantity: 5 price: -5 - category: + category: diff --git a/test/models/product_test.rb b/test/models/product_test.rb index 7b3f586bd0..d74a54afd6 100644 --- a/test/models/product_test.rb +++ b/test/models/product_test.rb @@ -46,16 +46,22 @@ end -#merging this into master with the following test commented out because -#im getting some wierd errors on the Merchant model that I can't get green -#right now + describe "testing relations" do + it "has one merchant (belongs to a merchant)" do + product = products(:one) + product.merchant.nil?.must_equal false + end + + it "can find a merchant's name through the relationship" do + product = products(:one) + product.merchant.name.must_equal "Marisol" + end - # describe "testing relations" do - # - # it "has one merchant (belongs to a merchant)" do - # product = products(:one) - # product.merchant.nil?.must_equal false - # end + it "can be found through the merchant's information" do + merchant = merchants(:one) + merchant.products.find_by(name:"aliens").class.must_equal Product + end + #Order-Products class has not been built out. Will test more after end end From 26091853ade7c8ca81dfaa80f0d0a99d7582258a Mon Sep 17 00:00:00 2001 From: Tehut Getahun Date: Sat, 22 Apr 2017 08:32:23 -0700 Subject: [PATCH 2/2] added edit and delete tests and methods on product controller --- app/controllers/products_controller.rb | 53 ++++++++++ app/views/layouts/application.html.erb | 16 +++ app/views/products/edit.html.erb | 0 app/views/products/index.html.erb | 0 app/views/products/new.html.erb | 12 +++ app/views/products/show.html.erb | 12 +++ config/routes.rb | 13 +++ test/controllers/products_controller_test.rb | 105 ++++++++++++++++++- test/controllers/welcome_controller_test.rb | 7 -- test/fixtures/products.yml | 1 + 10 files changed, 209 insertions(+), 10 deletions(-) create mode 100644 app/views/products/edit.html.erb create mode 100644 app/views/products/index.html.erb create mode 100644 app/views/products/new.html.erb create mode 100644 app/views/products/show.html.erb delete mode 100644 test/controllers/welcome_controller_test.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index f1ad12ddea..c00a9b3778 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -1,2 +1,55 @@ class ProductsController < ApplicationController + def index + @products = Product.all + end + + def new + @product = Product.new + end + + def create + @product = Product.new(product_params) + @product.save + if @product.save + redirect_to products_path + else + + render :new, status: :bad_request + flash[:failure] = "Could not add your product" + flash[:messages] = @product.errors.messages + end + end + + def show + @product = Product.find_by(id: params[:id]) + head :not_found if @product.nil? + end + + def edit + @product = Product.find_by(id: params[:id]) + end + + + def update + @product = Product.find_by(id: params[:id]) + @product.update_attributes(product_params) + @product.save + + if @product.save + redirect_to product_path(params[:id]) + else + render :edit, status: :bad_request + end + end + + def destroy + @product = Product.find_by(id: params[:id]) + @product.destroy + redirect_to products_path + end + + private + def product_params + return params.require(:product).permit(:name, :quantity, :category, :price, :description, :merchant_id) + end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 008c56f923..9efede1252 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,6 +9,22 @@ +
+
+
+

<%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %>

+ <% if flash[:message] %> + + <% flash.each do |name, problems| %> + <%= problems %>
+ <% end %> + + <% end %> +
+ <%end%> + <%= yield %> diff --git a/app/views/products/edit.html.erb b/app/views/products/edit.html.erb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/views/products/index.html.erb b/app/views/products/index.html.erb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/views/products/new.html.erb b/app/views/products/new.html.erb new file mode 100644 index 0000000000..8b09658279 --- /dev/null +++ b/app/views/products/new.html.erb @@ -0,0 +1,12 @@ +<% if flash[:result_text] or flash[:message] %> +
+

<%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %>

+ <% if flash[:message] %> + + <% flash.each do |name, problems| %> + <%= problems %>
+ <% end %> + + <% end %> +
+ <%end%> diff --git a/app/views/products/show.html.erb b/app/views/products/show.html.erb new file mode 100644 index 0000000000..8b09658279 --- /dev/null +++ b/app/views/products/show.html.erb @@ -0,0 +1,12 @@ +<% if flash[:result_text] or flash[:message] %> +
+

<%= flash[:status] == :failure ? "A problem occurred: " : "" %><%= flash[:result_text] %>

+ <% if flash[:message] %> + + <% flash.each do |name, problems| %> + <%= problems %>
+ <% end %> + + <% end %> +
+ <%end%> diff --git a/config/routes.rb b/config/routes.rb index 787824f888..87b28e011a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,16 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + get '/products', to: 'products#index' + get '/products/new', to:'products#new', as: "new_product" + post '/products', to: 'products#create' + get '/products/:id', to:'products#show', as:'product' + get 'products/:id/edit', to:'products#edit', as: 'edit_product' + patch '/products/:id', to:'products#update' + + + delete '/products/:id', to:'products#destroy' + + + # get '/' + end diff --git a/test/controllers/products_controller_test.rb b/test/controllers/products_controller_test.rb index 392a20e292..d4b36ea4e2 100644 --- a/test/controllers/products_controller_test.rb +++ b/test/controllers/products_controller_test.rb @@ -1,7 +1,106 @@ require "test_helper" describe ProductsController do - # it "must be a real test" do - # flunk "Need real tests" - # end + describe "index" do + it "lists all the products" do + Product.count.must_be :>, 0 + get products_path + must_respond_with :success + end + + it "still responds successfully when there are no works" do + Product.destroy_all + get products_path + must_respond_with :success + end + + end + + describe "new" do + it "successfully loads the new product page" do + get new_product_path + must_respond_with :success + end + end + + describe "create" do + it "adds a product to the database" do + start_count = Product.count + product_data = {product:{name:"green", quantity:5, price:10, category:"toy", merchant_id: Merchant.first.id}} + + post products_path, params: product_data + must_redirect_to products_path + end_count = Product.count + end_count.must_be :==, start_count + 1 + end + + it "re-renders the new work form if the work is invalid" do + start_count = Product.count + product_data = {product:{name:"green", quantity:5, price:10, category:"toy", merchant_id:""}} + + post products_path, params: product_data + must_respond_with :bad_request + end_count = Product.count + end_count.must_be :==, start_count + + end + end + + describe "show" do + it "must show a specified product" do + first = products(:one) + get product_path(first.id) + must_respond_with :success + end + + it "returns 404 not found if the product id doesn't exist" do + product_id = Product.last.id + product_id += 1 + get product_path(product_id) + must_respond_with :not_found + end + end + + describe "edit" do + it "successfully loads the edit product page" do + product = products(:one) + get edit_product_path(product.id) + must_respond_with :success + end + end + + describe "update" do + it "changes a record in the database" do + product = products(:one) + product_data = {product:{name:"NewName"}} + patch product_path(product.id), params: product_data + + must_redirect_to product_path(product.id) + end + + it "redirects to edit path if update invalid" do + product = products(:one) + product_data = {product:{name:""}} + patch product_path(product), params: product_data + + must_respond_with :bad_request + # must_redirect_to edit_product_path + #removed test for edit redirect because the :bad_request message was + #all that was getting through + + #if I delete that line in the controller then I can detec the redirect + end + end + + describe "destroy" do + it "destroys a record in the database" do + product = products(:one) + start_count = Product.count + delete product_path(product) + must_redirect_to products_path + end_count = Product.count + + end_count.must_be :==, start_count - 1 + end + end end diff --git a/test/controllers/welcome_controller_test.rb b/test/controllers/welcome_controller_test.rb deleted file mode 100644 index 877b7e7456..0000000000 --- a/test/controllers/welcome_controller_test.rb +++ /dev/null @@ -1,7 +0,0 @@ -require "test_helper" - -describe WelcomeController do - # it "must be a real test" do - # flunk "Need real tests" - # end -end diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml index 4388620738..45df328d36 100644 --- a/test/fixtures/products.yml +++ b/test/fixtures/products.yml @@ -7,6 +7,7 @@ one: price: 1.5 category: doll merchant: :one + id: 1 aliens_two: name: aliens