Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ DatabaseCleaner[:sequel].db = :default
DatabaseCleaner[:sequel, db: :default]
DatabaseCleaner[:sequel, db: Sequel.connect(uri)]
```

## Transaction strategy with fiber concurrency extension

```ruby
around do |example|
DatabaseCleaner[:sequel].cleaning(&example)
end
```

## COPYRIGHT

See [LICENSE](LICENSE) for details.
7 changes: 7 additions & 0 deletions lib/database_cleaner/sequel/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def clean
f = @fibers.pop
f.resume
end

def cleaning(&block)
db = self.db
db.transaction(:rollback => :always, :savepoint => true, :auto_savepoint => true) do
yield
end
end
end
end
end
79 changes: 48 additions & 31 deletions spec/database_cleaner/sequel/transaction_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,37 @@

module DatabaseCleaner
module Sequel
RSpec.shared_examples "cleaning" do
it "should clean database" do
subject.cleaning do
connection[:users].insert
expect(connection[:users].count).to eq(1)
end

expect(connection[:users]).to be_empty
end

it "should work with nested transaction" do
subject.cleaning do
begin
connection.transaction do
connection[:users].insert
connection[:users].insert

expect(connection[:users].count).to eq(2)
raise ::Sequel::Rollback
end
rescue
end

connection[:users].insert
expect(connection[:users].count).to eq(1)
end

expect(connection[:users]).to be_empty
end
end

RSpec.describe Transaction do
it_should_behave_like "a database_cleaner strategy"

Expand All @@ -19,37 +50,6 @@ module Sequel

before { subject.db = connection }

context "cleaning" do
it "should clean database" do
subject.cleaning do
connection[:users].insert
expect(connection[:users].count).to eq(1)
end

expect(connection[:users]).to be_empty
end

it "should work with nested transaction" do
subject.cleaning do
begin
connection.transaction do
connection[:users].insert
connection[:users].insert

expect(connection[:users].count).to eq(2)
raise ::Sequel::Rollback
end
rescue
end

connection[:users].insert
expect(connection[:users].count).to eq(1)
end

expect(connection[:users]).to be_empty
end
end

context "start/clean" do
it "should clean database" do
subject.start
Expand Down Expand Up @@ -83,6 +83,23 @@ module Sequel
expect(connection[:users]).to be_empty
end
end

context "fiber concurrency" do
# Making stub on the current implementation of the Sequel extension :fiber_concurrency.
before do
allow(::Sequel).to receive(:current).and_return(Fiber.current)
end

it "should use fibers" do
expect(::Sequel.current).to be_a(Fiber)
end

it_should_behave_like "cleaning"
end

context "tread concurrency" do
it_should_behave_like "cleaning"
end
end
end

Expand Down