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

Replace tag/status fixtures with factories #2593

Merged
merged 5 commits into from
Oct 12, 2022
Merged

Conversation

glennmatthews
Copy link
Contributor

Closes: #DNE

What's Changed

  • Introduce TagFactory and StatusFactory
  • Delete static test fixtures for Tag and Status
  • Remove documentation and tooling for working with static test fixtures since we've changed direction.
  • Update VLANFactory.name and VRFFactory.name to reduce likelihood of the names appearing by coincidence in page HTML and causing test cases failures as a consequence.
  • Revert changes to clear_status_choices that were made in Add RouteTarget and VRF factories. Add TenancyFilterTestCaseMixin and refactor #2514 and instead just remove the tests for this function.

TODO

  • Explanation of Change(s)
  • Added change log fragment(s) (for more information see the documentation)
  • Attached Screenshots, Payload Example
  • Unit, Integration Tests
  • Documentation Updates (when adding/changing features)
  • n/a Example Plugin Updates (when adding/changing features)
  • Outline Remaining Work, Constraints from Design

@glennmatthews glennmatthews self-assigned this Oct 7, 2022
Copy link
Contributor

@jathanism jathanism left a comment

Choose a reason for hiding this comment

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

👀
image

nautobot/extras/factory.py Show resolved Hide resolved
nautobot/extras/factory.py Outdated Show resolved Hide resolved
nautobot/extras/factory.py Outdated Show resolved Hide resolved
@@ -837,12 +834,14 @@ def setUpTestData(cls):
)

def test_name(self):
params = {"name": VLAN.objects.all().values_list("name", flat=True)[:2]}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
names = list(VLAN.objects.all().values_list("name", flat=True))[:2]
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm realizing in cases like this it's gonna be a tad more optimal to slice the queryset first, and cast to a list second, assuming we have more than a handful of VLAN objects, that is.

For example:

Suggested change
names = list(VLAN.objects.all().values_list("name", flat=True))[:2]
names = list(VLAN.objects.all().values_list("name", flat=True)[:2])

Perhaps since we're starting to repeat this pattern a lot as we refactor to use the fixture factories, we might consider factoring it into a test case helper method? Just thinking aloud. Please don't change it right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good thought. Since we've already got this pattern in a bunch of tests after #2479 and #2514, maybe we should get a housekeeping item open to clean up all of these?

Copy link
Contributor

Choose a reason for hiding this comment

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

Solid plan.

Copy link
Contributor

Choose a reason for hiding this comment

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

nautobot/utilities/factory.py Show resolved Hide resolved
@@ -158,7 +158,7 @@ class Meta:
)

# note that name is *not* globally unique
name = factory.Faker("color_name")
name = factory.LazyFunction(lambda: f"{faker.Faker().color_name()}{faker.Faker().pyint()}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would this make sense to be a LazyAttributeSequence?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could do, then the names would be (random color + sequential number) instead of (random color + random number). I don't feel strongly about one versus the other, I just needed to add something because color names alone weren't proving to be unambiguous enough.

Copy link
Contributor

Choose a reason for hiding this comment

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

My thought was this line would be easier to read as:

name = factory.LazyAttributeSequence(lambda n: f"{faker.Faker().color_name()}{n}")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Easier to read but guarantees that names are globally unique, which isn't actually a requirement of the model.

Co-authored-by: Gary Snider <75227981+gsnider2195@users.noreply.github.com>
@glennmatthews glennmatthews merged commit e686dfb into next Oct 12, 2022
@glennmatthews glennmatthews deleted the gfm-tags-factory branch October 12, 2022 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants