from django.test import TestCase from django.core.exceptions import ValidationError from .models import Directie, Serviciu, Compartiment, Employee class UsersModelTests(TestCase): def setUp(self): self.directie = Directie.objects.create(name="Direcția Economică") self.serviciu = Serviciu.objects.create( name="Serviciul Contabilitate", directie=self.directie ) def test_compartiment_validation_valid(self): # A Compartiment assigned to a Serviciu should pass validation comp = Compartiment(name="Compartiment Salarizare", serviciu=self.serviciu) comp.full_clean() # Validates the model without saving to DB self.assertEqual(comp.serviciu, self.serviciu) def test_compartiment_validation_missing_parent(self): # A Compartiment with no parent should raise a ValidationError comp = Compartiment(name="Compartiment Orfan") with self.assertRaises(ValidationError): comp.full_clean() def test_compartiment_validation_multiple_parents(self): # A Compartiment assigned to BOTH a Serviciu and a Directie should fail comp = Compartiment( name="Compartiment Dublu Subordonat", directie=self.directie, serviciu=self.serviciu, ) with self.assertRaises(ValidationError): comp.full_clean() def test_employee_creation(self): emp = Employee.objects.create( name="Test User", username="testuser", role="Inspector", office_number="12", email="test@primariaroman.ro", directie=self.directie, ) self.assertEqual(emp.name, "Test User") self.assertEqual(str(emp), "Test User (testuser)")