test_crud.py 11.7 KB
Newer Older
1
import unittest
2

3
from xmodule import templates
4
from xmodule.modulestore import ModuleStoreEnum
5 6
from xmodule.modulestore.tests import persistent_factories
from xmodule.course_module import CourseDescriptor
Don Mitchell committed
7
from xmodule.modulestore.django import modulestore, clear_existing_modulestores
8 9
from xmodule.seq_module import SequenceDescriptor
from xmodule.capa_module import CapaDescriptor
10
from opaque_keys.edx.locator import BlockUsageLocator, LocalId
11
from xmodule.modulestore.exceptions import ItemNotFoundError, DuplicateCourseError
12
from xmodule.html_module import HtmlDescriptor
13
from xmodule.modulestore.tests.django_utils import ModuleStoreTestCase
14 15 16 17 18 19 20


class TemplateTests(unittest.TestCase):
    """
    Test finding and using the templates (boilerplates) for xblocks.
    """

21
    def setUp(self):
22
        clear_existing_modulestores()  # redundant w/ cleanup but someone was getting errors
23
        self.addCleanup(ModuleStoreTestCase.drop_mongo_collections)
24
        self.addCleanup(clear_existing_modulestores)
25
        self.split_store = modulestore()._get_modulestore_by_type(ModuleStoreEnum.Type.split)
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
    def test_get_templates(self):
        found = templates.all_templates()
        self.assertIsNotNone(found.get('course'))
        self.assertIsNotNone(found.get('about'))
        self.assertIsNotNone(found.get('html'))
        self.assertIsNotNone(found.get('problem'))
        self.assertEqual(len(found.get('course')), 0)
        self.assertEqual(len(found.get('about')), 1)
        self.assertGreaterEqual(len(found.get('html')), 2)
        self.assertGreaterEqual(len(found.get('problem')), 10)
        dropdown = None
        for template in found['problem']:
            self.assertIn('metadata', template)
            self.assertIn('display_name', template['metadata'])
            if template['metadata']['display_name'] == 'Dropdown':
                dropdown = template
                break
        self.assertIsNotNone(dropdown)
        self.assertIn('markdown', dropdown['metadata'])
        self.assertIn('data', dropdown)
        self.assertRegexpMatches(dropdown['metadata']['markdown'], r'^Dropdown.*')
        self.assertRegexpMatches(dropdown['data'], r'<problem>\s*<p>Dropdown.*')

    def test_get_some_templates(self):
        self.assertEqual(len(SequenceDescriptor.templates()), 0)
        self.assertGreater(len(HtmlDescriptor.templates()), 0)
        self.assertIsNone(SequenceDescriptor.get_template('doesntexist.yaml'))
        self.assertIsNone(HtmlDescriptor.get_template('doesntexist.yaml'))
        self.assertIsNotNone(HtmlDescriptor.get_template('announcement.yaml'))

    def test_factories(self):
58
        test_course = persistent_factories.PersistentCourseFactory.create(
59
            course='course', run='2014', org='testx',
60 61
            display_name='fun test course', user_id='testbot'
        )
62 63
        self.assertIsInstance(test_course, CourseDescriptor)
        self.assertEqual(test_course.display_name, 'fun test course')
64
        index_info = self.split_store.get_course_index_info(test_course.id)
65
        self.assertEqual(index_info['org'], 'testx')
66 67
        self.assertEqual(index_info['course'], 'course')
        self.assertEqual(index_info['run'], '2014')
68 69 70 71 72

        test_chapter = persistent_factories.ItemFactory.create(display_name='chapter 1',
            parent_location=test_course.location)
        self.assertIsInstance(test_chapter, SequenceDescriptor)
        # refetch parent which should now point to child
73
        test_course = self.split_store.get_course(test_course.id.version_agnostic())
74
        self.assertIn(test_chapter.location, test_course.children)
75

76 77
        with self.assertRaises(DuplicateCourseError):
            persistent_factories.PersistentCourseFactory.create(
78
                course='course', run='2014', org='testx',
79 80 81
                display_name='fun test course', user_id='testbot'
            )

