Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add factory_boy factory #20

Merged
merged 5 commits into from
Aug 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ History
Next Release
------------

- Nothing yet!
- Add UserFactory to make testing easier for developers using the
pacakge; requires factory_boy (PR `#20`_)

.. _#20: https://github.com/jambonsw/django-improved-user/pull/20

0.3.0 (2017-08-10)
------------------
Expand Down
43 changes: 42 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,28 @@ default by making a few modern and international changes.
* Replace ``first_name`` and ``last_name`` with international friendly
``short_name`` ``full name`` fields

Installation
------------

In a Terminal, use `pip` to install the package from
[PyPI](https://pypi.org/).

.. code:: console

pip install django-improved-user

If you intend to use the `UserFactory` provided by the package to allow
for testing with [factory_boy](https://github.com/FactoryBoy/factory_boy),
you can specify so during install.

.. code:: console

pip install django-improved-user[factory]

If you do not but wish to use the `UserFactory`, you will need to
install [factory_boy](https://github.com/FactoryBoy/factory_boy)
yourself.

Usage
-----

Expand Down Expand Up @@ -40,8 +62,27 @@ Perform the following steps in your ``settings.py`` file.
Testing
-------

From the root directory of the project, run the code below.
To run the test suite on a single version of Django (assuming you have a
version of Django installed), run the `runtests.py` script from the root
of the project.

.. code:: console

$ python runtests.py

You can limit the tests or pass paramaters as if you had called
`manage.py test`.

.. code:: console

$ ./runtests.py tests.test_basic -v 3

To run all linters and test multiple Python and Django versions, use
`tox`.

.. code:: console

$ tox

You will need to install Python 3.4, 3.5, and 3.6 on your system for
this to work.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
bumpversion==0.5.3
factory_boy==2.9.2
Faker==0.7.18
python-dateutil==2.6.1
check-manifest==0.35
coverage==4.4.1
docutils==0.14
Expand Down
7 changes: 7 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ def run_tests(self):
install_requires=[
'django>=1.8',
],
extras_require={
'factory': [
'factory_boy==2.9.2',
'Faker==0.7.18',
'python-dateutil==2.6.1',
],
},
zip_safe=False,

cmdclass={
Expand Down
27 changes: 27 additions & 0 deletions src/improved_user/factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
"""Factories to make testing with Improved User easier"""
from .models import User

try:
from factory import Faker, PostGenerationMethodCall
from factory.django import DjangoModelFactory
except (ImportError, ModuleNotFoundError): # pragma: no cover
raise Exception(
"Please install factory_boy to use Improved User's UserFactory.\n"
'pip install factory_boy==2.9.2')


# pylint: disable=too-few-public-methods
class UserFactory(DjangoModelFactory):
"""Factory Boy factory for Improved User"""
class Meta:
"""Configuration Options"""
model = User

email = Faker('email')
password = PostGenerationMethodCall('set_password', 'password!')
full_name = Faker('name')
short_name = Faker('first_name')
is_active = True
is_staff = False
is_superuser = False
# pylint: enable=too-few-public-methods
71 changes: 71 additions & 0 deletions tests/test_factories.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"""Test model factories provided by Improved User"""
from django.test import TestCase

from improved_user.factories import UserFactory
from improved_user.models import User


class UserFactoryTests(TestCase):
"""Test for UserFactory used with Factory Boy"""

def test_basic_build(self):
"""Test creation of User via factory"""
user = UserFactory.build()
self.assertIsInstance(user, User)
self.assertIsInstance(user.email, str)
self.assertIsInstance(user.short_name, str)
self.assertIsInstance(user.full_name, str)
self.assertGreater(len(user.email), 1)
self.assertGreater(len(user.short_name), 1)
self.assertGreater(len(user.full_name), 1)
self.assertTrue(user.check_password('password!'))
self.assertTrue(user.is_active)
self.assertFalse(user.is_staff)
self.assertFalse(user.is_superuser)
self.assertEqual(User.objects.all().count(), 0)
user.save()
self.assertEqual(User.objects.all().count(), 1)

def test_basic_create(self):
"""Test creation of User via factory saves to DB"""
user = UserFactory()
self.assertIsInstance(user, User)
self.assertEqual(User.objects.all().count(), 1)

def test_attributes_override_build(self):
"""Test that all model fields can be modified"""
user = UserFactory.build(
email='hello@jambonsw.com',
password='my_secret_password87',
short_name='René',
full_name='René Magritte',
is_active=False,
is_staff=True,
is_superuser=True,
)
self.assertIsInstance(user, User)
self.assertEqual(user.email, 'hello@jambonsw.com')
self.assertEqual(user.short_name, 'René')
self.assertEqual(user.full_name, 'René Magritte')
self.assertTrue(user.check_password('my_secret_password87'))
self.assertFalse(user.is_active)
self.assertTrue(user.is_staff)
self.assertTrue(user.is_superuser)
self.assertEqual(User.objects.all().count(), 0)
user.save()
self.assertEqual(User.objects.all().count(), 1)

def test_attributes_override_create(self):
"""Test that all model fields can be modified during creation"""
user = UserFactory(
email='hello@jambonsw.com',
password='my_secret_password87',
short_name='René',
full_name='René Magritte',
is_active=False,
is_staff=True,
is_superuser=True,
)
self.assertIsInstance(user, User)
self.assertTrue(user.check_password('my_secret_password87'))
self.assertEqual(User.objects.all().count(), 1)
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ setenv =
PYTHONDONTWRITEBYTECODE=1
PYTHONWARNINGS=once
deps =
coverage
coverage==4.4.1
factory_boy==2.9.2
Faker==0.7.18
python-dateutil==2.6.1
django18: Django>=1.8,<1.9
django110: Django>=1.10,<1.11
django111: Django>=1.11,<2.0
Expand Down