diff --git a/src/growth_forge/tests/test_models.py b/src/growth_forge/tests/test_models.py index 1d2e98b..edde650 100644 --- a/src/growth_forge/tests/test_models.py +++ b/src/growth_forge/tests/test_models.py @@ -1 +1,56 @@ -#test models code yet to be inserted \ No newline at end of file +from django.test import TestCase +from django.contrib.auth import get_user_model +from growth_forge.models import Profile +from projects.models import Project + +User = get_user_model() + +class ProfileTests(TestCase): + @classmethod + def setUpClass(cls): + # Create a user for testing + cls.user = User.objects.create_user( + # username='testuser2', + password='testpassword2', + email='test@test.com' + ) + cls.user2 = User.objects.create_user(email='testuser2@example.com', password='testpassword') + + @classmethod + def tearDownClass(cls): + # Create a user for testing + cls.user.delete() + + cls.user2.delete() + + def test_create_profile(self): + # The Profile should be created automatically due to the post_save signal + profile = Profile.objects.get(user=self.user) + self.assertIsNotNone( + profile + ) + self.assertEqual( + profile.user.email, + 'test@test.com' + ) + + def test_retrieve_profile(self): + profile = Profile.objects.get(user=self.user) + retrieved_profile = Profile.objects.get(id=profile.id) + self.assertEqual(retrieved_profile.user.email, 'test@test.com') + + def test_update_profile(self): + profile, _ = Profile.objects.get_or_create(user=self.user) + # Create a Project instance and add it to the profile's projects + project = Project.objects.create(name='Test Project') + profile.projects.add(project) + # Verify the profile has the project added + self.assertIn(project, profile.projects.all()) + + + def test_delete_profile(self): + profile = Profile.objects.get(user=self.user) + profile_id = profile.id + profile.delete() + with self.assertRaises(Profile.DoesNotExist): + Profile.objects.get(id=profile_id) diff --git a/src/growth_forge/users/managers.py b/src/growth_forge/users/managers.py index 5d832aa..91be61f 100644 --- a/src/growth_forge/users/managers.py +++ b/src/growth_forge/users/managers.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import TYPE_CHECKING - +from typing import Optional from django.contrib.auth.hashers import make_password from django.contrib.auth.models import UserManager as DjangoUserManager @@ -14,7 +14,7 @@ class UserManager(DjangoUserManager['User']): """Custom manager for the User model.""" def _create_user( - self, email: str, password: str | None, **extra_fields + self, email: str, password: str, username: Optional[str] = None, **extra_fields ) -> User: """ Create and save a user with the given email and password. @@ -23,7 +23,7 @@ def _create_user( msg = 'The given email must be set' raise ValueError(msg) email = self.normalize_email(email) - user = self.model(email=email, **extra_fields) + user = self.model(email=email) user.password = make_password(password) user.save(using=self._db) return user @@ -36,7 +36,7 @@ def create_user( ): # type: ignore[override] extra_fields.setdefault('is_staff', False) extra_fields.setdefault('is_superuser', False) - return self._create_user(email, password, **extra_fields) + return self._create_user(email, password) def create_superuser( self, diff --git a/src/growth_plan/tests.py b/src/growth_plan/tests.py deleted file mode 100644 index a39b155..0000000 --- a/src/growth_plan/tests.py +++ /dev/null @@ -1 +0,0 @@ -# Create your tests here. diff --git a/src/growth_plan/tests/__init__.py b/src/growth_plan/tests/__init__.py index e69de29..f853b10 100644 --- a/src/growth_plan/tests/__init__.py +++ b/src/growth_plan/tests/__init__.py @@ -0,0 +1 @@ +from tests import * \ No newline at end of file diff --git a/src/growth_plan/tests/test_models.py b/src/growth_plan/tests/test_models.py index 1d2e98b..1a0c868 100644 --- a/src/growth_plan/tests/test_models.py +++ b/src/growth_plan/tests/test_models.py @@ -1 +1,58 @@ -#test models code yet to be inserted \ No newline at end of file +#test models code yet to be inserted +#test models code yet to be inserted +from django.test import TestCase +from django.contrib.auth import get_user_model +from growth_plan.models import GrowthPlanItem +from django.utils import timezone +from datetime import timedelta + +User = get_user_model() + +class GrowthPlanItemTests(TestCase): + + def setUp(self): + # Create a user for testing + self.user = User.objects.create_user( + # username='testuser', + password='testpassword', + email='test@test.com' + ) + self.client.login( + # username='testuser', + password='testpassword', + email='test@test.com' + ) + + # Common data for test cases + self.growth_plan_item_data = { + 'user': self.user, + 'title': 'Test Growth Plan', + 'description': 'This is a test description.', + 'start_date': timezone.now().date(), + 'end_date': (timezone.now() + timedelta(days=10)).date(), + 'progress_percentage': 50, + } + + def test_create_growth_plan_item(self): + growth_plan_item = GrowthPlanItem.objects.create(**self.growth_plan_item_data) + self.assertEqual(GrowthPlanItem.objects.count(), 1) + self.assertEqual(growth_plan_item.title, 'Test Growth Plan') + + def test_retrieve_growth_plan_item(self): + growth_plan_item = GrowthPlanItem.objects.create(**self.growth_plan_item_data) + retrieved_item = GrowthPlanItem.objects.get(id=growth_plan_item.id) + self.assertEqual(retrieved_item.title, growth_plan_item.title) + + def test_update_growth_plan_item(self): + growth_plan_item = GrowthPlanItem.objects.create(**self.growth_plan_item_data) + growth_plan_item.title = 'Updated Title' + growth_plan_item.save() + updated_item = GrowthPlanItem.objects.get(id=growth_plan_item.id) + self.assertEqual(updated_item.title, 'Updated Title') + + def test_delete_growth_plan_item(self): + growth_plan_item = GrowthPlanItem.objects.create(**self.growth_plan_item_data) + growth_plan_item_id = growth_plan_item.id + growth_plan_item.delete() + with self.assertRaises(GrowthPlanItem.DoesNotExist): + GrowthPlanItem.objects.get(id=growth_plan_item_id) diff --git a/src/one_on_one/tests.py b/src/one_on_one/tests.py deleted file mode 100644 index a39b155..0000000 --- a/src/one_on_one/tests.py +++ /dev/null @@ -1 +0,0 @@ -# Create your tests here. diff --git a/src/one_on_one/tests/test_models.py b/src/one_on_one/tests/test_models.py index 1d2e98b..724c07c 100644 --- a/src/one_on_one/tests/test_models.py +++ b/src/one_on_one/tests/test_models.py @@ -1 +1,57 @@ -#test models code yet to be inserted \ No newline at end of file +#test models code yet to be inserted +from django.test import TestCase +from django.contrib.auth import get_user_model +from djf_surveys.models import Survey +from one_on_one.models import Link + +User = get_user_model() + +class LinkTests(TestCase): + + def setUp(self): + # Create users for testing + self.mentor = User.objects.create_user(email='mentor@example.com', password='testpassword') + self.mentee = User.objects.create_user(email='mentee@example.com', password='testpassword') + + # Create surveys for testing + self.mentor_survey = Survey.objects.create(name='Mentor Survey') + self.mentee_survey = Survey.objects.create(name='Mentee Survey') + + # Common data for test cases + self.link_data = { + 'mentor': self.mentor, + 'mentee': self.mentee, + 'mentor_survey': self.mentor_survey, + 'mentee_survey': self.mentee_survey, + 'periodicity': 'weekly', + 'times': 5, + } + + def test_create_link(self): + link = Link.objects.create(**self.link_data) + self.assertEqual(Link.objects.count(), 1) + self.assertEqual(link.mentor.email, 'mentor@example.com') + self.assertEqual(link.mentee.email, 'mentee@example.com') + self.assertEqual(link.periodicity, 'weekly') + + def test_retrieve_link(self): + link = Link.objects.create(**self.link_data) + retrieved_link = Link.objects.get(id=link.id) + self.assertEqual(retrieved_link.mentor.email, 'mentor@example.com') + self.assertEqual(retrieved_link.mentee.email, 'mentee@example.com') + + def test_update_link(self): + link = Link.objects.create(**self.link_data) + link.periodicity = 'monthly' + link.times = 10 + link.save() + updated_link = Link.objects.get(id=link.id) + self.assertEqual(updated_link.periodicity, 'monthly') + self.assertEqual(updated_link.times, 10) + + def test_delete_link(self): + link = Link.objects.create(**self.link_data) + link_id = link.id + link.delete() + with self.assertRaises(Link.DoesNotExist): + Link.objects.get(id=link_id) diff --git a/src/projects/tests.py b/src/projects/tests.py deleted file mode 100644 index a39b155..0000000 --- a/src/projects/tests.py +++ /dev/null @@ -1 +0,0 @@ -# Create your tests here. diff --git a/src/projects/tests/test_models.py b/src/projects/tests/test_models.py index e69de29..1cec5c4 100644 --- a/src/projects/tests/test_models.py +++ b/src/projects/tests/test_models.py @@ -0,0 +1,52 @@ +from django.test import TestCase +from django.contrib.auth import get_user_model +from projects.models import Project + +User = get_user_model() + +class ProjectTests(TestCase): + + def setUp(self): + # Create users for testing + self.user1 = User.objects.create_user(email='user1@example.com', password='testpassword') + self.user2 = User.objects.create_user(email='user2@example.com', password='testpassword') + + # Common data for test cases + self.project_data = { + 'name': 'Test Project', + } + + def test_create_project(self): + project = Project.objects.create(**self.project_data) + project.observers.add(self.user1, self.user2) + self.assertEqual(Project.objects.count(), 1) + self.assertEqual(project.name, 'Test Project') + self.assertIn(self.user1, project.observers.all()) + self.assertIn(self.user2, project.observers.all()) + + def test_retrieve_project(self): + project = Project.objects.create(**self.project_data) + project.observers.add(self.user1, self.user2) + retrieved_project = Project.objects.get(id=project.id) + self.assertEqual(retrieved_project.name, 'Test Project') + self.assertIn(self.user1, retrieved_project.observers.all()) + self.assertIn(self.user2, retrieved_project.observers.all()) + + def test_update_project(self): + project = Project.objects.create(**self.project_data) + project.observers.add(self.user1) + project.name = 'Updated Project' + project.observers.remove(self.user1) + project.observers.add(self.user2) + project.save() + updated_project = Project.objects.get(id=project.id) + self.assertEqual(updated_project.name, 'Updated Project') + self.assertNotIn(self.user1, updated_project.observers.all()) + self.assertIn(self.user2, updated_project.observers.all()) + + def test_delete_project(self): + project = Project.objects.create(**self.project_data) + project_id = project.id + project.delete() + with self.assertRaises(Project.DoesNotExist): + Project.objects.get(id=project_id) \ No newline at end of file