82 83
    def test_temporary_xblocks(self):
        """
84
        Test create_xblock to create non persisted xblocks
85
        """
86
        test_course = persistent_factories.PersistentCourseFactory.create(
87
            course='course', run='2014', org='testx',
88 89
            display_name='fun test course', user_id='testbot'
        )
90

91
        test_chapter = self.split_store.create_xblock(
92 93
            test_course.system, test_course.id, 'chapter', fields={'display_name': 'chapter n'},
            parent_xblock=test_course
94
        )
95 96 97 98 99 100
        self.assertIsInstance(test_chapter, SequenceDescriptor)
        self.assertEqual(test_chapter.display_name, 'chapter n')
        self.assertIn(test_chapter, test_course.get_children())

        # test w/ a definition (e.g., a problem)
        test_def_content = '<problem>boo</problem>'
101
        test_problem = self.split_store.create_xblock(
102 103
            test_course.system, test_course.id, 'problem', fields={'data': test_def_content},
            parent_xblock=test_chapter
104
        )
105 106 107 108 109 110 111 112 113 114
        self.assertIsInstance(test_problem, CapaDescriptor)
        self.assertEqual(test_problem.data, test_def_content)
        self.assertIn(test_problem, test_chapter.get_children())
        test_problem.display_name = 'test problem'
        self.assertEqual(test_problem.display_name, 'test problem')

    def test_persist_dag(self):
        """
        try saving temporary xblocks
        """
Don Mitchell committed
115
        test_course = persistent_factories.PersistentCourseFactory.create(
116
            course='course', run='2014', org='testx',
117 118
            display_name='fun test course', user_id='testbot'
        )
119
        test_chapter = self.split_store.create_xblock(
120 121
            test_course.system, test_course.id, 'chapter', fields={'display_name': 'chapter n'},
            parent_xblock=test_course
122 123
        )
        self.assertEqual(test_chapter.display_name, 'chapter n')
124
        test_def_content = '<problem>boo</problem>'
125
        # create child
126
        new_block = self.split_store.create_xblock(
127
            test_course.system, test_course.id,
128 129
            'problem',
            fields={
Don Mitchell committed
130 131
                'data': test_def_content,
                'display_name': 'problem'
132
            },
133 134 135 136
            parent_xblock=test_chapter
        )
        self.assertIsNotNone(new_block.definition_locator)
        self.assertTrue(isinstance(new_block.definition_locator.definition_id, LocalId))
137 138 139
        # better to pass in persisted parent over the subdag so
        # subdag gets the parent pointer (otherwise 2 ops, persist dag, update parent children,
        # persist parent
140
        persisted_course = self.split_store.persist_xblock_dag(test_course, 'testbot')
141 142 143 144 145 146 147 148
        self.assertEqual(len(persisted_course.children), 1)
        persisted_chapter = persisted_course.get_children()[0]
        self.assertEqual(persisted_chapter.category, 'chapter')
        self.assertEqual(persisted_chapter.display_name, 'chapter n')
        self.assertEqual(len(persisted_chapter.children), 1)
        persisted_problem = persisted_chapter.get_children()[0]
        self.assertEqual(persisted_problem.category, 'problem')
        self.assertEqual(persisted_problem.data, test_def_content)
Don Mitchell committed
149 150
        # update it
        persisted_problem.display_name = 'altered problem'
151
        persisted_problem = self.split_store.persist_xblock_dag(persisted_problem, 'testbot')
Don Mitchell committed
152
        self.assertEqual(persisted_problem.display_name, 'altered problem')
153 154 155

    def test_delete_course(self):
        test_course = persistent_factories.PersistentCourseFactory.create(
156
            course='history', run='doomed', org='edu.harvard',
157 158 159 160 161
            display_name='doomed test course',
            user_id='testbot')
        persistent_factories.ItemFactory.create(display_name='chapter 1',
            parent_location=test_course.location)

