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 smart_proxy factories and tests #10143

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

ekohl
Copy link
Member

@ekohl ekohl commented Apr 27, 2024

This makes the factories consistently use association, which works properly with build and create strategies. They're implemented as traits so can be mixed.

Because they're now consistent, it can use loops to define all features.

Currently a draft because I only did limited testing. If this works well, other factories should be cleaned up in a similar way. They can be made a lot shorter and more powerful. With association you can also use build_stubbed in more places, so eventually we can speed up more tests to avoid database interactions.

This makes the factories consistently use association, which works
properly with build and create strategies. They're implemented as traits
so can be mixed.

Because they're now consistent, it can use loops to define all features.
before_save :sanitize_url, :associate_features
# But it can be useful to skip in tests
attr_accessor :skip_associate_features
before_save :associate_features, unless: :skip_associate_features
Copy link
Member Author

Choose a reason for hiding this comment

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

I wasn't sure if this was the best way, but it's quite useful in tests to avoid stubbing things.

Comment on lines +7 to +8
subnet = FactoryBot.build_stubbed(:subnet_ipv4, :name => 'My subnet')
proxy = FactoryBot.build_stubbed(:smart_proxy, :dhcp, subnets: [subnet])
Copy link
Member Author

Choose a reason for hiding this comment

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

By using build_stubbed it saves a database hit. It would be interesting to see how much more we can save. I'd love to have pure unit tests that can run with nulldb. Perhaps it could even use build.

proxy_features.each do |feature|
trait feature do
smart_proxy_features do
[association(:smart_proxy_feature, feature, :smart_proxy => @instance)]
Copy link
Member Author

Choose a reason for hiding this comment

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

https://thoughtbot.github.io/factory_bot/cookbook/interconnected-associations.html said you should be able to use instance, but I only got it to work with @instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant