| 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 |
from unittest import skipIf |
| 12 |
|
| 13 |
from django import forms |
| 14 |
from django.conf import settings |
| 15 |
|
| 16 |
from hypothesis import assume, given |
| 17 |
from hypothesis.extra.django import ( |
| 18 |
TestCase, |
| 19 |
from_field, |
| 20 |
from_form, |
| 21 |
register_field_strategy, |
| 22 |
) |
| 23 |
from hypothesis.strategies import booleans, sampled_from |
| 24 |
|
| 25 |
from tests.django.toystore.forms import ( |
| 26 |
BasicFieldForm, |
| 27 |
BroadBooleanField, |
| 28 |
ChoiceFieldForm, |
| 29 |
CustomerForm, |
| 30 |
DynamicForm, |
| 31 |
EmailFieldForm, |
| 32 |
FileFieldsForm, |
| 33 |
InternetProtocolForm, |
| 34 |
ManyMultiValueForm, |
| 35 |
ManyNumericsForm, |
| 36 |
ManyTimesForm, |
| 37 |
MultipleCompaniesForm, |
| 38 |
OddFieldsForm, |
| 39 |
RegexFieldForm, |
| 40 |
ShortStringForm, |
| 41 |
SlugFieldForm, |
| 42 |
StoreForm, |
| 43 |
TemporalFieldForm, |
| 44 |
URLFieldForm, |
| 45 |
UsernameForm, |
| 46 |
UUIDFieldForm, |
| 47 |
WithValidatorsForm, |
| 48 |
) |
| 49 |
from tests.django.toystore.models import Company |
| 50 |
|
| 51 |
register_field_strategy( |
| 52 |
BroadBooleanField, booleans() | sampled_from(["1", "0", "True", "False"]) |
| 53 |
) |
| 54 |
|
| 55 |
has_contrib_auth = "django.contrib.auth" in settings.INSTALLED_APPS |
| 56 |
|
| 57 |
|
| 58 |
class TestGetsBasicForms(TestCase): |
| 59 |
@given(from_form(CustomerForm)) |
| 60 |
def test_valid_customer(self, customer_form): |
| 61 |
self.assertTrue(customer_form.is_valid()) |
| 62 |
|
| 63 |
@given(from_form(ManyNumericsForm)) |
| 64 |
def test_valid_numerics(self, numerics_form): |
| 65 |
self.assertTrue(numerics_form.is_valid()) |
| 66 |
|
| 67 |
@given(from_form(ManyTimesForm)) |
| 68 |
def test_valid_times(self, times_form): |
| 69 |
self.assertTrue(times_form.is_valid()) |
| 70 |
|
| 71 |
@given(from_form(OddFieldsForm)) |
| 72 |
def test_valid_odd_fields(self, odd_form): |
| 73 |
self.assertTrue(odd_form.is_valid()) |
| 74 |
|
| 75 |
def test_dynamic_form(self): |
| 76 |
for field_count in range(2, 7): |
| 77 |
|
| 78 |
@given(from_form(DynamicForm, form_kwargs={"field_count": field_count})) |
| 79 |
def _test(dynamic_form): |
| 80 |
self.assertTrue(dynamic_form.is_valid()) |
| 81 |
|
| 82 |
_test() |
| 83 |
|
| 84 |
@given(from_form(BasicFieldForm)) |
| 85 |
def test_basic_fields_form(self, basic_field_form): |
| 86 |
self.assertTrue(basic_field_form.is_valid()) |
| 87 |
|
| 88 |
@given(from_form(TemporalFieldForm)) |
| 89 |
def test_temporal_fields_form(self, time_field_form): |
| 90 |
self.assertTrue(time_field_form.is_valid()) |
| 91 |
|
| 92 |
@given(from_form(EmailFieldForm)) |
| 93 |
def test_email_field_form(self, email_field_form): |
| 94 |
self.assertTrue(email_field_form.is_valid()) |
| 95 |
|
| 96 |
@given(from_form(SlugFieldForm)) |
| 97 |
def test_slug_field_form(self, slug_field_form): |
| 98 |
self.assertTrue(slug_field_form.is_valid()) |
| 99 |
|
| 100 |
@given(from_form(URLFieldForm)) |
| 101 |
def test_url_field_form(self, url_field_form): |
| 102 |
self.assertTrue(url_field_form.is_valid()) |
| 103 |
|
| 104 |
@given(from_form(RegexFieldForm)) |
| 105 |
def test_regex_field_form(self, regex_field_form): |
| 106 |
self.assertTrue(regex_field_form.is_valid()) |
| 107 |
|
| 108 |
@given(from_form(UUIDFieldForm)) |
| 109 |
def test_uuid_field_form(self, uuid_field_form): |
| 110 |
self.assertTrue(uuid_field_form.is_valid()) |
| 111 |
|
| 112 |
@given(from_form(ChoiceFieldForm)) |
| 113 |
def test_choice_fields_form(self, choice_field_form): |
| 114 |
self.assertTrue(choice_field_form.is_valid()) |
| 115 |
|
| 116 |
@given(from_form(InternetProtocolForm)) |
| 117 |
def test_ip_fields_form(self, ip_field_form): |
| 118 |
self.assertTrue(ip_field_form.is_valid()) |
| 119 |
|
| 120 |
@given(from_form(ManyMultiValueForm, form_kwargs={"subfield_count": 2})) |
| 121 |
def test_many_values_in_multi_value_field(self, many_multi_value_form): |
| 122 |
self.assertTrue(many_multi_value_form.is_valid()) |
| 123 |
|
| 124 |
@given(from_form(ManyMultiValueForm, form_kwargs={"subfield_count": 105})) |
| 125 |
def test_excessive_values_in_multi_value_field(self, excessive_form): |
| 126 |
self.assertTrue(excessive_form.is_valid()) |
| 127 |
|
| 128 |
@given(from_form(ShortStringForm)) |
| 129 |
def test_short_string_form(self, short_string_form): |
| 130 |
self.assertTrue(short_string_form.is_valid()) |
| 131 |
|
| 132 |
@given(from_form(WithValidatorsForm)) |
| 133 |
def test_tight_validators_form(self, x): |
| 134 |
self.assertTrue(1 <= x.data["_int_one_to_five"] <= 5) |
| 135 |
self.assertTrue(1 <= x.data["_decimal_one_to_five"] <= 5) |
| 136 |
self.assertTrue(1 <= x.data["_float_one_to_five"] <= 5) |
| 137 |
self.assertTrue(5 <= len(x.data["_string_five_to_ten"]) <= 10) |
| 138 |
|
| 139 |
@given(from_form(FileFieldsForm)) |
| 140 |
def test_file_fields_form(self, x): |
| 141 |
assert x.is_valid() |
| 142 |
# form.data is empty, and form.files has one entry: file1 |
| 143 |
self.assertFalse(x.data) |
| 144 |
self.assertTrue(set(x.files.keys()) == {"file1"}) |
| 145 |
self.assertTrue(x.files["file1"]) |
| 146 |
|
| 147 |
@skipIf(not has_contrib_auth, "contrib.auth not installed") |
| 148 |
@given(from_form(UsernameForm)) |
| 149 |
def test_username_form(self, username_form): |
| 150 |
self.assertTrue(username_form.is_valid()) |
| 151 |
|
| 152 |
@skipIf(not has_contrib_auth, "contrib.auth not installed") |
| 153 |
@given(from_form(UsernameForm)) |
| 154 |
def test_read_only_password_hash_field_form(self, password_form): |
| 155 |
self.assertTrue(password_form.is_valid()) |
| 156 |
|
| 157 |
|
| 158 |
class TestFormsWithModelChoices(TestCase): |
| 159 |
@classmethod |
| 160 |
def setUpTestData(cls): |
| 161 |
super().setUpTestData() |
| 162 |
|
| 163 |
# Set up example Company records to use as choices for |
| 164 |
# Store.company. These must exist before creating a strategy |
| 165 |
# for the ModelChoiceField. |
| 166 |
cls.company_names = ("Bill's Flowers", "Jane's Sporting Goods") |
| 167 |
for name in cls.company_names: |
| 168 |
Company.objects.create(name=name) |
| 169 |
|
| 170 |
@given( |
| 171 |
choice=from_field( |
| 172 |
forms.ModelChoiceField(queryset=Company.objects.order_by("name")) |
| 173 |
) |
| 174 |
) |
| 175 |
def test_from_model_choices_field(self, choice): |
| 176 |
assume(choice != "") # Skip the empty choice. |
| 177 |
self.assertIsInstance(choice, int) |
| 178 |
Company.objects.get(id=choice) |
| 179 |
|
| 180 |
@given( |
| 181 |
choice=from_field( |
| 182 |
forms.ModelChoiceField( |
| 183 |
queryset=Company.objects.order_by("name"), empty_label=None |
| 184 |
) |
| 185 |
) |
| 186 |
) |
| 187 |
def test_from_model_choices_field_no_empty_choice(self, choice): |
| 188 |
Company.objects.get(id=choice) |
| 189 |
|
| 190 |
@given(choice=from_field(forms.ModelChoiceField(queryset=Company.objects.none()))) |
| 191 |
def test_from_model_choices_field_empty(self, choice): |
| 192 |
self.assertEqual(choice, "") |
| 193 |
|
| 194 |
@given(form=from_form(StoreForm)) |
| 195 |
def test_store_form_valid(self, form): |
| 196 |
assume(form.data["company"]) |
| 197 |
self.assertTrue(form.is_valid()) |
| 198 |
|
| 199 |
@given( |
| 200 |
choice=from_field( |
| 201 |
forms.ModelMultipleChoiceField(queryset=Company.objects.order_by("name")) |
| 202 |
) |
| 203 |
) |
| 204 |
def test_from_model_multiple_choices_field(self, choice): |
| 205 |
n_choices = len(choice) |
| 206 |
self.assertEqual(n_choices, len(set(choice))) |
| 207 |
self.assertEqual(n_choices, Company.objects.filter(pk__in=choice).count()) |
| 208 |
|
| 209 |
@given(form=from_form(MultipleCompaniesForm)) |
| 210 |
def test_multiple_companies_form_valid(self, form): |
| 211 |
self.assertTrue(form.is_valid()) |