162
        id_locator = test_course.id.for_branch(ModuleStoreEnum.BranchName.draft)
163 164
        guid_locator = test_course.location.course_agnostic()
        # verify it can be retrieved by id
165
        self.assertIsInstance(self.split_store.get_course(id_locator), CourseDescriptor)
166
        # and by guid -- TODO reenable when split_draft supports getting specific versions
167
#         self.assertIsInstance(self.split_store.get_item(guid_locator), CourseDescriptor)
168
        self.split_store.delete_course(id_locator, 'testbot')
169
        # test can no longer retrieve by id
170
        self.assertRaises(ItemNotFoundError, self.split_store.get_course, id_locator)
171
        # but can by guid -- same TODO as above
172
#         self.assertIsInstance(self.split_store.get_item(guid_locator), CourseDescriptor)
173 174 175 176 177 178

    def test_block_generations(self):
        """
        Test get_block_generations
        """
        test_course = persistent_factories.PersistentCourseFactory.create(
179
            course='history', run='hist101', org='edu.harvard',
180
            display_name='history test course',
181 182
            user_id='testbot'
        )
183 184 185 186
        chapter = persistent_factories.ItemFactory.create(display_name='chapter 1',
            parent_location=test_course.location, user_id='testbot')
        sub = persistent_factories.ItemFactory.create(display_name='subsection 1',
            parent_location=chapter.location, user_id='testbot', category='vertical')
187 188
        first_problem = persistent_factories.ItemFactory.create(
            display_name='problem 1', parent_location=sub.location, user_id='testbot', category='problem',
189
            data="<problem></problem>"
190
        )
191
        first_problem.max_attempts = 3
192
        first_problem.save()  # decache the above into the kvs
193
        updated_problem = self.split_store.update_item(first_problem, 'testbot')
194 195 196
        self.assertIsNotNone(updated_problem.previous_version)
        self.assertEqual(updated_problem.previous_version, first_problem.update_version)
        self.assertNotEqual(updated_problem.update_version, first_problem.update_version)
197
        self.split_store.delete_item(updated_problem.location, 'testbot')
198

199 200
        second_problem = persistent_factories.ItemFactory.create(
            display_name='problem 2',
201
            parent_location=BlockUsageLocator.make_relative(
202
                test_course.location.version_agnostic(), block_type='problem', block_id=sub.location.block_id
203
            ),
204
            user_id='testbot', category='problem',
205
            data="<problem></problem>"
206
        )
207 208

        # course root only updated 2x
209
        version_history = self.split_store.get_block_generations(test_course.location)
210 211
        # create course causes 2 versions for the time being; skip the first.
        version_history = version_history.children[0]
212 213 214 215 216 217
        self.assertEqual(version_history.locator.version_guid, test_course.location.version_guid)
        self.assertEqual(len(version_history.children), 1)
        self.assertEqual(version_history.children[0].children, [])
        self.assertEqual(version_history.children[0].locator.version_guid, chapter.location.version_guid)

        # sub changed on add, add problem, delete problem, add problem in strict linear seq
218
        version_history = self.split_store.get_block_generations(sub.location)
219 220 221 222 223 224
        self.assertEqual(len(version_history.children), 1)
        self.assertEqual(len(version_history.children[0].children), 1)
        self.assertEqual(len(version_history.children[0].children[0].children), 1)
        self.assertEqual(len(version_history.children[0].children[0].children[0].children), 0)

        # first and second problem may show as same usage_id; so, need to ensure their histories are right
225
        version_history = self.split_store.get_block_generations(updated_problem.location)
226 227 228 229
        self.assertEqual(version_history.locator.version_guid, first_problem.location.version_guid)
        self.assertEqual(len(version_history.children), 1)  # updated max_attempts
        self.assertEqual(len(version_history.children[0].children), 0)

230
        version_history = self.split_store.get_block_generations(second_problem.location)
231
        self.assertNotEqual(version_history.locator.version_guid, first_problem.location.version_guid)