On Tuesday, April 8, 2014 9:07:37 AM UTC-7, Arturo Herrero wrote:
>
>
> 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.
>
Well, this isn't self-contained, as it relies on the tables already being
created. But it is at least enough to show the issue.
>
> 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
>
This is expected to fail as you are still inside the same database
transaction, and you haven't rolled the transaction back yet.
If you want to test transactional behavior, you can't run your examples
inside a transaction, unless all of your transactions inside the examples
are using savepoints. You have to run your examples outside of a
transaction, and then clean up after by deleting the rows.
Thanks,
Jeremy
--
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.