Skip to content

Instantly share code, notes, and snippets.

@khadzhinov
Created October 16, 2014 18:09
Show Gist options
  • Save khadzhinov/7bee8a735916afbc0c2d to your computer and use it in GitHub Desktop.
Save khadzhinov/7bee8a735916afbc0c2d to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe Organization do
it { should have_many(:organization_bank_accounts).dependent(:destroy) }
it { should have_many(:organization_branches).dependent(:destroy) }
it { should have_many(:departments).dependent(:destroy) }
it { should have_many(:employees).dependent(:destroy) }
it { should have_many(:reports).dependent(:destroy) }
it { should have_many(:payments).dependent(:destroy) }
it { should have_many(:events).dependent(:destroy) }
it { should have_many(:feeds).dependent(:nullify) }
it { should have_many(:invites).dependent(:destroy) }
it { should have_many(:consultant_services).dependent(:destroy) }
it { should have_many(:incoming_contracts).dependent(:destroy) }
it { should have_many(:outgoing_contracts).dependent(:destroy) }
it { should have_many(:users_organizations).dependent(:destroy) }
it { should have_many(:users).dependent(:destroy) }
it { should have_many(:incoming_partnerships).dependent(:destroy) }
it { should have_many(:outgoing_partnerships).dependent(:destroy) }
it { should have_many(:external_organizations).dependent(:destroy) }
it { should have_many(:organization_addresses).dependent(:destroy) }
it { should have_many(:warehouses).dependent(:destroy) }
it { should have_many(:nomenclatures).dependent(:destroy) }
it { should have_many(:nomenclature_categories).dependent(:destroy) }
it { should have_many(:measure_types).dependent(:destroy) }
it { should have_many(:incoming_waybills).dependent(:destroy) }
it { should have_many(:outgoing_waybills).dependent(:destroy) }
it { should have_many(:write_off_acts).dependent(:destroy) }
it { should have_many(:inventory_of_materials).dependent(:destroy) }
it { should have_many(:movement_waybills).dependent(:destroy) }
it { should have_many(:stocktake_settings).dependent(:destroy) }
it { should have_many(:cash_in_orders).dependent(:destroy) }
it { should have_many(:cash_out_orders).dependent(:destroy) }
it { should have_many(:incoming_payments).dependent(:destroy) }
it { should have_many(:outgoing_payments).dependent(:destroy) }
it { should have_many(:outgoing_invoices).dependent(:destroy) }
it { should have_many(:invoices).dependent(:destroy) }
it { should have_many(:service_acts).dependent(:destroy) }
it { should have_many(:power_of_attorneys).dependent(:destroy) }
it { should have_many(:payrolls).dependent(:destroy) }
it { should have_many(:payroll_expenses).dependent(:destroy) }
it { should have_many(:bank_statements).dependent(:destroy) }
it { should have_many(:advance_statements).dependent(:destroy) }
it { should have_many(:reconciliation_acts).dependent(:destroy) }
it { should have_many(:agreements).dependent(:destroy) }
it { should have_many(:agreement_templates).dependent(:destroy) }
it { should have_many(:agreement_comments).dependent(:destroy) }
it { should have_many(:fixed_assets).dependent(:destroy) }
it { should have_many(:fixed_asset_receipts).dependent(:destroy) }
it { should have_many(:fixed_asset_acceptances).dependent(:destroy) }
it { should have_many(:fixed_asset_transfers).dependent(:destroy) }
it { should have_many(:fixed_asset_movements).dependent(:destroy) }
it { should have_many(:fixed_asset_renewals).dependent(:destroy) }
it { should have_many(:fixed_asset_writeoffs).dependent(:destroy) }
it { should have_many(:intangible_assets).dependent(:destroy) }
it { should have_many(:intangible_asset_receipts).dependent(:destroy) }
it { should have_many(:intangible_asset_acceptances).dependent(:destroy) }
it { should have_many(:intangible_asset_transfers).dependent(:destroy) }
it { should have_many(:intangible_asset_writeoffs).dependent(:destroy) }
it { should have_many(:account_entries).dependent(:destroy) }
it { should have_many(:account_rests).dependent(:destroy) }
it { should have_many(:balances).dependent(:destroy) }
it { should have_many(:pattern_numbers).dependent(:destroy) }
it { should have_many(:reports_settings).dependent(:destroy) }
it { should have_one(:organization_responsible_person).dependent(:destroy) }
it { should have_one(:organization_custom_requisite).dependent(:destroy) }
it { should have_one(:organization_document).dependent(:destroy) }
it { should belong_to(:organization_economic_type) } # test is valid
it { should belong_to(:user) }
# it { should belong_to(:country) }
#added in the associations' testing process
it { should have_many(:balances).dependent(:destroy) }
it { should have_many(:calendar_days).dependent(:destroy) }
it { should have_many(:fixed_asset_acceptance).dependent(:destroy) }
it { should have_many(:fixed_asset_movements).dependent(:destroy) }
##################################################################
it { should accept_nested_attributes_for(:organization_responsible_person).allow_destroy(true).update_only(true) }
it { should accept_nested_attributes_for(:organization_custom_requisite).allow_destroy(true).update_only(true) }
it { should accept_nested_attributes_for(:organization_document).allow_destroy(true).update_only(true) }
it { should accept_nested_attributes_for(:organization_bank_accounts).allow_destroy(true) }
it { should accept_nested_attributes_for(:organization_branches).allow_destroy(true) }
it { should accept_nested_attributes_for(:departments).allow_destroy(true) }
#it { should accept_nested_attributes_for(:consultant_services).allow_destroy(true) }
it { should accept_nested_attributes_for(:organization_addresses).allow_destroy(true) }
it { should validate_presence_of(:name) }
it { should ensure_length_of(:name).is_at_most(64) }
it { should validate_presence_of(:organization_type) }
it { should ensure_length_of(:organization_type).is_at_most(100) }
it { should validate_presence_of(:verification_status) }
it { should ensure_inclusion_of(:verification_status).in_array(Organization::VERIFICATION_STATUSES) }
it { should validate_presence_of(:organization_type) }
it { should ensure_inclusion_of(:organization_type).in_array(Organization::ORGANIZATION_TYPES) }
it { should validate_numericality_of(:tic).only_integer }
it { should ensure_length_of(:tic).is_equal_to(12)}
it { should allow_value(nil).for(:tic) }
it { should validate_numericality_of(:phone).only_integer }
it { should ensure_length_of(:phone).is_at_least(12) }
it { should allow_value(nil).for(:phone) }
it { should ensure_length_of(:email).is_at_most(32) }
it { should allow_value(nil).for(:email) }
it { should ensure_length_of(:tax_system).is_at_most(100) }
it { should allow_value(true).for(:is_consultant) }
it { should allow_value(false).for(:is_consultant) }
it { should_not allow_value(nil).for(:is_consultant) }
it { should validate_presence_of(:user_id) }
it { should validate_presence_of(:organization_economic_type_id) }
it { should validate_presence_of(:vat_payer) }
it { should validate_presence_of(:has_branch) }
it { should validate_presence_of(:has_accountant) }
it { should validate_presence_of(:matches_address) }
it { should validate_presence_of(:are_responsible_persons_real) }
context "when pe" do
before { @organization = create :organization_pe }
it "should return true when pe" do
expect(@organization.is_pe?).to eq(true)
expect(@organization.is_llp?).to eq(false)
expect(@organization.organization_type_valid?).to eq(true)
expect(@organization.id_number).to eq(@organization.iin)
expect(@organization.organization_full_name). to eq("#{I18n.t("activerecord.attributes.organization.organization_types.pe")} #{@organization.name}")
end
end
context "when pe" do
before { @organization = create :organization_llp }
it "should return true when llp" do
expect(@organization.is_pe?).to eq(false)
expect(@organization.is_llp?).to eq(true)
expect(@organization.organization_type_valid?).to eq(true)
expect(@organization.id_number).to eq(@organization.bin)
expect(@organization.organization_full_name). to eq("#{I18n.t("activerecord.attributes.organization.organization_types.llp")} #{@organization.name}")
end
end
context "with verify" do
before { @organization = create :organization }
it "should be unverified after creation" do
expect(@organization.is_verified?).to eq(false)
#expect(@organization.is_unverified?).to eq(true)
end
it "can be verified" do
@organization.update_attributes(:verification_status => "verified")
expect(@organization.is_verified?).to eq(true)
end
it "should wait sms code" do
@organization.send_verification_code!
expect(@organization.verification_status).to eq("waiting_sms_code")
end
it "set_as_unverified!" do
@organization.set_as_unverified!
expect(@organization.is_verified?).to eq(false)
end
end
describe "#responsible_persons_full_name" do
let(:org) { create :organization }
it { expect(org.responsible_persons_full_name).to eq(org.organization_responsible_person.boss_fio)}
end
describe "#send_eds_on_hsm" do
let(:org) { create :organization }
it "set unverified status when something goes wrong" do
external_obj = mock('external_obj')
# RabbitMQProducer.stub(:new) { external_obj }
# external_obj.should_receive(:publish).and_return(true)
# org.send_eds_on_hsm
# expect(org.verification_status).to eq("pending_verify_sms")
end
#TODO research how to mock RabbitMQProducer
end
describe "#organization_number_name" do
let(:org_pe) { create :organization_pe }
let(:org_llp) { create :organization_llp }
it "bin when org. type is llp " do
expect(org_llp.organization_number_name).to eq(I18n.t("activerecord.attributes.organization_custom_requisite.bin"))
end
it "iin when org. type is pe" do
expect(org_pe.organization_number_name).to eq(I18n.t("activerecord.attributes.organization_custom_requisite.iin"))
end
it "nil when org. type is incorrect" do
org_pe.organization_type = "foo"
expect(org_pe.organization_number_name).to eq(nil)
end
end
describe "#organization_type_name" do
let(:org_pe) { create :organization_pe }
let(:org_llp) { create :organization_llp }
it "llp" do
expect(org_llp.organization_type_name).to eq(I18n.t("activerecord.attributes.organization.organization_types.llp"))
end
it "pe" do
expect(org_pe.organization_type_name).to eq(I18n.t("activerecord.attributes.organization.organization_types.pe"))
end
it "nil when org. type is incorrect" do
org_pe.organization_type = "foo"
expect(org_pe.organization_type_name).to eq(nil)
end
end
describe "#check_addresses" do
let(:org) { create :organization }
it "when addresses match they should be equal" do
org.matches_address = true
org.send(:check_addresses)
actual = org.organization_addresses.select{ |a| a.address_type == "actual" }.first
legal = org.organization_addresses.select{ |a| a.address_type == "legal" }.first
expect(actual.street).to eq(legal.street)
expect(actual.house).to eq(legal.house)
expect(actual.building).to eq(legal.building)
expect(actual.apartment).to eq(legal.apartment)
expect(actual.zip_code).to eq(legal.zip_code)
expect(actual.coordinates_latitude).to eq(legal.coordinates_latitude)
expect(actual.coordinates_longitude).to eq(legal.coordinates_longitude)
end
it "addresses match and only one exists, second should be created" do
org.matches_address = true
org.organization_addresses[1] = nil
org.send(:check_addresses)
expect(org.organization_addresses.count).to eq(2)
actual = org.organization_addresses.select{ |a| a.address_type == "actual" }.first
legal = org.organization_addresses.select{ |a| a.address_type == "legal" }.first
expect(actual.street).to eq(legal.street)
expect(actual.house).to eq(legal.house)
expect(actual.building).to eq(legal.building)
expect(actual.apartment).to eq(legal.apartment)
expect(actual.zip_code).to eq(legal.zip_code)
expect(actual.coordinates_latitude).to eq(legal.coordinates_latitude)
expect(actual.coordinates_longitude).to eq(legal.coordinates_longitude)
end
end
describe "#create_pattern_numbers" do
let(:org) { create :organization }
it "creates pattern numbers" do
expect {org.send(:create_pattern_numbers) }.to change(PatternNumber, :count)
end
end
describe "#create_default_warehouse" do
it "creates pattern numbers" do
expect { create :organization }.to change(Warehouse, :count).by(1)
end
end
describe "#add_calendar_events" do
let(:org) { create :organization }
it "calls add_report_events and add_salary_events" do
org.should_receive(:add_report_events)
org.should_receive(:add_salary_events)
org.add_calendar_events
end
end
describe "#add_salary_events" do
let(:org) { create :organization }
it "creates new event with right parametres when event doesn't exist" do
StocktakeSettings.create!(:organization_id => org.id, :pay_month => "next", :pay_day => 25 )
expect(Event.all).to match_array([])
expect{ org.add_salary_events }.to change(Event, :count).by(1)
event = Event.all.first
expect(event.organization_id).to eq(org.id)
expect(event.eventable_type).to eq("Salary")
end
it "returns existing event" do
StocktakeSettings.create!(:organization_id => org.id, :pay_month => "next", :pay_day => 25 )
org.add_salary_events
expect{ org.add_salary_events }.to_not change(Event, :count)
returning_event = org.add_salary_events
event = Event.all.first
expect(event).to eq(returning_event)
end
end
describe "#add_report_events" do
let(:org) { create :organization }
it "creates new events with right parametres when events don't exist" do
expect { org.add_report_events }.to change(Event, :count)
end
it "returns existing events" do
events = org.add_report_events
expect { org.add_report_events }.to_not change(Event, :count)
expect(org.add_report_events).to eq(events)
end
end
describe
describe 'as_json' do
before { @organization = create :organization }
specify do
@organizations_json = @organization.as_json
@organizations_json.should_not include("is_deleted", "last_change")
end
end
describe "::find_organization_by_hsm_payload" do
before do
@org_llp = create :organization, :organization_type => 'llp'
@org_pe = create :organization, :organization_type => 'pe'
end
it "when org_type llp" do
payload = { "org_type" => "llp", "bin" => @org_llp.organization_custom_requisite.bin }
@res = @org_llp
@res.id = @org_llp.organization_custom_requisite.id
expect(Organization.find_organization_by_hsm_payload(payload)).to eq(@org_llp)
end
it "when org_type pe" do
payload = { "org_type" => "pe", "iin" => @org_pe.organization_custom_requisite.iin }
@res = @org_pe
@res.id = @org_pe.organization_custom_requisite.id
expect(Organization.find_organization_by_hsm_payload(payload)).to eq(@res)
end
it "when org_type invalid" do
payload = { "org_type" => "invalid"}
expect(Organization.find_organization_by_hsm_payload(payload)).to eq(false)
end
end
describe "::on_create_eds" do
before do
@org_llp = create :organization, :organization_type => 'llp'
@payload = { "org_type" => "llp", "bin" => @org_llp.organization_custom_requisite.bin, "verification_status" => "verified" }
end
it "updates organization's verification status" do
# FayeClient.should_receive(:publish)
# Organization.on_create_eds(@payload)
end
end
context "Given partnerships" do
before do
@org1, @org2, @org3 = create_list(:verified_organization, 3)
@org4 = create(:organization)
@ext_org = ExternalOrganization.mimic_organization(@org3, :parent_organization => @org2)
@ext_org.save!
@p12 = Partnership.create(:organization => @org1, :partner => @org2)
@p23 = Partnership.create(:organization => @org2, :partner => @org3)
@p24 = Partnership.create(:organization => @org2, :partner => @org4)
@p2e = Partnership.create(:organization => @org2, :partner => @ext_org)
end
it "is able to list partnerships initiated by an organization" do
expect(@org2.outgoing_partnerships).to match_array([@p23, @p24, @p2e])
end
it "lists partnerships initiated by other organizations as :incoming_partnerships" do
expect(@org2.incoming_partnerships).to eq([@p12])
end
describe "partnerships" do
it "lists only correct (legitimate) incoming and initiated partnerships by default" do
partnerships = @org2.partnerships
expect(partnerships).to match_array([@p12, @p23, @p24])
end
it "is capable of listing all partnerships (initiated + incoming, both correct and incorrect)" do
partnerships = @org2.partnerships(:all, :all)
expect(partnerships).to match_array([@p12, @p23, @p24, @p2e])
end
it "is capable of listing only correct initiated partnerships" do
partnerships = @org2.partnerships(:correct, :outgoing)
expect(partnerships).to match_array([@p23, @p24])
end
it "is capable of listing only incorrect initiated partnerships" do
partnerships = @org2.partnerships(:incorrect, :outgoing)
expect(partnerships).to eq([@p2e])
end
it "is capable of listing only verified initiated partnerships" do
partnerships = @org2.partnerships(:verified, :outgoing)
expect(partnerships).to eq([@p23])
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment