test_mongo.py 4.37 KB
Newer Older
1 2
import pymongo

3
from nose.tools import assert_equals, assert_raises, assert_not_equals, with_setup
4
from path import path
5
from pprint import pprint
6

7
from xmodule.modulestore import Location
8
from xmodule.modulestore.exceptions import InvalidLocationError, ItemNotFoundError, NoPathToItem
9
from xmodule.modulestore.mongo import MongoModuleStore
10 11 12 13 14 15 16 17 18 19 20
from xmodule.modulestore.xml_importer import import_from_xml

# from ~/mitx_all/mitx/common/lib/xmodule/xmodule/modulestore/tests/
# to   ~/mitx_all/mitx/common/test
TEST_DIR = path(__file__).abspath().dirname()
for i in range(5):
    TEST_DIR = TEST_DIR.dirname()
TEST_DIR = TEST_DIR / 'test'

DATA_DIR = TEST_DIR / 'data'

21

22 23 24 25 26 27
HOST = 'localhost'
PORT = 27017
DB = 'test'
COLLECTION = 'modulestore'
FS_ROOT = DATA_DIR  # TODO (vshnayder): will need a real fs_root for testing load_item
DEFAULT_CLASS = 'xmodule.raw_module.RawDescriptor'
28 29


30
class TestMongoModuleStore(object):
31

32 33 34 35
    @classmethod
    def setupClass(cls):
        cls.connection = pymongo.connection.Connection(HOST, PORT)
        cls.connection.drop_database(DB)
36

37 38 39 40 41 42
        # NOTE: Creating a single db for all the tests to save time.  This
        # is ok only as long as none of the tests modify the db.
        # If (when!) that changes, need to either reload the db, or load
        # once and copy over to a tmp db for each test.
        cls.store = cls.initdb()

43 44 45
    @classmethod
    def teardownClass(cls):
        pass
46

47 48
    @staticmethod
    def initdb():
49
        # connect to the db
50
        store = MongoModuleStore(HOST, DB, COLLECTION, FS_ROOT, default_class=DEFAULT_CLASS)
51 52
        # Explicitly list the courses to load (don't want the big one)
        courses = ['toy', 'simple']
53 54 55 56 57 58 59 60 61 62
        import_from_xml(store, DATA_DIR, courses)
        return store

    @staticmethod
    def destroy_db(connection):
        # Destroy the test db.
        connection.drop_database(DB)

    def setUp(self):
        # make a copy for convenience
63
        self.connection = TestMongoModuleStore.connection
64

65
    def tearDown(self):
66
        pass
67

68
    def test_init(self):
69 70
        '''Make sure the db loads, and print all the locations in the db.
        Call this directly from failing tests to see what's loaded'''
71
        ids = list(self.connection[DB][COLLECTION].find({}, {'_id': True}))
72 73 74

        pprint([Location(i['_id']).url() for i in ids])

75 76 77 78 79 80 81 82
    def test_get_courses(self):
        '''Make sure the course objects loaded properly'''
        courses = self.store.get_courses()
        assert_equals(len(courses), 2)
        courses.sort(key=lambda c: c.id)
        assert_equals(courses[0].id, 'edX/simple/2012_Fall')
        assert_equals(courses[1].id, 'edX/toy/2012_Fall')

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
    def test_loads(self):
        assert_not_equals(
            self.store.get_item("i4x://edX/toy/course/2012_Fall"),
            None)

        assert_not_equals(
            self.store.get_item("i4x://edX/simple/course/2012_Fall"),
            None)

        assert_not_equals(
            self.store.get_item("i4x://edX/toy/video/Welcome"),
            None)



    def test_find_one(self):
        assert_not_equals(
            self.store._find_one(Location("i4x://edX/toy/course/2012_Fall")),
            None)

        assert_not_equals(
            self.store._find_one(Location("i4x://edX/simple/course/2012_Fall")),
            None)

        assert_not_equals(
            self.store._find_one(Location("i4x://edX/toy/video/Welcome")),
            None)

    def test_path_to_location(self):
112 113
        '''Make sure that path_to_location works'''
        should_work = (
114 115 116 117
            ("i4x://edX/toy/video/Welcome",
             ("edX/toy/2012_Fall", "Overview", "Welcome", None)),
            ("i4x://edX/toy/html/toylab",
             ("edX/toy/2012_Fall", "Overview", "Toy_Videos", None)),
118 119 120 121 122 123 124 125 126
            )
        for location, expected in should_work:
            assert_equals(self.store.path_to_location(location), expected)

        not_found = (
            "i4x://edX/toy/video/WelcomeX",
            )
        for location in not_found:
            assert_raises(ItemNotFoundError, self.store.path_to_location, location)
127 128 129 130

        # Since our test files are valid, there shouldn't be any
        # elements with no path to them.  But we can look for them in
        # another course.
131
        no_path = (
132
            "i4x://edX/simple/video/Lost_Video",
133
            )
134 135 136
        for location in no_path:
            assert_raises(NoPathToItem, self.store.path_to_location, location, "toy")