Skip to content

Commit

Permalink
Added userprofile unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Osccu committed Apr 21, 2015
1 parent 3d3fd08 commit cfada69
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 18 deletions.
3 changes: 2 additions & 1 deletion exercise/exercise_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class LearningObjectCategory(models.Model):
UserProfile,
related_name="hidden_categories",
blank=True,
null=True)
null=True
)

class Meta:
unique_together = ("name", "course_instance")
Expand Down
3 changes: 0 additions & 3 deletions exercise/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Django
from django.test import TestCase
from django.test.client import Client
from django.utils.datastructures import MultiValueDict

# Aalto+
Expand All @@ -13,8 +12,6 @@

class ExerciseTest(TestCase):
def setUp(self):
self.client = Client()

self.user = User(username="testUser", first_name="First", last_name="Last")
self.user.set_password("testPassword")
self.user.save()
Expand Down
2 changes: 0 additions & 2 deletions notification/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from django.db import models
from userprofile.models import UserProfile
from course.models import *

class Notification(models.Model):
Expand Down
3 changes: 0 additions & 3 deletions userprofile/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
from django.middleware.common import CommonMiddleware
from django.shortcuts import redirect
from django.http import HttpResponseForbidden, HttpResponseBadRequest
from django.contrib.auth.models import User

from userprofile import STUDENT_GROUP, STUDENT_GROUP_ID

Expand Down
18 changes: 9 additions & 9 deletions userprofile/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Django
from django.contrib.auth.models import User
from django.db import models
from django.db.models.signals import post_save
from django.db.models import Q
from django.db.models.signals import post_save


class UserProfile(models.Model):
Expand Down Expand Up @@ -34,7 +34,9 @@ def get_shortname(self):
return self.user.username

shortname = property(get_shortname)


# TODO: REFACTOR - Why is an import within the method? Is it there on purpose (instead of at the top of the file as usual)
# TODO: REFACTOR - Also, the method name is misleading. One could assume it returns a set of users (staff)
def get_courseinstance_staff_queryset(self):
from course.models import CourseInstance
return CourseInstance.objects.filter( Q(assistants__id=self.id) | Q(course__teachers__id=self.id) )
Expand All @@ -55,13 +57,11 @@ def get_unread_notification_course_instances(self):
class Meta:
ordering = ['id']

# TODO: Remove this method and replace all usages with prefetch_related
# when updating to newer Django version.
# TODO: Remove this method and replace all usages with prefetch_related when updating to newer Django version.
def reset_hidden_categories_cache(self):
self.cached_hidden_categories = self.hidden_categories.all()

# TODO: Remove this method and replace all usages with prefetch_related
# when updating to newer Django version.
# TODO: Remove this method and replace all usages with prefetch_related when updating to newer Django version.
def get_hidden_categories_cache(self):
self.cached_hidden_categories = getattr(self, "cached_hidden_categories", self.hidden_categories.all())
return self.cached_hidden_categories
Expand Down Expand Up @@ -95,7 +95,8 @@ def get_names(self):

def has_space_left(self):
return self.members.count() < self.member_limit


