Switching from :transaction to :truncation for some tests doesn't work
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 45/100
Hướng nghiên cứu
Bắt đầu trong spec/rails_helper.rb và tái hiện RSpec example được đánh dấu bằng truncate: true trong khi Sidekiq chạy như một process riêng biệt. Kiểm tra strategy của DatabaseCleaner sau hook và xác minh khả năng nhìn thấy các row từ PostgreSQL; hoàn tất khi các example được đánh dấu sử dụng truncation và expose các record ra bên ngoài, trong khi phần suite còn lại vẫn mang tính transactional.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
So, I have some tests that need truncation because I am testing the whole Sidekiq stack and as a separate process Sidekiq needs to be able to see the records created in the original process. I want to keep the suite in general as :transaction though as the rest of the tests are fine with that strategy.
To do this I add come config to the rspec config:
config.before(:each) do |example|
DatabaseCleaner.start
if example.metadata[:truncate]
DatabaseCleaner.strategy = :truncation
else
DatabaseCleaner.strategy = :transaction
end
end
Then add metadata to the test:
it 'should doit', truncate: true do
# create records then run the background worker
# wait for the background worker to finish
end
The problem is that Sidekiq gets ActiveRecord not found errors, and sure enough, there are no rows available to on a separate database (postgres) connection:
connector_test=# select count(*) from contacts;
count
-------
0
(1 row)
If I stick byebug in the test I can confirm that there are in fact records:
(byebug) Contact.count
3
but the strategey does seem to be truncation:
(byebug) DatabaseCleaner[:active_record].strategy.class
DatabaseCleaner::ActiveRecord::Truncation
(byebug) DatabaseCleaner[:active_record].strategy
#<DatabaseCleaner::ActiveRecord::Truncation:0x00007f95e600ff68 @only=nil, @tables_to_exclude=["schema_migrations"], @pre_count=nil, @reset_ids=nil, @cache_tables=true, @db=:default>
If I re-run the test after configuring truncation for all of the tests:
config.before(:suite) do
DatabaseCleaner.strategy = :truncation
…
end
I can see that the strategy in the test is the same:
(byebug) DatabaseCleaner[:active_record].strategy
#<DatabaseCleaner::ActiveRecord::Truncation:0x00007f8faf0ccec0 @only=nil, @tables_to_exclude=["schema_migrations"], @pre_count=nil, @reset_ids=nil, @cache_tables=true, @db=:default>
and the records are available in the test:
(byebug) Contact.count
3
but this time the records are also available in the database:
connector_test=# select count(*) from contacts;
count
-------
3
(1 row)
and my test passes. These are the versions of relevant gems I am using
$ bundle info database_cleaner
* database_cleaner (1.7.0)
Summary: Strategies for cleaning databases. Can be used to ensure a clean state for testing.
Homepage: http://github.com/DatabaseCleaner/database_cleaner
Path: /Users/will/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/database_cleaner-1.7.0
$ bundle info rspec-rails
* rspec-rails (3.8.3)
Summary: RSpec for Rails
Homepage: https://github.com/rspec/rspec-rails
Path: /Users/will/.rbenv/versions/2.6.5/lib/ruby/gems/2.6.0/gems/rspec-rails-3.8.3
Upgrading database cleaner
So I noticed as I was gathering the version data that database_cleaner was out of date (I could have sworn that I updated it already!). Anyway, after upgrading database_cleaner it now looks like inside the test the reported strategy matches the observed behaviour:
(byebug) DatabaseCleaner[:active_record].strategy
#<DatabaseCleaner::ActiveRecord::Transaction:0x00007ff8ba938928 @db=:default>
This is despite my strategy switch seeming to work OK in the rspec config:
[57, 66] in /Users/will/clients/foo/repos/web-app/spec/rails_helper.rb
57: config.before(:each) do |example|
58: $redis.flushall
59: DatabaseCleaner.start
60:
61: byebug
=> 62: if example.metadata[:truncate]
63: DatabaseCleaner.strategy = :truncation
64: else
65: DatabaseCleaner.strategy = :transaction
66: end
(byebug) example.metadata[:truncate]
true
and as the test is using transactions not truncation the external process can't see the records and the test fails. Any ideas what's going on here?
- Ngôn ngữ chính
- Ruby
- Star
- 3k
- Fork
- 485
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DatabaseCleaner/database_cleaner
-
:transaction strategy: uncommitted data disappears after perform_enqueued_jobs on Rails ≥ 7.2Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 67/100
-
Cleaning of dev DBĐang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
-
Cannot truncate a table referenced in a foreign key constraint (ActiveRecord::StatementInvalid)Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
DatabaseCleaner/database_cleaner#717 · 5 bình luận · 1 reaction ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
DatabaseCleaner/database_cleaner#716 · 2 bình luận ·
Tất cả issue của DatabaseCleaner/database_cleaner
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
forem/forem#23933 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Allow customizing the page titleĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
rails/mission_control-jobs#347 ·