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)
unique_supplier_user_ids = Set.new
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]
end
end
@@ -140,7 +140,7 @@ class List
def self.enrich_users_number_of_lists_at_supplier(supplier_id, lists)
counts = get_suppliers_users_count(lists, supplier_id: supplier_id)
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
end
end
@@ -1,5 +1,6 @@
# encoding: UTF-8
require 'rails_helper'
describe DashboardController, type: :controller do
before :each do
setup_supplier_for_controller
@@ -9,13 +10,19 @@ describe DashboardController, type: :controller do
it "does render the svg image" do
table = create :table, supplier: @supplier, number: 7
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
it "does render the svg image with table label if present" do
table = create :table, supplier: @supplier, number: 7, label: "The love seat"
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
@@ -38,19 +38,19 @@ describe Suppliers::ListsController, type: :controller do
describe "GET #show" do
it "assigns the requested list to @list" do
list = create :list, supplier: @supplier
get :show, id: list
get :show, params: {id: list.id}
assigns(:list).should eq(list)
end
it "should not display a list of another supplier" do
list = create :list
get :show, id: list
get :show, params: {id: list.id}
response.status.should == 404
end
it "renders the #show view" do
list = create :list, supplier: @supplier
get :show, id: list
get :show, params: {id: list.id}
response.body.should include 'orders'
end
end
@@ -66,13 +66,13 @@ describe Suppliers::ListsController, type: :controller do
context "with valid attributes" do
it "creates a new list" do
expect{
post :create, list: valid_attributes
post :create, params: {list: valid_attributes}
}.to change(List, :count).by(1)
end
it "should not be possible to create a list for another supplier" do
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
end
end
@@ -80,13 +80,13 @@ describe Suppliers::ListsController, type: :controller do
context "with invalid attributes", broken: true do
it "does not save the new list" do
expect{
post :create, format: :json, list: invalid_params
post :create, params: {list: invalid_params}, format: :json
}.not_to change{ List.count }
List.count.should be_zero
end
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
end
end
@@ -99,27 +99,27 @@ describe Suppliers::ListsController, type: :controller do
context "valid attributes" 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
assigns(:list).should eq(@list)
end
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.price.should eq(7.22)
end
it "should not be possible to update a list to another supplier" do
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.supplier_id.should == @supplier.id
end
it "should not be possible to update a list of another supplier" do
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.price.should == 7.22
end
@@ -127,7 +127,7 @@ describe Suppliers::ListsController, type: :controller do
context "invalid attributes", broken: true 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
end
end
@@ -140,14 +140,14 @@ describe Suppliers::ListsController, type: :controller do
it "deletes the list" do
expect{
delete :destroy, id: @list
delete :destroy, params: {id: @list.id}
}.to change(List, :count).by(-1)
end
it "should not be possible to delete a list of another supplier" do
list = create :list
expect{
delete :destroy, id: list
delete :destroy, params: {id: list.id}
}.to_not change(List, :count)
end
end
@@ -27,13 +27,13 @@ describe Suppliers::ProductCategoriesController, type: :controller do
describe "GET #show" do
it "assigns the requested product_category to @product_category" do
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)
end
it "should not display a product_category of another supplier" do
product_category = create :product_category
get :show, id: product_category, format: :json
get :show, params: {id: product_category}, format: :json
response.status.should == 404
end
end
@@ -42,18 +42,18 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with valid attributes" do
it "creates a new product_category" do
expect{
post :create, product_category: valid_attributes, format: :json
post :create, params: {product_category: valid_attributes}, format: :json
}.to change(ProductCategory, :count).by(1)
end
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]
end
it "should not be possible to create a product category for another supplier" do
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
end
end
@@ -61,12 +61,12 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with invalid attributes" do
it "does not save the new product_category" do
expect{
post :create, product_category: invalid_attributes, format: :json
post :create, params: {product_category: invalid_attributes}, format: :json
}.not_to change{ ProductCategory.count }
end
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
end
end
@@ -79,20 +79,20 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "valid attributes" 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
assigns(:product_category).should eq(@product_category)
end
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.name.should eq("ChangedByTest")
end
it "should not be possible to update a product category to another supplier" do
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
expect( @product_category.name ).to eq 'Trying to hack'
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
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
product_category.reload
product_category.name.should == 'Other supplier product_category'
@@ -109,7 +109,7 @@ describe Suppliers::ProductCategoriesController, type: :controller do
context "with invalid attributes" 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
end
end
@@ -122,14 +122,14 @@ describe Suppliers::ProductCategoriesController, type: :controller do
it "deletes the product_category" do
expect{
delete :destroy, id: @product_category
delete :destroy, params: {id: @product_category.id}
}.to change(ProductCategory, :count).by(-1)
end
it "should not be possible to delete a product category of another supplier" do
product_category = create :product_category
expect{
delete :destroy, id: product_category
delete :destroy, params: {id: product_category.id}
}.to_not change{ProductCategory.count }
end
end
@@ -27,13 +27,13 @@ describe Suppliers::SectionsController, type: :controller do
describe "GET #show" do
it "assigns the requested section to @section" do
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)
end
it "should not display a section of another supplier" do
section = create :section
get :show, id: section, format: :json
get :show, params: {id: section.id}, format: :json
response.status.should == 404
end
end
@@ -42,14 +42,14 @@ describe Suppliers::SectionsController, type: :controller do
context "with valid attributes" do
it "creates a new section" do
expect{
post :create, section: valid_attributes
post :create, params: {section: valid_attributes}
}.to change(Section, :count).by(1)
expect( Section.last.supplier_id ).to eq @supplier.id
end
it "should not be possible to create a section for another supplier" do
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
end
end
@@ -57,12 +57,12 @@ describe Suppliers::SectionsController, type: :controller do
context "with invalid attributes" do
it "does not save the new section" do
expect{
post :create, format: :json, section: invalid_attributes
post :create, params: {section: invalid_attributes}, format: :json
}.to_not change(Section, :count)
end
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
end
end
@@ -75,27 +75,27 @@ describe Suppliers::SectionsController, type: :controller do
context "valid attributes" 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
assigns(:section).should eq(@section)
end
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
expect( @section.title ).to eq "ChangedByTest"
end
it "should not be possible to update a section to another supplier" do
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.supplier_id.should == @supplier.id
end
it "should not be possible to update a section of another supplier" do
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
section.reload
section.title.should == 'Other supplier section'
@@ -104,12 +104,12 @@ describe Suppliers::SectionsController, type: :controller do
context "invalid attributes" 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)
end
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
end
end
@@ -122,14 +122,14 @@ describe Suppliers::SectionsController, type: :controller do
it "deletes the section" do
expect{
delete :destroy, id: @section
delete :destroy, params: {id: @section.id}
}.to change(Section, :count).by(-1)
end
it "should not be possible to delete a section of another supplier" do
section = create :section
expect{
delete :destroy, id: section
delete :destroy, params: {id: section.id}
}.to_not change(Section, :count)
end
end
@@ -24,7 +24,7 @@ describe Suppliers::TablesController, type: :controller do
describe "GET #show" do
it "assigns the requested table to @table" do
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)
end
+30 -5
View File
@@ -2,20 +2,45 @@ require 'spec_helper'
describe "persistance" do
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
it "persists with proper ruby class" do
employee = create :employee
response = Net::HTTP.get URI(File.join(db_uri, employee.id))
response.should include %|"ruby_class":"Employee"|
response.should_not include %|"id":|
uri = URI.parse(File.join(db.uri, employee.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
it "stores time in UTC iso8601 format" do
time = Time.utc(1981, 3, 9, 13, 22, 2).in_time_zone
Timecop.travel time do
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['updated_at'].should eq "1981-03-09T13:22:02Z"
response['start_from'].should eq "1981-03-09T13:22:02Z"