feat(django_nepkit/tests): add tests for _get_field_ne_setting and NepaliDateFormField validation
Distinguish explicit `False` from default and ensure proper `ValidationError` for malformed Nepali dates
This commit is contained in:
2 files changed
+62
No files matched your search
@@ -3,6 +3,7 @@ Tests for ``NepaliModelAdmin`` media and address-chaining integration.
|
||||
"""
|
||||
|
||||
from django.db import models
|
||||
from django.test import override_settings
|
||||
|
||||
from django_nepkit.admin import NepaliModelAdmin
|
||||
from django_nepkit.models import (
|
||||
@@ -85,3 +86,46 @@ class TestFormfieldForDbfieldIdempotency:
|
||||
_PlainModel._meta.get_field("name"), request=None
|
||||
)
|
||||
assert ff is not None
|
||||
|
||||
|
||||
class TestGetFieldNeSetting:
|
||||
"""``_get_field_ne_setting`` must distinguish 'no opinion' from explicit ``False``."""
|
||||
|
||||
def test_ne_true_field(self):
|
||||
class _Model(models.Model):
|
||||
t = NepaliTimeField(ne=True)
|
||||
|
||||
class Meta:
|
||||
app_label = "django_nepkit"
|
||||
|
||||
admin_cls = NepaliModelAdmin(_Model, AdminSite())
|
||||
assert admin_cls._get_field_ne_setting("t") is True
|
||||
|
||||
def test_ne_false_field_keeps_explicit_false(self):
|
||||
"""An explicit ``ne=False`` must NOT be silently overwritten."""
|
||||
|
||||
class _Model(models.Model):
|
||||
t = NepaliTimeField(ne=False)
|
||||
|
||||
class Meta:
|
||||
app_label = "django_nepkit"
|
||||
|
||||
admin_cls = NepaliModelAdmin(_Model, AdminSite())
|
||||
# Even with the project defaulting to "ne", the explicit False wins.
|
||||
with override_settings(NEPKIT={"DEFAULT_LANGUAGE": "ne"}):
|
||||
assert admin_cls._get_field_ne_setting("t") is False
|
||||
|
||||
def test_unknown_field_returns_none(self):
|
||||
admin_cls = NepaliModelAdmin(_TimeModel, AdminSite())
|
||||
assert admin_cls._get_field_ne_setting("nonexistent") is None
|
||||
|
||||
def test_plain_field_without_ne_returns_none(self):
|
||||
class _Model(models.Model):
|
||||
name = models.CharField(max_length=10)
|
||||
|
||||
class Meta:
|
||||
app_label = "django_nepkit"
|
||||
|
||||
admin_cls = NepaliModelAdmin(_Model, AdminSite())
|
||||
# ``CharField`` has no ``ne`` attribute → None (was previously False).
|
||||
assert admin_cls._get_field_ne_setting("name") is None
|
||||
@@ -64,3 +64,21 @@ class TestNepaliDateFormField:
|
||||
|
||||
result = field.clean(date_obj)
|
||||
assert result is date_obj
|
||||
|
||||
def test_unparseable_string_raises_validation_error(self):
|
||||
"""A non-date string yields a ValidationError, not a raw exception."""
|
||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||
|
||||
field = NepaliDateFormField()
|
||||
with pytest.raises(DjangoValidationError):
|
||||
field.clean("totally not a date")
|
||||
|
||||
def test_nepali_format_not_match_raises_validation_error(self):
|
||||
"""``FormatNotMatchException`` from the nepali package is caught
|
||||
explicitly (it doesn't inherit from ``ValueError``)."""
|
||||
from django.core.exceptions import ValidationError as DjangoValidationError
|
||||
|
||||
field = NepaliDateFormField()
|
||||
# 32/13/2081 is well-formed-looking but matches no Nepali format.
|
||||
with pytest.raises(DjangoValidationError):
|
||||
field.clean("32/13/2081")
|
||||
Reference in new issue
Block a user