Created
March 24, 2021 14:29
-
-
Save dagron/a0e0de2cf1a2705c48ab004982a5e840 to your computer and use it in GitHub Desktop.
django hypothesis factory boy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import string | |
| import factory | |
| from django.contrib.auth import get_user_model | |
| from hypothesis import strategies as st | |
| UserModel = get_user_model() | |
| DEFAULT_PASSWORD = 'default_password' | |
| class UserFactory(factory.django.DjangoModelFactory): | |
| first_name = factory.Sequence(lambda n: 'user%d' % n) | |
| email = factory.LazyAttribute(lambda obj: '%s@example.com' % obj.first_name) | |
| password = factory.PostGenerationMethodCall('set_password', DEFAULT_PASSWORD) | |
| class Meta: | |
| model = 'users.User' | |
| django_get_or_create = ('email',) | |
| def builds_email(**kwargs): | |
| d = dict( | |
| hostname=st.text(alphabet=string.ascii_letters + string.digits, min_size=3, max_size=20), | |
| domain=st.text(alphabet=string.ascii_letters + string.digits, min_size=3, max_size=20), | |
| ) | |
| d.update(kwargs) | |
| return st.builds( | |
| lambda hostname, domain: f'{hostname}@{domain}.cOm', | |
| **d, | |
| ) | |
| def builds_user_registration_data(**kwargs): | |
| """ data to be sent via POST to registration view """ | |
| d = dict( | |
| email=builds_email(), | |
| password1=st.just(DEFAULT_PASSWORD), | |
| password2=st.just(DEFAULT_PASSWORD), | |
| first_name=st.text(min_size=2, max_size=40, alphabet=string.ascii_letters), | |
| ) | |
| d.update(kwargs) | |
| return st.builds( | |
| lambda **k: k, | |
| **d | |
| ) | |
| def builds_user_model(**kwargs): | |
| d = dict( | |
| email=builds_email(), | |
| password=st.just(DEFAULT_PASSWORD), | |
| ) | |
| d.update(kwargs) | |
| return st.builds( | |
| UserFactory, | |
| **d, | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from hypothesis import given, strategies as st, settings as hsettings | |
| from hypothesis.extra.django import TestCase | |
| from .. import factories as ft | |
| settings = hsettings(max_examples=10) | |
| class UserModelUserTestCase(TestCase): | |
| ############################################## | |
| # ------------------TESTS----------------- # | |
| ############################################# | |
| @hsettings(max_examples=10) | |
| @given(user=ft.builds_user_model()) | |
| def test_created_user_is_inactive(self, user): | |
| """ User is not active. """ | |
| self.assertFalse(user.is_active) | |
| @settings | |
| @given(ft.builds_user_model(is_active=st.just(True))) | |
| def test_created_user_is_active(self, user): | |
| """ Now user is active """ | |
| self.assertTrue(user.is_active) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment