Skip to content
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 2 commits into from
Oct 24, 2024
Merged

Conversation

timriley
Copy link
Member

Resolves #27

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want database cleaning to be something that can be easily opted into from any test, which is why the feature is activated on examples tagged as db: true (see support_db_cleaning.rb for this).

However, I still want us to provide the convenience of database cleaning by default for feature specs (i.e. tests defined using Capybara's RSpec.feature API). This file here provides this via a "loose coupling"-style of approach: it adds the db: true metadata to any example marked as a feature.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is sensible. I did something very similar with with :slice metadata, because my persistence is configured at the slice level, my slice tests are structured as spec/slices/slice_name and automatically receive this metadata, and the integration detects whether or not the slice in question has persistence configured.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. Structuring the tests around slices is something I'd like to add in a future version too. This would let you do things like you already do here, and also other neat things, like give us a way to provide different factories per slice, etc.

But as per my below, I'm keeping things un-opinionated for now, just until we can get all the basics in place (2.2 is the last stop on that journey!).

Comment on lines +7 to +15
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
}
Copy link
Member Author

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.

Copy link

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.

Copy link
Member Author

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!)

@timriley timriley merged commit 26bac83 into main Oct 24, 2024
6 checks passed
@timriley timriley deleted the add-database-cleaner-support branch October 24, 2024 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generate DatabaseCleaner setup in new apps
2 participants