Cleaning does not occur on subsequent specs when there are two databases and shared examples
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 30/100
Research direction
Reproduce the shared-example spec using the two database.yml connections and the hooks in spec/support/database_cleaner.rb. Start with DatabaseCleaner’s per-database start, clean, and cleaning calls around the two it_behaves_like examples. Done means both examples begin with RawDatum.count equal to zero and retain their expected record counts after each job runs.
Written by the indexing model from the issue text.
Description
This might be a rare use case, but I have an app that uses two databases. Here's a snippet from my database.yml file:
test:
primary:
<<: *default
database: primary_test
cache:
<<: *default
database: secondary_test
I have one ActiveRecord model (RawDatum) set up to read/write to/from the secondary database.
Here's a summarized version of my RSpec spec:
require "rails_helper"
describe SomeJob, type: :job do
shared_examples "perform" do |api_server_id, expected_records_count|
describe "#perform" do
it "inserts records into RawDatum" do
# This will fail on the 2nd `it_behaves_like` below
expect(RawDatum.count).to eq(0)
described_class.perform_now(api_server_id)
expect(RawDatum.count).to eq(expected_records_count)
end
end
end
describe "API One" do
it_behaves_like "perform", 1, 822
end
describe "API Two" do
it_behaves_like "perform", 2, 439
end
end
Here is my spec/support/database_cleaner.rb file:
require "database_cleaner"
# see https://github.com/DatabaseCleaner/database_cleaner#how-to-use
RSpec.configure do |config|
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner[:active_record, db: :primary].strategy = :transaction
DatabaseCleaner[:active_record, db: :cache].strategy = :transaction
DatabaseCleaner[:active_record, db: :primary].clean_with(:truncation)
DatabaseCleaner[:active_record, db: :cache].clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner[:active_record, db: :primary].start
DatabaseCleaner[:active_record, db: :cache].start
end
config.after(:each) do
DatabaseCleaner[:active_record, db: :primary].clean
DatabaseCleaner[:active_record, db: :cache].clean
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end
- Dominant language
- Ruby
- Stars
- 76
- Forks
- 76
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DatabaseCleaner/database_cleaner-active_record
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 5/5 Over a week Newbie friendliness 30/100
DatabaseCleaner/database_cleaner-active_record#128 · 1 comment ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
DatabaseCleaner/database_cleaner-active_record#127 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
All issues in DatabaseCleaner/database_cleaner-active_record
Similar issues
-
security
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
CircuitVerse/CircuitVerse#7967 · 1 comment · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
simp/pupmod-simp-stunnel#173 ·
-
Polish QDrantOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
o19s/quepid#1825 · 1 comment ·
Maintainers usually reply within 1 day