tests.py 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
"""
Tests for coursewarehistoryextended
Many aspects of this app are covered by the courseware tests,
but these are specific to the new storage model with multiple
backend tables.
"""

import json
from unittest import skipUnless

11 12 13 14
from django.conf import settings
from django.test import TestCase
from mock import patch
from nose.plugins.attrib import attr
15

16 17
from courseware.models import BaseStudentModuleHistory, StudentModule, StudentModuleHistory
from courseware.tests.factories import StudentModuleFactory, course_id, location
18 19


20
@attr(shard=1)
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
@skipUnless(settings.FEATURES["ENABLE_CSMH_EXTENDED"], "CSMH Extended needs to be enabled")
class TestStudentModuleHistoryBackends(TestCase):
    """ Tests of data in CSMH and CSMHE """
    # Tell Django to clean out all databases, not just default
    multi_db = True

    def setUp(self):
        super(TestStudentModuleHistoryBackends, self).setUp()
        for record in (1, 2, 3):
            # This will store into CSMHE via the post_save signal
            csm = StudentModuleFactory.create(module_state_key=location('usage_id'),
                                              course_id=course_id,
                                              state=json.dumps({'type': 'csmhe', 'order': record}))
            # This manually gets us a CSMH record to compare
            csmh = StudentModuleHistory(student_module=csm,
                                        version=None,
                                        created=csm.modified,
                                        state=json.dumps({'type': 'csmh', 'order': record}),
                                        grade=csm.grade,
                                        max_grade=csm.max_grade)
            csmh.save()

    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_CSMH_EXTENDED": True})
    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_READING_FROM_MULTIPLE_HISTORY_TABLES": True})
    def test_get_history_true_true(self):
        student_module = StudentModule.objects.all()
        history = BaseStudentModuleHistory.get_history(student_module)
        self.assertEquals(len(history), 6)
        self.assertEquals({'type': 'csmhe', 'order': 3}, json.loads(history[0].state))
        self.assertEquals({'type': 'csmhe', 'order': 2}, json.loads(history[1].state))
        self.assertEquals({'type': 'csmhe', 'order': 1}, json.loads(history[2].state))
        self.assertEquals({'type': 'csmh', 'order': 3}, json.loads(history[3].state))
        self.assertEquals({'type': 'csmh', 'order': 2}, json.loads(history[4].state))
        self.assertEquals({'type': 'csmh', 'order': 1}, json.loads(history[5].state))

    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_CSMH_EXTENDED": True})
    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_READING_FROM_MULTIPLE_HISTORY_TABLES": False})
    def test_get_history_true_false(self):
        student_module = StudentModule.objects.all()
        history = BaseStudentModuleHistory.get_history(student_module)
        self.assertEquals(len(history), 3)
        self.assertEquals({'type': 'csmhe', 'order': 3}, json.loads(history[0].state))
        self.assertEquals({'type': 'csmhe', 'order': 2}, json.loads(history[1].state))
        self.assertEquals({'type': 'csmhe', 'order': 1}, json.loads(history[2].state))

    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_CSMH_EXTENDED": False})
    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_READING_FROM_MULTIPLE_HISTORY_TABLES": True})
    def test_get_history_false_true(self):
        student_module = StudentModule.objects.all()
        history = BaseStudentModuleHistory.get_history(student_module)
        self.assertEquals(len(history), 3)
        self.assertEquals({'type': 'csmh', 'order': 3}, json.loads(history[0].state))
        self.assertEquals({'type': 'csmh', 'order': 2}, json.loads(history[1].state))
        self.assertEquals({'type': 'csmh', 'order': 1}, json.loads(history[2].state))

    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_CSMH_EXTENDED": False})
    @patch.dict("django.conf.settings.FEATURES", {"ENABLE_READING_FROM_MULTIPLE_HISTORY_TABLES": False})
    def test_get_history_false_false(self):
        student_module = StudentModule.objects.all()
        history = BaseStudentModuleHistory.get_history(student_module)
        self.assertEquals(len(history), 0)