-
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
Generate DatabaseCleaner setup in new apps #27
Comments
Here's mine from my app, fwiw: # frozen_string_literal: true
require "database_cleaner/sequel"
# We need to have this because we're using a relative path to a sqlite file
DatabaseCleaner.allow_remote_database_url = true
# We avoid touching the DB in our tests, except for request and repo specs
RSpec.configure do |config|
config.before(:suite) do
# This isn't necessary but a good safeguard in case our suite fails
DatabaseCleaner[:sequel].clean_with(:deletion)
end
config.around(:each, type: :repo) do |example|
DatabaseCleaner[:sequel].strategy = :transaction
DatabaseCleaner.cleaning do
example.run
end
end
config.around(:each, type: :request) do |example|
DatabaseCleaner[:sequel].strategy = :deletion
DatabaseCleaner.cleaning do
example.run
end
end
end I don't know if we want to be as opinionated as this. I think we should be able to avoid |
Mine is probably way too idiosyncratic to be useful require "database_cleaner/sequel"
require "persistence"
RSpec.configure do |config|
config.before :suite do
Persistence.specification.each do |_, spec|
DatabaseCleaner[:sequel, db: spec.slice["persistence.db"]].clean_with :truncation, { except: %w[schema_migrations system_metadata] }
end
end
config.around :each, :slice do |example|
container["persistence.db"].transaction(rollback: :always, auto_savepoint: true) do
example.run
end
end
end Although I think that part about excepting the @cllns does yours not interfere with your |
FYI, I've left a nudge here about getting a new database_cleaner released so we don't have to include Speaking of that default config, now that we have multiple gateways, I think it's fairly important that it now handles all possible database arrangements out of the box. I'm going to start work on putting something together. |
Here's a first cut at a config that operates on all databases. It exposes more "wiring" than I would like, but with some comments added in, I think it would be OK as a good enough starting point. require "database_cleaner/sequel"
RSpec.configure do |config|
all_databases = -> {
slices = [Hanami.app] + Hanami.app.slices.with_nested
slices.each_with_object([]) { |slice, dbs|
dbs.concat slice["db.rom"].gateways.values.map(&:connection) if slice["db.rom"]
}.uniq
}
config.before :suite do
all_databases.call.each do |db|
DatabaseCleaner[:sequel, db:].clean_with :truncation, except: ["schema_migrations"]
end
end
config.prepend_before :each, :db do |example|
strategy = example.metadata[:js] ? :truncation : :transaction
all_databases.call.each do |db|
DatabaseCleaner[:sequel, db:].strategy = strategy
DatabaseCleaner[:sequel, db:].start
end
end
config.append_after :each, :db do
all_databases.call.each do |db|
DatabaseCleaner[:sequel, db:].clean
end
end
end The downside of this config is that it is "eager", in that it'll go and operate on every database in the app, even if an individual test is being run that doesn't even use those databases. In the future I think we can add APIs to hanami-db or hanami-rspec to make this nicer, as well as introduce a more opinionated structure to our default RSpec setup (e.g. a directory per slice, with the slice name added to the examples as metadata) so that the database cleaner config could know which specific databases to operate on. However, at this current (late) stage in the 2.2 development cycle, I'd prefer to keep things simple and not rush to introduce new public API or directory structures. I think we could learn from some real-world usage and discussions with our community about what could serve them best. What do you think? |
One minor thought: it would be nice if we could reduce |
Only difficuly would be thinking of an appropriate interface.
|
Cleaning the database between tests is essential functionality, and I don't think users should have to think about it.
We need a setup that:
I think we should just generate the appropriate config into
spec/support/database_cleaner.rb
for the app database, and include some commented examples of how to make it work for additional databases.Later (post-2.2) we can look at ways at codifying certain aspects of iterating over/interacting with the app's set of databases into hanami-db, and then find ways for that to be used to configure DatabaseCleaner.
The text was updated successfully, but these errors were encountered: