I think that I need both database_cleaner
and $DB.transaction(:rollback=>:always) because I need to clean all the
database data between specs and not all the time that I write to PostgreSQL
I use a transaction block.

Anyway, I've created the the most simple self-contained example only using
:rollback=>:always and still fails.

I attached the same code in a file as well.

Thank you.

require "sequel"

$DB = Sequel.postgres

RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.order = "random"

  config.around(:each) do |spec|
    $DB.transaction(:rollback => :always) { spec.run }
  end
end

class Account < Sequel::Model
  set_dataset $DB[:account]
  many_to_one :user, key: :users_id
end

class User < Sequel::Model
  set_dataset $DB[:users]
  one_to_many :accounts, :key => :users_id
end

class CreateUser
  def call(args)
    $DB.transaction do
      user = User.create(:name => name)
      raise StandardError if args.fetch(:raise_exception)
      Account.create(:user => user)
    end
  rescue StandardError => e
  end
end

describe CreateUser do
  subject(:create_user) { CreateUser.new }
  let(:raise_exception) { false }
  let(:args) do
    {
      :name => "Arturo",
      :raise_exception => raise_exception,
    }
  end

  it "creates a user and account" do
    create_user.call(args)
    expect(User.count).to eq 1
    expect(Account.count).to eq 1
  end

  context "raising an exception after creates the user" do
    let(:raise_exception) { true }

    it "rolls back transaction and doesn't create any user or account" do
      create_user.call(args)
      expect(Account.count).to eq 0
      expect(User.count).to eq 0 # FAILS
    end
  end
end

<http://goog_850365925>
--
Arturo Herrero <http://arturoherrero.com>


On 8 April 2014 16:16, Jeremy Evans <[email protected]> wrote:

> On Tuesday, April 8, 2014 3:19:31 AM UTC-7, Arturo Herrero wrote:
>>
>> This a complete example:
>>
>> https://github.com/arturoherrero/rolling-back-transactions
>>
>> Can anyone help me? Pull request are welcome :)
>>
>
> I'm sorry, but I'm not checking out a full project just to diagnose the
> bugs.  You need to submit the simplest possible example that shows the
> error, with all code in a single file (I said "self-contained example
> file", not "whole project").
>
> Looking at your spec_helper.rb file, I'm not sure why you are trying to
> use transactions with both database_cleaner and manually with Sequel.  You
> shouldn't use both.  Try using it with just $DB.transaction and without
> database_cleaner.
>
> Thanks,
> Jeremy
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "sequel-talk" group.
> To unsubscribe from this topic, visit
> https://groups.google.com/d/topic/sequel-talk/i_QhpnsSoik/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/sequel-talk.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.
require "sequel"

$DB = Sequel.postgres

RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.order = "random"

  config.around(:each) do |spec|
    $DB.transaction(:rollback => :always) { spec.run }
  end
end

class Account < Sequel::Model
  set_dataset $DB[:account]
  many_to_one :user, key: :users_id
end

class User < Sequel::Model
  set_dataset $DB[:users]
  one_to_many :accounts, :key => :users_id
end

class CreateUser
  def call(args)
    $DB.transaction do
      user = User.create(:name => name)
      raise StandardError if args.fetch(:raise_exception)
      Account.create(:user => user)
    end
  rescue StandardError => e
  end
end

describe CreateUser do
  subject(:create_user) { CreateUser.new }
  let(:raise_exception) { false }
  let(:args) do
    {
      :name => "Arturo",
      :raise_exception => raise_exception,
    }
  end

  it "creates a user and account" do
    create_user.call(args)
    expect(User.count).to eq 1
    expect(Account.count).to eq 1
  end

  context "raising an exception after creates the user" do
    let(:raise_exception) { true }

    it "rolls back transaction and doesn't create any user or account" do
      create_user.call(args)
      expect(Account.count).to eq 0
      expect(User.count).to eq 0 # FAILS
    end
  end
end

Reply via email to