Debian Codegraph @

hypothesis-python/tests/django/toystore/test_basic_configuration.py

raw
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 TestCase as VanillaTestCase
12
13 import pytest
14 from django.db import IntegrityError
15 from django.test import TestCase as DjangoTestCase
16
17 from hypothesis import HealthCheck, Verbosity, given, settings
18 from hypothesis.errors import InvalidArgument
19 from hypothesis.extra.django import SimpleTestCase, TestCase, TransactionTestCase
20 from hypothesis.internal.compat import GRAALPY, PYPY
21 from hypothesis.strategies import integers
22
23 from tests.django.toystore.models import Company
24
25
26 class SomeStuff:
27 @settings(
28 suppress_health_check=[HealthCheck.too_slow, HealthCheck.differing_executors]
29 )
30 @given(integers())
31 def test_is_blank_slate(self, unused):
32 Company.objects.create(name="MickeyCo")
33
34 def test_normal_test_1(self):
35 Company.objects.create(name="MickeyCo")
36
37 def test_normal_test_2(self):
38 Company.objects.create(name="MickeyCo")
39
40
41 class TestConstraintsWithTransactions(SomeStuff, TestCase):
42 pass
43
44
45 if not (PYPY or GRAALPY):
46 # xfail
47 # This is excessively slow in general, but particularly on pypy. We just
48 # disable it altogether there as it's a niche case.
49 class TestConstraintsWithoutTransactions(SomeStuff, TransactionTestCase):
50 pass
51
52
53 class TestWorkflow(VanillaTestCase):
54 def test_does_not_break_later_tests(self):
55 def break_the_db(i):
56 Company.objects.create(name="MickeyCo")
57 Company.objects.create(name="MickeyCo")
58
59 class LocalTest(TestCase):
60 @given(integers().map(break_the_db))
61 @settings(
62 suppress_health_check=list(HealthCheck), verbosity=Verbosity.quiet
63 )
64 def test_does_not_break_other_things(self, unused):
65 pass
66
67 def test_normal_test_1(self):
68 Company.objects.create(name="MickeyCo")
69
70 t = LocalTest("test_normal_test_1")
71 try:
72 t.test_does_not_break_other_things()
73 except IntegrityError:
74 pass
75 t.test_normal_test_1()
76
77 def test_given_needs_hypothesis_test_case(self):
78 class LocalTest(DjangoTestCase):
79 @given(integers())
80 def tst(self, i):
81 raise AssertionError("InvalidArgument should be raised in @given")
82
83 with pytest.raises(InvalidArgument):
84 LocalTest("tst").tst()
85
86
87 class TestSimple(SimpleTestCase):
88 @given(integers())
89 def test_that_doesnt_need_db(self, z: int):
90 company = Company(name="Company-" + str(z))
91 assert company.name.endswith(str(z))