Git Product home page Git Product logo

factory_djoy's Introduction

Read the Docs

PyPI version

Python version

PyPI - Django Version

factory-djoy is licensed under the MIT License

Factory Djoy

Factories for Django, creating valid model instances every time.

Factory Djoy provides two simple classes, UserFactory and CleanModelFactory, which wrap Factory Boy. They call full_clean() when creating Django model instances to ensure that only valid data enters your Django database.

Compatibility

Factory Djoy is compatible with:

  • Django 2.2, 3.0 and 3.1
  • Python 3 (3.7, 3.8, 3.9, 3.10, 3.11)
  • Factory Boy version 2.11 or greater.

Resources

factory_djoy's People

Contributors

dependabot[bot] avatar jamescooke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

factory_djoy's Issues

Automate package updates

Update requirements for:

  • main project at requirements/base.txt
  • test framework requirements at test_framework/requirements/*.txt

Script (makefile recipe), should:

  • Ensure git branch is master.
  • Create a new branch for package update.
  • Update requirements files
  • Commit requirements files
  • Push branch for testing.

Test `get_or_create` behaviour

Ensure that get_or_create works with _create and CleanModelFactory.

Document that get_or_create functionality is not provided.

Improve testing

  • Add lint step to tox.ini and remove from Makefile. This will create an isort error because there is a Django import in the wrong place.

  • Switch to Travis for better Tox integration.

Allow for bypass of `full_clean`

In a test run, it can be helpful to be able to create invalid data that has slipped into the Django database. However, invalid data can not be created because the call to full_clean happens on both create and build.

A simple solution is to allow build to create unsaved, invalid instances. What the developer does with that instance after it has been built is up to them... if they want to just save() it and create invalid data, that's their choice.

Steps would be:

  • Allow build to not have full_clean called.
  • Document the solution: UserFactory.build(username='something bad').save()

Provide standalone access to UserFactory username generation

When using a custom user model I'd love to still use your unique username generation.

Would it be possible to provide the Username generator as it's own standalone module/provider?

Something like:

from factory.django import DjangoModelFactory
from factory_djoy import UniqueUsername

class AccountFactory(DjangoModelFactory):

    username = UniqueUsername()

Fix collision in UserFactory

There is a possible collision when creating User instances.

======================================================================
FAIL: test_make_multi (tests.test_user_factory.TestUserFactory)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/ubuntu/factory_djoy/test_framework/django18/tests/test_user_factory.py", line 52, in test_make_multi
    'Both users found to have same "{}" attribute'.format(varname)
AssertionError: 'Devin' == 'Devin' : Both users found to have same "first_name" attribute
----------------------------------------------------------------------

Add better stack trace when validation fails

When a Factory fails to create an instance because full_clean() fails - it's hard to work out why the failure happened because the original value is not captured in Django's ValidationError. This can be super-hard to work out when working with multiple SubFactories - which one created the error? Especially when multiple sub-factories might have fields named the same... like name.

Therefore, add some information to the raised ValueError to indicate the model that failed to build and the values for those failed fields that were tried by factory boy:

        try:
            obj.full_clean()
        except ValidationError as ve:
            message = 'Error building {}.\nBad values:\n'.format(model_class)
            for field in ve.error_dict.keys():
                message += '  {}: "{}"\n'.format(field, getattr(obj, field))
            raise RuntimeError(message) from ve

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.