- Add Model Mixins: TimestampMixin, SoftDeleteMixin, AuditMixin, etc. - Add Base Services: BaseService, BulkOperationsMixin, QueryOptimizerMixin - Add Base ViewSets with bulk operations - Add BackgroundJob model for Celery task tracking - Add BaseAppCommand for management commands - Add permissions, pagination, filters, cache, logging - Migrate tests to factory_boy + faker - Add CHANGELOG.md - 297 tests passing
104 lines
3.4 KiB
Python
104 lines
3.4 KiB
Python
"""Tests for core services"""
|
|
|
|
from apps.core.exceptions import NotFoundError
|
|
from apps.core.services import BaseService
|
|
from django.contrib.auth import get_user_model
|
|
from django.test import TestCase
|
|
|
|
User = get_user_model()
|
|
|
|
|
|
class UserTestService(BaseService[User]):
|
|
"""Test service using User model"""
|
|
|
|
model = User
|
|
|
|
|
|
class BaseServiceTest(TestCase):
|
|
"""Tests for BaseService"""
|
|
|
|
def setUp(self):
|
|
self.user = User.objects.create_user(
|
|
username="testuser",
|
|
email="test@example.com",
|
|
password="testpass123",
|
|
)
|
|
|
|
def test_get_by_id_success(self):
|
|
"""Test get_by_id returns entity"""
|
|
result = UserTestService.get_by_id(self.user.pk)
|
|
self.assertEqual(result.pk, self.user.pk)
|
|
self.assertEqual(result.email, self.user.email)
|
|
|
|
def test_get_by_id_not_found(self):
|
|
"""Test get_by_id raises NotFoundError for non-existent ID"""
|
|
with self.assertRaises(NotFoundError) as context:
|
|
UserTestService.get_by_id(99999)
|
|
|
|
self.assertEqual(context.exception.code, "not_found")
|
|
self.assertIn("User", context.exception.message)
|
|
|
|
def test_get_by_id_or_none_found(self):
|
|
"""Test get_by_id_or_none returns entity when found"""
|
|
result = UserTestService.get_by_id_or_none(self.user.pk)
|
|
self.assertEqual(result.pk, self.user.pk)
|
|
|
|
def test_get_by_id_or_none_not_found(self):
|
|
"""Test get_by_id_or_none returns None when not found"""
|
|
result = UserTestService.get_by_id_or_none(99999)
|
|
self.assertIsNone(result)
|
|
|
|
def test_get_all(self):
|
|
"""Test get_all returns queryset"""
|
|
User.objects.create_user(
|
|
username="testuser2",
|
|
email="test2@example.com",
|
|
password="testpass123",
|
|
)
|
|
|
|
result = UserTestService.get_all()
|
|
self.assertEqual(result.count(), 2)
|
|
|
|
def test_filter(self):
|
|
"""Test filter returns filtered queryset"""
|
|
result = UserTestService.filter(email="test@example.com")
|
|
self.assertEqual(result.count(), 1)
|
|
self.assertEqual(result.first().email, "test@example.com")
|
|
|
|
def test_exists_true(self):
|
|
"""Test exists returns True when entity exists"""
|
|
result = UserTestService.exists(email="test@example.com")
|
|
self.assertTrue(result)
|
|
|
|
def test_exists_false(self):
|
|
"""Test exists returns False when entity does not exist"""
|
|
result = UserTestService.exists(email="nonexistent@example.com")
|
|
self.assertFalse(result)
|
|
|
|
def test_count_all(self):
|
|
"""Test count returns total count"""
|
|
result = UserTestService.count()
|
|
self.assertEqual(result, 1)
|
|
|
|
def test_count_filtered(self):
|
|
"""Test count with filter"""
|
|
result = UserTestService.count(email="test@example.com")
|
|
self.assertEqual(result, 1)
|
|
|
|
result = UserTestService.count(email="nonexistent@example.com")
|
|
self.assertEqual(result, 0)
|
|
|
|
def test_update(self):
|
|
"""Test update modifies entity fields"""
|
|
UserTestService.update(self.user, username="updated_username")
|
|
|
|
self.user.refresh_from_db()
|
|
self.assertEqual(self.user.username, "updated_username")
|
|
|
|
def test_delete(self):
|
|
"""Test delete removes entity"""
|
|
user_pk = self.user.pk
|
|
UserTestService.delete(self.user)
|
|
|
|
self.assertFalse(User.objects.filter(pk=user_pk).exists())
|