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

refactor: change item seeding removal strategy #4264

Merged
merged 2 commits into from
May 1, 2024

Conversation

elasticspoon
Copy link
Collaborator

@elasticspoon elasticspoon commented Apr 10, 2024

New change in item seeding strategy:

All specs are seeded by default except specs tagged with skip_seed: true. Thread.current[:seeded_last] stores if the last run seeded data or not.

Cost of the Change (based on how long it takes me to run the full specs)

  • old strategy with pre-seeded data: 9:16
  • before(:all) with lots of seed_db: true: 12:56
  • before(:all) with a few skip_seed: true: 12:36

@elasticspoon elasticspoon force-pushed the change-item-seeding-strategy branch 2 times, most recently from 475a741 to b60abd4 Compare April 10, 2024 17:23
@elasticspoon elasticspoon marked this pull request as ready for review April 10, 2024 17:26
spec/rails_helper.rb Outdated Show resolved Hide resolved
@elasticspoon elasticspoon force-pushed the change-item-seeding-strategy branch 13 times, most recently from 08a811d to e045c2f Compare April 13, 2024 20:06
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only real file with changes worth looking at, all the rest is adding tags to spec files.

elasticspoon added a commit to elasticspoon/human-essentials that referenced this pull request Apr 13, 2024
As discussed here rubyforgood#4264,
some tests modify global variables and expect them to
be reset before each test. This makes it more difficult
to move forward with removal of pre-seeding.

This commit refactors certain tests to be less brittle to
changes in global state and to rely less on assumptions
on global state. Ex: not assuming that there are 0 records
of a certain type when a test starts, etc.
elasticspoon added a commit to elasticspoon/human-essentials that referenced this pull request Apr 13, 2024
As discussed here rubyforgood#4264,
some tests modify global variables and expect them to
be reset before each test. This makes it more difficult
to move forward with removal of pre-seeding.

This commit refactors certain tests to be less brittle to
changes in global state and to rely less on assumptions
on global state. Ex: not assuming that there are 0 records
of a certain type when a test starts, etc.
@elasticspoon elasticspoon added the BLOCKED This issue is blocked by another label Apr 13, 2024
@elasticspoon elasticspoon changed the title fix: change item seeding removal strategy refactor: change item seeding removal strategy Apr 13, 2024
@elasticspoon elasticspoon force-pushed the change-item-seeding-strategy branch 3 times, most recently from 4ed91bc to 3829473 Compare April 15, 2024 01:59
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

I think this PR is trying to do too much at once... can we land on the seed strategy first and then start changing the specs?

spec/rails_helper.rb Outdated Show resolved Hide resolved
spec/rails_helper.rb Outdated Show resolved Hide resolved
spec/requests/distributions_by_county_spec.rb Outdated Show resolved Hide resolved
@elasticspoon elasticspoon marked this pull request as draft April 17, 2024 00:59
spec/rails_helper.rb Outdated Show resolved Hide resolved
dorner pushed a commit that referenced this pull request Apr 25, 2024
* fix: remove more test order dependence

As discussed here #4264,
some tests modify global variables and expect them to
be reset before each test. This makes it more difficult
to move forward with removal of pre-seeding.

This commit refactors certain tests to be less brittle to
changes in global state and to rely less on assumptions
on global state. Ex: not assuming that there are 0 records
of a certain type when a test starts, etc.

* fix: minor cleanup / lint
@elasticspoon elasticspoon removed the BLOCKED This issue is blocked by another label Apr 25, 2024
@elasticspoon elasticspoon marked this pull request as ready for review April 25, 2024 20:11
@elasticspoon
Copy link
Collaborator Author

has 1 failing test but that is unrelated and fixed here: #4305

spec/rails_helper.rb Show resolved Hide resolved
Thread.current[:skipped_last_seeding] = true
unless self.class.metadata[:skip_seed]
if seeded_last && seed_current
define_global_variables
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can reorganize this a bit like so:

if seeded_last && !seed_current
  DatabaseCleaner.clean_with(:truncation) # remove previously seeded data
end
if seed_current
  seed_base_data_for_tests if !seeded_last # need to re-seed data
  define_global_variables
end

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was doing it the other way do be explicit about all the cases, also why I added the comment about the !seeded && !seeded case. You think this is better?

All specs tagged with seed_db will get seeded. This happens on a group
level, so it will happen before the whole group. Ex: if the outer most
Rspec.describe is tagged with seed_db: true the seeding will run once
before the entire spec file and once after.

Tracks the previous execution group.
- seed or not seed base on flag on current group
- truncate if the previous group seeded
Previously we were truncating if prev run had been seed
and seeding if current run needed seeding.

The more correct approach is:
if seeded_last && seed_this => only define global vars
if seeded_last && !seed_this => truncate
if !seeded_last && seed_this => seed + define globals
if !seed_last && !seed_this => do nothing
Copy link
Collaborator

@dorner dorner left a comment

Choose a reason for hiding this comment

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

Meh, it's a nit. I think this is fine.

@dorner dorner merged commit 0b6bc5e into rubyforgood:main May 1, 2024
19 checks passed
@elasticspoon
Copy link
Collaborator Author

@dorner ill rebase the other existing PRs off this. You think its possible if I get the refactors in early enough to get this done before the event?

@elasticspoon elasticspoon deleted the change-item-seeding-strategy branch May 1, 2024 20:47
@dorner
Copy link
Collaborator

dorner commented May 1, 2024

Can't hurt to try! 🎉

@elasticspoon elasticspoon mentioned this pull request May 2, 2024
21 tasks
Copy link
Contributor

github-actions bot commented May 5, 2024

@elasticspoon: Your PR refactor: change item seeding removal strategy is part of today's Human Essentials production release: 2024.05.05.
Thank you very much for your contribution!

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.

None yet

2 participants