-
Notifications
You must be signed in to change notification settings - Fork 263
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
Add RouteTarget and VRF factories. Add TenancyFilterTestCaseMixin and refactor #2514
Conversation
… refactor accordingly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking pretty good. I'd like to understand a little bit more the need for two separate random instance getters.
One is for getting a single random instance (e.g., ForeignKey), the other is for getting 0-to-n random instances (e.g., ManyToManyField). The latter is "new" in this PR but only inasmuch as it's a generalization of the |
…ANGroupFactory, VLANFactory; change API for random_instance() and get_random_instances(); improve reliability of view-filtering test cases
One change I want to draw attention to is the change in the various self.assertEqual(self.filterset(params, self.queryset).qs.count(), HARDCODED_NUMBER) to: self.assertQuerysetEqual(self.filterset(params, self.queryset), self.queryset.filter(...)) First, it accounts for the fact that due to factories we don't necessarily have a specific number of filtered instances known in advance. But secondly, using In some cases, the queryset ordering may not necessarily be guaranteed, such as in the VRF case where we might have two VRFs with the same |
params = {"vlan_groups": [vlan_groups[0].pk, vlan_groups[1].slug]} | ||
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) | ||
self.assertQuerysetEqual( | ||
self.filterset(params, self.queryset).qs, self.queryset.filter(vlan_groups__in=vlan_groups).distinct() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was distinct required here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general it seems that querysets using a reverse lookup, e.g. queryset.filter(<related_name>__in=...)
can return duplicate entries. Not sure if every single case where I added distinct()
is needed but at least some of them definitely were.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I only have the TODO
questions, but nothing blocking. This is looking pretty solid. Another @glennmatthews hit.
) * Cleanup and resiliency * Fix #2536 * Address some review feedback * Address review feedback, rename command to 'generate_test_data'
… refactor (#2514) * Add RouteTarget and VRF factories. Add TenancyFilterTestCaseMixin and refactor accordingly. * Add change fragment * Add BaseModelFactory that calls validated_save(); add RoleFactory, VLANGroupFactory, VLANFactory; change API for random_instance() and get_random_instances(); improve reliability of view-filtering test cases * Restore API for random_instance / get_random_instances, address review feedback * Linting * update changelog * Address review comments, various test fixes. * Address more review feedback * More test fixes * More test fixes * Add housekeeping issue reference * Make use of test factories opt-in so as to not break plugin tests (#2570) * Cleanup and resiliency * Fix #2536 * Address some review feedback * Address review feedback, rename command to 'generate_test_data'
Towards: #2283
What's Changed
OrganizationalModelFactory
andPrimaryModelFactory
base classes - currently these are mostly empty but I've left TODO comments regarding potential areas for extension of these. I moved the Tag mapping logic out of the individual factories and intoPrimaryModelFactory
so that it doesn't need to be repeated everywhere.BaseModelFactory
as a common parent of these two classes, and overrode the_create
API (which despite the leading underscore is a documented extension point) to call our.validated_save()
method when creating model instances to ensure they are valid.get_random_instances
factory helper function, and used it for Tag mapping as well asimport_target
/export_target
mapping on the VRF factory.tenant_group
filters that I made in Factory-boy fixtures: Tenant, TenantGroup, RIR, Aggregate #2479, specifically that the tests weren't accounting for the fact thattenant_group
is a TreeNodeMultipleChoice filter and so matches not only against the specific tenant group(s) provided, but also any of their descendants. As a result many of thetest_tenant_group
tests began failing after I modified the factories (and hence the specific set of random data being generated) because they weren't accounting for the possibility of sub-groups.tenant
andtenant_group
filters is basically boilerplate, and I got tired of making the same change in a dozen different places, so I went ahead and created aTenancyFilterTestCaseMixin
class and made use of it throughout our tests. This results in a decent amount of test code reduction, yay!TODO