Sha256: d6236f24f49a1dfcbdc663f8e210619f39ba267cb47a545a039c576b89ba5f14

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

require_relative 'helper'
describe 'Adapter' do
  supported_by Swift::DB::Postgres do #, Swift::DB::Mysql do
    before do
      Swift.db.execute %q{drop table if exists users}
      Swift.db.execute %q{create table users(id serial, name text, created_at timestamp)}
    end

    describe 'transactions' do
      before do
        @name = 'test1 - transaction 1'
        @db   = Swift.db
      end

      it 'yields db to block' do
        @db.transaction do |db|
          assert_kind_of Swift::Adapter, db
        end

        @db.transaction :sweet do |db|
          assert_kind_of Swift::Adapter, db
        end
      end

      describe 'commits work' do
        before do
          @db.execute('truncate users')
        end

        after do
          @db.execute('select count(*) as c from users where name = ?', @name) {|r| assert_equal 1, r[:c] }
        end

        it 'should allow explicit commits' do
          @db.transaction do |db|
            db.execute('insert into users(name) values(?)', @name)
            db.commit
          end
        end

        it 'should autocommit' do
          @db.transaction do |db|
            db.execute('insert into users(name) values(?)', @name)
          end
        end
      end

      describe 'rollbacks work' do

        before do
          @db.execute('truncate users')
        end

        after do
          @db.execute('select count(*) as c from users where name = ?', @name) {|r| assert_equal 0, r[:c] }
        end

        it 'should allow explicit rollbacks' do
          @db.transaction do |db|
            db.execute('insert into users(name) values(?)', @name)
            db.rollback
          end
        end

        it 'should auto rollback' do
          assert_raises(RuntimeError) do
            @db.transaction do |db|
              db.execute('insert into users(name) values(?)', @name)
              raise 'foo'
            end
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
swift-0.6.1 test/test_transactions.rb
swift-0.6.0 test/test_transactions.rb
swift-0.5.1 test/test_transactions.rb
swift-0.5.0 test/test_transactions.rb