require 'spec_helper' describe MultiTenant, 'Record modifications' do let(:account) { Account.create! name: 'test' } let(:account2) { Account.create! name: 'test2' } let(:project) { Project.create! name: 'something', account: account } let(:project2) { Project.create! name: 'something2', account: account2, id: project.id } it 'includes the tenant_id in DELETEs when using object.destroy' do # two records with same id but different account_id # when doing project.destroy it should delete only the current one # by adding account_id to the destroy expect(project.account).to eq(account) expect(project2.account).to eq(account2) expect(project.id).to eq(project2.id) MultiTenant.without() do expect(Project.count).to eq(2) project.destroy expect(Project.count).to eq(1) end MultiTenant.with(account) do expect(Project.where(id: project.id).first).not_to be_present end MultiTenant.with(account2) do expect(Project.where(id: project2.id).first).to be_present end end it 'includes the tenant_id in DELETEs when using object.delete' do # two records with same id but different account_id # when project.delete it should delete only the current one # by adding account_id to the destroy expect(project.account).to eq(account) expect(project2.account).to eq(account2) expect(project.id).to eq(project2.id) MultiTenant.without() do expect(Project.count).to eq(2) project.delete expect(Project.count).to eq(1) end MultiTenant.with(account) do expect(Project.where(id: project.id).first).not_to be_present end MultiTenant.with(account2) do expect(Project.where(id: project2.id).first).to be_present end end it 'should not update other objects with same id when calling object.update_columns' do # When two records with same id but different account_id are updated, it should only update the current one expect(project.account).to eq(account) expect(project2.account).to eq(account2) expect(project.id).to eq(project2.id) MultiTenant.without do project2.update_columns(name: 'newthing2') expect(project.reload.name).to eq('something') expect(project2.reload.name).to eq('newthing2') end end it 'should return the same object when calling object.reload' do # When two records with same id but different account_id are updated, it should not return the other object expect(project.reload.account_id).to eq(account.id) expect(project2.reload.account_id).to eq(account2.id) end it 'test delete for reference tables' do category1 = Category.create! name: 'Category 1' expect(Category.count).to eq(1) category1.delete expect(Category.count).to eq(0) end it 'test delete for non distributed tables' do unscoped = UnscopedModel.create! name: 'Canada' expect(UnscopedModel.count).to eq(1) unscoped.delete expect(UnscopedModel.count).to eq(0) end it 'includes the tenant_id in UPDATEs' do project.name = 'something else' project.save! MultiTenant.with(account) do expect(Project.find(project.id).name).to eq 'something else' end end end