MCPcopy
hub / github.com/django/django / test_validate

Method test_validate

tests/constraints/tests.py:252–270  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

250 self.assertIn("constraints_childmodel_adult", constraints)
251
252 def test_validate(self):
253 check = models.Q(price__gt=models.F("discounted_price"))
254 constraint = models.CheckConstraint(condition=check, name="price")
255 # Invalid product.
256 invalid_product = Product(price=10, discounted_price=42)
257 with self.assertRaises(ValidationError):
258 constraint.validate(Product, invalid_product)
259 with self.assertRaises(ValidationError):
260 constraint.validate(Product, invalid_product, exclude={"unit"})
261 # Fields used by the check constraint are excluded.
262 constraint.validate(Product, invalid_product, exclude={"price"})
263 constraint.validate(Product, invalid_product, exclude={"discounted_price"})
264 constraint.validate(
265 Product,
266 invalid_product,
267 exclude={"discounted_price", "price"},
268 )
269 # Valid product.
270 constraint.validate(Product, Product(price=10, discounted_price=5))
271
272 def test_validate_custom_error(self):
273 check = models.Q(price__gt=models.F("discounted_price"))

Callers

nothing calls this directly

Calls 3

validateMethod · 0.95
FMethod · 0.80
ProductClass · 0.70

Tested by

no test coverage detected