-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up DatabaseCleaner for tests (#28)
- Loading branch information
Showing
6 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
|
||
group :test do | ||
# Web integration | ||
gem "capybara" | ||
gem "rack-test" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
|
||
group :test do | ||
# Database | ||
gem "database_cleaner-sequel" | ||
|
||
# Web integration | ||
gem "capybara" | ||
gem "rack-test" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# frozen_string_literal: true | ||
|
||
# Tag feature spec examples as `:db` | ||
# | ||
# See support/db/cleaning.rb for how the database is cleaned around these `:db` examples. | ||
RSpec.configure do |config| | ||
config.define_derived_metadata(type: :feature) do |metadata| | ||
metadata[:db] = true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require "database_cleaner/sequel" | ||
|
||
# Clean the databases between tests tagged as `:db` | ||
RSpec.configure do |config| | ||
# Returns all the configured databases across the app and its slices. | ||
# | ||
# Used in the before/after hooks below to ensure each database is cleaned between examples. | ||
# | ||
# Modify this proc (or any code below) if you only need specific databases cleaned. | ||
all_databases = -> { | ||
slices = [Hanami.app] + Hanami.app.slices.with_nested | ||
|
||
slices.each_with_object([]) { |slice, dbs| | ||
next unless slice.key?("db.rom") | ||
|
||
dbs.concat slice["db.rom"].gateways.values.map(&:connection) | ||
}.uniq | ||
} | ||
|
||
config.before :suite do | ||
all_databases.call.each do |db| | ||
DatabaseCleaner[:sequel, db: db].clean_with :truncation, except: ["schema_migrations"] | ||
end | ||
end | ||
|
||
config.before :each, :db do |example| | ||
strategy = example.metadata[:js] ? :truncation : :transaction | ||
|
||
all_databases.call.each do |db| | ||
DatabaseCleaner[:sequel, db: db].strategy = strategy | ||
DatabaseCleaner[:sequel, db: db].start | ||
end | ||
end | ||
|
||
config.after :each, :db do | ||
all_databases.call.each do |db| | ||
DatabaseCleaner[:sequel, db: db].clean | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters