feat(*): add more tests

pull/174/head
Christian Merten 4 months ago committed by mariusrklein
parent 98a6d707a4
commit ed67669b29

@ -1,3 +1,56 @@
from django.test import TestCase from django.test import TestCase
from django.contrib.auth import get_user_model
from contrib.models import CommonModel
from contrib.rules import has_global_perm
# Create your tests here. User = get_user_model()
class CommonModelTestCase(TestCase):
def test_common_model_abstract_base(self):
"""Test that CommonModel provides the correct meta attributes"""
meta = CommonModel._meta
self.assertTrue(meta.abstract)
expected_permissions = (
'add_global', 'change_global', 'view_global', 'delete_global', 'list_global', 'view',
)
self.assertEqual(meta.default_permissions, expected_permissions)
def test_common_model_inheritance(self):
"""Test that CommonModel has rules mixin functionality"""
# Test that CommonModel has the expected functionality
# Since it's abstract, we can't instantiate it directly
# but we can check its metaclass and mixins
from rules.contrib.models import RulesModelMixin, RulesModelBase
self.assertTrue(issubclass(CommonModel, RulesModelMixin))
self.assertEqual(CommonModel.__class__, RulesModelBase)
class GlobalPermissionRulesTestCase(TestCase):
def setUp(self):
self.user = User.objects.create_user(
username='testuser',
email='test@example.com',
password='testpass123'
)
def test_has_global_perm_predicate_creation(self):
"""Test that has_global_perm creates a predicate function"""
# has_global_perm is a decorator factory, not a direct predicate
predicate = has_global_perm('auth.add_user')
self.assertTrue(callable(predicate))
def test_has_global_perm_with_superuser(self):
"""Test that superusers have global permissions"""
self.user.is_superuser = True
self.user.save()
predicate = has_global_perm('auth.add_user')
result = predicate(self.user, None)
self.assertTrue(result)
def test_has_global_perm_with_regular_user(self):
"""Test that regular users don't automatically have global permissions"""
predicate = has_global_perm('auth.add_user')
result = predicate(self.user, None)
self.assertFalse(result)

@ -2,6 +2,7 @@ from unittest import skip
from django.test import TestCase from django.test import TestCase
from django.utils import timezone from django.utils import timezone
from django.conf import settings from django.conf import settings
from decimal import Decimal
from .models import Statement, StatementUnSubmitted, StatementSubmitted, Bill, Ledger, Transaction,\ from .models import Statement, StatementUnSubmitted, StatementSubmitted, Bill, Ledger, Transaction,\
StatementUnSubmittedManager, StatementSubmittedManager, StatementConfirmedManager,\ StatementUnSubmittedManager, StatementSubmittedManager, StatementConfirmedManager,\
StatementConfirmed, TransactionIssue, StatementManager StatementConfirmed, TransactionIssue, StatementManager
@ -371,6 +372,41 @@ class StatementTestCase(TestCase):
bills = self.st2.grouped_bills() bills = self.st2.grouped_bills()
self.assertTrue('amount' in bills[0]) self.assertTrue('amount' in bills[0])
def test_euro_per_km_no_excursion(self):
"""Test euro_per_km when no excursion is associated"""
statement = Statement.objects.create(
short_description="Test Statement",
explanation="Test explanation",
night_cost=25
)
self.assertEqual(statement.euro_per_km, 0)
def test_submit_workflow(self):
"""Test statement submission workflow"""
statement = Statement.objects.create(
short_description="Test Statement",
explanation="Test explanation",
night_cost=25,
created_by=self.fritz
)
self.assertFalse(statement.submitted)
self.assertIsNone(statement.submitted_by)
self.assertIsNone(statement.submitted_date)
# Test submission - submit method doesn't return a value, just changes state
statement.submit(submitter=self.fritz)
self.assertTrue(statement.submitted)
self.assertEqual(statement.submitted_by, self.fritz)
self.assertIsNotNone(statement.submitted_date)
def test_template_context_with_excursion(self):
"""Test statement template context when excursion is present"""
# Use existing excursion from setUp
context = self.st3.template_context()
self.assertIn('euro_per_km', context)
self.assertIsInstance(context['euro_per_km'], (int, float, Decimal))
class LedgerTestCase(TestCase): class LedgerTestCase(TestCase):
def setUp(self): def setUp(self):
@ -431,9 +467,20 @@ class TransactionTestCase(TestCase):
self.assertTrue(str(self.trans.pk) in str(self.trans)) self.assertTrue(str(self.trans.pk) in str(self.trans))
def test_escape_reference(self): def test_escape_reference(self):
self.assertEqual(Transaction.escape_reference('harmless'), 'harmless') """Test transaction reference escaping with various special characters"""
self.assertEqual(Transaction.escape_reference('äöüÄÖÜß'), 'aeoeueAeOeUess') test_cases = [
self.assertEqual(Transaction.escape_reference('ha@r!?mless+09'), 'har?mless+09') ('harmless', 'harmless'),
('äöüÄÖÜß', 'aeoeueAeOeUess'),
('ha@r!?mless+09', 'har?mless+09'),
("simple", "simple"),
("test@email.com", "testemail.com"),
("ref!with#special$chars%", "refwithspecialchars"),
("normal_text-123", "normaltext-123"), # underscores are removed
]
for input_ref, expected in test_cases:
result = Transaction.escape_reference(input_ref)
self.assertEqual(result, expected)
def test_code(self): def test_code(self):
self.trans.amount = 0 self.trans.amount = 0
@ -446,6 +493,35 @@ class TransactionTestCase(TestCase):
self.fritz.iban = 'DE89370400440532013000' self.fritz.iban = 'DE89370400440532013000'
self.assertNotEqual(self.trans.code(), '') self.assertNotEqual(self.trans.code(), '')
def test_code_with_zero_amount(self):
"""Test transaction code generation with zero amount"""
transaction = Transaction.objects.create(
reference="test-ref",
amount=Decimal('0.00'),
member=self.fritz,
ledger=self.personal_account,
statement=self.st
)
# Zero amount should return empty code
self.assertEqual(transaction.code(), '')
def test_code_with_invalid_iban(self):
"""Test transaction code generation with invalid IBAN"""
self.fritz.iban = "INVALID_IBAN"
self.fritz.save()
transaction = Transaction.objects.create(
reference="test-ref",
amount=Decimal('100.00'),
member=self.fritz,
ledger=self.personal_account,
statement=self.st
)
# Invalid IBAN should return empty code
self.assertEqual(transaction.code(), '')
class BillTestCase(TestCase): class BillTestCase(TestCase):
def setUp(self): def setUp(self):
@ -461,6 +537,30 @@ class BillTestCase(TestCase):
def test_pretty_amount(self): def test_pretty_amount(self):
self.assertTrue('' in self.bill.pretty_amount()) self.assertTrue('' in self.bill.pretty_amount())
def test_pretty_amount_formatting(self):
"""Test bill pretty_amount formatting with specific values"""
bill = Bill.objects.create(
statement=self.st,
short_description="Test Bill",
amount=Decimal('42.50')
)
pretty = bill.pretty_amount()
self.assertIn("42.50", pretty)
self.assertIn("", pretty)
def test_zero_amount(self):
"""Test bill with zero amount"""
bill = Bill.objects.create(
statement=self.st,
short_description="Zero Bill",
amount=Decimal('0.00')
)
self.assertEqual(bill.amount, Decimal('0.00'))
pretty = bill.pretty_amount()
self.assertIn("0.00", pretty)
class TransactionIssueTestCase(TestCase): class TransactionIssueTestCase(TestCase):
def setUp(self): def setUp(self):