# TODO: REFACTOR - Should this method return True if the member already is in the group? Currently it does, but the member count does not increase
def add_member(self, new_member):
if self.members.count() >= self.member_limit:
return False
Expand All @@ -120,8 +121,7 @@ def get_students_from_request(cls, request):
"""
# Get the group of the student, if one is set in the request's META
student_group = request.META.get("STUDENT_GROUP", None)
students = None


if student_group:
students = student_group.members.all()
else:
Expand Down
276 changes: 276 additions & 0 deletions userprofile/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
from django.test import TestCase
from django.http import HttpRequest

from userprofile.models import *
from notification.models import *
from exercise.exercise_models import *

from datetime import datetime, timedelta

class UserProfileTest(TestCase):
def setUp(self):
self.student = User(username="testUser", first_name="Superb", last_name="Student", email="test@aplus.com")
self.student.set_password("testPassword")
self.student.save()
self.student_profile = self.student.get_profile()
self.student_profile.student_id = "12345X"
self.student_profile.save()

self.grader = User(username="grader", first_name="Grumpy", last_name="Grader", email="grader@aplus.com")
self.grader.set_password("graderPassword")
self.grader.save()
self.grader_profile = self.grader.get_profile()
self.grader_profile.student_id = "67890Y"
self.grader_profile.save()

self.teacher = User(username="teacher", first_name="Tedious", last_name="Teacher", email="teacher@aplus.com", is_staff=True)
self.teacher.set_password("teacherPassword")
self.teacher.save()
self.teacher_profile = self.teacher.get_profile()

self.superuser = User(username="superuser", first_name="Super", last_name="User", email="superuser@aplus.com", is_superuser=True)
self.superuser.set_password("superuserPassword")
self.superuser.save()
self.superuser_profile = self.superuser.get_profile()

self.student_group1 = StudentGroup.objects.create(
name="group1",
description="testGroup1",
member_limit=1,
is_public=False
)
self.student_group1.members.add(self.student_profile)

self.student_group2 = StudentGroup.objects.create(
name="group2",
description="testGroup2",
member_limit=3,
is_public=False
)
self.student_group2.members.add(self.student_profile)
self.student_group2.members.add(self.grader_profile)

self.student_group3 = StudentGroup.objects.create(
name="group3",
description="testGroup3",
member_limit=3,
is_public=False
)
self.student_group3.members.add(self.student_profile)
self.student_group3.members.add(self.grader_profile)

self.course = Course.objects.create(
name="test course",
code="123456",
url="Course-Url"
)
self.course.teachers.add(self.teacher.get_profile())

self.today = datetime.now()
self.yesterday = self.today - timedelta(days=1)
self.tomorrow = self.today + timedelta(days=1)
self.two_days_from_now = self.tomorrow + timedelta(days=1)
self.three_days_from_now = self.two_days_from_now + timedelta(days=1)

self.course_instance1 = CourseInstance.objects.create(
instance_name="Fall 2011 day 1",
website="http://www.example.com",
starting_time=self.today,
ending_time=self.tomorrow,
course=self.course,
url="T-00.1000_d1"
)
self.course_instance1.assistants.add(self.grader.get_profile())

self.course_instance2 = CourseInstance.objects.create(
instance_name="Fall 2011 day 2",
website="http://www.example.com",
starting_time=self.tomorrow,
ending_time=self.two_days_from_now,
course=self.course,
url="T-00.1000_d2"
)

self.notification1 = Notification.objects.create(
subject="test1",
notification="testNotification1",
sender=self.superuser_profile,
recipient=self.student_profile,
course_instance=self.course_instance1
)

self.notification2 = Notification.objects.create(
subject="test2",
notification="testNotification2",
sender=self.superuser_profile,
recipient=self.grader_profile,
course_instance=self.course_instance1
)

self.notification3 = Notification.objects.create(
subject="test3",
notification="testNotification3",
sender=self.superuser_profile,
recipient=self.student_profile,
course_instance=self.course_instance2
)

self.notification4 = Notification.objects.create(
subject="test4",
notification="testNotification4",
sender=self.superuser_profile,
recipient=self.grader_profile,
course_instance=self.course_instance1
)

self.notification5 = Notification.objects.create(
subject="test5",
notification="testNotification5",
sender=self.superuser_profile,
recipient=self.student_profile,
course_instance=self.course_instance2
)

self.learning_object_category1 = LearningObjectCategory.objects.create(
name="test category 1",
course_instance=self.course_instance1
)
self.learning_object_category1.hidden_to.add(self.student_profile)
self.learning_object_category1.hidden_to.add(self.grader_profile)

self.learning_object_category2 = LearningObjectCategory.objects.create(
name="test category 2",
course_instance=self.course_instance1
)
self.learning_object_category2.hidden_to.add(self.student_profile)

self.learning_object_category3 = LearningObjectCategory.objects.create(
name="test category 3",
course_instance=self.course_instance2
)

def test_userprofile_get_by_student_id(self):
self.assertEqual(self.student_profile, UserProfile.get_by_student_id("12345X"))
self.assertEqual(self.grader_profile, UserProfile.get_by_student_id("67890Y"))
self.assertRaises(UserProfile.DoesNotExist, UserProfile.get_by_student_id, "111111")

def test_userprofile_unicode_string(self):
self.assertEqual("testUser", str(self.student_profile))
self.assertEqual("grader", str(self.grader_profile))
self.assertEqual("teacher", str(self.teacher_profile))
self.assertEqual("superuser", str(self.superuser_profile))

def test_userprofile_gravatar_url(self):
self.assertEqual("http://www.gravatar.com/avatar/36eb57f675f34b81bd859c525cb2b676?d=identicon", self.student_profile._generate_gravatar_url())
self.assertEqual("http://www.gravatar.com/avatar/e2321e37326539393fbae72b7558df8e?d=identicon", self.grader_profile._generate_gravatar_url())
self.assertEqual("http://www.gravatar.com/avatar/1bfe4ecc42454c9c1dc02bf93073a414?d=identicon", self.teacher_profile._generate_gravatar_url())
self.assertEqual("http://www.gravatar.com/avatar/f35e575136edbfb920643d10560e8814?d=identicon", self.superuser_profile._generate_gravatar_url())

def test_userprofile_shortname(self):
self.assertEqual("Superb S.", self.student_profile.get_shortname())
self.assertEqual("Grumpy G.", self.grader_profile.get_shortname())
self.assertEqual("Tedious T.", self.teacher_profile.get_shortname())
self.assertEqual("Super U.", self.superuser_profile.get_shortname())

def test_userprofile_courseinstance_staff_queryset(self):
student_staff_courseinstances = self.student_profile.get_courseinstance_staff_queryset()
self.assertEqual(0, len(student_staff_courseinstances))
grader_staff_courseinstances = self.grader_profile.get_courseinstance_staff_queryset()
self.assertEqual(1, len(grader_staff_courseinstances))
self.assertEqual(self.course_instance1, grader_staff_courseinstances[0])
teacher_staff_courseinstances = self.teacher_profile.get_courseinstance_staff_queryset()
self.assertEqual(2, len(teacher_staff_courseinstances))
self.assertEqual(self.course_instance1, teacher_staff_courseinstances[0])
self.assertEqual(self.course_instance2, teacher_staff_courseinstances[1])

def test_userprofile_is_staff(self):
self.assertFalse(self.student_profile.is_staff())
self.assertFalse(self.grader_profile.is_staff())
self.assertTrue(self.teacher_profile.is_staff())
self.assertTrue(self.superuser_profile.is_staff())

def test_userprofile_unread_notification_count(self):
self.assertEqual(3, self.student_profile.get_unread_notification_count())
self.assertEqual(2, self.grader_profile.get_unread_notification_count())
self.assertEqual(0, self.teacher_profile.get_unread_notification_count())
self.assertEqual(0, self.superuser_profile.get_unread_notification_count())

def test_userprofile_unread_notification_course_instances(self):
self.assertEqual(2, len(self.student_profile.get_unread_notification_course_instances()))
self.assertEqual(1, len(self.grader_profile.get_unread_notification_course_instances()))
self.assertEqual(0, len(self.teacher_profile.get_unread_notification_course_instances()))
self.assertEqual(0, len(self.superuser_profile.get_unread_notification_course_instances()))

def test_userprofile_reset_hidden_categories_cache(self):
self.student_profile.reset_hidden_categories_cache()
self.assertEqual(2, len(self.student_profile.cached_hidden_categories))
self.assertEqual(self.learning_object_category1, self.student_profile.cached_hidden_categories[0])
self.assertEqual(self.learning_object_category2, self.student_profile.cached_hidden_categories[1])

self.grader_profile.reset_hidden_categories_cache()
self.assertEqual(1, len(self.grader_profile.cached_hidden_categories))
self.assertEqual(self.learning_object_category1, self.grader_profile.cached_hidden_categories[0])

self.teacher_profile.reset_hidden_categories_cache()
self.assertEqual(0, len(self.teacher_profile.cached_hidden_categories))

self.superuser_profile.reset_hidden_categories_cache()
self.assertEqual(0, len(self.superuser_profile.cached_hidden_categories))


def test_userprofile_hidden_categories_cache(self):
student_hidden_categories_cache = self.student_profile.get_hidden_categories_cache()
self.assertEqual(2, len(student_hidden_categories_cache))
self.assertEqual(self.learning_object_category1, student_hidden_categories_cache[0])
self.assertEqual(self.learning_object_category2, student_hidden_categories_cache[1])

grader_hidden_categories_cache = self.grader_profile.get_hidden_categories_cache()
self.assertEqual(1, len(grader_hidden_categories_cache))
self.assertEqual(self.learning_object_category1, grader_hidden_categories_cache[0])

self.assertEqual(0, len(self.teacher_profile.get_hidden_categories_cache()))

self.assertEqual(0, len(self.superuser_profile.get_hidden_categories_cache()))

def test_studentgroup_names(self):
self.assertEqual("group1", str(self.student_group1))
self.assertEqual("group2", str(self.student_group2))
self.assertEqual("group3", str(self.student_group3))

def test_studentgroup_names(self):
self.assertEqual("Superb S.", self.student_group1.get_names())
self.assertEqual("Superb S., Grumpy G.", self.student_group2.get_names())
self.assertEqual("Superb S., Grumpy G.", self.student_group3.get_names())

def test_studentgroup_has_space_left(self):
self.assertFalse(self.student_group1.has_space_left())
self.assertTrue(self.student_group2.has_space_left())
self.assertTrue(self.student_group3.has_space_left())

def test_studentgroup_add_member(self):
self.assertEqual(1, len(self.student_group1.members.all()))
self.assertFalse(self.student_group1.add_member(self.teacher_profile))
self.assertEqual(1, len(self.student_group1.members.all()))

self.assertEqual(2, len(self.student_group2.members.all()))
self.assertTrue(self.student_group2.add_member(self.teacher_profile))
self.assertEqual(3, len(self.student_group2.members.all()))

self.assertEqual(2, len(self.student_group3.members.all()))
self.assertTrue(self.student_group3.add_member(self.student_profile))
self.assertEqual(2, len(self.student_group3.members.all()))

def test_studentgroup_students_from_request(self):
requestWithGroup = HttpRequest()
requestWithGroup.user = self.student
requestWithGroup.META["STUDENT_GROUP"] = self.student_group2
studentsFromRequestWithGroup = StudentGroup.get_students_from_request(requestWithGroup)
self.assertEqual(2, len(studentsFromRequestWithGroup))
self.assertEqual(self.student_profile, studentsFromRequestWithGroup[0])
self.assertEqual(self.grader_profile, studentsFromRequestWithGroup[1])

requestWithoutGroup = HttpRequest()
requestWithoutGroup.user = self.student
studentsFromRequestWithoutGroup = StudentGroup.get_students_from_request(requestWithoutGroup)
self.assertEqual(1, len(studentsFromRequestWithoutGroup))
self.assertEqual(self.student_profile, studentsFromRequestWithoutGroup[0])

0 comments on commit cfada69

Please sign in to comment.