factory_boy
factory_boy copied to clipboard
Unexpected behavior in nested sequences
Description
Sequences behave unexpectedly when nested through subfactories.
To Reproduce
Reset an outer factory's sequence – the inner factory's sequence continues to increment. Vice versa applies as well. There is no obvious way to reset all sequences.
Model / Factory code
A contrived example:
from dataclasses import dataclass
from typing import Any
import factory
@dataclass
class Example:
id: Any
a: Any
class ExampleA(factory.Factory):
class Meta:
model = Example
id = factory.Sequence(lambda n: n)
a = "blah"
class ExampleB(factory.Factory):
class Meta:
model = Example
id = factory.Sequence(lambda n: 1000 + n)
a = factory.SubFactory(ExampleA)
The issue
One example of strange output:
print([(e.id, e.a.id) for e in ExampleB.create_batch(10)])
ExampleA.reset_sequence()
print([(e.id, e.a.id) for e in ExampleB.create_batch(10)])
ExampleB.reset_sequence()
print([(e.id, e.a.id) for e in ExampleB.create_batch(10)])
Output:
[(1000, 0), (1001, 1), (1002, 2), (1003, 3), (1004, 4), (1005, 5), (1006, 6), (1007, 7), (1008, 8), (1009, 9)]
[(1010, 0), (1011, 1), (1012, 2), (1013, 3), (1014, 4), (1015, 5), (1016, 6), (1017, 7), (1018, 8), (1019, 9)]
[(1000, 10), (1001, 11), (1002, 12), (1003, 13), (1004, 14), (1005, 15), (1006, 16), (1007, 17), (1008, 18), (1009, 19)]
How would we reset all the sequences at once? It seems to me like there's no way to do this.
Realized this issue had to do with Django test parallelization and not with factory_boy.