From 9834eab77aa1715f574cf71f401b4887d2f7d69f Mon Sep 17 00:00:00 2001 From: Ivar Nymoen Date: Mon, 26 Jul 2010 17:00:05 +0200 Subject: [PATCH 1/5] some work on the test suite --- app/controllers/application_controller.rb | 4 +- .../paycheck_line_templates_controller.rb | 4 +- app/controllers/paycheck_lines_controller.rb | 7 + .../paycheck_templates_controller.rb | 22 -- app/models/company.rb | 2 - ...26113926_change_paycheck_line_templates.rb | 11 + test/blueprints.rb | 368 +++++++++++------- ...paycheck_line_templates_controller_test.rb | 30 +- .../paycheck_lines_controller_test.rb | 44 +-- .../paycheck_templates_controller_test.rb | 34 ++ test/functional/paychecks_controller_test.rb | 10 +- test/test_helper.rb | 9 + 12 files changed, 320 insertions(+), 225 deletions(-) create mode 100644 db/migrate/20100726113926_change_paycheck_line_templates.rb create mode 100644 test/functional/paycheck_templates_controller_test.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 1300bb6..e14c86c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -44,11 +44,9 @@ def company_required end def permission_denied -# raise 'snopp' - print "LAAAAAAAAAAAAAAAAAAAA: " + current_user.role_symbols.inspect msg = I18n.t("access_denied") flash[:error] = msg - logger.info "Error: #{msg}, user: #{current_user}, url: #{request.request_uri}, pid: #{Process.pid}, Time: #{Time.now}" + logger.info "Error: #{msg}, user: #{current_user}, url: #{request.fullpath}, pid: #{Process.pid}, Time: #{Time.now}" redirect_to root_url end diff --git a/app/controllers/paycheck_line_templates_controller.rb b/app/controllers/paycheck_line_templates_controller.rb index 25b8c89..b6851b7 100644 --- a/app/controllers/paycheck_line_templates_controller.rb +++ b/app/controllers/paycheck_line_templates_controller.rb @@ -1,8 +1,6 @@ class PaycheckLineTemplatesController < ApplicationController before_filter :setup_form - - def setup_form @accounts = current_user.current_company.accounts @units = current_user.current_company.units @@ -13,6 +11,7 @@ def setup_form # GET /paycheck_line_templates # GET /paycheck_line_templates.xml def index + raise "asdlfkjsldjkfee" @paycheck_line_templates = PaycheckLineTemplate.all respond_to do |format| @@ -24,6 +23,7 @@ def index # GET /paycheck_line_templates/1 # GET /paycheck_line_templates/1.xml def show + raise "dslkfjasldfjas" @paycheck_line_template = PaycheckLineTemplate.find(params[:id]) respond_to do |format| diff --git a/app/controllers/paycheck_lines_controller.rb b/app/controllers/paycheck_lines_controller.rb index 68bb5bd..428b4e5 100644 --- a/app/controllers/paycheck_lines_controller.rb +++ b/app/controllers/paycheck_lines_controller.rb @@ -2,6 +2,7 @@ class PaycheckLinesController < ApplicationController # GET /paycheck_lines # GET /paycheck_lines.xml def index + raise "aksljdfklasdf" @paycheck_lines = PaycheckLine.all respond_to do |format| @@ -13,6 +14,7 @@ def index # GET /paycheck_lines/1 # GET /paycheck_lines/1.xml def show + raise "lsadkfjljwefl" @paycheck_line = PaycheckLine.find(params[:id]) respond_to do |format| @@ -24,6 +26,7 @@ def show # GET /paycheck_lines/new # GET /paycheck_lines/new.xml def new + raise "sdfjdlaskjflkwjdsf" @paycheck_line = PaycheckLine.new respond_to do |format| @@ -34,12 +37,14 @@ def new # GET /paycheck_lines/1/edit def edit + raise "asdljfjadsf" @paycheck_line = PaycheckLine.find(params[:id]) end # POST /paycheck_lines # POST /paycheck_lines.xml def create + raise "aklsdfjlaksdjf" @paycheck_line = PaycheckLine.new(params[:paycheck_line]) respond_to do |format| @@ -56,6 +61,7 @@ def create # PUT /paycheck_lines/1 # PUT /paycheck_lines/1.xml def update + raise "aklsdfjslkdf" @paycheck_line = PaycheckLine.find(params[:id]) respond_to do |format| @@ -72,6 +78,7 @@ def update # DELETE /paycheck_lines/1 # DELETE /paycheck_lines/1.xml def destroy + raise "asdklfjsahdf" @paycheck_line = PaycheckLine.find(params[:id]) @paycheck_line.destroy diff --git a/app/controllers/paycheck_templates_controller.rb b/app/controllers/paycheck_templates_controller.rb index 8887025..f132bbe 100644 --- a/app/controllers/paycheck_templates_controller.rb +++ b/app/controllers/paycheck_templates_controller.rb @@ -52,26 +52,4 @@ def create end end - # GET /paycheck_line_templates/1/edit - def edit - raise "NOT IMPLEMENTED" - - respond_to do |format| - format.html # show.html.erb - format.xml { render :xml => @paycheck_line_template } - end - end - - # DELETE /paycheck_line_templates/1 - # DELETE /paycheck_line_templates/1.xml - def destroy - raise "NOT IMPLEMENTED" - @paycheck_line_template = PaycheckLineTemplate.find(params[:id]) - @paycheck_line_template.destroy - - respond_to do |format| - format.html { redirect_to(paycheck_line_templates_url) } - format.xml { head :ok } - end - end end diff --git a/app/models/company.rb b/app/models/company.rb index 6af0b66..851505f 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -97,8 +97,6 @@ def employees def paychecks r = Paycheck.where(:employee_id => employees).joins([:employee, :period]).order("periods.year desc, periods.nr desc, lower(ledgers.name)") - print r.to_sql - print "..." r end diff --git a/db/migrate/20100726113926_change_paycheck_line_templates.rb b/db/migrate/20100726113926_change_paycheck_line_templates.rb new file mode 100644 index 0000000..d7e706c --- /dev/null +++ b/db/migrate/20100726113926_change_paycheck_line_templates.rb @@ -0,0 +1,11 @@ +class ChangePaycheckLineTemplates < ActiveRecord::Migration + def self.up + change_column :paycheck_line_templates, :employee_id, :integer, :null => true + change_column :paycheck_line_templates, :company_id, :integer, :null => false + end + + def self.down + change_column :paycheck_line_templates, :employee_id, :integer, :null => false + change_column :paycheck_line_templates, :company_id, :integer, :null => true + end +end diff --git a/test/blueprints.rb b/test/blueprints.rb index 1e90265..572c17d 100644 --- a/test/blueprints.rb +++ b/test/blueprints.rb @@ -2,18 +2,48 @@ # when creating users. raise "Set RAILS_ENV=test before loading blueprint" unless Rails.env == 'test' -# Smallish data set for unit and selenium test purposes +# Number of users +USER_COUNT = 5 +#15000 +# Number of companies +COMPANY_COUNT = 2 +#10000 +# Average number of products for a company +PRODUCT_COUNT = 20 +# 50 +# Average number of units +UNIT_COUNT = 7 +# 7 +# Average number of projects +PROJECT_COUNT = 7 +# 7 +YEARS=(2000..2010) +MAX_JOURNAL_COUNT=500 + +def print_time name, &block + print "#{name}..." + a = Time.now + foo = yield block + b = Time.now + print " Used #{b-a} seconds.\n" + return foo +end + +# largish data set to test scaling or whatever require 'machinist/active_record' require 'sham' require 'faker' -require File.expand_path(File.dirname(__FILE__) + "/../db/seed") if Role.all.empty? -raise "roles not seeded" if Role.all.empty? +if not $BULK_APPEND + require File.expand_path(File.dirname(__FILE__) + "/../db/seed") if Role.all.empty? + raise "roles not seeded" if Role.all.empty? +end +suffix = Time.now.usec.to_s Sham.login { Faker::Internet.user_name } Sham.email { Faker::Internet.email } -Sham.company_name { Faker::Company.name } +Sham.company_name { Faker::Company.name + suffix } Sham.street1(:unique => false) { Faker::Address.street_name } Sham.street2(:unique => false) { Faker::Address.secondary_address } Sham.postal_code(:unique => false) { Faker::Address.uk_postcode } @@ -92,163 +122,229 @@ name { Sham.role_name } end +bob = nil +admin = nil +companies = [] +users = nil +roles=nil # ********** Now create some data *************** -unless User.find_by_email("bob@bobsdomain.com") - - - 20.times {|i| user = User.make } - - bob = User.make(:email => "bob@bobsdomain.com") - admin = Admin.make(:email => "admin@adminsdomain.com") - - 3.times {|i| Company.make} +ActiveRecord::Base.transaction do + + if not $BULK_APPEND + print_time "Creating users" do + + USER_COUNT.times {|i| user = User.make } + + bob = User.make(:email => "bob@bobsdomain.com") + + admin = Admin.make(:email => "admin@adminsdomain.com") + end + else + bob = User.where(:email => "bob@bobsdomain.com").first + admin = User.where(:email => "admin@adminsdomain.com").first + end + + print_time "Creating companies" do + COMPANY_COUNT.times {|i| companies << Company.make } + end users = User.all - companies = Company.all roles = Role.all - - # attach users to companies - (Company.count * 2).times do - c = companies.rand - u = users.rand - r = roles.rand - c.assignments.create(:user => u, :role => r) + + print_time "Attaching users to companies" do + # attach users to companies + (COMPANY_COUNT * 5).times do + c = companies.rand + u = users.rand + r = roles.rand + c.assignments.create(:user => u, :role => r) + end + + # make sure bob is assigned to a few companies + companies.shuffle[0..[COMPANY_COUNT, 5].min].each do |c| + c.assignments.create(:user => bob, :role => Role.find_by_name("accountant")) + end end - # make sure bob is assigned to a few companies - companies.each do |c| - c.assignments.create(:user => bob, :role => Role.find_by_name("accountant")) + print_time "Creating units" do + # create a bunch of units/projects and attach them to companies + (companies.size * UNIT_COUNT).times do + Unit.make(:company => companies.rand) + end end - - # create a bunch of units/projects and attach them to companies - (Company.count * 3).times do - Unit.make(:company => companies.rand) - Project.make(:company => companies.rand) + print_time "Creating projects" do + (companies.size * PROJECT_COUNT).times do + Project.make(:company => companies.rand) + end end - # create some accounts - Company.all.each do |c| - # Vat accounts from empatix @ lodo.no - - # **** Outgoing vat; sales *** - a2700 = Account.make(:company => c, :name => "Utg mva kode 10", :number => 2700) - va2700 = VatAccount.create!(:company => c, :overridable => true, :target_account => a2700) - VatAccountPeriod.create!(:vat_account => va2700, :percentage => 0, :valid_from => "1990-01-01") - - - a2701 = Account.make(:company => c, :name => "Utg mva kode 11", :number => 2701) - va2701 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2701) - VatAccountPeriod.create!(:vat_account => va2701, :percentage => 24, :valid_from => "1990-01-01") - VatAccountPeriod.create!(:vat_account => va2701, :percentage => 25, :valid_from => "2005-01-01") - - - a2702 = Account.make(:company => c, :name => "Utg mva kode 12", :number => 2702) - va2702 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2702) - VatAccountPeriod.create!(:vat_account => va2702, :percentage => 12, :valid_from => "1990-01-01") - VatAccountPeriod.create!(:vat_account => va2702, :percentage => 11, :valid_from => "2005-01-01") - VatAccountPeriod.create!(:vat_account => va2702, :percentage => 13, :valid_from => "2006-01-01") - VatAccountPeriod.create!(:vat_account => va2702, :percentage => 14, :valid_from => "2007-01-01") - - - a2703 = Account.make(:company => c, :name => "Utg mva kode 13", :number => 2703) - va2703 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2703) - VatAccountPeriod.create!(:vat_account => va2703, :percentage => 6, :valid_from => "1990-01-01") - VatAccountPeriod.create!(:vat_account => va2703, :percentage => 7, :valid_from => "2005-01-01") - VatAccountPeriod.create!(:vat_account => va2703, :percentage => 8, :valid_from => "2006-01-01") - - # *** Incoming vat; expenses *** - a2710 = Account.make(:company => c, :name => "Ing mva kode 40", :number => 2710) - va2710 = VatAccount.create!(:company => c, :overridable => true, :target_account => a2710) - VatAccountPeriod.create!(:vat_account => va2710, :percentage => 0, :valid_from => "1990-01-01") - - - a2711 = Account.make(:company => c, :name => "Ing mva kode 41", :number => 2711) - va2711 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2711) - VatAccountPeriod.create!(:vat_account => va2711, :percentage => 24, :valid_from => "1990-01-01") - VatAccountPeriod.create!(:vat_account => va2711, :percentage => 25, :valid_from => "2005-01-01") - - - a2712 = Account.make(:company => c, :name => "Ing mva kode 42", :number => 2712) - va2712 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2712) - VatAccountPeriod.create!(:vat_account => va2712, :percentage => 12, :valid_from => "1990-01-01") - VatAccountPeriod.create!(:vat_account => va2712, :percentage => 11, :valid_from => "2005-01-01") - VatAccountPeriod.create!(:vat_account => va2712, :percentage => 13, :valid_from => "2006-01-01") - VatAccountPeriod.create!(:vat_account => va2712, :percentage => 14, :valid_from => "2007-01-01") - - - a2713 = Account.make(:company => c, :name => "Ing mva kode 43", :number => 2713) - va2713 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2713) - VatAccountPeriod.create!(:vat_account => va2713, :percentage => 6, :valid_from => "1990-01-01") - VatAccountPeriod.create!(:vat_account => va2713, :percentage => 7, :valid_from => "2005-01-01") - VatAccountPeriod.create!(:vat_account => va2713, :percentage => 8, :valid_from => "2006-01-01") - - - # Other accounts - Account.make(:company => c, :name => "Sales", :number => 3000, :vat_account => va2701) - Account.make(:company => c, :name => "Cash", :number => 1900) - Account.make(:company => c, :name => "Bank", :number => 1920) - Account.make(:company => c, :name => "Materials", :number => 4000, :vat_account => va2711) - Account.make(:company => c, :name => "Salaries", :number => 5000) - Account.make(:company => c, :name => "Arbeidsgiveravgift", :number => 5400) - Account.make(:company => c, :name => "NAV-refusjon, sykepenger", :number => 5800) - Account.make(:company => c, :name => "Office supplies", :number => 6800, :vat_account => va2711) - Account.make(:company => c, :name => "Phone expenses", :number => 6900, :vat_account => va2711) - - Account.make(:company => c, :name => "Accounts receivable", :number => 1500) - # TODO: create some customers - - Account.make(:company => c, :name => "Accounts payable", :number => 2400) + print_time "Creating company accounts" do + # create some accounts + companies.each do |c| + print '.' + # Vat accounts from empatix @ lodo.no + + # **** Outgoing vat; sales *** + a2700 = Account.make(:company => c, :name => "Utg mva kode 10", :number => 2700) + va2700 = VatAccount.create!(:company => c, :overridable => true, :target_account => a2700) + VatAccountPeriod.create!(:vat_account => va2700, :percentage => 0, :valid_from => "1990-01-01") + + + a2701 = Account.make(:company => c, :name => "Utg mva kode 11", :number => 2701) + va2701 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2701) + VatAccountPeriod.create!(:vat_account => va2701, :percentage => 24, :valid_from => "1990-01-01") + VatAccountPeriod.create!(:vat_account => va2701, :percentage => 25, :valid_from => "2005-01-01") + + + a2702 = Account.make(:company => c, :name => "Utg mva kode 12", :number => 2702) + va2702 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2702) + VatAccountPeriod.create!(:vat_account => va2702, :percentage => 12, :valid_from => "1990-01-01") + VatAccountPeriod.create!(:vat_account => va2702, :percentage => 11, :valid_from => "2005-01-01") + VatAccountPeriod.create!(:vat_account => va2702, :percentage => 13, :valid_from => "2006-01-01") + VatAccountPeriod.create!(:vat_account => va2702, :percentage => 14, :valid_from => "2007-01-01") + + + a2703 = Account.make(:company => c, :name => "Utg mva kode 13", :number => 2703) + va2703 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2703) + VatAccountPeriod.create!(:vat_account => va2703, :percentage => 6, :valid_from => "1990-01-01") + VatAccountPeriod.create!(:vat_account => va2703, :percentage => 7, :valid_from => "2005-01-01") + VatAccountPeriod.create!(:vat_account => va2703, :percentage => 8, :valid_from => "2006-01-01") + + # *** Incoming vat; expenses *** + a2710 = Account.make(:company => c, :name => "Ing mva kode 40", :number => 2710) + va2710 = VatAccount.create!(:company => c, :overridable => true, :target_account => a2710) + VatAccountPeriod.create!(:vat_account => va2710, :percentage => 0, :valid_from => "1990-01-01") + + + a2711 = Account.make(:company => c, :name => "Ing mva kode 41", :number => 2711) + va2711 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2711) + VatAccountPeriod.create!(:vat_account => va2711, :percentage => 24, :valid_from => "1990-01-01") + VatAccountPeriod.create!(:vat_account => va2711, :percentage => 25, :valid_from => "2005-01-01") + + + a2712 = Account.make(:company => c, :name => "Ing mva kode 42", :number => 2712) + va2712 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2712) + VatAccountPeriod.create!(:vat_account => va2712, :percentage => 12, :valid_from => "1990-01-01") + VatAccountPeriod.create!(:vat_account => va2712, :percentage => 11, :valid_from => "2005-01-01") + VatAccountPeriod.create!(:vat_account => va2712, :percentage => 13, :valid_from => "2006-01-01") + VatAccountPeriod.create!(:vat_account => va2712, :percentage => 14, :valid_from => "2007-01-01") + + + a2713 = Account.make(:company => c, :name => "Ing mva kode 43", :number => 2713) + va2713 = VatAccount.create!(:company => c, :overridable => false, :target_account => a2713) + VatAccountPeriod.create!(:vat_account => va2713, :percentage => 6, :valid_from => "1990-01-01") + VatAccountPeriod.create!(:vat_account => va2713, :percentage => 7, :valid_from => "2005-01-01") + VatAccountPeriod.create!(:vat_account => va2713, :percentage => 8, :valid_from => "2006-01-01") + + + # Other accounts + Account.make(:company => c, :name => "Sales", :number => 3000, :vat_account => va2701) + Account.make(:company => c, :name => "Cash", :number => 1900) + Account.make(:company => c, :name => "Bank", :number => 1920) + Account.make(:company => c, :name => "Materials", :number => 4000, :vat_account => va2711) + Account.make(:company => c, :name => "Salaries", :number => 5000) + Account.make(:company => c, :name => "Arbeidsgiveravgift", :number => 5400) + Account.make(:company => c, :name => "NAV-refusjon, sykepenger", :number => 5800) + Account.make(:company => c, :name => "Office supplies", :number => 6800, :vat_account => va2711) + Account.make(:company => c, :name => "Phone expenses", :number => 6900, :vat_account => va2711) + + Account.make(:company => c, :name => "Accounts receivable", :number => 1500) + # TODO: create some customers + + Account.make(:company => c, :name => "Accounts payable", :number => 2400) # TODO: create some suppliers - - Account.make(:company => c, :name => "Employees", :number => 2930) - # TODO: create some employees - - # create some random filler accounts - (rand(120) + 40).times do - begin - Account.make(:company => c) - rescue + + Account.make(:company => c, :name => "Employees", :number => 2930) + # TODO: create some employees + + # create some random filler accounts + (rand(200) + 40).times do + begin + Account.make(:company => c) + rescue + end end end end - # let's go with an avg of 15 products / company - (Company.count * 15).times do - Product.make(:account => companies.rand.accounts.rand) - end - - # create a random journal entry (bilag) - def create_journal_entry(company, period) - date = Date.civil(period.year, period.nr, rand(28) + 1) - je = Journal.make(:company => company, :period => period, :journal_date => date) - total = 0 - (rand(3) + 1).times do - amount = BigDecimal.new( sprintf("%.2f", 100 * rand - 100 * rand) ) - jo = JournalOperation.make(:journal => je, :account => company.accounts.rand, :amount => amount) - total += amount + print_time "Creating products" do + (companies.size * PRODUCT_COUNT).times do |i| + Product.make(:account => companies.rand.accounts.rand) end - # close transaction - jo = JournalOperation.make(:journal => je, :account => company.accounts.rand, :amount => -total) + end + + # create random journal entries for the given period + def create_journal_entries(company, period) + # puts "creating journal entries for #{company.name}, period: #{period.year}-#{period.nr}" + # create journal entries + date = Date.civil(period.year, period.nr, 1) + sql = "insert into journals (company_id, period_id, journal_date) select #{company.id}, #{period.id}, ('#{date}'::date + interval '28 days' * random())::date from generate_series (1, #{rand(MAX_JOURNAL_COUNT)})" + ActiveRecord::Base.connection.execute sql + + end + + def create_journal_operations(company) + # create 4 journal_operations for every empty journal entry + # sql = "insert into journal_operations (journal_id, account_id, amount) select id as journal_id, #{sample_array_sql(company.accounts)}, (random() * 200 - 100)::numeric(16,2) as amount from (select j.id from journals j where j.company_id = #{company.id} and not exists (select 1 from journal_operations jo where jo.journal_id = j.id)) as aa, (select 1 from generate_series(1, (1 + random() * 5)::integer)) as bb" + sql = " +insert into journal_operations +(journal_id, account_id, amount) +select + id as journal_id, + (select id from accounts where company_id=#{company.id} order by random() limit 1) as account_id, + (random() * 200 - 100)::numeric(16,2) as amount +from (select j.id from journals j +where j.company_id = #{company.id} + and not exists ( + select 1 from journal_operations jo + where jo.journal_id = j.id)) as aa, (select 1 from generate_series(1, 4)) as bb" + ActiveRecord::Base.connection.execute sql + + # find and close open journal entries + sql = " +insert into journal_operations +(journal_id, account_id, amount) +select + journal_id, + (select id from accounts where company_id=#{company.id} order by random() limit 1) as account_id, + -amount from ( + select journal_id, sum(amount) as amount + from journals j + inner join journal_operations jo + on (j.id = jo.journal_id) + where company_id = #{company.id} group by journal_id having sum(amount) <> 0) as qq" + ActiveRecord::Base.connection.execute sql end def create_period(company, year, nr) p = Period.make(:company => company, :year => year, :nr => nr) - rand(10).times do - create_journal_entry(company, p) - end + create_journal_entries(company, p) # TODO: close period as appropriate + #puts "p.journal count: #{p.journals.count} -- operations: #{p.journal_operations.count}" end - # create periods and fill these with tx data - companies.each do |c| - (2010..2010).each do |year| - (1..12).each do |month| - create_period(c, year, month) - end +end + +# create periods and fill these with tx data +companies.each_with_index do |c,idx| + if idx%40 == 0 + print_time "Vacuuming the house" do + ActiveRecord::Base.connection.execute "vacuum analyze" end end + print_time "Creating periods for company #{idx+1} of #{COMPANY_COUNT}" do + ActiveRecord::Base.transaction do + YEARS.each do |year| + (1..12).each do |month| + create_period(c, year, month) + end + end + create_journal_operations(c) + end + end # ActiveRecord::Base.transaction do +end + -end # unless User.find(bob) diff --git a/test/functional/paycheck_line_templates_controller_test.rb b/test/functional/paycheck_line_templates_controller_test.rb index 226fbf2..2785f81 100644 --- a/test/functional/paycheck_line_templates_controller_test.rb +++ b/test/functional/paycheck_line_templates_controller_test.rb @@ -2,17 +2,10 @@ class PaycheckLineTemplatesControllerTest < ActionController::TestCase setup do - @bob = User.find_by_email("bob@bobsdomain.com") - @company = @bob.assignments.select {|a| a.role.name == "accountant"}.map {|a| a.company}.first + log_in_as_bob @employee_one = Ledger.create!(:account => @company.accounts.first, :name => "Employee One", :number => "99", :credit_text => "credit", :debit_text => "debit") - @dummy_line_attributes = {:count => "1", :rate => "750 ", :amount => "750", :employee_id => @employee_one.id, :salary_code => "abc", :payroll_tax => "1", :vacation_basis => "1", :description => "qq", :line_type => "duh", :account_id => @company.accounts.first.id} - sign_in :user, @bob - end - - test "should get index" do - get :index - assert_response :success - assert_not_nil assigns(:paycheck_line_templates) + @dummy_line_attributes = {:count => "1", :rate => "750 ", :amount => "750", :employee_id => @employee_one.id, :salary_code => "abc", :payroll_tax => "1", :vacation_basis => "1", :description => "qq", :line_type => "duh", :account_id => @company.accounts.first.id, :company_id => @company.id} + @paycheck_line_template = PaycheckLineTemplate.create!(@dummy_line_attributes) end test "should get new" do @@ -25,12 +18,17 @@ class PaycheckLineTemplatesControllerTest < ActionController::TestCase post :create, :paycheck_line_template => @dummy_line_attributes end - assert_redirected_to paycheck_line_template_path(assigns(:paycheck_line_template)) + assert_redirected_to paycheck_template_path(@employee_one.id) end - test "should show paycheck_line_template" do - get :show, :id => @paycheck_line_template.to_param - assert_response :success + test "should create global template line" do + qq = @dummy_line_attributes.clone + qq.delete(:employee_id) + assert_difference('PaycheckLineTemplate.count') do + post :create, :paycheck_line_template => qq + end + + assert_redirected_to paycheck_template_path(:global) end test "should get edit" do @@ -40,7 +38,7 @@ class PaycheckLineTemplatesControllerTest < ActionController::TestCase test "should update paycheck_line_template" do put :update, :id => @paycheck_line_template.to_param, :paycheck_line_template => @paycheck_line_template.attributes - assert_redirected_to paycheck_line_template_path(assigns(:paycheck_line_template)) + assert_redirected_to paycheck_template_path(@employee_one.id) end test "should destroy paycheck_line_template" do @@ -49,6 +47,6 @@ class PaycheckLineTemplatesControllerTest < ActionController::TestCase delete :destroy, :id => @line.to_param end - assert_redirected_to paycheck_line_templates_path + assert_redirected_to paycheck_template_path(@employee_one.id) end end diff --git a/test/functional/paycheck_lines_controller_test.rb b/test/functional/paycheck_lines_controller_test.rb index 703e15a..3f7b4b6 100644 --- a/test/functional/paycheck_lines_controller_test.rb +++ b/test/functional/paycheck_lines_controller_test.rb @@ -2,48 +2,10 @@ class PaycheckLinesControllerTest < ActionController::TestCase setup do - @paycheck_line = paycheck_lines(:one) + log_in_as_bob end - test "should get index" do - get :index - assert_response :success - assert_not_nil assigns(:paycheck_lines) - end - - test "should get new" do - get :new - assert_response :success - end - - test "should create paycheck_line" do - assert_difference('PaycheckLine.count') do - post :create, :paycheck_line => @paycheck_line.attributes - end - - assert_redirected_to paycheck_line_path(assigns(:paycheck_line)) - end - - test "should show paycheck_line" do - get :show, :id => @paycheck_line.to_param - assert_response :success - end - - test "should get edit" do - get :edit, :id => @paycheck_line.to_param - assert_response :success - end - - test "should update paycheck_line" do - put :update, :id => @paycheck_line.to_param, :paycheck_line => @paycheck_line.attributes - assert_redirected_to paycheck_line_path(assigns(:paycheck_line)) - end - - test "should destroy paycheck_line" do - assert_difference('PaycheckLine.count', -1) do - delete :destroy, :id => @paycheck_line.to_param - end - - assert_redirected_to paycheck_lines_path + test "dummy test" do + assert true end end diff --git a/test/functional/paycheck_templates_controller_test.rb b/test/functional/paycheck_templates_controller_test.rb new file mode 100644 index 0000000..d12c74b --- /dev/null +++ b/test/functional/paycheck_templates_controller_test.rb @@ -0,0 +1,34 @@ +require 'test_helper' + +class PaycheckTemplatesControllerTest < ActionController::TestCase + setup do + log_in_as_bob + @employee_one = Ledger.create!(:account => @company.accounts.first, :name => "Employee One", :number => "99", :credit_text => "credit", :debit_text => "debit") + @dummy_line_attributes = {:count => "1", :rate => "750 ", :amount => "750", :employee_id => @employee_one.id, :salary_code => "abc", :payroll_tax => "1", :vacation_basis => "1", :description => "qq", :line_type => "duh", :account_id => @company.accounts.first.id, :company_id => @company.id} + @paycheck_line_template = PaycheckLineTemplate.create!(@dummy_line_attributes) + qq = @dummy_line_attributes + qq.delete(:employee_id) + @global_paycheck_line_template = PaycheckLineTemplate.create!(qq) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:employees) + end + + test "should create template" do + @new_employee = Ledger.create!(:account => @company.accounts.first, :name => "New Employee", :number => "98", :credit_text => "credit", :debit_text => "debit") + assert_difference('PaycheckLineTemplate.count') do + post :create, :employee_id => @new_employee.id + end + + assert_redirected_to paycheck_template_path(@new_employee.id) + end + + test "should show paycheck" do + get :show, :id => @employee_one.id + assert_response :success + end + +end diff --git a/test/functional/paychecks_controller_test.rb b/test/functional/paychecks_controller_test.rb index 0be4df9..3e35c14 100644 --- a/test/functional/paychecks_controller_test.rb +++ b/test/functional/paychecks_controller_test.rb @@ -2,7 +2,11 @@ class PaychecksControllerTest < ActionController::TestCase setup do - @paycheck = paychecks(:one) + log_in_as_bob + @employee_one = Ledger.create!(:account => @company.accounts.where(:number => '2930').first, :name => "Employee One", :number => "99", :credit_text => "credit", :debit_text => "debit") + @dummy_line_attributes = {:count => "1", :rate => "750 ", :amount => "750", :employee_id => @employee_one.id, :salary_code => "abc", :payroll_tax => "1", :vacation_basis => "1", :description => "qq", :line_type => "duh", :account_id => @company.accounts.first.id, :company_id => @company.id} + @paycheck_line_template = PaycheckLineTemplate.create!(@dummy_line_attributes) + @paycheck = Paycheck.create!({"period_id"=>@company.periods.first.id, "employee_id"=>@employee_one.id, "paycheck_lines_attributes"=>{"0"=>{"line_type"=>"0", "description"=>"Loenn", "count"=>"150.0", "rate"=>"300.0", "amount"=>"45000.0", "account_id"=>@company.accounts.first.id, "unit_id"=>@company.units.first.id, "payroll_tax"=>"true", "vacation_basis"=>"true", "salary_code"=>"88a"}, "1"=>{"line_type"=>"2", "description"=>"Info", "count"=>"0.0", "rate"=>"345.0", "amount"=>"0.0", "account_id"=>"", "payroll_tax"=>"false", "vacation_basis"=>"false", "salary_code"=>"14b"}}}) end test "should get index" do @@ -12,13 +16,13 @@ class PaychecksControllerTest < ActionController::TestCase end test "should get new" do - get :new + get :new, :employee_id => @employee_one.id assert_response :success end test "should create paycheck" do assert_difference('Paycheck.count') do - post :create, :paycheck => @paycheck.attributes + post :create, {:paycheck => {"period_id"=>@company.periods.first.id, "employee_id"=>@employee_one.id, "paycheck_lines_attributes"=>{"0"=>{"line_type"=>"0", "description"=>"Loenn", "count"=>"150.0", "rate"=>"300.0", "amount"=>"45000.0", "account_id"=>@company.accounts.first.id, "unit_id"=>@company.units.first.id, "payroll_tax"=>"true", "vacation_basis"=>"true", "salary_code"=>"88a"}, "1"=>{"line_type"=>"2", "description"=>"Info", "count"=>"0.0", "rate"=>"345.0", "amount"=>"0.0", "account_id"=>"", "payroll_tax"=>"false", "vacation_basis"=>"false", "salary_code"=>"14b"}}}} end assert_redirected_to paycheck_path(assigns(:paycheck)) diff --git a/test/test_helper.rb b/test/test_helper.rb index c15c890..6d4f72b 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,5 +17,14 @@ class ActiveSupport::TestCase class ActionController::TestCase include Devise::TestHelpers + + + def log_in_as_bob + @user = User.find_by_email("bob@bobsdomain.com") + @company = @user.assignments.select {|a| a.role.name == "accountant"}.map {|a| a.company}.first + @user.current_company = @company + @user.save! + sign_in :user, @user + end end From 88794387baf1d48e382bd1281c9b67304035965b Mon Sep 17 00:00:00 2001 From: Ivar Nymoen Date: Tue, 27 Jul 2010 14:15:28 +0200 Subject: [PATCH 2/5] upgrade to rails 3.0.0.rc --- Gemfile | 5 +++-- config/application.rb | 2 ++ config/environments/development.rb | 3 +++ config/initializers/jquery.rb | 7 ------- 4 files changed, 8 insertions(+), 9 deletions(-) delete mode 100644 config/initializers/jquery.rb diff --git a/Gemfile b/Gemfile index f29fc3b..7c36551 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,8 @@ source 'http://rubygems.org' -gem 'rails', '3.0.0.beta4' +gem 'bundler', '>=1.0.0.rc.1' + +gem 'rails', '3.0.0.rc' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' @@ -9,7 +11,6 @@ gem 'rails', '3.0.0.beta4' gem 'pg' gem 'ruby_parser' -gem 'bundler' gem "machinist" gem "faker" gem "devise", "1.1.rc2" diff --git a/config/application.rb b/config/application.rb index 86e273e..368beb2 100644 --- a/config/application.rb +++ b/config/application.rb @@ -60,6 +60,8 @@ class Application < Rails::Application :secret => '29e52c8e64c464b41e52b98c9cceb4e43d1d8870df42124cd96be3c4004179720c0326431b0126f1f27d9c65bd834d1028c8f745ddcc8484e32ff0ac3e7d75fa' } + # define js asset loading order (:all adds :defaults in front) + config.action_view.javascript_expansions = { :defaults => ['jquery', 'rails'] } end end diff --git a/config/environments/development.rb b/config/environments/development.rb index b485404..26c839c 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,6 +1,9 @@ Lodo::Application.configure do # Settings specified here will take precedence over those in config/environment.rb + # log deprecation warnings + config.active_support.deprecation = :log + # In the development environment your application's code is reloaded on # every request. This slows down response time but is perfect for development # since you don't have to restart the webserver when you make code changes. diff --git a/config/initializers/jquery.rb b/config/initializers/jquery.rb deleted file mode 100644 index 5087b4b..0000000 --- a/config/initializers/jquery.rb +++ /dev/null @@ -1,7 +0,0 @@ -module ActionView::Helpers::AssetTagHelper - remove_const :JAVASCRIPT_DEFAULT_SOURCES - JAVASCRIPT_DEFAULT_SOURCES = %w(jquery.js rails.js) - - reset_javascript_include_default -end - From 716fba6dd5807395d2fade0f9fcc73eb79527f49 Mon Sep 17 00:00:00 2001 From: Ivar Nymoen Date: Tue, 27 Jul 2010 14:22:29 +0200 Subject: [PATCH 3/5] add support for copying paycheck templates --- app/models/company.rb | 16 ++++++++++++++++ test/blueprints.rb | 11 +++++++++++ test/bulkdata.rb | 11 +++++++++++ test/unit/company_test.rb | 19 +++++++++++++++++-- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/app/models/company.rb b/app/models/company.rb index 851505f..342c207 100644 --- a/app/models/company.rb +++ b/app/models/company.rb @@ -16,6 +16,8 @@ class Company < ActiveRecord::Base has_many :periods has_many :journals + has_many :paycheck_line_templates + # company acting as a template on create attr_accessor :template_company_id after_create :init_from_template, :if => proc { !self.template_company_id.blank? } @@ -70,20 +72,34 @@ def init_from_template accounts[acc.id].update_attributes!(:vat_account_id => vat_accounts[acc.vat_account_id].id) end + units = {} # copy units. shallow, copying addresses doesn't make sense.. template.units.each do |unit| u = Unit.new(unit.attributes) u.company = self u.address = Address.create! u.save! + units[unit.id] = u end + projects = {} # copy projects. not copying addresses. template.projects.each do |project| p = Project.new(project.attributes) p.company = self p.address = Address.create! p.save! + projects[project.id] = p + end + + # copy salary templates + template.paycheck_line_templates.where(:employee_id => nil).each do |line| + l = PaycheckLineTemplate.new(line.attributes) + l.company = self + l.account = accounts[line.account_id] + l.unit = units[line.unit_id] + l.project = projects[line.project_id] + l.save! end end diff --git a/test/blueprints.rb b/test/blueprints.rb index 572c17d..bdfc1e0 100644 --- a/test/blueprints.rb +++ b/test/blueprints.rb @@ -122,6 +122,17 @@ def print_time name, &block name { Sham.role_name } end +PaycheckLineTemplate.blueprint do + line_type { PaycheckLineTemplate::TYPES.keys.rand } + description { Sham.product_name } + salary_code { "#{rand(1000)}-#{'abc'.chars.to_a.rand}" } + count { rand(200) } + rate { rand(1200) } + amount { count * rate } + payroll_tax { rand(10) > 3 } + vacation_basis { rand(10) > 3 } +end + bob = nil admin = nil companies = [] diff --git a/test/bulkdata.rb b/test/bulkdata.rb index a7c79aa..209f7c8 100644 --- a/test/bulkdata.rb +++ b/test/bulkdata.rb @@ -122,6 +122,17 @@ def print_time name, &block name { Sham.role_name } end +PaycheckLineTemplate.blueprint do + line_type { PaycheckLineTemplate::TYPES.keys.rand } + description { Sham.product_name } + salary_code { "#{rand(1000)}-#{'abc'.chars.to_a.rand}" } + count { rand(200) } + rate { rand(1200) } + amount { count * rate } + payroll_tax { rand(10) > 3 } + vacation_basis { rand(10) > 3 } +end + bob = nil admin = nil companies = [] diff --git a/test/unit/company_test.rb b/test/unit/company_test.rb index 871ada0..e8a4639 100644 --- a/test/unit/company_test.rb +++ b/test/unit/company_test.rb @@ -8,6 +8,16 @@ class CompanyTest < ActiveSupport::TestCase # this probably is -- it certainly should be! -- non-ideomatic use of AR template = Company.select("companies.id").joins(:accounts).group("companies.id").having("count (*) > 10").first.reload + 100.times do |i| + u = nil + u = template.units.rand if rand(10) > 7 + p = nil + p = template.projects.rand if rand(10) > 6 + e = nil + e = template.employees.rand if rand(10) > 7 + PaycheckLineTemplate.make(:company => template, :unit => u, :project => p, :employee => e, :account => template.accounts.rand) + end + company = Company.new(:name => "Jalla Inc", :template_company_id => template.id) assert company.save @@ -28,8 +38,13 @@ class CompanyTest < ActiveSupport::TestCase assert_not_nil copy.vat_account_id assert_equal a1.vat_account.target_account.name, copy.vat_account.target_account.name - # ensure the tax rates for this vat account were copied as well - assert_equal Set.new(a1.vat_account.vat_account_periods.map {|p| [p.vat_account.target_account.name, p.valid_from, p.percentage]}), Set.new(copy.vat_account.vat_account_periods.map {|p| [p.vat_account.target_account.name, p.valid_from, p.percentage]}) + # ensure the tax rates for this vat account were copied + f = proc {|p| [p.vat_account.target_account.name, p.valid_from, p.percentage]} + assert_equal Set.new(a1.vat_account.vat_account_periods.map(&f)), Set.new(copy.vat_account.vat_account_periods.map(&f)) + + # compare paycheck templates + f = proc {|line| [line.line_type, line.description, line.count, line.amount, line.unit ? line.unit.name : "", line.project ? line.project.name : "", line.account ? line.account.name : ""]} + assert_equal Set.new(company.paycheck_line_templates.map(&f)), Set.new(template.paycheck_line_templates.map(&f)) end end From 92f3c9eac36ae81e7fe6479d16688965dc57faf8 Mon Sep 17 00:00:00 2001 From: Ivar Nymoen Date: Wed, 28 Jul 2010 14:11:27 +0200 Subject: [PATCH 4/5] reasonable settings for deprecation warnings? --- config/environments/production.rb | 4 ++++ config/environments/test.rb | 2 ++ 2 files changed, 6 insertions(+) diff --git a/config/environments/production.rb b/config/environments/production.rb index 0ada093..94eb5dd 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -40,4 +40,8 @@ # Enable threaded mode # config.threadsafe! config.action_mailer.default_url_options = { :host => "localhost" } + + # log deprecation warnings + config.active_support.deprecation = :stderr end + diff --git a/config/environments/test.rb b/config/environments/test.rb index 6638ac8..d656704 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -31,5 +31,7 @@ # config.active_record.schema_format = :sql config.action_mailer.default_url_options = { :host => "localhost" } + + config.active_support.deprecation = :stderr end From dfe164795cdfa81c6a07c4d72f8e15f579e31795 Mon Sep 17 00:00:00 2001 From: Ivar Nymoen Date: Wed, 28 Jul 2010 14:15:16 +0200 Subject: [PATCH 5/5] fix authorization mishap we did some authorization debugging earlier, and it seems we left a line commented out by accident. --- app/controllers/vat_accounts_controller.rb | 2 +- config/authorization_rules.rb | 2 +- test/functional/vat_accounts_controller_test.rb | 17 ++++++++++++++++- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/app/controllers/vat_accounts_controller.rb b/app/controllers/vat_accounts_controller.rb index 4d0df40..b9aa64b 100644 --- a/app/controllers/vat_accounts_controller.rb +++ b/app/controllers/vat_accounts_controller.rb @@ -4,7 +4,7 @@ class VatAccountsController < ApplicationController # GET /vat_accounts # GET /vat_accounts.xml def index - @vat_accounts = VatAccount.with_permissions_to(:index).all(:order => "accounts.number", :include => :target_account) + @vat_accounts = VatAccount.with_permissions_to(:index).where(:company_id => current_user.current_company.id).order("accounts.number").includes("target_account") respond_to do |format| format.html # index.html.erb diff --git a/config/authorization_rules.rb b/config/authorization_rules.rb index 1318f8d..5f15ec7 100644 --- a/config/authorization_rules.rb +++ b/config/authorization_rules.rb @@ -18,7 +18,7 @@ has_permission_on :accounts, :to => :create has_permission_on :vat_accounts, :to => :manage do -# if_attribute :company_id => is {user.current_company.id} + if_attribute :company_id => is {user.current_company.id} end has_permission_on :vat_accounts, :to => :create diff --git a/test/functional/vat_accounts_controller_test.rb b/test/functional/vat_accounts_controller_test.rb index a110ae6..de11ed6 100644 --- a/test/functional/vat_accounts_controller_test.rb +++ b/test/functional/vat_accounts_controller_test.rb @@ -1,4 +1,19 @@ require 'test_helper' -class VATAccountsControllerTest < ActionController::TestCase +class VatAccountsControllerTest < ActionController::TestCase + + setup do + log_in_as_bob + end + + test "should not display other companies vat accounts" do + get :index + + assert_response :success + assert_template :index + + not_ours = assigns(:vat_accounts).select {|va| va.company != @company} + assert not_ours.empty?, "It seems we're listing #{not_ours.size} vat accounts which belong to another company." + end + end