Skip to content

Commit 7a4f47d

Browse files
committed
Add a start/setup hook to cache db ids before the test start
Create the strategy once in the initializer, so start and clean can use it.
1 parent ac8c329 commit 7a4f47d

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

config/initializers/cypress_on_rails.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# [1] https://github.com/shakacode/cypress-playwright-on-rails/blob/ac5d69f9ea951c7545e5141db2abb2cb1350e740/lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb
33
# * install_folder (uses our rails engine path)
44
# * require ENV['CYPRESS'] to be set
5+
# * requires and instantiates the seeded_deletion strategy
56
if defined?(CypressOnRails) && ENV['CYPRESS'].present?
67
CypressOnRails.configure do |c|
78
c.api_prefix = ""
@@ -39,4 +40,6 @@
3940
# config.assets.unknown_asset_fallback = false
4041
# end
4142
# end
43+
require 'extensions/database_cleaner-activerecord-seeded_deletion'
44+
DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::SeededDeletion.new(:pre_count => true, :except => %w[audit_events sessions])
4245
end

cypress/e2e/app_commands/clean.rb

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
if defined?(DatabaseCleaner)
2-
# cleaning the database using database_cleaner
3-
require 'extensions/database_cleaner-activerecord-seeded_deletion'
4-
DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::SeededDeletion.new(:pre_count => true, :except => %w[audit_events sessions])
52
DatabaseCleaner.clean
63
else
74
msg = "add database_cleaner or update cypress/app_commands/clean.rb"

cypress/e2e/app_commands/start.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
if defined?(DatabaseCleaner)
2+
DatabaseCleaner.start
3+
else
4+
msg = "XXX DatabaseCleaner not defined, can't start!!!"
5+
logger.warn(msg)
6+
raise msg
7+
end
8+
puts "DatabaseCleaner started"

cypress/e2e/rails_examples/using_factory_bot.cy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
describe('Rails using factory bot examples', function() {
22
beforeEach(() => {
3-
cy.app('clean') // have a look at e2e/app_commands/clean.rb
4-
})
3+
cy.app('start');
4+
});
55

66
afterEach(() => {
7-
cy.app('clean')
8-
})
7+
cy.app('clean');
8+
});
99

1010
it('using single factory bot', function() {
1111
cy.appFactories([

cypress/e2e/ui/Settings/Application-Settings/schedule.cy.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,10 @@ describe('Automate Schedule form operations: Settings > Application Settings > S
533533
cy.expect_flash(flashClassMap.success, FLASH_MESSAGE_SCHEDULE_QUEUED);
534534
});
535535

536+
beforeEach(() => {
537+
cy.app('start');
538+
});
539+
536540
afterEach(() => {
537541
cy.app('clean');
538542
});

lib/extensions/database_cleaner-activerecord-seeded_deletion.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ module ActiveRecord
55
# SeededDeletion is a strategy that deletes all records from tables except those that existed before it was instantiated
66
# This is useful for tests that need the seeded data to be present.
77
class SeededDeletion < Deletion
8-
def initialize(opts = {})
9-
super
10-
cache_max_seeded_ids
8+
def start
9+
Rails.logger.info "XXX SeededDeletion strategy setup"
10+
self.class.table_max_id_cache = table_max_id_hash
1111
end
1212

1313
def self.table_max_id_cache
@@ -19,10 +19,6 @@ def self.table_max_id_cache=(table_id_hash)
1919
@table_max_id_cache ||= table_id_hash
2020
end
2121

22-
def cache_max_seeded_ids
23-
self.class.table_max_id_cache = table_max_id_hash
24-
end
25-
2622
delegate :table_max_id_cache, to: :class
2723

2824
private

0 commit comments

Comments
 (0)