Skip to content

Commit

Permalink
fix tenancy spec to use a test tenant class
Browse files Browse the repository at this point in the history
  • Loading branch information
mbulat committed Oct 31, 2019
1 parent a4c5915 commit ccea8bd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class TenantPlutusTables < ActiveRecord::Migration[4.2]
def change
# add a tenant column to plutus accounts table.
add_column :plutus_accounts, :tenant_id, :integer, index: true
end
end
3 changes: 2 additions & 1 deletion fixture_rails_root/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20191031162011) do
ActiveRecord::Schema.define(version: 20191031170618) do

create_table "plutus_accounts", force: :cascade do |t|
t.string "name"
t.string "type"
t.boolean "contra", default: false
t.datetime "created_at"
t.datetime "updated_at"
t.integer "tenant_id"
t.index ["name", "type"], name: "index_plutus_accounts_on_name_and_type"
end

Expand Down
12 changes: 12 additions & 0 deletions spec/factories/tenant_factory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Plutus
class Tenant < ActiveRecord::Base
end
end

FactoryGirl.define do
factory :tenant, :class => Plutus::Tenant do
sequence :name do |n|
"Tenant #{n}"
end
end
end
28 changes: 23 additions & 5 deletions spec/models/tenancy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@
module Plutus
describe Account do
describe 'tenancy support' do

before(:all) do
m = ActiveRecord::Migration.new
m.verbose = false
m.create_table :plutus_tenants do |t|
t.string :name
end
end

after :all do
m = ActiveRecord::Migration.new
m.verbose = false
m.drop_table :plutus_tenants
end

before(:each) do
ActiveSupportHelpers.clear_model('Account')
ActiveSupportHelpers.clear_model('Asset')

Plutus.enable_tenancy = true
Plutus.tenant_class = 'Plutus::Entry'
Plutus.tenant_class = 'Plutus::Tenant'

FactoryGirlHelpers.reload()
Plutus::Asset.new
Expand All @@ -27,17 +42,20 @@ module Plutus
end

it 'validate uniqueness of name scoped to tenant' do
account = FactoryGirl.create(:asset, tenant_id: 10)
tenant = FactoryGirl.create(:tenant)
account = FactoryGirl.create(:asset, tenant: tenant)

record = FactoryGirl.build(:asset, name: account.name, tenant_id: 10)
record = FactoryGirl.build(:asset, name: account.name, tenant: tenant)
expect(record).not_to be_valid
expect(record.errors[:name]).to eq(['has already been taken'])
end

it 'allows same name scoped under a different tenant' do
account = FactoryGirl.create(:asset, tenant_id: 10)
tenant_1 = FactoryGirl.create(:tenant)
tenant_2 = FactoryGirl.create(:tenant)
account = FactoryGirl.create(:asset, tenant: tenant_1)

record = FactoryGirl.build(:asset, name: account.name, tenant_id: 11)
record = FactoryGirl.build(:asset, name: account.name, tenant: tenant_2)
expect(record).to be_valid
end
end
Expand Down

0 comments on commit ccea8bd

Please sign in to comment.