@ -1,3 +1,114 @@
from django.test import TestCase from django.test import TestCase
from django.utils import timezone
from datetime import date
from decimal import Decimal
from material.models import MaterialCategory, MaterialPart, Ownership
from members.models import Member, MALE, FEMALE, DIVERSE
# Create your tests here.
class MaterialCategoryTestCase(TestCase):
def setUp(self):
self.category = MaterialCategory.objects.create(name="Climbing Gear")
def test_str(self):
"""Test string representation of MaterialCategory"""
self.assertEqual(str(self.category), "Climbing Gear")
def test_verbose_names(self):
"""Test verbose names are set correctly"""
meta = MaterialCategory._meta
self.assertTrue(hasattr(meta, 'verbose_name'))
self.assertTrue(hasattr(meta, 'verbose_name_plural'))
class MaterialPartTestCase(TestCase):
def setUp(self):
self.category = MaterialCategory.objects.create(name="Ropes")
self.material_part = MaterialPart.objects.create(
name="Dynamic Rope 10mm",
description="60m dynamic climbing rope",
quantity=5,
buy_date=date(2020, 1, 15),
lifetime=Decimal('8')
)
self.material_part.material_cat.add(self.category)
self.member = Member.objects.create(
prename="John",
lastname="Doe",
birth_date=date(1990, 1, 1),
email="john@example.com",
gender=MALE
)
def test_str(self):
"""Test string representation of MaterialPart"""
self.assertEqual(str(self.material_part), "Dynamic Rope 10mm")
def test_quantity_real_no_ownership(self):
"""Test quantity_real when no ownership exists"""
result = self.material_part.quantity_real()
self.assertEqual(result, "0/5")
def test_quantity_real_with_ownership(self):
"""Test quantity_real with ownership records"""
Ownership.objects.create(
material=self.material_part,
owner=self.member,
count=3
)
Ownership.objects.create(
material=self.material_part,
owner=self.member,
count=1
)
result = self.material_part.quantity_real()
self.assertEqual(result, "4/5")
def test_verbose_names(self):
"""Test field verbose names"""
# Just test that verbose names exist, since they might be translated
field_names = ['name', 'description', 'quantity', 'buy_date', 'lifetime', 'photo', 'material_cat']
for field_name in field_names:
field = self.material_part._meta.get_field(field_name)
self.assertTrue(hasattr(field, 'verbose_name'))
self.assertIsNotNone(field.verbose_name)
class OwnershipTestCase(TestCase):
def setUp(self):
self.category = MaterialCategory.objects.create(name="Hardware")
self.material_part = MaterialPart.objects.create(
name="Carabiner Set",
description="Lightweight aluminum carabiners",
quantity=10,
buy_date=date(2021, 6, 1),
lifetime=Decimal('10')
)
self.member = Member.objects.create(
prename="Alice",
lastname="Smith",
birth_date=date(1985, 3, 15),
email="alice@example.com",
gender=FEMALE
)
self.ownership = Ownership.objects.create(
material=self.material_part,
owner=self.member,
count=6
)
def test_ownership_creation(self):
"""Test ownership record creation"""
self.assertEqual(self.ownership.material, self.material_part)
self.assertEqual(self.ownership.owner, self.member)
self.assertEqual(self.ownership.count, 6)
def test_material_part_relationship(self):
"""Test relationship between MaterialPart and Ownership"""
ownerships = Ownership.objects.filter(material=self.material_part)
self.assertEqual(ownerships.count(), 1)
self.assertEqual(ownerships.first(), self.ownership)

Loading…
Cancel
Save