Skip to content

Commit

Permalink
models test successful
Browse files Browse the repository at this point in the history
  • Loading branch information
investor-uyah committed Jun 25, 2024
1 parent d564098 commit a57e32a
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 10 deletions.
57 changes: 56 additions & 1 deletion src/growth_forge/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
#test models code yet to be inserted
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='[email protected]'
)
cls.user2 = User.objects.create_user(email='[email protected]', 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,
'[email protected]'
)

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, '[email protected]')

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)
8 changes: 4 additions & 4 deletions src/growth_forge/users/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/growth_plan/tests.py

This file was deleted.

1 change: 1 addition & 0 deletions src/growth_plan/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from tests import *
59 changes: 58 additions & 1 deletion src/growth_plan/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1 +1,58 @@
#test models code yet to be inserted
#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='[email protected]'
)
self.client.login(
# username='testuser',
password='testpassword',
email='[email protected]'
)

# 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)
1 change: 0 additions & 1 deletion src/one_on_one/tests.py

This file was deleted.

58 changes: 57 additions & 1 deletion src/one_on_one/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
#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 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='[email protected]', password='testpassword')
self.mentee = User.objects.create_user(email='[email protected]', 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, '[email protected]')
self.assertEqual(link.mentee.email, '[email protected]')
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, '[email protected]')
self.assertEqual(retrieved_link.mentee.email, '[email protected]')

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)
1 change: 0 additions & 1 deletion src/projects/tests.py

This file was deleted.

52 changes: 52 additions & 0 deletions src/projects/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -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='[email protected]', password='testpassword')
self.user2 = User.objects.create_user(email='[email protected]', 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)

0 comments on commit a57e32a

Please sign in to comment.