PyFactory
PyFactory is a library for writing generic model factories useful for unit testing.
Example
Basic Example
The example below shows a very simple example. Say somewhere in our tests we need various instances of User objects. In our tests we would simply write the following to create a user:
user = UserFactory().create("basic")
The PyFactory code necessary to make this happen is shown below. Note that the ModelBuilder created would only have to be done once.
from pyfactory import Factory, schema
import models
class ModelBuilder(object):
"""
The model builder is responsible for knowing how to build
and create models based on their attributes. This is what
allows PyFactory to be completely model-agnostic.
"""
@classmethod
def build(cls, model_cls, attrs):
return model_cls(attributes)
@classmethod
def create(cls, model_cls, attrs):
result = cls.build(model_cls, attrs)
result.save()
return result
class UserFactory(Factory):
"""
This shows a simple factory which creates a type of User.
"""
_model = models.user.User
_model_builder = ModelBuilder
@schema()
def basic(self):
return {
"first_name": "Test",
"last_name": "User",
}
Associations
In any application, models typically have associations. Let’s look at the case where we have a Post model which is written by a User. If we want a valid Post object in our tests, once again we only need to do the following:
post = PostFactory().create("basic")
And the PyFactory factories to make this possible are equally simple:
class PostFactory(Factory):
@schema()
def basic(self):
return {
"title": "Fake Title",
"body": "Lorem ipsum...",
"author_id": association(UserFactory(), "basic", "id")
}
Attribute Overrides
Given the above examples, what if you already had a User that you wanted as a post author? Well, its simple to override attributes by just passing the override attributes as additional keyword arguments to the factory method:
author = # Pretend we got an author somewhere
post = PostFactory().create("basic", author_id=author.id)
Metadata
Release files for pyfactory 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pyfactory-0.4.0.tar.gz | 6.1 kB | Without provenance | Details for pyfactory-0.4.0.tar.gz |
Release files / pyfactory-0.4.0.tar.gz
| Download URL | pyfactory-0.4.0.tar.gz |
|---|---|
| Size | 6.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3138d19ef47ad0b198b167fde132b9fdcd4816539e598bfae1a7ba5961389736
|
|
BLAKE2b-256 checksum How to use checksums |
22b62c745779a0625b8e2626fff0b1c9493dd793cab030ecc01e7ce27a256020
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pyfactory 0.4.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.