|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'apartment/adapters/trilogy_adapter' |
| 5 | + |
| 6 | +describe Apartment::Adapters::TrilogyAdapter, database: :mysql do |
| 7 | + unless defined?(JRUBY_VERSION) |
| 8 | + |
| 9 | + subject(:adapter) { Apartment::Tenant.adapter } |
| 10 | + |
| 11 | + def tenant_names |
| 12 | + ActiveRecord::Base.connection.execute('SELECT schema_name FROM information_schema.schemata').pluck(0) |
| 13 | + end |
| 14 | + |
| 15 | + let(:default_tenant) { subject.switch { ActiveRecord::Base.connection.current_database } } |
| 16 | + |
| 17 | + it_behaves_like 'a generic apartment adapter callbacks' |
| 18 | + |
| 19 | + context 'when using - the equivalent of - schemas' do |
| 20 | + before { Apartment.use_schemas = true } |
| 21 | + |
| 22 | + it_behaves_like 'a generic apartment adapter' |
| 23 | + |
| 24 | + describe '#default_tenant' do |
| 25 | + it 'is set to the original db from config' do |
| 26 | + expect(subject.default_tenant).to eq(config[:database]) |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe '#init' do |
| 31 | + include Apartment::Spec::AdapterRequirements |
| 32 | + |
| 33 | + before do |
| 34 | + Apartment.configure do |config| |
| 35 | + config.excluded_models = ['Company'] |
| 36 | + end |
| 37 | + end |
| 38 | + |
| 39 | + after do |
| 40 | + # Apartment::Tenant.init creates per model connection. |
| 41 | + # Remove the connection after testing not to unintentionally keep the connection across tests. |
| 42 | + Apartment.excluded_models.each do |excluded_model| |
| 43 | + excluded_model.constantize.remove_connection |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + it 'processes model exclusions' do |
| 48 | + Apartment::Tenant.init |
| 49 | + |
| 50 | + expect(Company.table_name).to eq("#{default_tenant}.companies") |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + context 'when using connections' do |
| 56 | + before { Apartment.use_schemas = false } |
| 57 | + |
| 58 | + it_behaves_like 'a generic apartment adapter' |
| 59 | + it_behaves_like 'a generic apartment adapter able to handle custom configuration' |
| 60 | + it_behaves_like 'a connection based apartment adapter' |
| 61 | + end |
| 62 | + end |
| 63 | +end |
0 commit comments