Files
django-nepkit/django_nepkit/tests/test_utils.py
T
sankalp d7c13a0cf5 test: add comprehensive pytest test suite with 67% coverage
This commit adds a complete test suite for django-nepkit with 62 test cases covering all major components.

Test Suite:
- 8 test modules covering models, forms, widgets, serializers, filters, validators, and utilities
- 62 total test cases with 50 passing and 12 expected failures (xfail)
- 67% overall code coverage with 85-100% on core components

Test Files Added:
- django_nepkit/tests/conftest.py - pytest configuration and shared fixtures
- django_nepkit/tests/settings.py - Django settings for test environment
- django_nepkit/tests/test_models.py - 15 tests for model fields
- django_nepkit/tests/test_forms.py - 6 tests for form validation
- django_nepkit/tests/test_validators.py - 7 tests for phone validation
- django_nepkit/tests/test_utils.py - 10 tests for date/datetime parsing
- django_nepkit/tests/test_serializers.py - 10 tests for DRF serializers
- django_nepkit/tests/test_filters.py - 4 tests for DRF filters
- django_nepkit/tests/test_widgets.py - 11 tests for widget rendering
- django_nepkit/tests/README.md - test documentation

Configuration:
- pytest.ini - pytest configuration with Django settings module
- Makefile - convenience commands for running tests
- pyproject.toml - moved pytest and pytest-cov to dev dependencies

Coverage Highlights:
- validators.py: 100%
- forms.py: 91%
- conf.py: 93%
- serializers.py: 89%
- widgets.py: 90%
- models.py: 60%

Running Tests:
DJANGO_SETTINGS_MODULE=django_nepkit.tests.settings uv run pytest django_nepkit/tests/ -v

With Coverage:
DJANGO_SETTINGS_MODULE=django_nepkit.tests.settings uv run pytest django_nepkit/tests/ --cov=django_nepkit --cov-report=html
2026-01-30 18:41:38 +05:45

93 lines
2.9 KiB
Python

"""
Tests for django-nepkit utility functions.
"""
import pytest
from nepali.datetime import nepalidate, nepalidatetime
from django_nepkit.utils import (
try_parse_nepali_date,
try_parse_nepali_datetime,
)
class TestDateParsing:
"""Tests for date parsing utilities."""
def test_parse_nepali_date_from_string(self):
"""Test parsing BS date from string."""
result = try_parse_nepali_date("2081-01-15")
assert result is not None
assert isinstance(result, nepalidate)
assert result.year == 2081
assert result.month == 1
assert result.day == 15
def test_parse_nepali_date_multiple_formats(self):
"""Test parsing BS date from different formats."""
formats = [
"2081-01-15",
"15/01/2081",
"15-01-2081",
]
for date_str in formats:
result = try_parse_nepali_date(date_str)
assert result is not None
assert isinstance(result, nepalidate)
def test_parse_nepali_date_from_object(self):
"""Test that passing a nepalidate object returns it unchanged."""
date_obj = nepalidate(2081, 1, 15)
result = try_parse_nepali_date(date_obj)
assert result is date_obj
def test_parse_nepali_date_none(self):
"""Test that None returns None."""
result = try_parse_nepali_date(None)
assert result is None
def test_parse_nepali_date_empty_string(self):
"""Test that empty string returns None."""
result = try_parse_nepali_date("")
assert result is None
def test_parse_nepali_date_invalid(self):
"""Test that invalid date string returns None."""
result = try_parse_nepali_date("invalid-date")
assert result is None
class TestDateTimeParsing:
"""Tests for datetime parsing utilities."""
def test_parse_nepali_datetime_from_string(self):
"""Test parsing BS datetime from string."""
result = try_parse_nepali_datetime("2081-01-15 14:30:00")
assert result is not None
assert isinstance(result, nepalidatetime)
assert result.year == 2081
assert result.month == 1
assert result.day == 15
assert result.hour == 14
assert result.minute == 30
def test_parse_nepali_datetime_from_object(self):
"""Test that passing a nepalidatetime object returns it unchanged."""
dt_obj = nepalidatetime(2081, 1, 15, 14, 30, 0)
result = try_parse_nepali_datetime(dt_obj)
assert result is dt_obj
def test_parse_nepali_datetime_none(self):
"""Test that None returns None."""
result = try_parse_nepali_datetime(None)
assert result is None
def test_parse_nepali_datetime_invalid(self):
"""Test that invalid datetime string returns None."""
result = try_parse_nepali_datetime("invalid-datetime")
assert result is None