| 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")) |