Sha256: 8fdc57f64044569795fa4c25a4c64d346163abea1b058d7046c594f133683681

Contents?: true

Size: 1.53 KB

Versions: 19

Compression:

Stored size: 1.53 KB

Contents

require "spec_helper"

describe "Connections" do
  describe "Simple::SQL.connect" do
    before do
      org = create(:organization)
      2.times { create(:user, organization_id: org.id) }
    end

    after do
      expect(User.count).to eq(2)
    end

    describe 'automatic connections' do
      let(:db) { Simple::SQL.connect }

      it 'can mix Simple::SQL inside ActiveRecord' do
        User.transaction do
          User.delete_all
          expect(db.ask("SELECT count(*) FROM users")).to eq(0)

          raise ActiveRecord::Rollback
        end

        db.transaction do
          db.ask("DELETE FROM users")

          expect(User.count).to eq(0)

          raise ActiveRecord::Rollback
        end
      end
    end

    describe 'explizit connections' do
      let(:db) { Simple::SQL.connect(::Simple::SQL::Config.determine_url) }

      it 'runs in separate transactions' do
        User.transaction do
          User.delete_all
          expect(db.ask("SELECT count(*) FROM users")).to eq(2)

          raise ActiveRecord::Rollback
        end

        db.transaction do
          db.ask("DELETE FROM users")

          expect(User.count).to eq(2)

          raise ActiveRecord::Rollback
        end
      end
    end
  end

  describe "Simple::SQL.disconnect!" do
    let(:default_db) { Simple::SQL.connect }
    let(:db) { Simple::SQL.connect(::Simple::SQL::Config.determine_url) }

    it 'disconnects everything' do
      Simple::SQL.disconnect!
      expect(ActiveRecord::Base.connection_pool.connections.length).to eq(0)
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
simple-sql-0.5.37 spec/simple/sql/connect_spec.rb
simple-sql-0.5.36 spec/simple/sql/connect_spec.rb
simple-sql-0.5.35 spec/simple/sql/connect_spec.rb
simple-sql-0.5.34 spec/simple/sql/connect_spec.rb
simple-sql-0.5.33 spec/simple/sql/connect_spec.rb
simple-sql-0.5.32 spec/simple/sql/connect_spec.rb
simple-sql-0.5.31 spec/simple/sql/connect_spec.rb
simple-sql-0.5.30 spec/simple/sql/connect_spec.rb
simple-sql-0.5.29 spec/simple/sql/connect_spec.rb
simple-sql-0.5.28 spec/simple/sql/connect_spec.rb
simple-sql-0.5.27 spec/simple/sql/connect_spec.rb
simple-sql-0.5.26 spec/simple/sql/connect_spec.rb
simple-sql-0.5.25 spec/simple/sql/connect_spec.rb
simple-sql-0.5.24 spec/simple/sql/connect_spec.rb
simple-sql-0.5.23 spec/simple/sql/connect_spec.rb
simple-sql-0.5.22 spec/simple/sql/connect_spec.rb
simple-sql-0.5.21 spec/simple/sql/connect_spec.rb
simple-sql-0.5.20 spec/simple/sql/connect_spec.rb
simple-sql-0.5.19 spec/simple/sql/connect_spec.rb