make more specs work

This commit is contained in:
2025-09-23 17:45:00 -05:00
parent 6596693238
commit 7dadb28004
7 changed files with 84 additions and 52 deletions
+2 -2
View File
@@ -118,7 +118,7 @@ class List
def self.get_suppliers_users_count(lists, supplier_id: nil) def self.get_suppliers_users_count(lists, supplier_id: nil)
unique_supplier_user_ids = Set.new unique_supplier_user_ids = Set.new
lists.each do |list| lists.each do |list|
list.user_ids.each do |uid| Array.wrap(list.user_ids).each do |uid|
unique_supplier_user_ids << [list.supplier_id, uid] unique_supplier_user_ids << [list.supplier_id, uid]
end end
end end
@@ -140,7 +140,7 @@ class List
def self.enrich_users_number_of_lists_at_supplier(supplier_id, lists) def self.enrich_users_number_of_lists_at_supplier(supplier_id, lists)
counts = get_suppliers_users_count(lists, supplier_id: supplier_id) counts = get_suppliers_users_count(lists, supplier_id: supplier_id)
lists.each do |list| lists.each do |list|
list.users.each do |user| Array.wrap(list.users).each do |user|
user.number_of_lists_at_supplier = counts[user.id] || 1 user.number_of_lists_at_supplier = counts[user.id] || 1
end end
end end
@@ -1,5 +1,6 @@
# encoding: UTF-8 # encoding: UTF-8
require 'rails_helper' require 'rails_helper'
describe DashboardController, type: :controller do describe DashboardController, type: :controller do
before :each do before :each do
setup_supplier_for_controller setup_supplier_for_controller
@@ -9,13 +10,19 @@ describe DashboardController, type: :controller do
it "does render the svg image" do it "does render the svg image" do
table = create :table, supplier: @supplier, number: 7 table = create :table, supplier: @supplier, number: 7
get :table_qr_image, params: {table_id: table.id}, format: :svg get :table_qr_image, params: {table_id: table.id}, format: :svg
expect(response.body).to include %[id="tspan3801">7</tspan>] doc = Nokogiri.parse(response.body)
element = doc.at_css('#tspan3801')
#expect(response.body).to include %[id="tspan3801">7</tspan>]
expect(element.text).to eq '7'
end end
it "does render the svg image with table label if present" do it "does render the svg image with table label if present" do
table = create :table, supplier: @supplier, number: 7, label: "The love seat" table = create :table, supplier: @supplier, number: 7, label: "The love seat"
get :table_qr_image, params: {table_id: table.id}, format: :svg get :table_qr_image, params: {table_id: table.id}, format: :svg
expect(response.body).to include %[id="tspan3801">7 - The love seat</tspan>] doc = Nokogiri.parse(response.body)
element = doc.at_css('#tspan3801')
#expect(response.body).to include %[id="tspan3801">7 - The love seat</tspan>]
expect(element.text).to eq '7 - The love seat'
end end
end end
end end
@@ -38,19 +38,19 @@ describe Suppliers::ListsController, type: :controller do
describe "GET #show" do describe "GET #show" do
it "assigns the requested list to @list" do it "assigns the requested list to @list" do
list = create :list, supplier: @supplier list = create :list, supplier: @supplier
get :show, id: list get :show, params: {id: list.id}
assigns(:list).should eq(list) assigns(:list).should eq(list)
end end
it "should not display a list of another supplier" do it "should not display a list of another supplier" do
list = create :list list = create :list
get :show, id: list get :show, params: {id: list.id}
response.status.should == 404 response.status.should == 404
end end
it "renders the #show view" do it "renders the #show view" do
list = create :list, supplier: @supplier list = create :list, supplier: @supplier
get :show, id: list get :show, params: {id: list.id}
response.body.should include 'orders' response.body.should include 'orders'
end end
end end
@@ -66,13 +66,13 @@ describe Suppliers::ListsController, type: :controller do
context "with valid attributes" do context "with valid attributes" do
it "creates a new list" do it "creates a new list" do
expect{ expect{
post :create, list: valid_attributes post :create, params: {list: valid_attributes}
}.to change(List, :count).by(1) }.to change(List, :count).by(1)
end end
it "should not be possible to create a list for another supplier" do it "should not be possible to create a list for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
post :create, list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id) post :create, params: {list: valid_attributes.merge(price: '6.66', supplier_id: supplier2.id)}
expect( List.find_by_price(6.66).supplier_id ).to eq @supplier.id expect( List.find_by_price(6.66).supplier_id ).to eq @supplier.id
end end
end end
@@ -80,13 +80,13 @@ describe Suppliers::ListsController, type: :controller do
context "with invalid attributes", broken: true do context "with invalid attributes", broken: true do
it "does not save the new list" do it "does not save the new list" do
expect{ expect{
post :create, format: :json, list: invalid_params post :create, params: {list: invalid_params}, format: :json
}.not_to change{ List.count } }.not_to change{ List.count }
List.count.should be_zero List.count.should be_zero
end end
it 'returns an error object' do it 'returns an error object' do
post :create, format: :json, list: invalid_params post :create, params: {list: invalid_params}, format: :json
expect( JSON.parse(response.body)['errors'] ).to be_present expect( JSON.parse(response.body)['errors'] ).to be_present
end end
end end
@@ -99,27 +99,27 @@ describe Suppliers::ListsController, type: :controller do
context "valid attributes" do context "valid attributes" do
it "located the requested list" do it "located the requested list" do
put :update, id: @list, list: valid_attributes put :update, params: {id: @list.id, list: valid_attributes}
@list.reload @list.reload
assigns(:list).should eq(@list) assigns(:list).should eq(@list)
end end
it "changes @list's price attribute" do it "changes @list's price attribute" do
put :update, id: @list, list: valid_attributes.merge(price: '7.22') put :update, params: {id: @list.id, list: valid_attributes.merge(price: '7.22')}
@list.reload @list.reload
@list.price.should eq(7.22) @list.price.should eq(7.22)
end end
it "should not be possible to update a list to another supplier" do it "should not be possible to update a list to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
put :update, id: @list, list: valid_attributes.merge(supplier_id: supplier2.id) put :update, params: {id: @list.id, list: valid_attributes.merge(supplier_id: supplier2.id)}
@list.reload @list.reload
@list.supplier_id.should == @supplier.id @list.supplier_id.should == @supplier.id
end end
it "should not be possible to update a list of another supplier" do it "should not be possible to update a list of another supplier" do
list = create :list, price: '7.22' list = create :list, price: '7.22'
put :update, id: list, list: {price: '6.66'} put :update, params: {id: list.id, list: {price: '6.66'}}
list.reload list.reload
list.price.should == 7.22 list.price.should == 7.22
end end
@@ -127,7 +127,7 @@ describe Suppliers::ListsController, type: :controller do
context "invalid attributes", broken: true do context "invalid attributes", broken: true do
it "returns an error response" do it "returns an error response" do
put :update, id: @list, format: :json, list: invalid_params put :update, params: {id: @list, list: invalid_params}, format: :json
expect( JSON.parse( response.body )['errors']).to be_present expect( JSON.parse( response.body )['errors']).to be_present
end end
end end
@@ -140,14 +140,14 @@ describe Suppliers::ListsController, type: :controller do
it "deletes the list" do it "deletes the list" do
expect{ expect{
delete :destroy, id: @list delete :destroy, params: {id: @list.id}
}.to change(List, :count).by(-1) }.to change(List, :count).by(-1)
end end
it "should not be possible to delete a list of another supplier" do it "should not be possible to delete a list of another supplier" do
list = create :list list = create :list
expect{ expect{
delete :destroy, id: list delete :destroy, params: {id: list.id}
}.to_not change(List, :count) }.to_not change(List, :count)
end end
end end
@@ -27,13 +27,13 @@ describe Suppliers::ProductCategoriesController, type: :controller do
describe "GET #show" do describe "GET #show" do
it "assigns the requested product_category to @product_category" do it "assigns the requested product_category to @product_category" do
product_category = create :product_category, supplier: @supplier product_category = create :product_category, supplier: @supplier
get :show, id: product_category, format: :json get :show, params: {id: product_category}, format: :json
assigns(:product_category).should eq(product_category) assigns(:product_category).should eq(product_category)
end end
it "should not display a product_category of another supplier" do it "should not display a product_category of another supplier" do
product_category = create :product_category product_category = create :product_category
get :show, id: product_category, format: :json get :show, params: {id: product_category}, format: :json
response.status.should == 404 response.status.should == 404
end end
end end
@@ -42,18 +42,18 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with valid attributes" do context "with valid attributes" do
it "creates a new product_category" do it "creates a new product_category" do
expect{ expect{
post :create, product_category: valid_attributes, format: :json post :create, params: {product_category: valid_attributes}, format: :json
}.to change(ProductCategory, :count).by(1) }.to change(ProductCategory, :count).by(1)
end end
it "redirects to the new product_category" do it "redirects to the new product_category" do
post :create, product_category: valid_attributes post :create, params: {product_category: valid_attributes}
response.should redirect_to [:suppliers, :product_categories] response.should redirect_to [:suppliers, :product_categories]
end end
it "should not be possible to create a product category for another supplier" do it "should not be possible to create a product category for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
post :create, product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id) post :create, params: {product_category: valid_attributes.merge(name: 'Trying to hack', supplier_id: supplier2.id)}
expect( ProductCategory.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id expect( ProductCategory.find_by_name('Trying to hack').supplier_id ).to eq @supplier.id
end end
end end
@@ -61,12 +61,12 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with invalid attributes" do context "with invalid attributes" do
it "does not save the new product_category" do it "does not save the new product_category" do
expect{ expect{
post :create, product_category: invalid_attributes, format: :json post :create, params: {product_category: invalid_attributes}, format: :json
}.not_to change{ ProductCategory.count } }.not_to change{ ProductCategory.count }
end end
it 'returns an error object' do it 'returns an error object' do
post :create, format: :json, product_category: invalid_attributes, format: :json post :create, params: {product_category: invalid_attributes}, format: :json
expect( JSON.parse(response.body)['errors'] ).to be_present expect( JSON.parse(response.body)['errors'] ).to be_present
end end
end end
@@ -79,20 +79,20 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "valid attributes" do context "valid attributes" do
it "located the requested product_category" do it "located the requested product_category" do
put :update, id: @product_category, product_category: valid_attributes put :update, params: {id: @product_category, product_category: valid_attributes}
@product_category.reload @product_category.reload
assigns(:product_category).should eq(@product_category) assigns(:product_category).should eq(@product_category)
end end
it "changes @product_category's attributes" do it "changes @product_category's attributes" do
put :update, id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest") put :update, params: {id: @product_category, product_category: valid_attributes.merge(name: "ChangedByTest")}
@product_category.reload @product_category.reload
@product_category.name.should eq("ChangedByTest") @product_category.name.should eq("ChangedByTest")
end end
it "should not be possible to update a product category to another supplier" do it "should not be possible to update a product category to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
put :update, id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id) put :update, params: {id: @product_category, product_category: valid_attributes.merge(name: "Trying to hack", supplier_id: supplier2.id)}
@product_category.reload @product_category.reload
expect( @product_category.name ).to eq 'Trying to hack' expect( @product_category.name ).to eq 'Trying to hack'
expect( @product_category.supplier_id ).to eq @supplier.id expect( @product_category.supplier_id ).to eq @supplier.id
@@ -100,7 +100,7 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "should not be possible to update a product_category of another supplier" do it "should not be possible to update a product_category of another supplier" do
product_category = create :product_category, name: 'Other supplier product_category' product_category = create :product_category, name: 'Other supplier product_category'
put :update, id: product_category, product_category: {name: "Trying to hack"} put :update, params: {id: product_category, product_category: {name: "Trying to hack"}}
expect(response.status).to eq 404 expect(response.status).to eq 404
product_category.reload product_category.reload
product_category.name.should == 'Other supplier product_category' product_category.name.should == 'Other supplier product_category'
@@ -109,7 +109,7 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with invalid attributes" do context "with invalid attributes" do
it "returns an error response" do it "returns an error response" do
put :update, id: @product_category, format: :json, product_category: invalid_attributes put :update, params: {id: @product_category, product_category: invalid_attributes}, format: :json
expect( JSON.parse( response.body )['errors']).to be_present expect( JSON.parse( response.body )['errors']).to be_present
end end
end end
@@ -122,14 +122,14 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "deletes the product_category" do it "deletes the product_category" do
expect{ expect{
delete :destroy, id: @product_category delete :destroy, params: {id: @product_category.id}
}.to change(ProductCategory, :count).by(-1) }.to change(ProductCategory, :count).by(-1)
end end
it "should not be possible to delete a product category of another supplier" do it "should not be possible to delete a product category of another supplier" do
product_category = create :product_category product_category = create :product_category
expect{ expect{
delete :destroy, id: product_category delete :destroy, params: {id: product_category.id}
}.to_not change{ProductCategory.count } }.to_not change{ProductCategory.count }
end end
end end
@@ -27,13 +27,13 @@ describe Suppliers::SectionsController, type: :controller do
describe "GET #show" do describe "GET #show" do
it "assigns the requested section to @section" do it "assigns the requested section to @section" do
section = create :section, supplier: @supplier section = create :section, supplier: @supplier
get :show, id: section.id, format: :json get :show, params: {id: section.id}, format: :json
assigns(:section).should eq(section) assigns(:section).should eq(section)
end end
it "should not display a section of another supplier" do it "should not display a section of another supplier" do
section = create :section section = create :section
get :show, id: section, format: :json get :show, params: {id: section.id}, format: :json
response.status.should == 404 response.status.should == 404
end end
end end
@@ -42,14 +42,14 @@ describe Suppliers::SectionsController, type: :controller do
context "with valid attributes" do context "with valid attributes" do
it "creates a new section" do it "creates a new section" do
expect{ expect{
post :create, section: valid_attributes post :create, params: {section: valid_attributes}
}.to change(Section, :count).by(1) }.to change(Section, :count).by(1)
expect( Section.last.supplier_id ).to eq @supplier.id expect( Section.last.supplier_id ).to eq @supplier.id
end end
it "should not be possible to create a section for another supplier" do it "should not be possible to create a section for another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
post :create, section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id) post :create, params: {section: valid_attributes.merge(title: 'Trying to hack', supplier_id: supplier2.id)}
expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id expect( Section.find_by_title('Trying to hack').supplier_id ).to eq @supplier.id
end end
end end
@@ -57,12 +57,12 @@ describe Suppliers::SectionsController, type: :controller do
context "with invalid attributes" do context "with invalid attributes" do
it "does not save the new section" do it "does not save the new section" do
expect{ expect{
post :create, format: :json, section: invalid_attributes post :create, params: {section: invalid_attributes}, format: :json
}.to_not change(Section, :count) }.to_not change(Section, :count)
end end
it "returns an error response" do it "returns an error response" do
post :create, format: :json, section: invalid_attributes post :create, params: {section: invalid_attributes}, format: :json
expect( JSON.parse(response.body )['errors'] ).to be_present expect( JSON.parse(response.body )['errors'] ).to be_present
end end
end end
@@ -75,27 +75,27 @@ describe Suppliers::SectionsController, type: :controller do
context "valid attributes" do context "valid attributes" do
it "located the requested section" do it "located the requested section" do
put :update, id: @section, format: :json, section: valid_attributes put :update, params: {id: @section, section: valid_attributes}, format: :json
@section.reload @section.reload
assigns(:section).should eq(@section) assigns(:section).should eq(@section)
end end
it "changes @section's attributes" do it "changes @section's attributes" do
put :update, id: @section.id, format: :json, section: valid_attributes.merge(title: "ChangedByTest") put :update, params: {id: @section.id, section: valid_attributes.merge(title: "ChangedByTest")}, format: :json
@section.reload @section.reload
expect( @section.title ).to eq "ChangedByTest" expect( @section.title ).to eq "ChangedByTest"
end end
it "should not be possible to update a section to another supplier" do it "should not be possible to update a section to another supplier" do
supplier2 = create :supplier supplier2 = create :supplier
put :update, id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id) put :update, params: {id: @section, section: valid_attributes.merge(title: "Trying to hack", supplier_id: supplier2.id)}
@section.reload @section.reload
@section.supplier_id.should == @supplier.id @section.supplier_id.should == @supplier.id
end end
it "should not be possible to update a section of another supplier" do it "should not be possible to update a section of another supplier" do
section = create :section, title: 'Other supplier section' section = create :section, title: 'Other supplier section'
put :update, id: section.id, format: :json, section: {title: "Trying to hack"} put :update, params: {id: section.id, section: {title: "Trying to hack"}}, format: :json
expect( response ).to be_not_found expect( response ).to be_not_found
section.reload section.reload
section.title.should == 'Other supplier section' section.title.should == 'Other supplier section'
@@ -104,12 +104,12 @@ describe Suppliers::SectionsController, type: :controller do
context "invalid attributes" do context "invalid attributes" do
it "locates the requested section" do it "locates the requested section" do
put :update, id: @section, format: :json, section: invalid_attributes put :update, params: {id: @section, section: invalid_attributes}, format: :json
assigns(:section).should eq(@section) assigns(:section).should eq(@section)
end end
it "returns an error response" do it "returns an error response" do
put :update, id: @section.id, format: :json, section: invalid_attributes put :update, params: {id: @section.id, section: invalid_attributes}, format: :json
expect( JSON.parse(response.body )['errors'] ).to be_present expect( JSON.parse(response.body )['errors'] ).to be_present
end end
end end
@@ -122,14 +122,14 @@ describe Suppliers::SectionsController, type: :controller do
it "deletes the section" do it "deletes the section" do
expect{ expect{
delete :destroy, id: @section delete :destroy, params: {id: @section.id}
}.to change(Section, :count).by(-1) }.to change(Section, :count).by(-1)
end end
it "should not be possible to delete a section of another supplier" do it "should not be possible to delete a section of another supplier" do
section = create :section section = create :section
expect{ expect{
delete :destroy, id: section delete :destroy, params: {id: section.id}
}.to_not change(Section, :count) }.to_not change(Section, :count)
end end
end end
@@ -24,7 +24,7 @@ describe Suppliers::TablesController, type: :controller do
describe "GET #show" do describe "GET #show" do
it "assigns the requested table to @table" do it "assigns the requested table to @table" do
table = create :table, supplier: @supplier table = create :table, supplier: @supplier
get :show, params: {id: table}, format: :json get :show, params: {id: table.id}, format: :json
assigns(:table).should eq(table) assigns(:table).should eq(table)
end end
+30 -5
View File
@@ -2,20 +2,45 @@ require 'spec_helper'
describe "persistance" do describe "persistance" do
let(:db){CouchPotato.database.couchrest_database} let(:db){CouchPotato.database.couchrest_database}
let(:db_uri){ File.join(db.host, db.name)} let(:debug) { StringIO.new }
#let(:db_uri){ File.join(db.uri.host, db.name)}
describe "database format" do describe "database format" do
it "persists with proper ruby class" do it "persists with proper ruby class" do
employee = create :employee employee = create :employee
response = Net::HTTP.get URI(File.join(db_uri, employee.id)) uri = URI.parse(File.join(db.uri, employee.id))
response.should include %|"ruby_class":"Employee"|
response.should_not include %|"id":| http = Net::HTTP.new uri.host, uri.port
http.set_debug_output(debug)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth uri.user, uri.password
response = http.request(request)
doc = JSON.parse(response.body)
doc['ruby_class'].should eq 'Employee'
doc.should_not have_key 'id'
#response.should include %|"ruby_class":"Employee"|
#response.should_not include %|"id":|
end end
it "stores time in UTC iso8601 format" do it "stores time in UTC iso8601 format" do
time = Time.utc(1981, 3, 9, 13, 22, 2).in_time_zone time = Time.utc(1981, 3, 9, 13, 22, 2).in_time_zone
Timecop.travel time do Timecop.travel time do
employee_shift = create :employee_shift, start_from: time, end_on: time + 2.hours employee_shift = create :employee_shift, start_from: time, end_on: time + 2.hours
response = JSON.parse Net::HTTP.get URI(File.join(db_uri, employee_shift.id))
uri = URI.parse(File.join(db.uri, employee_shift.id))
http = Net::HTTP.new uri.host, uri.port
http.set_debug_output(debug)
request = Net::HTTP::Get.new(uri.request_uri)
request.basic_auth uri.user, uri.password
response = http.request(request)
response = JSON.parse response.body
response['created_at'].should eq "1981-03-09T13:22:02Z" response['created_at'].should eq "1981-03-09T13:22:02Z"
response['updated_at'].should eq "1981-03-09T13:22:02Z" response['updated_at'].should eq "1981-03-09T13:22:02Z"
response['start_from'].should eq "1981-03-09T13:22:02Z" response['start_from'].should eq "1981-03-09T13:22:02Z"