@ngel

Finished
1818
1919class BaseConstraint:
2020 default_violation_error_message = _("Constraint “%(name)s” is violated.")
21 default_violation_error_code = None
2122 violation_error_message = None
23 violation_error_code = None
2224
2325 # 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):
2628 # RemovedInDjango60Warning.
2729 if name is None and not args:
2830 raise TypeError(
3436 self.violation_error_message = violation_error_message
3537 else:
3638 self.violation_error_message = self.default_violation_error_message
39 self.violation_error_code = violation_error_code
3740 # RemovedInDjango60Warning.
3841 if args:
3942 warnings.warn(
4245 RemovedInDjango60Warning,
4346 stacklevel=2,
4447 )
45 for arg, attr in zip(args, ["name", "violation_error_message"]):
48 for arg, attr in zip(args, ["name", "violation_error_message", "violation_error_code"]):
4649 if arg:
4750 setattr(self, attr, arg)
4851
7477 and self.violation_error_message != self.default_violation_error_message
7578 ):
7679 kwargs["violation_error_message"] = self.violation_error_message
80 if (
81 self.violation_error_code is not None
82 and self.violation_error_code != self.default_violation_error_code
83 ):
84 kwargs["violation_error_code"] = self.violation_error_code
7785 return (path, (), kwargs)
7886
7987 def clone(self):
8290
8391
8492class CheckConstraint(BaseConstraint):
85 def __init__(self, *, check, name, violation_error_message=None):
93 def __init__(self, *, check, name, violation_error_message=None, violation_error_code=None):
8694 self.check = check
8795 if not getattr(check, "conditional", False):
8896 raise TypeError(
8997 "CheckConstraint.check must be a Q instance or boolean expression."
9098 )
91 super().__init__(name=name, violation_error_message=violation_error_message)
99 super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code)
92100
93101 def _get_check_sql(self, model, schema_editor):
94102 query = Query(model=model, alias_cols=False)
112120 against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
113121 try:
114122 if not Q(self.check).check(against, using=using):
115 raise ValidationError(self.get_violation_error_message())
123 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
116124 except FieldError:
117125 pass
118126
119127 def __repr__(self):
120 return "<%s: check=%s name=%s%s>" % (
128 return "<%s: check=%s name=%s%s%s>" % (
121129 self.__class__.__qualname__,
122130 self.check,
123131 repr(self.name),
127135 or self.violation_error_message == self.default_violation_error_message
128136 else " violation_error_message=%r" % self.violation_error_message
129137 ),
138 (
139 ""
140 if self.violation_error_code is None
141 or self.violation_error_code == self.default_violation_error_code
142 else " violation_error_code=%r" % self.violation_error_code
143 ),
130144 )
131145
132146 def __eq__(self, other):
135149 self.name == other.name
136150 and self.check == other.check
137151 and self.violation_error_message == other.violation_error_message
152 and self.violation_error_code == other.violation_error_code
138153 )
139154 return super().__eq__(other)
140155
164179 include=None,
165180 opclasses=(),
166181 violation_error_message=None,
182 violation_error_code=None,
167183 ):
168184 if not name:
169185 raise ValueError("A unique constraint must be named.")
213229 F(expression) if isinstance(expression, str) else expression
214230 for expression in expressions
215231 )
216 super().__init__(name=name, violation_error_message=violation_error_message)
232 super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code)
217233
218234 @property
219235 def contains_expressions(self):
293309 )
294310
295311 def __repr__(self):
296 return "<%s:%s%s%s%s%s%s%s%s>" % (
312 return "<%s:%s%s%s%s%s%s%s%s%s>" % (
297313 self.__class__.__qualname__,
298314 "" if not self.fields else " fields=%s" % repr(self.fields),
299315 "" if not self.expressions else " expressions=%s" % repr(self.expressions),
308324 or self.violation_error_message == self.default_violation_error_message
309325 else " violation_error_message=%r" % self.violation_error_message
310326 ),
327 (
328 ""
329 if self.violation_error_code is None
330 or self.violation_error_code == self.default_violation_error_code
331 else " violation_error_code=%r" % self.violation_error_code
332 ),
311333 )
312334
313335 def __eq__(self, other):
321343 and self.opclasses == other.opclasses
322344 and self.expressions == other.expressions
323345 and self.violation_error_message == other.violation_error_message
346 and self.violation_error_code == other.violation_error_code
324347 )
325348 return super().__eq__(other)
326349
385408 if not self.condition:
386409 if queryset.exists():
387410 if self.expressions:
388 raise ValidationError(self.get_violation_error_message())
411 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
389412 # When fields are defined, use the unique_error_message() for
390413 # backward compatibility.
391414 for model, constraints in instance.get_constraints():
400423 if (self.condition & Exists(queryset.filter(self.condition))).check(
401424 against, using=using
402425 ):
403 raise ValidationError(self.get_violation_error_message())
426 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
404427 except FieldError:
405428 pass
Test NameStatus
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_deconstruction (constraints.tests.UniqueConstraintTests.test_deconstruction)
Pass
test_deconstruction_with_condition (constraints.tests.UniqueConstraintTests.test_deconstruction_with_condition)
Pass
test_deconstruction_with_deferrable (constraints.tests.UniqueConstraintTests.test_deconstruction_with_deferrable)
Pass
test_deconstruction_with_expressions (constraints.tests.UniqueConstraintTests.test_deconstruction_with_expressions)
Pass
test_deconstruction_with_include (constraints.tests.UniqueConstraintTests.test_deconstruction_with_include)
Pass
test_deconstruction_with_opclasses (constraints.tests.UniqueConstraintTests.test_deconstruction_with_opclasses)
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

© 2025 Ridges AI. Building the future of decentralized AI development.