diff options
author | prathamesh | 2016-03-07 15:29:36 +0530 |
---|---|---|
committer | prathamesh | 2016-03-07 15:29:36 +0530 |
commit | b58a0b9831d8e6434362f776c89076c2d84e9b26 (patch) | |
tree | b9580710f6ecb2102b8c20548a60af68a004b1de | |
parent | d9793c01304498d7f59820cf2ab2d7a5483851f8 (diff) | |
download | online_test-b58a0b9831d8e6434362f776c89076c2d84e9b26.tar.gz online_test-b58a0b9831d8e6434362f776c89076c2d84e9b26.tar.bz2 online_test-b58a0b9831d8e6434362f776c89076c2d84e9b26.zip |
Added tests for Course model
-rw-r--r-- | yaksh/models.py | 29 | ||||
-rw-r--r-- | yaksh/tests.py | 105 | ||||
-rw-r--r-- | yaksh/views.py | 3 |
3 files changed, 106 insertions, 31 deletions
diff --git a/yaksh/models.py b/yaksh/models.py index d6fe972..46fb9a3 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -62,6 +62,7 @@ def get_assignment_dir(instance, filename): ############################################################################### class Course(models.Model): + """ Course for students""" name = models.CharField(max_length=128) enrollment = models.CharField(max_length=32, choices=enrollment_methods) active = models.BooleanField(default=True) @@ -77,17 +78,7 @@ class Course(models.Model): def get_requests(self): return self.requests.all() - def reject(self, was_enrolled=False, *users): - self.rejected.add(*users) - if not was_enrolled: - self.requests.remove(*users) - else: - self.students.remove(*users) - - def get_rejected(self): - return self.rejected.all() - - def enroll(self, was_rejected=False, *users): + def enroll(self, was_rejected, *users): self.students.add(*users) if not was_rejected: self.requests.remove(*users) @@ -97,12 +88,25 @@ class Course(models.Model): def get_enrolled(self): return self.students.all() + def reject(self, was_enrolled, *users): + self.rejected.add(*users) + if not was_enrolled: + self.requests.remove(*users) + else: + self.students.remove(*users) + + def get_rejected(self): + return self.rejected.all() + def is_enrolled(self, user_id): return self.students.filter(id=user_id).exists() def is_creator(self, user): return self.creator == user + def is_self_enroll(self): + return True if self.enrollment == enrollment_methods[1][0] else False + def get_quizzes(self): return self.quiz_set.all() @@ -112,9 +116,6 @@ class Course(models.Model): def deactivate(self): self.active = False - def is_self_enroll(self): - return True if self.enrollment == 'open' else False - def __unicode__(self): return self.name diff --git a/yaksh/tests.py b/yaksh/tests.py index 9c67c02..17e6130 100644 --- a/yaksh/tests.py +++ b/yaksh/tests.py @@ -1,8 +1,9 @@ from django.utils import unittest from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ - QuestionSet, AnswerPaper, Answer, TestCase + QuestionSet, AnswerPaper, Answer, TestCase, Course import datetime, json + def setUpModule(): # create user profile user = User.objects.create_user(username='demo_user', @@ -13,6 +14,15 @@ def setUpModule(): email='demo@test.com') Profile.objects.create(user=user, roll_number=1, institute='IIT', department='Chemical', position='Student') + student = User.objects.create_user(username='demo_user3', + password='demo', + email='demo3@test.com') + Profile.objects.create(user=student, roll_number=3, institute='IIT', + department='Chemical', position='Student') + + # create a course + course = Course.objects.create(name="Python Course", + enrollment="Enroll Request", creator=user) # create 20 questions for i in range(1, 21): @@ -20,10 +30,11 @@ def setUpModule(): # create a quiz Quiz.objects.create(start_date_time=datetime.datetime(2015, 10, 9, 10, 8, 15, 0), - duration=30, active=False, - attempts_allowed=-1, time_between_attempts=0, - description='demo quiz', pass_criteria=40, - language='Python', prerequisite=None) + duration=30, active=False, + attempts_allowed=-1, time_between_attempts=0, + description='demo quiz', pass_criteria=40, + language='Python', prerequisite=None, + course=course) def tearDownModule(): @@ -62,14 +73,14 @@ class QuestionTestCases(unittest.TestCase): func_name='def myfunc', kw_args='a=10,b=11', pos_args='12,13', expected_answer='15') answer_data = { "test": "", - "user_answer": "demo_answer", - "test_parameter": [{"func_name": "def myfunc", - "expected_answer": "15", - "test_id": self.testcase.id, - "pos_args": ["12", "13"], - "kw_args": {"a": "10", + "user_answer": "demo_answer", + "test_parameter": [{"func_name": "def myfunc", + "expected_answer": "15", + "test_id": self.testcase.id, + "pos_args": ["12", "13"], + "kw_args": {"a": "10", "b": "11"} - }], + }], "id": self.question.id, "ref_code_path": "", } @@ -93,10 +104,9 @@ class QuestionTestCases(unittest.TestCase): def test_consolidate_answer_data(self): """ Test consolidate_answer_data function """ - result = self.question.consolidate_answer_data([self.testcase], + result = self.question.consolidate_answer_data([self.testcase], self.user_answer) self.assertEqual(result, self.answer_data_json) - ############################################################################### @@ -107,7 +117,7 @@ class TestCaseTestCases(unittest.TestCase): description='Write a function', points=1.0, snippet='def myfunc()') self.question.save() - self.testcase = TestCase(question=self.question, + self.testcase = TestCase(question=self.question, func_name='def myfunc', kw_args='a=10,b=11', pos_args='12,13', expected_answer='15') @@ -188,7 +198,7 @@ class QuestionPaperTestCases(unittest.TestCase): def test_questionpaper(self): """ Test question paper""" self.assertEqual(self.question_paper.quiz.description, 'demo quiz') - self.assertEqual(list(self.question_paper.fixed_questions.all()), + self.assertSequenceEqual(self.question_paper.fixed_questions.all(), [self.questions[3], self.questions[5]]) self.assertTrue(self.question_paper.shuffle_questions) @@ -350,3 +360,66 @@ class AnswerPaperTestCases(unittest.TestCase): self.assertEqual(self.answerpaper.status, 'inprogress') self.answerpaper.update_status('completed') self.assertEqual(self.answerpaper.status, 'completed') + + +############################################################################### +class CourseTestCases(unittest.TestCase): + def setUp(self): + self.course = Course.objects.get(pk=1) + self.creator = User.objects.get(pk=1) + self.student1 = User.objects.get(pk=2) + self.student2 = User.objects.get(pk=3) + self.quiz = Quiz.objects.get(pk=1) + + def test_is_creator(self): + """ Test is_creator method of Course""" + self.assertTrue(self.course.is_creator(self.creator)) + + def test_is_self_enroll(self): + """ Test is_self_enroll method of Course""" + self.assertFalse(self.course.is_self_enroll()) + + def test_deactivate(self): + """ Test deactivate method of Course""" + self.course.deactivate() + self.assertFalse(self.course.active) + + def test_activate(self): + """ Test activate method of Course""" + self.course.activate() + self.assertTrue(self.course.active) + + def test_request(self): + """ Test request and get_requests methods of Course""" + self.course.request(self.student1, self.student2) + self.assertSequenceEqual(self.course.get_requests(), + [self.student1, self.student2]) + + def test_enroll_reject(self): + """ Test enroll, reject, get_enrolled and get_rejected methods""" + self.assertSequenceEqual(self.course.get_enrolled(), []) + was_rejected = False + self.course.enroll(was_rejected, self.student1) + self.assertSequenceEqual(self.course.get_enrolled(), [self.student1]) + + self.assertSequenceEqual(self.course.get_rejected(), []) + was_enrolled = False + self.course.reject(was_enrolled, self.student2) + self.assertSequenceEqual(self.course.get_rejected(), [self.student2]) + + was_rejected = True + self.course.enroll(was_rejected, self.student2) + self.assertSequenceEqual(self.course.get_enrolled(), + [self.student1, self.student2]) + self.assertSequenceEqual(self.course.get_rejected(), []) + + was_enrolled = True + self.course.reject(was_enrolled, self.student2) + self.assertSequenceEqual(self.course.get_rejected(), [self.student2]) + self.assertSequenceEqual(self.course.get_enrolled(), [self.student1]) + + self.assertTrue(self.course.is_enrolled(self.student1.id)) + + def test_get_quizzes(self): + """ Test get_quizzes method of Courses""" + self.assertSequenceEqual(self.course.get_quizzes(), [self.quiz]) diff --git a/yaksh/views.py b/yaksh/views.py index 9540a06..ffe8d93 100644 --- a/yaksh/views.py +++ b/yaksh/views.py @@ -1206,7 +1206,8 @@ def self_enroll(request, course_id): ci = RequestContext(request) course = get_object_or_404(Course, pk=course_id) if course.is_self_enroll(): - course.enroll(False, user) + was_rejected = False + course.enroll(was_rejected, user) return my_redirect('/exam/manage/') |