pytest-dev / pytest-dev/pytest-factoryboy

Override sequence counter

Open
#74 0 comments 0 reactions 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

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.

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 register(FooFactory, __sequence=8) entry point and the foo__sequence fixture shown in test_one.py, then trace how factory sequence arguments are passed through the plugin. Check the existing documentation linked in the issue and relevant tests; done means either a documented supported approach or a tested sequence override through registration or fixtures.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 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.