yahoo

Finished
1919class BaseConstraint:
2020 default_violation_error_message = _("Constraint “%(name)s” is violated.")
2121 violation_error_message = None
22 violation_error_code = None
2223
2324 # 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):
25 # def __init__(self, *, name, violation_error_message=None, violation_error_code=None):
26 def __init__(self, *args, name=None, violation_error_message=None, violation_error_code=None):
2627 # RemovedInDjango60Warning.
2728 if name is None and not args:
2829 raise TypeError(
3435 self.violation_error_message = violation_error_message
3536 else:
3637 self.violation_error_message = self.default_violation_error_message
38 self.violation_error_code = violation_error_code
3739 # RemovedInDjango60Warning.
3840 if args:
3941 warnings.warn(
4244 RemovedInDjango60Warning,
4345 stacklevel=2,
4446 )
45 for arg, attr in zip(args, ["name", "violation_error_message"]):
47 for arg, attr in zip(args, ["name", "violation_error_message", "violation_error_code"]):
4648 if arg:
4749 setattr(self, attr, arg)
4850
7476 and self.violation_error_message != self.default_violation_error_message
7577 ):
7678 kwargs["violation_error_message"] = self.violation_error_message
79 if self.violation_error_code is not None:
80 kwargs["violation_error_code"] = self.violation_error_code
7781 return (path, (), kwargs)
7882
7983 def clone(self):
8286
8387
8488class CheckConstraint(BaseConstraint):
85 def __init__(self, *, check, name, violation_error_message=None):
89 def __init__(self, *, check, name, violation_error_message=None, violation_error_code=None):
8690 self.check = check
8791 if not getattr(check, "conditional", False):
8892 raise TypeError(
8993 "CheckConstraint.check must be a Q instance or boolean expression."
9094 )
91 super().__init__(name=name, violation_error_message=violation_error_message)
95 super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code)
9296
9397 def _get_check_sql(self, model, schema_editor):
9498 query = Query(model=model, alias_cols=False)
112116 against = instance._get_field_value_map(meta=model._meta, exclude=exclude)
113117 try:
114118 if not Q(self.check).check(against, using=using):
115 raise ValidationError(self.get_violation_error_message())
119 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
116120 except FieldError:
117121 pass
118122
119123 def __repr__(self):
120 return "<%s: check=%s name=%s%s>" % (
124 return "<%s: check=%s name=%s%s%s>" % (
121125 self.__class__.__qualname__,
122126 self.check,
123127 repr(self.name),
127131 or self.violation_error_message == self.default_violation_error_message
128132 else " violation_error_message=%r" % self.violation_error_message
129133 ),
134 (
135 ""
136 if self.violation_error_code is None
137 else " violation_error_code=%r" % self.violation_error_code
138 ),
130139 )
131140
132141 def __eq__(self, other):
135144 self.name == other.name
136145 and self.check == other.check
137146 and self.violation_error_message == other.violation_error_message
147 and self.violation_error_code == other.violation_error_code
138148 )
139149 return super().__eq__(other)
140150
164174 include=None,
165175 opclasses=(),
166176 violation_error_message=None,
177 violation_error_code=None,
167178 ):
168179 if not name:
169180 raise ValueError("A unique constraint must be named.")
213224 F(expression) if isinstance(expression, str) else expression
214225 for expression in expressions
215226 )
216 super().__init__(name=name, violation_error_message=violation_error_message)
227 super().__init__(name=name, violation_error_message=violation_error_message, violation_error_code=violation_error_code)
217228
218229 @property
219230 def contains_expressions(self):
293304 )
294305
295306 def __repr__(self):
296 return "<%s:%s%s%s%s%s%s%s%s>" % (
307 return "<%s:%s%s%s%s%s%s%s%s%s>" % (
297308 self.__class__.__qualname__,
298309 "" if not self.fields else " fields=%s" % repr(self.fields),
299310 "" if not self.expressions else " expressions=%s" % repr(self.expressions),
308319 or self.violation_error_message == self.default_violation_error_message
309320 else " violation_error_message=%r" % self.violation_error_message
310321 ),
322 (
323 ""
324 if self.violation_error_code is None
325 else " violation_error_code=%r" % self.violation_error_code
326 ),
327 ),
328 "" if not self.expressions else " expressions=%s" % repr(self.expressions),
329 " name=%s" % repr(self.name),
330 "" if self.condition is None else " condition=%s" % self.condition,
331 "" if self.deferrable is None else " deferrable=%r" % self.deferrable,
332 "" if not self.include else " include=%s" % repr(self.include),
333 "" if not self.opclasses else " opclasses=%s" % repr(self.opclasses),
334 (
335 ""
336 if self.violation_error_message is None
337 or self.violation_error_message == self.default_violation_error_message
338 else " violation_error_message=%s" % % self.violation_error_message
339 ),
340 (
341 ""
342 if self.violation_error_message is None
343 or self.violation_error_message == self.default.violation_error_message
344 else " violation_error_message=%s" % % self.violation_error_message
345 ),
346 (
347 ""
348 if self.violation_error_message is None
349 or self.violation_error_message == self.default_violation_error_message
350 else " violation_error_message=%s" % % self.violation_error_message
351 ),
352 (
353 ""
354 if self.violation_error_message is None
355 or self.violation_error_message == self.default.violation_error_message
356 else " violation_error_message=%s" % % self.violation_error_message
357 ),
358 (
359 ""
360 if self.violation_error_message is None
361 or self.violation_error_message == self.default.violation_error_message
362 else " violation_error_message=%s" % % self.violation_error_message
363 ),
364 (
365 ""
366 if self.violation_error_message is None
367 or self.violation_error_message == self.default.violation_error_message
368 else " violation_error_message=%s" % % self.viol_error_error_message
369 ),
370 (
371 ""
372 if self.violation_error_message is None
373 or self.violation_error_message == self.default_violation_error_message
374 else " violation_error_message=%s" % % self.violation_error_message
375 ),
376 (
377 ""
378 if self.violation_error_message is None
379 or self.violation_error_message == self.default_violation_error_message
380 else " violation_error_message=%s" % % self.violation_error_message
381 ),
382 (
383 ""
384 if self.violation_error_message is None
385 or self.violation_error_message == self.default_violation_error_message
386 else " violation_error_message=%s" % % self.violation_error_message
387 ),
388 (
389 ""
390 if self.violation_error_message is None
391 or self.violation_error_message == self.default_violation_error_message
392 else " violation_error_message=%s" % % self.violation_error_message
393 ),
394 (
395 ""
396 if self.violation_error_message is None
397 or self.violation_error_message == self.default_violation_error_message
398 else " violation_error_message=%r" % self.violation_error_message
399 ),
400 (
401 ""
402 if self.violation_error_code is None
403 else " violation_error_code=%r" % self.violation_error_code
404 ),
311405 )
312406
313407 def __eq__(self, other):
321415 and self.opclasses == other.opclasses
322416 and self.expressions == other.expressions
323417 and self.violation_error_message == other.violation_error_message
418 and self.violation_error_code == other.violation_error_code
324419 )
325420 return super().__eq__(other)
326421
385480 if not self.condition:
386481 if queryset.exists():
387482 if self.expressions:
388 raise ValidationError(self.get_violation_error_message())
483 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
389484 # When fields are defined, use the unique_error_message() for
390485 # backward compatibility.
391486 for model, constraints in instance.get_constraints():
400495 if (self.condition & Exists(queryset.filter(self.condition))).check(
401496 against, using=using
402497 ):
403 raise ValidationError(self.get_violation_error_message())
498 raise ValidationError(self.get_violation_error_message(), code=self.violation_error_code)
404499 except FieldError:
405500 pass
Test NameStatus
test_custom_violation_code_message (constraints.tests.BaseConstraintTests.test_custom_violation_code_message)
Fail
test_deconstruction (constraints.tests.BaseConstraintTests.test_deconstruction)
Fail
test_eq (constraints.tests.CheckConstraintTests.test_eq)
Fail
test_repr_with_violation_error_code (constraints.tests.CheckConstraintTests.test_repr_with_violation_error_code)
Fail
test_validate_custom_error (constraints.tests.CheckConstraintTests.test_validate_custom_error)
Fail
test_eq (constraints.tests.UniqueConstraintTests.test_eq)
Fail
test_repr_with_violation_error_code (constraints.tests.UniqueConstraintTests.test_repr_with_violation_error_code)
Fail
test_validate_conditon_custom_error (constraints.tests.UniqueConstraintTests.test_validate_conditon_custom_error)
Fail
test_constraint_sql (constraints.tests.BaseConstraintTests.test_constraint_sql)
Fail
test_contains_expressions (constraints.tests.BaseConstraintTests.test_contains_expressions)
Fail
test_create_sql (constraints.tests.BaseConstraintTests.test_create_sql)
Fail
test_custom_violation_error_message (constraints.tests.BaseConstraintTests.test_custom_violation_error_message)
Fail
test_custom_violation_error_message_clone (constraints.tests.BaseConstraintTests.test_custom_violation_error_message_clone)
Fail
test_default_violation_error_message (constraints.tests.BaseConstraintTests.test_default_violation_error_message)
Fail
test_deprecation (constraints.tests.BaseConstraintTests.test_deprecation)
Fail
test_name_required (constraints.tests.BaseConstraintTests.test_name_required)
Fail
test_positional_arguments (constraints.tests.BaseConstraintTests.test_positional_arguments)
Fail
test_remove_sql (constraints.tests.BaseConstraintTests.test_remove_sql)
Fail
test_validate (constraints.tests.BaseConstraintTests.test_validate)
Fail
test_abstract_name (constraints.tests.CheckConstraintTests.test_abstract_name)
Fail
test_database_constraint (constraints.tests.CheckConstraintTests.test_database_constraint)
Fail
test_database_constraint_unicode (constraints.tests.CheckConstraintTests.test_database_constraint_unicode)
Fail
test_deconstruction (constraints.tests.CheckConstraintTests.test_deconstruction)
Fail
test_invalid_check_types (constraints.tests.CheckConstraintTests.test_invalid_check_types)
Fail
test_name (constraints.tests.CheckConstraintTests.test_name)
Fail
test_repr (constraints.tests.CheckConstraintTests.test_repr)
Fail
test_repr_with_violation_error_message (constraints.tests.CheckConstraintTests.test_repr_with_violation_error_message)
Fail
test_validate (constraints.tests.CheckConstraintTests.test_validate)
Fail
test_validate_boolean_expressions (constraints.tests.CheckConstraintTests.test_validate_boolean_expressions)
Fail
test_validate_nullable_field_with_none (constraints.tests.CheckConstraintTests.test_validate_nullable_field_with_none)
Fail
test_validate_rawsql_expressions_noop (constraints.tests.CheckConstraintTests.test_validate_rawsql_expressions_noop)
Fail
test_condition_must_be_q (constraints.tests.UniqueConstraintTests.test_condition_must_be_q)
Fail
test_database_constraint (constraints.tests.UniqueConstraintTests.test_database_constraint)
Fail
test_database_constraint_with_condition (constraints.tests.UniqueConstraintTests.test_database_constraint_with_condition)
Fail
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_deferrable_with_condition (constraints.tests.UniqueConstraintTests.test_deferrable_with_condition)
Fail
test_deferrable_with_expressions (constraints.tests.UniqueConstraintTests.test_deferrable_with_expressions)
Fail
test_deferrable_with_include (constraints.tests.UniqueConstraintTests.test_deferrable_with_include)
Fail
test_deferrable_with_opclasses (constraints.tests.UniqueConstraintTests.test_deferrable_with_opclasses)
Fail
test_eq_with_condition (constraints.tests.UniqueConstraintTests.test_eq_with_condition)
Fail
test_eq_with_deferrable (constraints.tests.UniqueConstraintTests.test_eq_with_deferrable)
Fail
test_eq_with_expressions (constraints.tests.UniqueConstraintTests.test_eq_with_expressions)
Fail
test_eq_with_include (constraints.tests.UniqueConstraintTests.test_eq_with_include)
Fail
test_eq_with_opclasses (constraints.tests.UniqueConstraintTests.test_eq_with_opclasses)
Fail
test_expressions_and_fields_mutually_exclusive (constraints.tests.UniqueConstraintTests.test_expressions_and_fields_mutually_exclusive)
Fail
test_expressions_with_opclasses (constraints.tests.UniqueConstraintTests.test_expressions_with_opclasses)
Fail
test_invalid_defer_argument (constraints.tests.UniqueConstraintTests.test_invalid_defer_argument)
Fail
test_invalid_include_argument (constraints.tests.UniqueConstraintTests.test_invalid_include_argument)
Fail
test_invalid_opclasses_argument (constraints.tests.UniqueConstraintTests.test_invalid_opclasses_argument)
Fail
test_model_validation (constraints.tests.UniqueConstraintTests.test_model_validation)
Fail
test_model_validation_constraint_no_code_error (constraints.tests.UniqueConstraintTests.test_model_validation_constraint_no_code_error)
Fail
Partial unique constraints are not ignored by
Fail
test_name (constraints.tests.UniqueConstraintTests.test_name)
Fail
test_opclasses_and_fields_same_length (constraints.tests.UniqueConstraintTests.test_opclasses_and_fields_same_length)
Fail
test_repr (constraints.tests.UniqueConstraintTests.test_repr)
Fail
test_repr_with_condition (constraints.tests.UniqueConstraintTests.test_repr_with_condition)
Fail
test_repr_with_deferrable (constraints.tests.UniqueConstraintTests.test_repr_with_deferrable)
Fail
test_repr_with_expressions (constraints.tests.UniqueConstraintTests.test_repr_with_expressions)
Fail
test_repr_with_include (constraints.tests.UniqueConstraintTests.test_repr_with_include)
Fail
test_repr_with_opclasses (constraints.tests.UniqueConstraintTests.test_repr_with_opclasses)
Fail
test_repr_with_violation_error_message (constraints.tests.UniqueConstraintTests.test_repr_with_violation_error_message)
Fail
test_requires_field_or_expression (constraints.tests.UniqueConstraintTests.test_requires_field_or_expression)
Fail
test_requires_name (constraints.tests.UniqueConstraintTests.test_requires_name)
Fail
test_validate (constraints.tests.UniqueConstraintTests.test_validate)
Fail
test_validate_condition (constraints.tests.UniqueConstraintTests.test_validate_condition)
Fail
test_validate_expression (constraints.tests.UniqueConstraintTests.test_validate_expression)
Fail
test_validate_expression_condition (constraints.tests.UniqueConstraintTests.test_validate_expression_condition)
Fail
test_validate_expression_str (constraints.tests.UniqueConstraintTests.test_validate_expression_str)
Fail
test_validate_ordered_expression (constraints.tests.UniqueConstraintTests.test_validate_ordered_expression)
Fail

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