"""Unit tests for profile service using unittest.""" import sys import os sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) import unittest from src.models.profile import Profile, UpdateProfileRequest from src.services.profile_service import ProfileService class TestProfileModel(unittest.TestCase): """Tests for Profile model.""" def test_profile_creation(self): """Test creating a profile.""" profile = Profile(id="user-1", name="Juan Pérez", language="es") self.assertEqual(profile.id, "user-1") self.assertEqual(profile.name, "Juan Pérez") self.assertEqual(profile.language, "es") def test_profile_name_validation_letters(self): """Test valid name with letters only.""" profile = Profile(id="user-1", name="José García") self.assertEqual(profile.name, "José García") def test_profile_name_validation_invalid_chars(self): """Test invalid name with special characters.""" with self.assertRaises(ValueError) as ctx: Profile(id="user-1", name="Juan@123!") self.assertIn("Nombre inválido", str(ctx.exception)) def test_profile_avatar_url_valid(self): """Test valid avatar URL.""" profile = Profile(id="user-1", name="Test", avatar_url="https://cdn.example.com/img.jpg") self.assertEqual(profile.avatar_url, "https://cdn.example.com/img.jpg") def test_profile_avatar_url_invalid_protocol(self): """Test invalid avatar URL protocol.""" with self.assertRaises(ValueError) as ctx: Profile(id="user-1", name="Test", avatar_url="ftp://malicious.com/file.jpg") self.assertIn("Solo se permiten", str(ctx.exception)) def test_profile_language_values(self): """Test valid language values.""" for lang in ["en", "es", "fr", "de"]: profile = Profile(id="user-1", name="Test", language=lang) self.assertEqual(profile.language, lang) class TestUpdateProfileRequest(unittest.TestCase): """Tests for UpdateProfileRequest model.""" def test_partial_update_name_only(self): """Test updating only name.""" request = UpdateProfileRequest(name="Pedro") self.assertEqual(request.name, "Pedro") self.assertIsNone(request.avatar_url) self.assertIsNone(request.language) def test_partial_update_all_fields(self): """Test updating all fields.""" request = UpdateProfileRequest( name="Pedro", avatar_url="https://cdn.example.com/new.jpg", language="en" ) self.assertEqual(request.name, "Pedro") self.assertEqual(request.avatar_url, "https://cdn.example.com/new.jpg") self.assertEqual(request.language, "en") class TestProfileService(unittest.TestCase): """Tests for ProfileService.""" def setUp(self): """Create a fresh service instance.""" self.service = ProfileService() def test_get_existing_profile(self): """Test getting an existing profile.""" profile, status, error = self.service.get_profile("user-123") self.assertEqual(status, 200) self.assertIsNotNone(profile) self.assertEqual(profile.id, "user-123") self.assertIsNone(error) def test_get_nonexistent_profile(self): """Test getting a nonexistent profile.""" profile, status, error = self.service.get_profile("nonexistent") self.assertEqual(status, 404) self.assertIsNone(profile) self.assertEqual(error, "Usuario no encontrado") def test_update_profile_name(self): """Test updating profile name.""" request = UpdateProfileRequest(name="Pedro") profile, status, error = self.service.update_profile("user-123", request) self.assertEqual(status, 200) self.assertEqual(profile.name, "Pedro") def test_update_profile_avatar(self): """Test updating profile avatar.""" request = UpdateProfileRequest(avatar_url="https://cdn.example.com/new.jpg") profile, status, error = self.service.update_profile("user-123", request) self.assertEqual(status, 200) self.assertEqual(profile.avatar_url, "https://cdn.example.com/new.jpg") def test_update_profile_language(self): """Test updating profile language.""" request = UpdateProfileRequest(language="fr") profile, status, error = self.service.update_profile("user-123", request) self.assertEqual(status, 200) self.assertEqual(profile.language, "fr") def test_update_nonexistent_profile(self): """Test updating a nonexistent profile.""" request = UpdateProfileRequest(name="Test") profile, status, error = self.service.update_profile("nonexistent", request) self.assertEqual(status, 404) self.assertEqual(error, "Usuario no encontrado") def test_create_profile(self): """Test creating a new profile.""" profile = self.service.create_profile("user-new", "New User", "https://cdn.com/img.jpg", "es") self.assertEqual(profile.id, "user-new") self.assertEqual(profile.name, "New User") self.assertEqual(profile.language, "es") if __name__ == "__main__": unittest.main()