-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set up DatabaseCleaner for tests #28
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made this a proc because if I instead made it a method, then I'd need a place to put it. In my own apps I use
module Test
(and various nested modules within it) to put utility code like this, but for standard Hanami apps, I don't think we're yet at the point where we can introduce a structure like that. I'd like to do it, but it will need a bit more thought on the approach, as well as additional effort put into docs, both of which too much to fit in our remaining time before 2.2.And so we have the proc. It remains local, so no concrete method naming required, and given the size of this file, I think it's still readable enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with your caution. While RSpec is clearly the default in the community, it would be good to be forward-thinking and allow something new and interesting to be used in addition to just minitest, and that flexibility will be hard.
What concerns me about RSpec is how difficult parallelism is with it, which contributes to long-term slow test suites in my experience. The modular separation of Slices in Hanami would otherwise make parallel testing very attractive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, thanks for considering this, @alassek. Glad to hear you agree.
(I'd love for us to provide a parallel testing experience in the future!)