Debian Codegraph @

hypothesis-python/tests/django/toystore/models.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 import django
12 from django.core.exceptions import ValidationError
13 from django.core.validators import MinValueValidator
14 from django.db import models
15
16
17 class Company(models.Model):
18 name = models.CharField(max_length=100, unique=True)
19
20
21 class Store(models.Model):
22 name = models.CharField(max_length=100, unique=True)
23 company = models.ForeignKey(Company, null=False, on_delete=models.CASCADE)
24
25
26 class Car(models.Model):
27 # See https://github.com/HypothesisWorks/hypothesis/issues/2369
28 model = models.CharField(max_length=100, unique=True)
29
30
31 class CharmField(models.Field):
32 def db_type(self, connection):
33 return "char(1)"
34
35
36 class CustomishField(models.Field):
37 def db_type(self, connection):
38 return "char(1)"
39
40
41 class Customish(models.Model):
42 customish = CustomishField()
43
44
45 class Customer(models.Model):
46 name = models.CharField(max_length=100, unique=True)
47 email = models.EmailField(max_length=100, unique=True)
48 gender = models.CharField(max_length=50, null=True) # noqa # avoid nullable strs
49 age = models.IntegerField()
50 birthday = models.DateTimeField()
51
52
53 class Charming(models.Model):
54 charm = CharmField()
55
56
57 class CouldBeCharming(models.Model):
58 charm = CharmField(null=True)
59
60
61 class SelfLoop(models.Model):
62 me = models.ForeignKey("self", null=True, on_delete=models.SET_NULL)
63
64
65 class LoopA(models.Model):
66 b = models.ForeignKey("LoopB", null=False, on_delete=models.CASCADE)
67
68
69 class LoopB(models.Model):
70 a = models.ForeignKey("LoopA", null=True, on_delete=models.SET_NULL)
71
72
73 class ManyNumerics(models.Model):
74 i1 = models.IntegerField()
75 i2 = models.SmallIntegerField()
76 i3 = models.BigIntegerField()
77
78 p1 = models.PositiveIntegerField()
79 p2 = models.PositiveSmallIntegerField()
80
81 d = models.DecimalField(decimal_places=2, max_digits=5)
82
83
84 class ManyTimes(models.Model):
85 time = models.TimeField()
86 date = models.DateField()
87 duration = models.DurationField()
88
89
90 class OddFields(models.Model):
91 uuid = models.UUIDField()
92 slug = models.SlugField()
93 url = models.URLField()
94 ipv4 = models.GenericIPAddressField(protocol="IPv4")
95 ipv6 = models.GenericIPAddressField(protocol="IPv6")
96
97
98 class CustomishDefault(models.Model):
99 customish = CustomishField(default="b")
100
101
102 class FileFields(models.Model):
103 file1 = models.FileField()
104
105
106 class MandatoryComputed(models.Model):
107 name = models.CharField(max_length=100, unique=True)
108 company = models.ForeignKey(Company, null=False, on_delete=models.CASCADE)
109
110 def __init__(self, **kw):
111 if "company" in kw:
112 raise RuntimeError
113 cname = kw["name"] + "_company"
114 kw["company"] = Company.objects.create(name=cname)
115 super().__init__(**kw)
116
117
118 def validate_even(value):
119 if value % 2 != 0:
120 raise ValidationError("")
121
122
123 class RestrictedFields(models.Model):
124 text_field_4 = models.TextField(max_length=4, blank=True)
125 char_field_4 = models.CharField(max_length=4, blank=True)
126 choice_field_text = models.TextField(choices=(("foo", "Foo"), ("bar", "Bar")))
127 choice_field_int = models.IntegerField(choices=((1, "First"), (2, "Second")))
128 null_choice_field_int = models.IntegerField(
129 choices=((1, "First"), (2, "Second")), null=True, blank=True
130 )
131 choice_field_grouped = models.TextField(
132 choices=(
133 ("Audio", (("vinyl", "Vinyl"), ("cd", "CD"))),
134 ("Video", (("vhs", "VHS Tape"), ("dvd", "DVD"))),
135 ("unknown", "Unknown"),
136 )
137 )
138 even_number_field = models.IntegerField(validators=[validate_even])
139 non_blank_text_field = models.TextField(blank=False)
140
141
142 class SelfModifyingField(models.IntegerField):
143 def pre_save(self, model_instance, add):
144 value = getattr(model_instance, self.attname)
145 value += 1
146 setattr(model_instance, self.attname, value)
147 return value
148
149
150 class CompanyExtension(models.Model):
151 company = models.OneToOneField(Company, primary_key=True, on_delete=models.CASCADE)
152
153 self_modifying = SelfModifyingField()
154
155
156 class UserSpecifiedAutoId(models.Model):
157 my_id = models.AutoField(primary_key=True)
158
159
160 if django.VERSION >= (5, 0, 0):
161 import math
162
163 class Pizza(models.Model):
164 AREA = math.pi * models.F("radius") ** 2
165
166 radius = models.IntegerField(validators=[MinValueValidator(1)])
167 slices = models.PositiveIntegerField(validators=[MinValueValidator(2)])
168 total_area = models.GeneratedField(
169 expression=AREA,
170 output_field=models.FloatField(),
171 db_persist=True,
172 )
173 slice_area = models.GeneratedField(
174 expression=AREA / models.F("slices"),
175 output_field=models.FloatField(),
176 db_persist=False,
177 )