require 'spec_helper' require 'test_db/test_db' describe RemoteDb do let(:connection_spec) do { 'adapter' => 'sqlite3', 'database' => 'test_database.sqlite3', 'pool' => 250, 'username' => 'root', 'password' => 'password', 'host' => '127.0.0.1' } end describe 'configuring the gem' do it 'should allow to set and read database settings' do expect(TestDb.configuration.adapter).to be_nil expect(TestDb.configuration.pool).to be_nil expect(TestDb.configuration.host).to be_nil expect(TestDb.configuration.username).to be_nil expect(TestDb.configuration.password).to be_nil expect(TestDb.configuration.database).to be_nil TestDb.configure(connection_spec) expect(TestDb.configuration.adapter).to eq(connection_spec['adapter']) expect(TestDb.configuration.pool).to eq(connection_spec['pool']) expect(TestDb.configuration.host).to eq(connection_spec['host']) expect(TestDb.configuration.username).to eq(connection_spec['username']) expect(TestDb.configuration.password).to eq(connection_spec['password']) expect(TestDb.configuration.database).to eq(connection_spec['database']) end it 'should allow retrieve connection specifications' do TestDb.configure(connection_spec) expect(TestDb.configuration.connection_spec).to eq({ adapter: connection_spec['adapter'], pool: connection_spec['pool'], host: connection_spec['host'], username: connection_spec['username'], password: connection_spec['password'], database: connection_spec['database'] }) end end describe 'Loading models when the database settings are not valid' do before do TestDb.configure({adapter: nil, host: nil, username: nil, password: nil, database: nil}) end it 'should throw an exception, and not load models' do expect { TestDb.load_models! }.to raise_error end end describe 'Loading models when the database settings are valid' do before do TestDb.configure(connection_spec) TestDb.prepare_test_db! end after do TestDb.cleanup_test_db! end it 'should not throw any exceptions, and load models' do expect(TestDb.constants.include? :Person).to be_falsey TestDb.load_models! expect(TestDb.constants.include? :Person).to be_truthy end context 'when the models are loaded' do before do TestDb.load_models! end it 'should allow querying models' do expect(TestDb::Person.count).to eq(1) person = TestDb::Person.first expect(person.id).to eq(1) expect(person.first_name).to eq('John') end it 'should not allow creating or updating new models' do person = TestDb::Person.first person.first_name = 'Bob' expect { person.save }.to raise_error(ActiveRecord::ReadOnlyRecord) expect { person.destroy }.to raise_error(ActiveRecord::ReadOnlyRecord) expect { TestDb::Person.create(first_name: 'Bob') }.to raise_error(ActiveRecord::ReadOnlyRecord) expect { TestDb::Person.destroy_all }.to raise_error(ActiveRecord::ReadOnlyRecord) end it 'should not allow accessing hidden columns' do person = TestDb::Person.first expect { person.last_name }.to raise_error(RemoteDb::Concerns::RestrictedColumns::ForbiddenColumnException) expect { person[:last_name] }.to raise_error(RemoteDb::Concerns::RestrictedColumns::ForbiddenColumnException) end context '#without_readonly' do context 'when not specifying a environment' do it 'should raise an error' do expect { TestDb.without_readonly do TestDb::Person.create(first_name: 'Bob') end }.to raise_error(RemoteDb::Concerns::Configurable::ForbiddenActionException) end end context 'when not specifying a test environment' do before do TestDb.configuration.environment = :development end it 'should raise an error' do expect { TestDb.without_readonly do TestDb::Person.create(first_name: 'Bob') end }.to raise_error(RemoteDb::Concerns::Configurable::ForbiddenActionException) end end context 'when using a test environment' do before do TestDb.configuration.environment = :test end it 'should not raise an error' do expect(TestDb::Person.count).to eq(1) TestDb.without_readonly do TestDb::Person.create(first_name: 'Bob') end expect(TestDb::Person.count).to eq(2) expect(TestDb::Person.first.first_name).to eq('John') expect(TestDb::Person.last.first_name).to eq('Bob') end it 'should not raise error and successfully create with hidden column' do expect(TestDb::Person.count).to eq(1) TestDb.without_readonly do TestDb::Person.create(first_name: 'Alice', last_name: 'Smith') end expect(TestDb::Person.count).to eq(2) expect(TestDb::Person.last.first_name).to eq('Alice') end end end end end end