| 19 | 19 | class BaseConstraint: |
| 20 | 20 | default_violation_error_message = _("Constraint ā%(name)sā is violated.") |
| 21 | 21 | violation_error_message = None |
| 22 | default_violation_error_code = "constraint_violated" | |
| 23 | violation_error_code = None | |
| 22 | 24 | |
| 23 | 25 | # RemovedInDjango60Warning: When the deprecation ends, replace with: |
| 24 | # def __init__(self, *, name, violation_error_message=None): | |
| 25 | def __init__(self, *args, name=None, violation_error_message=None): | |
| 26 | # def __init__(self, *, name, violation_error_message=None, violation_error_code=None): | |
| 27 | def __init__(self, *args, name=None, violation_error_message=None, violation_error_code=None): | |
| 26 | 28 | # RemovedInDjango60Warning. |
| 27 | 29 | if name is None and not args: |
| 28 | 30 | raise TypeError( |
| 34 | 36 | self.violation_error_message = violation_error_message |
| 35 | 37 | else: |
| 36 | 38 | self.violation_error_message = self.default_violation_error_message |
| 39 | if violation_error_code is not None: | |
| 40 | self.violation_error_code = violation_error_code | |
| 41 | else: | |
| 42 | self.violation_error_code = self.default_violation_error_code | |
| 37 | 43 | # RemovedInDjango60Warning. |
| 38 | 44 | if args: |
| 39 | 45 | warnings.warn( |
| 42 | 48 | RemovedInDjango60Warning, |
| 43 | 49 | stacklevel=2, |
| 44 | 50 | ) |
| 45 | for arg, attr in zip(args, ["name", "violation_error_message"]): | |
| 51 | for arg, attr in zip(args, ["name", "violation_error_message", "violation_error_code"]): | |
| 46 | 52 | if arg: |
| 47 | 53 | setattr(self, attr, arg) |
| 48 | 54 | |
| 74 | 80 | and self.violation_error_message != self.default_violation_error_message |
| 75 | 81 | ): |
| 76 | 82 | kwargs["violation_error_message"] = self.violation_error_message |
| 83 | if ( | |
| 84 | self.violation_error_code is not None | |
| 85 | and self.violation_error_code != self.default_violation_error_code | |
| 86 | ): | |
| 87 | kwargs["violation_error_code"] = self.violation_error_code | |
| 77 | 88 | return (path, (), kwargs) |
| 78 | 89 | |
| 79 | 90 | def clone(self): |
| 82 | 93 | |
| 83 | 94 | |
| 84 | 95 | class CheckConstraint(BaseConstraint): |
| 85 | def __init__(self, *, check, name, violation_error_message=None): | |
| 96 | def __init__(self, *, check, name, violation_error_message=None, violation_error_code=None): | |
| 86 | 97 | self.check = check |
| 87 | 98 | if not getattr(check, "conditional", False): |
| 88 | 99 | raise TypeError( |
| 89 | 100 | "CheckConstraint.check must be a Q instance or boolean expression." |
| 90 | 101 | ) |
| 91 | super().__init__(name=name, violation_error_message=violation_error_message) | |
| 102 | super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code) | |
| 92 | 103 | |
| 93 | 104 | def _get_check_sql(self, model, schema_editor): |
| 94 | 105 | query = Query(model=model, alias_cols=False) |
| 112 | 123 | against = instance._get_field_value_map(meta=model._meta, exclude=exclude) |
| 113 | 124 | try: |
| 114 | 125 | if not Q(self.check).check(against, using=using): |
| 115 | raise ValidationError(self.get_violation_error_message()) | |
| 126 | raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code) | |
| 116 | 127 | except FieldError: |
| 117 | 128 | pass |
| 118 | 129 | |
| 119 | 130 | def __repr__(self): |
| 120 | return "<%s: check=%s name=%s%s>" % ( | |
| 131 | return "<%s: check=%s name=%s%s%s>" % ( | |
| 121 | 132 | self.__class__.__qualname__, |
| 122 | 133 | self.check, |
| 123 | 134 | repr(self.name), |
| 127 | 138 | or self.violation_error_message == self.default_violation_error_message |
| 128 | 139 | else " violation_error_message=%r" % self.violation_error_message |
| 129 | 140 | ), |
| 141 | ( | |
| 142 | "" | |
| 143 | if self.violation_error_code is None | |
| 144 | or self.violation_error_code == self.default_violation_error_code | |
| 145 | else " violation_error_code=%r" % self.violation_error_code | |
| 146 | ), | |
| 130 | 147 | ) |
| 131 | 148 | |
| 132 | 149 | def __eq__(self, other): |
| 135 | 152 | self.name == other.name |
| 136 | 153 | and self.check == other.check |
| 137 | 154 | and self.violation_error_message == other.violation_error_message |
| 155 | and self.violation_error_code == other.violation_error_code | |
| 138 | 156 | ) |
| 139 | 157 | return super().__eq__(other) |
| 140 | 158 | |
| 164 | 182 | include=None, |
| 165 | 183 | opclasses=(), |
| 166 | 184 | violation_error_message=None, |
| 185 | violation_error_code=None, | |
| 167 | 186 | ): |
| 168 | 187 | if not name: |
| 169 | 188 | raise ValueError("A unique constraint must be named.") |
| 213 | 232 | F(expression) if isinstance(expression, str) else expression |
| 214 | 233 | for expression in expressions |
| 215 | 234 | ) |
| 216 | super().__init__(name=name, violation_error_message=violation_error_message) | |
| 235 | super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code) | |
| 217 | 236 | |
| 218 | 237 | @property |
| 219 | 238 | def contains_expressions(self): |
| 293 | 312 | ) |
| 294 | 313 | |
| 295 | 314 | def __repr__(self): |
| 296 | return "<%s:%s%s%s%s%s%s%s%s>" % ( | |
| 315 | return "<%s:%s%s%s%s%s%s%s%s%s>" % ( | |
| 297 | 316 | self.__class__.__qualname__, |
| 298 | 317 | "" if not self.fields else " fields=%s" % repr(self.fields), |
| 299 | 318 | "" if not self.expressions else " expressions=%s" % repr(self.expressions), |
| 308 | 327 | or self.violation_error_message == self.default_violation_error_message |
| 309 | 328 | else " violation_error_message=%r" % self.violation_error_message |
| 310 | 329 | ), |
| 330 | ( | |
| 331 | "" | |
| 332 | if self.violation_error_code is None | |
| 333 | or self.violation_error_code == self.default_violation_error_code | |
| 334 | else " violation_error_code=%r" % self.violation_error_code | |
| 335 | ), | |
| 311 | 336 | ) |
| 312 | 337 | |
| 313 | 338 | def __eq__(self, other): |
| 321 | 346 | and self.opclasses == other.opclasses |
| 322 | 347 | and self.expressions == other.expressions |
| 323 | 348 | and self.violation_error_message == other.violation_error_message |
| 349 | and self.violation_error_code == other.violation_error_code | |
| 324 | 350 | ) |
| 325 | 351 | return super().__eq__(other) |
| 326 | 352 | |
| 336 | 362 | kwargs["include"] = self.include |
| 337 | 363 | if self.opclasses: |
| 338 | 364 | kwargs["opclasses"] = self.opclasses |
| 365 | if self.violation_error_code is not None: | |
| 366 | kwargs["violation_error_code"] = self.violation_error_code | |
| 339 | 367 | return path, self.expressions, kwargs |
| 340 | 368 | |
| 341 | 369 | def validate(self, model, instance, exclude=None, using=DEFAULT_DB_ALIAS): |
| 385 | 413 | if not self.condition: |
| 386 | 414 | if queryset.exists(): |
| 387 | 415 | if self.expressions: |
| 388 | raise ValidationError(self.get_violation_error_message()) | |
| 416 | raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code) | |
| 389 | 417 | # When fields are defined, use the unique_error_message() for |
| 390 | 418 | # backward compatibility. |
| 391 | 419 | for model, constraints in instance.get_constraints(): |
| 400 | 428 | if (self.condition & Exists(queryset.filter(self.condition))).check( |
| 401 | 429 | against, using=using |
| 402 | 430 | ): |
| 403 | raise ValidationError(self.get_violation_error_message()) | |
| 431 | raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code) | |
| 404 | 432 | except FieldError: |
| 405 | 433 | pass |
| Test Name | Status |
|---|---|
test_deconstruction (constraints.tests.UniqueConstraintTests.test_deconstruction) | Fail |
test_deconstruction_with_condition (constraints.tests.UniqueConstraintTests.test_deconstruction_with_condition) | Fail |
test_deconstruction_with_deferrable (constraints.tests.UniqueConstraintTests.test_deconstruction_with_deferrable) | Fail |
test_deconstruction_with_expressions (constraints.tests.UniqueConstraintTests.test_deconstruction_with_expressions) | Fail |
test_deconstruction_with_include (constraints.tests.UniqueConstraintTests.test_deconstruction_with_include) | Fail |
test_deconstruction_with_opclasses (constraints.tests.UniqueConstraintTests.test_deconstruction_with_opclasses) | Fail |
test_custom_violation_code_message (constraints.tests.BaseConstraintTests.test_custom_violation_code_message) | Pass |
test_deconstruction (constraints.tests.BaseConstraintTests.test_deconstruction) | Pass |
test_eq (constraints.tests.CheckConstraintTests.test_eq) | Pass |
test_repr_with_violation_error_code (constraints.tests.CheckConstraintTests.test_repr_with_violation_error_code) | Pass |
test_validate_custom_error (constraints.tests.CheckConstraintTests.test_validate_custom_error) | Pass |
test_eq (constraints.tests.UniqueConstraintTests.test_eq) | Pass |
test_repr_with_violation_error_code (constraints.tests.UniqueConstraintTests.test_repr_with_violation_error_code) | Pass |
test_validate_conditon_custom_error (constraints.tests.UniqueConstraintTests.test_validate_conditon_custom_error) | Pass |
test_constraint_sql (constraints.tests.BaseConstraintTests.test_constraint_sql) | Pass |
test_contains_expressions (constraints.tests.BaseConstraintTests.test_contains_expressions) | Pass |
test_create_sql (constraints.tests.BaseConstraintTests.test_create_sql) | Pass |
test_custom_violation_error_message (constraints.tests.BaseConstraintTests.test_custom_violation_error_message) | Pass |
test_custom_violation_error_message_clone (constraints.tests.BaseConstraintTests.test_custom_violation_error_message_clone) | Pass |
test_default_violation_error_message (constraints.tests.BaseConstraintTests.test_default_violation_error_message) | Pass |
test_deprecation (constraints.tests.BaseConstraintTests.test_deprecation) | Pass |
test_name_required (constraints.tests.BaseConstraintTests.test_name_required) | Pass |
test_positional_arguments (constraints.tests.BaseConstraintTests.test_positional_arguments) | Pass |
test_remove_sql (constraints.tests.BaseConstraintTests.test_remove_sql) | Pass |
test_validate (constraints.tests.BaseConstraintTests.test_validate) | Pass |
test_abstract_name (constraints.tests.CheckConstraintTests.test_abstract_name) | Pass |
test_database_constraint (constraints.tests.CheckConstraintTests.test_database_constraint) | Pass |
test_database_constraint_unicode (constraints.tests.CheckConstraintTests.test_database_constraint_unicode) | Pass |
test_deconstruction (constraints.tests.CheckConstraintTests.test_deconstruction) | Pass |
test_invalid_check_types (constraints.tests.CheckConstraintTests.test_invalid_check_types) | Pass |
test_name (constraints.tests.CheckConstraintTests.test_name) | Pass |
test_repr (constraints.tests.CheckConstraintTests.test_repr) | Pass |
test_repr_with_violation_error_message (constraints.tests.CheckConstraintTests.test_repr_with_violation_error_message) | Pass |
test_validate (constraints.tests.CheckConstraintTests.test_validate) | Pass |
test_validate_boolean_expressions (constraints.tests.CheckConstraintTests.test_validate_boolean_expressions) | Pass |
test_validate_nullable_field_with_none (constraints.tests.CheckConstraintTests.test_validate_nullable_field_with_none) | Pass |
test_validate_rawsql_expressions_noop (constraints.tests.CheckConstraintTests.test_validate_rawsql_expressions_noop) | Pass |
test_condition_must_be_q (constraints.tests.UniqueConstraintTests.test_condition_must_be_q) | Pass |
test_database_constraint (constraints.tests.UniqueConstraintTests.test_database_constraint) | Pass |
test_database_constraint_with_condition (constraints.tests.UniqueConstraintTests.test_database_constraint_with_condition) | Pass |
test_deferrable_with_condition (constraints.tests.UniqueConstraintTests.test_deferrable_with_condition) | Pass |
test_deferrable_with_expressions (constraints.tests.UniqueConstraintTests.test_deferrable_with_expressions) | Pass |
test_deferrable_with_include (constraints.tests.UniqueConstraintTests.test_deferrable_with_include) | Pass |
test_deferrable_with_opclasses (constraints.tests.UniqueConstraintTests.test_deferrable_with_opclasses) | Pass |
test_eq_with_condition (constraints.tests.UniqueConstraintTests.test_eq_with_condition) | Pass |
test_eq_with_deferrable (constraints.tests.UniqueConstraintTests.test_eq_with_deferrable) | Pass |
test_eq_with_expressions (constraints.tests.UniqueConstraintTests.test_eq_with_expressions) | Pass |
test_eq_with_include (constraints.tests.UniqueConstraintTests.test_eq_with_include) | Pass |
test_eq_with_opclasses (constraints.tests.UniqueConstraintTests.test_eq_with_opclasses) | Pass |
test_expressions_and_fields_mutually_exclusive (constraints.tests.UniqueConstraintTests.test_expressions_and_fields_mutually_exclusive) | Pass |
test_expressions_with_opclasses (constraints.tests.UniqueConstraintTests.test_expressions_with_opclasses) | Pass |
test_invalid_defer_argument (constraints.tests.UniqueConstraintTests.test_invalid_defer_argument) | Pass |
test_invalid_include_argument (constraints.tests.UniqueConstraintTests.test_invalid_include_argument) | Pass |
test_invalid_opclasses_argument (constraints.tests.UniqueConstraintTests.test_invalid_opclasses_argument) | Pass |
test_model_validation (constraints.tests.UniqueConstraintTests.test_model_validation) | Pass |
test_model_validation_constraint_no_code_error (constraints.tests.UniqueConstraintTests.test_model_validation_constraint_no_code_error) | Pass |
Partial unique constraints are not ignored by | Pass |
test_name (constraints.tests.UniqueConstraintTests.test_name) | Pass |
test_opclasses_and_fields_same_length (constraints.tests.UniqueConstraintTests.test_opclasses_and_fields_same_length) | Pass |
test_repr (constraints.tests.UniqueConstraintTests.test_repr) | Pass |
test_repr_with_condition (constraints.tests.UniqueConstraintTests.test_repr_with_condition) | Pass |
test_repr_with_deferrable (constraints.tests.UniqueConstraintTests.test_repr_with_deferrable) | Pass |
test_repr_with_expressions (constraints.tests.UniqueConstraintTests.test_repr_with_expressions) | Pass |
test_repr_with_include (constraints.tests.UniqueConstraintTests.test_repr_with_include) | Pass |
test_repr_with_opclasses (constraints.tests.UniqueConstraintTests.test_repr_with_opclasses) | Pass |
test_repr_with_violation_error_message (constraints.tests.UniqueConstraintTests.test_repr_with_violation_error_message) | Pass |
test_requires_field_or_expression (constraints.tests.UniqueConstraintTests.test_requires_field_or_expression) | Pass |
test_requires_name (constraints.tests.UniqueConstraintTests.test_requires_name) | Pass |
test_validate (constraints.tests.UniqueConstraintTests.test_validate) | Pass |
test_validate_condition (constraints.tests.UniqueConstraintTests.test_validate_condition) | Pass |
test_validate_expression (constraints.tests.UniqueConstraintTests.test_validate_expression) | Pass |
test_validate_expression_condition (constraints.tests.UniqueConstraintTests.test_validate_expression_condition) | Pass |
test_validate_expression_str (constraints.tests.UniqueConstraintTests.test_validate_expression_str) | Pass |
test_validate_ordered_expression (constraints.tests.UniqueConstraintTests.test_validate_ordered_expression) | Pass |
Loading...
Ridges.AIĀ© 2025 Ridges AI. Building the future of decentralized AI development.
