Global starting point for factory.Sequence counters
See original GitHub issueAfter reading the documentation it seems there’s no supported way of defining a starting point for all factories. I’m looking for something similar to:
factory.COUNTER_START = n
obj1 = SomeFactory()
obj2 = SomeFactory()
obj3 = SomeOtherFactory()
assert(obj1.someattr == factory.COUNTER_START)
assert(obj2.someattr == factory.COUNTER_START + 1)
assert(obj3.someattr == factory.COUNTER_START)
When running the tests it helps in having an unique COUNTER_START per process so you can run similar tests in multiple processes without having database conflicts; also if you want to preserve past test data and re-execute tests.
Right now it seems all the classes need to be modified to take something like COUNTER_START into account, so this issue is about having a helper that does that for all the factories (maybe a factory could optionally want to disable the feature though?).
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Reference — Factory Boy stable documentation
This method will compute the first value to use for the sequence counter of this factory. It is called when the first instance...
Read more >Factory Boy Documentation - Read the Docs
This method will compute the first value to use for the sequence counter of this factory. It is called when the first instance...
Read more >How to pass in a starting sequence number to a Django ...
We can use reset_sequence() function, which will reset the counter to a specific value. # File: make_data.py import factories factories.
Read more >Industry 4.0 implementation sequence for manufacturing ...
A possibility to counter these challenges is Industry 4.0, which focuses on optimizing industrial processes and is characterized by the ...
Read more >Add sequence - Hitachi Vantara Lumada and Pentaho ...
Use DB to generate the sequence? Select this check box if you want the sequence to be driven by a database sequence. Use...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@gpip Even if this does not match typical factory_boy use cases, I see how that could be useful, mostly for the “multiple parallel runs of tests in threads”.
So, if you’re ready to provide the code, I’d be glad to help you along 😉
A good starting point would be
BaseFactory._setup_next_sequence()
infactory/base.py
.Let’s simplify the situation and assume there’s one database which does not get dropped between test runs. So this database X has a table U, and U has an unique field named username.
Using FactoryBoy I create a class attribute to define an username exactly like this one:
username = factory.Sequence(lambda n: 'user%d' %d)
. It seems to be doing it in a similar way to what you mentioned, tackling an integer at the end. Now, I could go further on this and do exactly like you: pick a word at random from a dictionary instead of prefixing with “user”. But if I run tests that use this many times (remember, in this example the database is never dropped), I still have a good chance to hit a conflicting username.But, what’s
n
for FactoryBoy? What ifn
was the current timestamp? Ifn
was the current timestamp then I could re-run the same tests forever without conflicting on usernames (assuming the machine(s) is/are running ntp, etc).In the situation where there is a single database which gets dropped between test runs but multiple processes hitting it,
n
would start at 0 for each process and the tests would cause expected integrity errors. Ifn
started with (just making a guess here) thePID || timestamp
then, again, the tests wouldn’t run into conflicts. The exact process of defining a starting point depends on how you manage test runs, if you can pass an integer (starting from0
) to processes/threads you’re spawing then prepend that to the timestamp as a string to avoid the integrity errors.All of this does not really matter if you’re using
x
processes and each one uses its own copy of databaseX
, since you can start all counters from 0 and they will not run into the problem.