From 06cc3dc5d3671ca24fc412b6078906a582b83e98 Mon Sep 17 00:00:00 2001 From: prathamesh Date: Tue, 5 Dec 2017 19:33:31 +0530 Subject: Added basic model testcases. --- yaksh/test_models.py | 215 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 198 insertions(+), 17 deletions(-) (limited to 'yaksh/test_models.py') diff --git a/yaksh/test_models.py b/yaksh/test_models.py index 74c9f14..9174033 100644 --- a/yaksh/test_models.py +++ b/yaksh/test_models.py @@ -1,7 +1,8 @@ import unittest from yaksh.models import User, Profile, Question, Quiz, QuestionPaper,\ QuestionSet, AnswerPaper, Answer, Course, StandardTestCase,\ - StdIOBasedTestCase, FileUpload, McqTestCase, AssignmentUpload + StdIOBasedTestCase, FileUpload, McqTestCase, AssignmentUpload,\ + LearningModule, LearningUnit, Lesson, LessonFile from yaksh.code_server import(ServerPool, get_result as get_result_from_code_server ) @@ -23,7 +24,7 @@ from yaksh import settings def setUpModule(): # create user profile - user = User.objects.create_user(username='demo_user', + user = User.objects.create_user(username='creator', password='demo', email='demo@test.com') User.objects.create_user(username='demo_user2', @@ -75,6 +76,30 @@ def setUpModule(): with open(tmp_file1, 'wb') as f: f.write('2'.encode('ascii')) + # Learing module + learning_module_one = LearningModule.objects.create(name='LM1', + description='module one', + creator=user) + learning_module_two = LearningModule.objects.create(name='LM2', + description='module two', + creator=user, + order=1) + lesson = Lesson.objects.create(name='L1', description='Video Lesson', + creator=user) + learning_unit_lesson = LearningUnit.objects.create(order=1, lesson=lesson, + learning_type='lesson') + learning_unit_quiz = LearningUnit.objects.create(order=2, quiz=quiz, + learning_type='quiz') + learning_module_one.learning_unit.add(learning_unit_lesson) + learning_module_one.learning_unit.add(learning_unit_quiz) + learning_module_one.save() + course.learning_module.add(learning_module_one) + course.learning_module.add(learning_module_two) + course_user = User.objects.create(username='course_user') + course.students.add(course_user) + course.save() + LessonFile.objects.create(lesson=lesson) + def tearDownModule(): User.objects.all().delete() @@ -82,19 +107,133 @@ def tearDownModule(): Quiz.objects.all().delete() Course.objects.all().delete() QuestionPaper.objects.all().delete() + LessonFile.objects.all().delete() + Lesson.objects.all().delete() + LearningUnit.objects.all().delete() + LearningModule.objects.all().delete() ############################################################################### +class LessonTestCases(unittest.TestCase): + def setUp(self): + self.lesson = Lesson.objects.get(name='L1') + self.creator = User.objects.get(username='creator') + + def test_lesson(self): + self.assertEqual(self.lesson.name, 'L1') + self.assertEqual(self.lesson.description, 'Video Lesson') + self.assertEqual(self.lesson.creator.username, self.creator.username) + + +class LearningModuleTestCases(unittest.TestCase): + def setUp(self): + self.learning_module = LearningModule.objects.get(name='LM1') + self.learning_module_two = LearningModule.objects.get(name='LM2') + self.creator = User.objects.get(username='creator') + self.student = User.objects.get(username='course_user') + self.learning_unit_one = LearningUnit.objects.get(order=1) + self.learning_unit_two = LearningUnit.objects.get(order=2) + self.quiz = Quiz.objects.get(description='demo quiz 1') + self.lesson = Lesson.objects.get(name='L1') + self.course = Course.objects.get(name='Python Course') + + def test_learning_module(self): + self.assertEqual(self.learning_module.description, 'module one') + self.assertEqual(self.learning_module.creator, self.creator) + self.assertTrue(self.learning_module.check_prerequisite) + self.assertEqual(self.learning_module.order, 0) + + def test_get_quiz_units(self): + # Given + quizzes = [self.quiz] + # When + module_quizzes = self.learning_module.get_quiz_units() + # Then + self.assertSequenceEqual(module_quizzes, quizzes) + + def test_get_learning_units(self): + # Given + learning_units = [self.learning_unit_one, self.learning_unit_two] + # When + module_units = self.learning_module.get_learning_units() + # Then + self.assertSequenceEqual(module_units, learning_units) + + def test_get_added_quiz_lesson(self): + # Given + quiz_lessons = [('lesson', self.lesson), ('quiz', self.quiz)] + # When + module_quiz_lesson = self.learning_module.get_added_quiz_lesson() + # Then + self.assertEqual(module_quiz_lesson, quiz_lessons) + + def test_toggle_check_prerequisite(self): + self.assertTrue(self.learning_module.check_prerequisite) + # When + self.learning_module.toggle_check_prerequisite() + # Then + self.assertFalse(self.learning_module.check_prerequisite) + + # When + self.learning_module.toggle_check_prerequisite() + # Then + self.assertTrue(self.learning_module.check_prerequisite) + + def test_get_next_unit(self): + # Given + current_unit_id = self.learning_unit_one.id + next_unit = self.learning_unit_two + # When + unit = self.learning_module.get_next_unit(current_unit_id) + # Then + self.assertEqual(unit, next_unit) + + # Given + current_unit_id = self.learning_unit_two.id + next_unit = self.learning_unit_one + # When + unit = self.learning_module.get_next_unit(current_unit_id) + # Then + self.assertEqual(unit, next_unit) + + def test_get_status(self): + # Given + module_status = 'not attempted' + # When + status = self.learning_module.get_status(self.student, self.course) + # Then + self.assertEqual(status, module_status) + + +class LearningUnitTestCases(unittest.TestCase): + def setUp(self): + learning_module = LearningModule.objects.get(name='LM1') + self.learning_unit_one = learning_module.learning_unit.get(order=1) + self.learning_unit_two = learning_module.learning_unit.get(order=2) + self.lesson = Lesson.objects.get(name='L1') + self.quiz = Quiz.objects.get(description='demo quiz 1') + + def test_learning_unit(self): + self.assertEqual(self.learning_unit_one.learning_type, 'lesson') + self.assertEqual(self.learning_unit_two.learning_type, 'quiz') + self.assertEqual(self.learning_unit_one.lesson, self.lesson) + self.assertEqual(self.learning_unit_two.quiz, self.quiz) + self.assertIsNone(self.learning_unit_one.quiz) + self.assertIsNone(self.learning_unit_two.lesson) + self.assertTrue(self.learning_unit_one.check_prerequisite) + self.assertTrue(self.learning_unit_two.check_prerequisite) + + class ProfileTestCases(unittest.TestCase): def setUp(self): - self.user1 = User.objects.get(username="demo_user") + self.user1 = User.objects.get(username='creator') self.profile = Profile.objects.get(user=self.user1) self.user2 = User.objects.get(username='demo_user3') def test_user_profile(self): """ Test user profile""" - self.assertEqual(self.user1.username, 'demo_user') - self.assertEqual(self.profile.user.username, 'demo_user') + self.assertEqual(self.user1.username, 'creator') + self.assertEqual(self.profile.user.username, 'creator') self.assertEqual(int(self.profile.roll_number), 1) self.assertEqual(self.profile.institute, 'IIT') self.assertEqual(self.profile.department, 'Chemical') @@ -104,7 +243,7 @@ class ProfileTestCases(unittest.TestCase): class QuestionTestCases(unittest.TestCase): def setUp(self): # Single question details - self.user1 = User.objects.get(username="demo_user") + self.user1 = User.objects.get(username="creator") self.user2 = User.objects.get(username="demo_user2") self.question1 = Question.objects.create(summary='Demo Python 1', language='Python', @@ -288,7 +427,7 @@ class QuestionTestCases(unittest.TestCase): class QuizTestCases(unittest.TestCase): def setUp(self): self.course = Course.objects.get(name="Python Course") - self.creator = User.objects.get(username="demo_user") + self.creator = User.objects.get(username="creator") self.teacher = User.objects.get(username="demo_user2") self.quiz1 = Quiz.objects.get(description='demo quiz 1') self.quiz2 = Quiz.objects.get(description='demo quiz 2') @@ -446,7 +585,7 @@ class QuestionPaperTestCases(unittest.TestCase): # ip address for AnswerPaper self.ip = '127.0.0.1' - self.user = User.objects.get(username="demo_user") + self.user = User.objects.get(username="creator") self.attempted_papers = AnswerPaper.objects.filter( question_paper=self.question_paper, @@ -588,7 +727,7 @@ class AnswerPaperTestCases(unittest.TestCase): def setUpClass(self): self.course = Course.objects.get(name="Python Course") self.ip = '101.0.0.1' - self.user = User.objects.get(username='demo_user') + self.user = User.objects.get(username='creator') self.user2 = User.objects.get(username='demo_user2') self.profile = self.user.profile self.quiz = Quiz.objects.get(description='demo quiz 1') @@ -986,7 +1125,7 @@ class AnswerPaperTestCases(unittest.TestCase): def test_answerpaper(self): """ Test Answer Paper""" - self.assertEqual(self.answerpaper.user.username, 'demo_user') + self.assertEqual(self.answerpaper.user.username, 'creator') self.assertEqual(self.answerpaper.user_ip, self.ip) questions = self.answerpaper.get_questions() num_questions = len(questions) @@ -1159,13 +1298,15 @@ class AnswerPaperTestCases(unittest.TestCase): class CourseTestCases(unittest.TestCase): def setUp(self): self.course = Course.objects.get(name="Python Course") - self.creator = User.objects.get(username="demo_user") + self.creator = User.objects.get(username="creator") self.template_course_user = User.objects.get(username="demo_user4") + self.student = User.objects.get(username="course_user") self.student1 = User.objects.get(username="demo_user2") self.student2 = User.objects.get(username="demo_user3") self.quiz1 = Quiz.objects.get(description='demo quiz 1') self.quiz2 = Quiz.objects.get(description='demo quiz 2') self.questions = Question.objects.filter(active=True) + self.modules = LearningModule.objects.filter(creator=self.creator) # create courses with disabled enrollment self.enroll_request_course = Course.objects.create( @@ -1253,6 +1394,44 @@ class CourseTestCases(unittest.TestCase): self.questions[3] ) + def test_get_learning_modules(self): + # Given + modules = list(self.modules) + # When + course_modules = self.course.get_learning_modules() + # Then + self.assertSequenceEqual(list(course_modules), modules) + + # Given + modules = list(self.modules.filter(name='LM1')) + module_to_remove = self.modules.get(name='LM2') + # When + self.course.learning_module.remove(module_to_remove) + course_modules = self.course.get_learning_modules() + # Then + self.assertSequenceEqual(list(course_modules), modules) + + def test_get_quizzes(self): + # Given + quizzes = [self.quiz1] + # When + course_quizzes = self.course.get_quizzes() + # Then + self.assertSequenceEqual(course_quizzes, quizzes) + + def test_get_learning_units(self): + # Given + lesson = Lesson.objects.get(name='L1') + self.learning_unit_one = LearningUnit.objects.get(order=1, + lesson=lesson) + self.learning_unit_two = LearningUnit.objects.get(order=2, + quiz=self.quiz1) + learning_units = [self.learning_unit_one, self.learning_unit_two] + # When + course_learning_units = self.course.get_learning_units() + # Then + self.assertSequenceEqual(course_learning_units, learning_units) + def test_is_creator(self): """ Test is_creator method of Course""" self.assertTrue(self.course.is_creator(self.creator)) @@ -1279,10 +1458,11 @@ class CourseTestCases(unittest.TestCase): def test_enroll_reject(self): """ Test enroll, reject, get_enrolled and get_rejected methods""" - self.assertSequenceEqual(self.course.get_enrolled(), []) + self.assertSequenceEqual(self.course.get_enrolled(), [self.student]) was_rejected = False self.course.enroll(was_rejected, self.student1) - self.assertSequenceEqual(self.course.get_enrolled(), [self.student1]) + self.assertSequenceEqual(self.course.get_enrolled(), + [self.student1, self.student]) self.assertSequenceEqual(self.course.get_rejected(), []) was_enrolled = False @@ -1292,13 +1472,14 @@ class CourseTestCases(unittest.TestCase): was_rejected = True self.course.enroll(was_rejected, self.student2) self.assertSequenceEqual(self.course.get_enrolled(), - [self.student1, self.student2]) + [self.student1, self.student2, self.student]) 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.assertSequenceEqual(self.course.get_enrolled(), + [self.student1, self.student]) self.assertTrue(self.course.is_enrolled(self.student1)) @@ -1347,7 +1528,7 @@ class CourseTestCases(unittest.TestCase): ############################################################################### class TestCaseTestCases(unittest.TestCase): def setUp(self): - self.user = User.objects.get(username="demo_user") + self.user = User.objects.get(username="creator") self.question1 = Question(summary='Demo question 1', language='Python', type='Code', @@ -1419,7 +1600,7 @@ class TestCaseTestCases(unittest.TestCase): class AssignmentUploadTestCases(unittest.TestCase): def setUp(self): - self.user1 = User.objects.get(username="demo_user") + self.user1 = User.objects.get(username="creator") self.user1.first_name = "demo" self.user1.last_name = "user" self.user1.save() -- cgit