diff options
author | adityacp | 2020-12-04 09:56:25 +0530 |
---|---|---|
committer | adityacp | 2020-12-04 09:56:25 +0530 |
commit | c5b26a3b0d7e29f9813aa4fcdcbcaaa66945b86d (patch) | |
tree | 24bcd6fde15cc5843632a400a34c87361b52d9c6 /stats | |
parent | 882fe5cdaee8bc61e51f6626cc3941356905bb61 (diff) | |
parent | 56883784cdcd2a4f1a60cdaa7ba28525cfd21132 (diff) | |
download | online_test-c5b26a3b0d7e29f9813aa4fcdcbcaaa66945b86d.tar.gz online_test-c5b26a3b0d7e29f9813aa4fcdcbcaaa66945b86d.tar.bz2 online_test-c5b26a3b0d7e29f9813aa4fcdcbcaaa66945b86d.zip |
Resolve conflicts in models
Diffstat (limited to 'stats')
-rw-r--r-- | stats/models.py | 18 | ||||
-rw-r--r-- | stats/test_models.py | 184 |
2 files changed, 194 insertions, 8 deletions
diff --git a/stats/models.py b/stats/models.py index 60bc7bd..84ac7ae 100644 --- a/stats/models.py +++ b/stats/models.py @@ -75,16 +75,17 @@ class TrackLesson(models.Model): self.current_time = ct def get_percentage_complete(self): - if self.current_time == '00:00:00' and self.video_duration == '00:00:00': + ctime = self.current_time + vduration = self.video_duration + if ctime == '00:00:00' and vduration == '00:00:00': return 0 - duration = str_to_time(self.video_duration) - watch_time = str_to_time(self.current_time) + duration = str_to_time(vduration) + watch_time = str_to_time(ctime) duration_seconds = time_to_seconds(duration) watched_seconds = time_to_seconds(watch_time) percentage = round((watched_seconds / duration_seconds) * 100) return percentage - def get_last_access_time(self): lesson_logs = self.lessonlog_set last_access_time = self.creation_time @@ -93,9 +94,11 @@ class TrackLesson(models.Model): return last_access_time def set_watched(self): - if self.current_time != '00:00:00' and self.video_duration != '00:00:00': - duration = str_to_time(self.video_duration) - watch_time = (str_to_datetime(self.current_time) + timezone.timedelta( + ctime = self.current_time + vduration = self.video_duration + if ctime != '00:00:00' and vduration != '00:00:00': + duration = str_to_time(vduration) + watch_time = (str_to_datetime(ctime) + timezone.timedelta( seconds=120)).time() self.watched = watch_time >= duration @@ -105,7 +108,6 @@ class TrackLesson(models.Model): self.save() return self.watched - def time_spent(self): if self.video_duration != '00:00:00': hits = self.get_log_counter() diff --git a/stats/test_models.py b/stats/test_models.py new file mode 100644 index 0000000..5506a64 --- /dev/null +++ b/stats/test_models.py @@ -0,0 +1,184 @@ +from django.test import TestCase +from django.contrib.auth.models import User +from django.utils import timezone +from stats.models import TrackLesson, LessonLog +from yaksh.models import Course, Lesson, LearningModule, LearningUnit + + +class TrackLessonTestCase(TestCase): + def setUp(self): + creator = User.objects.create(username='creator', password='test', + email='test1@test.com') + self.student = User.objects.create(username='student', password='test', + email='test2@test.com') + self.course = Course.objects.create( + name="Test Course", enrollment="Enroll Request", creator=creator + ) + learning_module = LearningModule.objects.create( + name='LM', description='module', creator=creator + ) + self.lesson = Lesson.objects.create( + name='Lesson', description='Video Lesson', creator=creator + ) + learning_unit = LearningUnit.objects.create(order=1, type='lesson', + lesson=self.lesson) + learning_module.learning_unit.add(learning_unit) + learning_module.save() + self.course.learning_module.add(learning_module) + self.course.students.add(self.student) + self.course.save() + self.tracker = TrackLesson.objects.create(user=self.student, + course=self.course, + lesson=self.lesson) + LessonLog.objects.create(track=self.tracker) + self.last_access_time = timezone.now() + LessonLog.objects.create(track=self.tracker, + last_access_time=self.last_access_time) + + def tearDown(self): + User.objects.all().delete() + Course.objects.all().delete() + Lesson.objects.all().delete() + LearningUnit.objects.all().delete() + LearningModule.objects.all().delete() + LessonLog.objects.all().delete() + TrackLesson.objects.all().delete() + + def test_track_lesson(self): + # Given + tracker = self.tracker + + # Then + self.assertEqual(tracker.user, self.student) + self.assertEqual(tracker.course, self.course) + self.assertEqual(tracker.lesson, self.lesson) + self.assertEqual(tracker.current_time, '00:00:00') + self.assertEqual(tracker.video_duration, '00:00:00') + self.assertFalse(tracker.watched) + + def test_log_counter(self): + # Given + tracker = self.tracker + expected_count = 2 + + # When + counts = tracker.get_log_counter() + + # Then + self.assertEqual(counts, expected_count) + + def test_get_current_time(self): + # Given + tracker = self.tracker + expected_time = 'just started' + + # When + current_time = tracker.get_current_time() + + # Then + self.assertEqual(current_time, expected_time) + + def test_get_video_duration(self): + # Given + tracker = self.tracker + expected_duration = 'will be available after 25% completion' + + # When + duration = tracker.get_video_duration() + + # Then + self.assertEqual(duration, expected_duration) + + def test_set_current_time(self): + # Given + tracker = self.tracker + ctime = timezone.now() + + # When + tracker.set_current_time(ctime.strftime('%H:%M:%S')) + tracker.save() + updated_time = tracker.get_current_time() + + # Then + self.assertEqual(updated_time, ctime.strftime('%H:%M:%S')) + + # Given + time_now = timezone.now() + invalid_ctime = ctime - timezone.timedelta(seconds=100) + + # When + tracker.set_current_time(invalid_ctime.strftime('%H:%M:%S')) + tracker.save() + old_time = tracker.get_current_time() + + # Then + self.assertEqual(old_time, ctime.strftime('%H:%M:%S')) + + def test_get_percentage_complete(self): + # Given + tracker = self.tracker + expected_percentage = 'less than 25%' + + # When + percentage = tracker.get_percentage_complete() + + # Then + self.assertEqual(percentage, expected_percentage) + + # Given + expected_percentage = 'approx 75 %' + + # When + tracker.set_current_time('00:03:00') + tracker.video_duration = '00:04:00' + tracker.save() + percentage = tracker.get_percentage_complete() + + # Then + self.assertEqual(percentage, expected_percentage) + + def test_get_last_access_time(self): + # Given + tracker = self.tracker + expected_time = self.last_access_time + + # When + time = tracker.get_last_access_time() + + # Then + self.assertEqual(time, expected_time) + + def test_set_get_watched(self): + # Given + tracker = self.tracker + + # When + tracker.set_watched() + + # Then + self.assertFalse(tracker.get_watched()) + + # Given + tracker = self.tracker + + # When + tracker.set_current_time('00:03:55') + tracker.video_duration = '00:04:00' + tracker.save() + tracker.set_watched() + + # Then + self.assertTrue(tracker.get_watched()) + + def test_time_spent(self): + # Given + tracker = self.tracker + expected_time = '00:02:00' + + # When + tracker.video_duration = '00:04:00' + tracker.save() + time = tracker.time_spent() + + # Then + self.assertTrue(expected_time, time) |