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

Override sequence counter #74

Open
timdiels opened this issue Dec 10, 2018 · 0 comments
Open

Override sequence counter #74

timdiels opened this issue Dec 10, 2018 · 0 comments

Comments

@timdiels
Copy link

timdiels commented Dec 10, 2018

I'd like to override the sequence counter.

My first attempt:

# models.py
from django.db import models

class Foo(models.Model):
    a = models.IntegerField()
    b = models.IntegerField()

# factories.py
from factory import DjangoModelFactory, Sequence
from .models import Foo

class FooFactory(DjangoModelFactory):
    class Meta:
        model = Foo

    a = Sequence(lambda x: x)
    b = Sequence(lambda x: x)

# test_one.py
from pytest_factoryboy import register
from .factories import FooFactory

register(FooFactory, __sequence=8)

@pytest.fixture
def foo__sequence():
    return 9

@pytest.fixture
def foo____sequence():
    return 10

def test_1(db, foo):
    assert foo.a in (8, 9, 10)

I ended up with the following workaround:

# factories.py
from factory import DjangoModelFactory, Sequence, SelfAttribute
from .models import Foo

class FooFactory(DjangoModelFactory):
    class Meta:
        model = Foo
        exclude = ('i',)

    i = Sequence(lambda x: x)
    a = SelfAttribute('i')
    b = SelfAttribute('i')

# test_one.py
from pytest_factoryboy import register
from .factories import FooFactory

register(FooFactory, i=8)

@pytest.fixture
def foo__i():
    return 9

def test_1(db, foo):
    assert foo.a in (8, 9)
    assert foo.b in (8, 9)

Either register or foo__i work, no need for both of course.

The workaround is simple, so it may suffice to just note the workaround in the docs for now. Or if I've missed something, please let me know.

Thanks for the nice plugin.

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

No branches or pull requests

1 participant