pytest-dev / pytest-dev/pytest-factoryboy

factory.SubFactory fixture collision when the model has the same name

Open
#218 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
400
Forks
44
Avg merge
6h 34m
Merged PRs (30d)
1

Description

Hi everyone! (Long time no talk)

I just came into the following situation that I can't seem to be able to untangle.

I have a "Product" model that is part of our core business logic, and a "Product" model coming in from Dj-stripe

When trying to set up the stripe models and relationships via the factories, I am running into a name collision


import factory
from djstripe.enums import SubscriptionStatus
from factory.django import DjangoModelFactory
from djstripe.models import (
    Product as SubscriptionProduct,
    Plan as SubscriptionPlan,
    Customer as SubscriptionCustomer,
    Price as SubscriptionPrice,
    Subscription, SubscriptionItem
)


class SubscriptionProductFactory(DjangoModelFactory):
    class Meta:
        model = SubscriptionProduct

    name = "Pro"

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        product = super()._create(model_class, *args, **kwargs)
        return product


class SubscriptionPriceFactory(DjangoModelFactory):
    class Meta:
        model = SubscriptionPrice

    active = True
    currency = "EUR"
    unit_amount = 1000
    product_test = factory.SubFactory("factories.stripe.SubscriptionProductFactory")
    lookup_key = "pro_monthly"

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        price = super()._create(model_class, *args, **kwargs)
        return price

But when the SubscriptionProductFactory is used as a SubFactory, the inner factoryboy code is evaluating it's model.name

image

Which returns the fixture of the other Product model.

Since the original Product class is used everywhere, refactoring is not an option, and the model coming in from the external package is not possible to change.

Is there any solution for this?

Thanks

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the factory.SubFactory reference to factories.stripe.SubscriptionProductFactory and trace how the fixture name is derived from model.name. Reproduce the case with two Django models named Product and inspect the fixture registration and lookup path. Done means both Product factories can be used through SubFactory without resolving to the wrong fixture.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.