| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import datetime as dt |
| 12 |
from unittest import skipIf |
| 13 |
from uuid import UUID |
| 14 |
|
| 15 |
import django |
| 16 |
from django.conf import settings as django_settings |
| 17 |
|
| 18 |
from hypothesis import HealthCheck, assume, given, settings, strategies as st |
| 19 |
from hypothesis.errors import InvalidArgument |
| 20 |
from hypothesis.extra.django import ( |
| 21 |
TestCase, |
| 22 |
TransactionTestCase, |
| 23 |
from_model, |
| 24 |
register_field_strategy, |
| 25 |
) |
| 26 |
from hypothesis.internal.conjecture.data import ConjectureData |
| 27 |
from hypothesis.strategies import just, lists |
| 28 |
|
| 29 |
if "django.contrib.auth" in django_settings.INSTALLED_APPS: |
| 30 |
from django.contrib.auth.models import User |
| 31 |
else: |
| 32 |
User = None |
| 33 |
|
| 34 |
from tests.common.debug import check_can_generate_examples |
| 35 |
from tests.django.toystore.models import ( |
| 36 |
Car, |
| 37 |
Company, |
| 38 |
CompanyExtension, |
| 39 |
CouldBeCharming, |
| 40 |
Customer, |
| 41 |
Customish, |
| 42 |
CustomishDefault, |
| 43 |
CustomishField, |
| 44 |
MandatoryComputed, |
| 45 |
ManyNumerics, |
| 46 |
ManyTimes, |
| 47 |
OddFields, |
| 48 |
RestrictedFields, |
| 49 |
SelfLoop, |
| 50 |
Store, |
| 51 |
UserSpecifiedAutoId, |
| 52 |
) |
| 53 |
|
| 54 |
register_field_strategy(CustomishField, just("a")) |
| 55 |
|
| 56 |
|
| 57 |
class TestGetsBasicModels(TestCase): |
| 58 |
@given(from_model(Company)) |
| 59 |
def test_is_company(self, company): |
| 60 |
self.assertIsInstance(company, Company) |
| 61 |
self.assertIsNotNone(company.pk) |
| 62 |
|
| 63 |
@given(from_model(Store, company=from_model(Company))) |
| 64 |
def test_can_get_a_store(self, store): |
| 65 |
assert store.company.pk |
| 66 |
|
| 67 |
@given(lists(from_model(Company))) |
| 68 |
def test_can_get_multiple_models_with_unique_field(self, companies): |
| 69 |
assume(len(companies) > 1) |
| 70 |
for c in companies: |
| 71 |
self.assertIsNotNone(c.pk) |
| 72 |
self.assertEqual( |
| 73 |
len({c.pk for c in companies}), len({c.name for c in companies}) |
| 74 |
) |
| 75 |
|
| 76 |
@settings(suppress_health_check=[HealthCheck.too_slow]) |
| 77 |
@given(from_model(Customer)) |
| 78 |
def test_is_customer(self, customer): |
| 79 |
self.assertIsInstance(customer, Customer) |
| 80 |
self.assertIsNotNone(customer.pk) |
| 81 |
self.assertIsNotNone(customer.email) |
| 82 |
|
| 83 |
@settings(suppress_health_check=[HealthCheck.too_slow]) |
| 84 |
@given(from_model(Customer)) |
| 85 |
def test_tz_presence(self, customer): |
| 86 |
if django_settings.USE_TZ: |
| 87 |
self.assertIsNotNone(customer.birthday.tzinfo) |
| 88 |
else: |
| 89 |
self.assertIsNone(customer.birthday.tzinfo) |
| 90 |
|
| 91 |
@given(from_model(CouldBeCharming)) |
| 92 |
def test_is_not_charming(self, not_charming): |
| 93 |
self.assertIsInstance(not_charming, CouldBeCharming) |
| 94 |
self.assertIsNotNone(not_charming.pk) |
| 95 |
self.assertIsNone(not_charming.charm) |
| 96 |
|
| 97 |
@given(from_model(SelfLoop)) |
| 98 |
def test_sl(self, sl): |
| 99 |
self.assertIsNone(sl.me) |
| 100 |
|
| 101 |
@given(lists(from_model(ManyNumerics))) |
| 102 |
def test_no_overflow_in_integer(self, manyints): |
| 103 |
pass |
| 104 |
|
| 105 |
@given(from_model(Customish)) |
| 106 |
def test_custom_field(self, x): |
| 107 |
assert x.customish == "a" |
| 108 |
|
| 109 |
def test_mandatory_fields_are_mandatory(self): |
| 110 |
with self.assertRaises(InvalidArgument): |
| 111 |
check_can_generate_examples(from_model(Store)) |
| 112 |
|
| 113 |
def test_mandatory_computed_fields_are_mandatory(self): |
| 114 |
with self.assertRaises(InvalidArgument): |
| 115 |
check_can_generate_examples(from_model(MandatoryComputed)) |
| 116 |
|
| 117 |
def test_mandatory_computed_fields_may_not_be_provided(self): |
| 118 |
with self.assertRaises(RuntimeError): |
| 119 |
check_can_generate_examples( |
| 120 |
from_model(MandatoryComputed, company=from_model(Company)) |
| 121 |
) |
| 122 |
|
| 123 |
@given(from_model(CustomishDefault, customish=...)) |
| 124 |
def test_customish_default_overridden_by_infer(self, x): |
| 125 |
assert x.customish == "a" |
| 126 |
|
| 127 |
@given(from_model(CustomishDefault, customish=...)) |
| 128 |
def test_customish_infer_uses_registered_instead_of_default(self, x): |
| 129 |
assert x.customish == "a" |
| 130 |
|
| 131 |
@given(from_model(OddFields)) |
| 132 |
def test_odd_fields(self, x): |
| 133 |
assert isinstance(x.uuid, UUID) |
| 134 |
assert isinstance(x.slug, str) |
| 135 |
assert " " not in x.slug |
| 136 |
assert isinstance(x.ipv4, str) |
| 137 |
assert len(x.ipv4.split(".")) == 4 |
| 138 |
assert all(int(i) in range(256) for i in x.ipv4.split(".")) |
| 139 |
assert isinstance(x.ipv6, str) |
| 140 |
assert set(x.ipv6).issubset(set("0123456789abcdefABCDEF:.")) |
| 141 |
|
| 142 |
@given(from_model(ManyTimes)) |
| 143 |
def test_time_fields(self, x): |
| 144 |
assert isinstance(x.time, dt.time) |
| 145 |
assert isinstance(x.date, dt.date) |
| 146 |
assert isinstance(x.duration, dt.timedelta) |
| 147 |
|
| 148 |
@given(from_model(Company)) |
| 149 |
def test_no_null_in_charfield(self, x): |
| 150 |
# regression test for #1045. Company just has a convenient CharField. |
| 151 |
assert "\x00" not in x.name |
| 152 |
|
| 153 |
@given(st.data()) |
| 154 |
def test_foreign_key_primary(self, data): |
| 155 |
# Regression test for #1307 |
| 156 |
company_strategy = from_model(Company, name=just("test")) |
| 157 |
strategy = from_model( |
| 158 |
CompanyExtension, company=company_strategy, self_modifying=just(2) |
| 159 |
) |
| 160 |
data.draw(strategy) |
| 161 |
|
| 162 |
# Draw again with the same choice sequence. This will cause a duplicate |
| 163 |
# primary key. |
| 164 |
d = ConjectureData.for_choices(data.conjecture_data.choices) |
| 165 |
d.draw(strategy) |
| 166 |
assert CompanyExtension.objects.all().count() == 1 |
| 167 |
|
| 168 |
|
| 169 |
class TestsNeedingRollback(TransactionTestCase): |
| 170 |
def test_can_get_examples(self): |
| 171 |
for _ in range(200): |
| 172 |
check_can_generate_examples(from_model(Company)) |
| 173 |
|
| 174 |
|
| 175 |
class TestRestrictedFields(TestCase): |
| 176 |
@given(from_model(RestrictedFields)) |
| 177 |
def test_constructs_valid_instance(self, instance): |
| 178 |
self.assertIsInstance(instance, RestrictedFields) |
| 179 |
instance.full_clean() |
| 180 |
self.assertLessEqual(len(instance.text_field_4), 4) |
| 181 |
self.assertLessEqual(len(instance.char_field_4), 4) |
| 182 |
self.assertIn(instance.choice_field_text, ("foo", "bar")) |
| 183 |
self.assertIn(instance.choice_field_int, (1, 2)) |
| 184 |
self.assertIn(instance.null_choice_field_int, (1, 2, None)) |
| 185 |
self.assertEqual( |
| 186 |
instance.choice_field_grouped, instance.choice_field_grouped.lower() |
| 187 |
) |
| 188 |
self.assertEqual(instance.even_number_field % 2, 0) |
| 189 |
self.assertTrue(instance.non_blank_text_field) |
| 190 |
|
| 191 |
|
| 192 |
@skipIf(User is None, "contrib.auth not installed") |
| 193 |
class TestValidatorInference(TestCase): |
| 194 |
@given(from_model(User)) |
| 195 |
def test_user_issue_1112_regression(self, user): |
| 196 |
assert user.username |
| 197 |
|
| 198 |
|
| 199 |
class TestPosOnlyArg(TestCase): |
| 200 |
@given(from_model(Car)) |
| 201 |
def test_user_issue_2369_regression(self, val): |
| 202 |
pass |
| 203 |
|
| 204 |
def test_from_model_signature(self): |
| 205 |
self.assertRaises(TypeError, from_model) |
| 206 |
self.assertRaises(TypeError, from_model, Car, None) |
| 207 |
self.assertRaises(TypeError, from_model, model=Customer) |
| 208 |
|
| 209 |
|
| 210 |
class TestUserSpecifiedAutoId(TestCase): |
| 211 |
@given(from_model(UserSpecifiedAutoId)) |
| 212 |
def test_user_specified_auto_id(self, user_specified_auto_id): |
| 213 |
self.assertIsInstance(user_specified_auto_id, UserSpecifiedAutoId) |
| 214 |
self.assertIsNotNone(user_specified_auto_id.pk) |
| 215 |
|
| 216 |
|
| 217 |
if django.VERSION >= (5, 0, 0): |
| 218 |
from tests.django.toystore.models import Pizza |
| 219 |
|
| 220 |
class TestModelWithGeneratedField(TestCase): |
| 221 |
@given(from_model(Pizza)) |
| 222 |
def test_create_pizza(self, pizza): |
| 223 |
""" |
| 224 |
Strategies are not inferred for GeneratedField. |
| 225 |
""" |
| 226 |
|
| 227 |
# Check we generate valid objects. |
| 228 |
pizza.full_clean() |
| 229 |
|
| 230 |
# Refresh the instance from the database to make sure the |
| 231 |
# generated fields are populated correctly. |
| 232 |
pizza.refresh_from_db() |
| 233 |
|
| 234 |
# Check the expected types of the generated fields. |
| 235 |
self.assertIsInstance(pizza.slice_area, float) |
| 236 |
self.assertIsInstance(pizza.total_area, float) |