def set_up_assets(self, deprecated): """ Setup contentstore w/ proper overriding of deprecated. """ # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well self.contentstore = MongoContentStore(HOST, DB, port=PORT) self.addCleanup(self.contentstore._drop_database) # pylint: disable=protected-access setattr(AssetLocator, 'deprecated', deprecated) setattr(CourseLocator, 'deprecated', deprecated) self.course1_key = CourseLocator('test', 'asset_test', '2014_07') self.course2_key = CourseLocator('test', 'asset_test2', '2014_07') self.course1_files = ['contains.sh', 'picture1.jpg', 'picture2.jpg'] self.course2_files = ['picture1.jpg', 'picture3.jpg', 'door_2.ogg'] def load_assets(course_key, files): locked = False for filename in files: asset_key = course_key.make_asset_key('asset', filename) self.save_asset(filename, asset_key, filename, locked) locked = not locked load_assets(self.course1_key, self.course1_files) load_assets(self.course2_key, self.course2_files)
def build(self): """ A contextmanager that returns a MongoContentStore, and deletes its contents when the context closes. """ contentstore = MongoContentStore(db='contentstore{}'.format( random.randint(0, 10000)), collection='content', **COMMON_DOCSTORE_CONFIG) try: yield contentstore finally: # Delete the created database contentstore._drop_database()
def set_up_assets(self, deprecated): """ Setup contentstore w/ proper overriding of deprecated. """ # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well self.contentstore = MongoContentStore(HOST, DB, port=PORT) self.addCleanup(self.contentstore._drop_database) # pylint: disable=protected-access AssetLocator.deprecated = deprecated CourseLocator.deprecated = deprecated self.course1_key = CourseLocator('test', 'asset_test', '2014_07') self.course2_key = CourseLocator('test', 'asset_test2', '2014_07') self.course1_files = ['contains.sh', 'picture1.jpg', 'picture2.jpg'] self.course2_files = ['picture1.jpg', 'picture3.jpg', 'door_2.ogg'] def load_assets(course_key, files): locked = False for filename in files: asset_key = course_key.make_asset_key('asset', filename) self.save_asset(filename, asset_key, filename, locked) locked = not locked load_assets(self.course1_key, self.course1_files) load_assets(self.course2_key, self.course2_files)
def build(self): """ A contextmanager that returns a MongoContentStore, and deletes its contents when the context closes. """ contentstore = MongoContentStore( db='contentstore{}'.format(random.randint(0, 10000)), collection='content', **COMMON_DOCSTORE_CONFIG ) try: yield contentstore finally: # Delete the created database contentstore._drop_database()
def initdb(): # connect to the db store = MongoModuleStore(HOST, DB, COLLECTION, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS) # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well content_store = MongoContentStore(HOST, DB) # # Also test draft store imports # draft_store = DraftModuleStore(HOST, DB, COLLECTION, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS) # Explicitly list the courses to load (don't want the big one) courses = ['toy', 'simple', 'simple_with_draft', 'test_unicode'] import_from_xml(store, DATA_DIR, courses, draft_store=draft_store, static_content_store=content_store) # also test a course with no importing of static content import_from_xml( store, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True ) return store, content_store, draft_store
def initdb(cls): # connect to the db doc_store_config = { 'host': HOST, 'db': DB, 'collection': COLLECTION, } # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well content_store = MongoContentStore(HOST, DB) # # Also test draft store imports # draft_store = DraftModuleStore( content_store, doc_store_config, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS, branch_setting_func=lambda: ModuleStoreEnum.Branch.draft_preferred) import_from_xml(draft_store, 999, DATA_DIR, cls.courses, static_content_store=content_store) # also test a course with no importing of static content import_from_xml(draft_store, 999, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True) return content_store, draft_store
def initdb(): # connect to the db doc_store_config = { 'host': HOST, 'db': DB, 'collection': COLLECTION, } store = MongoModuleStore(doc_store_config, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS) # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well content_store = MongoContentStore(HOST, DB) # # Also test draft store imports # draft_store = DraftModuleStore(doc_store_config, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS) import_from_xml(store, DATA_DIR, TestMongoModuleStore.courses, draft_store=draft_store, static_content_store=content_store) # also test a course with no importing of static content import_from_xml( store, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True ) return store, content_store, draft_store
def build(self): """ A contextmanager that returns a MongoContentStore, and deletes its contents when the context closes. """ contentstore = MongoContentStore( db='contentstore{}'.format(THIS_UUID), collection='content', **COMMON_DOCSTORE_CONFIG ) contentstore.ensure_indexes() try: yield contentstore finally: # Delete the created database contentstore._drop_database() # pylint: disable=protected-access
def initdb(cls): # lint-amnesty, pylint: disable=missing-function-docstring # connect to the db doc_store_config = { 'host': HOST, 'port': PORT, 'db': DB, 'collection': COLLECTION, } cls.add_asset_collection(doc_store_config) # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well content_store = MongoContentStore(HOST, DB, port=PORT) # # Also test draft store imports # draft_store = DraftModuleStore( content_store, doc_store_config, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS, branch_setting_func=lambda: ModuleStoreEnum.Branch.draft_preferred, xblock_mixins=(EditInfoMixin, InheritanceMixin, LocationMixin, XModuleMixin)) import_course_from_xml(draft_store, 999, DATA_DIR, cls.courses, static_content_store=content_store) # also test a course with no importing of static content import_course_from_xml(draft_store, 999, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True) # also import a course under a different course_id (especially ORG) import_course_from_xml( draft_store, 999, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True, target_id=CourseKey.from_string('guestx/foo/bar')) # Import a course for `test_reference_converters` since it manipulates the saved course # which can cause any other test using the same course to have a flakey error import_course_from_xml(draft_store, 999, DATA_DIR, ['test_import_course_2'], static_content_store=content_store) return content_store, draft_store
def initdb(cls): # connect to the db doc_store_config = { 'host': HOST, 'port': PORT, 'db': DB, 'collection': COLLECTION, } cls.add_asset_collection(doc_store_config) # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well content_store = MongoContentStore(HOST, DB, port=PORT) # # Also test draft store imports # draft_store = DraftModuleStore( content_store, doc_store_config, FS_ROOT, RENDER_TEMPLATE, default_class=DEFAULT_CLASS, branch_setting_func=lambda: ModuleStoreEnum.Branch.draft_preferred, xblock_mixins=(EditInfoMixin, InheritanceMixin, LocationMixin, XModuleMixin) ) with patch('xmodule.tabs.CourseTab.from_json', side_effect=mock_tab_from_json): import_course_from_xml( draft_store, 999, DATA_DIR, cls.courses, static_content_store=content_store ) # also test a course with no importing of static content import_course_from_xml( draft_store, 999, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True ) # also import a course under a different course_id (especially ORG) import_course_from_xml( draft_store, 999, DATA_DIR, ['test_import_course'], static_content_store=content_store, do_import_static=False, verbose=True, target_id=CourseKey.from_string('guestx/foo/bar') ) return content_store, draft_store
def _get_assets_for_page(request, course_key, options): """ Returns the list of assets for the specified page and page size. """ current_page = options['current_page'] page_size = options['page_size'] sort = options['sort'] filter_params = options['filter_params'] if options['filter_params'] else None start = current_page * page_size connection = MongoContentStore("172.16.7.63","lv_lms",27017,True,"root","123456") return get_all( connection,start=start, maxresults=page_size, sort=sort, filter_params=filter_params )
def build(self): """ A contextmanager that returns a MongoContentStore, and deletes its contents when the context closes. """ contentstore = MongoContentStore(db=f'contentstore{THIS_UUID}', collection='content', **COMMON_DOCSTORE_CONFIG) contentstore.ensure_indexes() try: yield contentstore finally: # Delete the created database contentstore._drop_database() # pylint: disable=protected-access
class TestContentstore(unittest.TestCase): """ Test the methods in contentstore.mongo using deprecated and non-deprecated keys """ # don't use these 2 class vars as they restore behavior once the tests are done asset_deprecated = None ssck_deprecated = None @classmethod def tearDownClass(cls): """ Restores deprecated values """ if cls.asset_deprecated is not None: AssetLocator.deprecated = cls.asset_deprecated else: del AssetLocator.deprecated if cls.ssck_deprecated is not None: CourseLocator.deprecated = cls.ssck_deprecated else: del CourseLocator.deprecated return super(TestContentstore, cls).tearDownClass() def set_up_assets(self, deprecated): """ Setup contentstore w/ proper overriding of deprecated. """ # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well self.contentstore = MongoContentStore(HOST, DB, port=PORT) self.addCleanup(self.contentstore._drop_database) # pylint: disable=protected-access AssetLocator.deprecated = deprecated CourseLocator.deprecated = deprecated self.course1_key = CourseLocator('test', 'asset_test', '2014_07') self.course2_key = CourseLocator('test', 'asset_test2', '2014_07') self.course1_files = ['contains.sh', 'picture1.jpg', 'picture2.jpg'] self.course2_files = ['picture1.jpg', 'picture3.jpg', 'door_2.ogg'] def load_assets(course_key, files): locked = False for filename in files: asset_key = course_key.make_asset_key('asset', filename) self.save_asset(filename, asset_key, filename, locked) locked = not locked load_assets(self.course1_key, self.course1_files) load_assets(self.course2_key, self.course2_files) def save_asset(self, filename, asset_key, displayname, locked): """ Load and save the given file. """ with open("{}/static/{}".format(DATA_DIR, filename), "rb") as f: content = StaticContent( asset_key, displayname, mimetypes.guess_type(filename)[0], f.read(), locked=locked ) self.contentstore.save(content) @ddt.data(True, False) def test_delete(self, deprecated): """ Test that deleting assets works """ self.set_up_assets(deprecated) asset_key = self.course1_key.make_asset_key('asset', self.course1_files[0]) self.contentstore.delete(asset_key) with self.assertRaises(NotFoundError): self.contentstore.find(asset_key) # ensure deleting a non-existent file is a noop self.contentstore.delete(asset_key) @ddt.data(True, False) def test_find(self, deprecated): """ Test using find """ self.set_up_assets(deprecated) asset_key = self.course1_key.make_asset_key('asset', self.course1_files[0]) self.assertIsNotNone(self.contentstore.find(asset_key), "Could not find {}".format(asset_key)) self.assertIsNotNone(self.contentstore.find(asset_key, as_stream=True), "Could not find {}".format(asset_key)) unknown_asset = self.course1_key.make_asset_key('asset', 'no_such_file.gif') with self.assertRaises(NotFoundError): self.contentstore.find(unknown_asset) self.assertIsNone( self.contentstore.find(unknown_asset, throw_on_not_found=False), "Found unknown asset {}".format(unknown_asset) ) @ddt.data(True, False) def test_export_for_course(self, deprecated): """ Test export """ self.set_up_assets(deprecated) root_dir = path.Path(mkdtemp()) try: self.contentstore.export_all_for_course( self.course1_key, root_dir, path.Path(root_dir / "policy.json"), ) for filename in self.course1_files: filepath = path.Path(root_dir / filename) self.assertTrue(filepath.isfile(), "{} is not a file".format(filepath)) for filename in self.course2_files: if filename not in self.course1_files: filepath = path.Path(root_dir / filename) self.assertFalse(filepath.isfile(), "{} is unexpected exported a file".format(filepath)) finally: shutil.rmtree(root_dir) @ddt.data(True, False) def test_get_all_content(self, deprecated): """ Test get_all_content_for_course """ self.set_up_assets(deprecated) course1_assets, count = self.contentstore.get_all_content_for_course(self.course1_key) self.assertEqual(count, len(self.course1_files), course1_assets) for asset in course1_assets: parsed = AssetKey.from_string(asset['filename']) self.assertIn(parsed.block_id, self.course1_files) course1_assets, __ = self.contentstore.get_all_content_for_course(self.course1_key, 1, 1) self.assertEqual(len(course1_assets), 1, course1_assets) fake_course = CourseLocator('test', 'fake', 'non') course_assets, count = self.contentstore.get_all_content_for_course(fake_course) self.assertEqual(count, 0) self.assertEqual(course_assets, []) @ddt.data(True, False) def test_attrs(self, deprecated): """ Test setting and getting attrs """ self.set_up_assets(deprecated) for filename in self.course1_files: asset_key = self.course1_key.make_asset_key('asset', filename) prelocked = self.contentstore.get_attr(asset_key, 'locked', False) self.contentstore.set_attr(asset_key, 'locked', not prelocked) self.assertEqual(self.contentstore.get_attr(asset_key, 'locked', False), not prelocked) @ddt.data(True, False) def test_copy_assets(self, deprecated): """ copy_all_course_assets """ self.set_up_assets(deprecated) dest_course = CourseLocator('test', 'destination', 'copy') self.contentstore.copy_all_course_assets(self.course1_key, dest_course) for filename in self.course1_files: asset_key = self.course1_key.make_asset_key('asset', filename) dest_key = dest_course.make_asset_key('asset', filename) source = self.contentstore.find(asset_key) copied = self.contentstore.find(dest_key) for propname in ['name', 'content_type', 'length', 'locked']: self.assertEqual(getattr(source, propname), getattr(copied, propname)) __, count = self.contentstore.get_all_content_for_course(dest_course) self.assertEqual(count, len(self.course1_files)) @ddt.data(True, False) def test_delete_assets(self, deprecated): """ delete_all_course_assets """ self.set_up_assets(deprecated) self.contentstore.delete_all_course_assets(self.course1_key) __, count = self.contentstore.get_all_content_for_course(self.course1_key) self.assertEqual(count, 0) # ensure it didn't remove any from other course __, count = self.contentstore.get_all_content_for_course(self.course2_key) self.assertEqual(count, len(self.course2_files))
class TestContentstore(unittest.TestCase): """ Test the methods in contentstore.mongo using deprecated and non-deprecated keys """ # don't use these 2 class vars as they restore behavior once the tests are done asset_deprecated = None ssck_deprecated = None @classmethod def tearDownClass(cls): """ Restores deprecated values """ if cls.asset_deprecated is not None: setattr(AssetLocator, 'deprecated', cls.asset_deprecated) else: delattr(AssetLocator, 'deprecated') if cls.ssck_deprecated is not None: setattr(CourseLocator, 'deprecated', cls.ssck_deprecated) else: delattr(CourseLocator, 'deprecated') return super(TestContentstore, cls).tearDownClass() def set_up_assets(self, deprecated): """ Setup contentstore w/ proper overriding of deprecated. """ # since MongoModuleStore and MongoContentStore are basically assumed to be together, create this class # as well self.contentstore = MongoContentStore(HOST, DB, port=PORT) self.addCleanup(self.contentstore._drop_database) # pylint: disable=protected-access setattr(AssetLocator, 'deprecated', deprecated) setattr(CourseLocator, 'deprecated', deprecated) self.course1_key = CourseLocator('test', 'asset_test', '2014_07') self.course2_key = CourseLocator('test', 'asset_test2', '2014_07') self.course1_files = ['contains.sh', 'picture1.jpg', 'picture2.jpg'] self.course2_files = ['picture1.jpg', 'picture3.jpg', 'door_2.ogg'] def load_assets(course_key, files): locked = False for filename in files: asset_key = course_key.make_asset_key('asset', filename) self.save_asset(filename, asset_key, filename, locked) locked = not locked load_assets(self.course1_key, self.course1_files) load_assets(self.course2_key, self.course2_files) def save_asset(self, filename, asset_key, displayname, locked): """ Load and save the given file. """ with open("{}/static/{}".format(DATA_DIR, filename), "rb") as f: content = StaticContent(asset_key, displayname, mimetypes.guess_type(filename)[0], f.read(), locked=locked) self.contentstore.save(content) @ddt.data(True, False) def test_delete(self, deprecated): """ Test that deleting assets works """ self.set_up_assets(deprecated) asset_key = self.course1_key.make_asset_key('asset', self.course1_files[0]) self.contentstore.delete(asset_key) with self.assertRaises(NotFoundError): self.contentstore.find(asset_key) # ensure deleting a non-existent file is a noop self.contentstore.delete(asset_key) @ddt.data(True, False) def test_find(self, deprecated): """ Test using find """ self.set_up_assets(deprecated) asset_key = self.course1_key.make_asset_key('asset', self.course1_files[0]) self.assertIsNotNone(self.contentstore.find(asset_key), "Could not find {}".format(asset_key)) self.assertIsNotNone(self.contentstore.find(asset_key, as_stream=True), "Could not find {}".format(asset_key)) unknown_asset = self.course1_key.make_asset_key( 'asset', 'no_such_file.gif') with self.assertRaises(NotFoundError): self.contentstore.find(unknown_asset) self.assertIsNone( self.contentstore.find(unknown_asset, throw_on_not_found=False), "Found unknown asset {}".format(unknown_asset)) @ddt.data(True, False) def test_export_for_course(self, deprecated): """ Test export """ self.set_up_assets(deprecated) root_dir = path.path(mkdtemp()) try: self.contentstore.export_all_for_course( self.course1_key, root_dir, path.path(root_dir / "policy.json"), ) for filename in self.course1_files: filepath = path.path(root_dir / filename) self.assertTrue(filepath.isfile(), "{} is not a file".format(filepath)) for filename in self.course2_files: if filename not in self.course1_files: filepath = path.path(root_dir / filename) self.assertFalse( filepath.isfile(), "{} is unexpected exported a file".format(filepath)) finally: shutil.rmtree(root_dir) @ddt.data(True, False) def test_get_all_content(self, deprecated): """ Test get_all_content_for_course """ self.set_up_assets(deprecated) course1_assets, count = self.contentstore.get_all_content_for_course( self.course1_key) self.assertEqual(count, len(self.course1_files), course1_assets) for asset in course1_assets: parsed = AssetKey.from_string(asset['filename']) self.assertIn(parsed.name, self.course1_files) course1_assets, __ = self.contentstore.get_all_content_for_course( self.course1_key, 1, 1) self.assertEqual(len(course1_assets), 1, course1_assets) fake_course = CourseLocator('test', 'fake', 'non') course_assets, count = self.contentstore.get_all_content_for_course( fake_course) self.assertEqual(count, 0) self.assertEqual(course_assets, []) @ddt.data(True, False) def test_attrs(self, deprecated): """ Test setting and getting attrs """ self.set_up_assets(deprecated) for filename in self.course1_files: asset_key = self.course1_key.make_asset_key('asset', filename) prelocked = self.contentstore.get_attr(asset_key, 'locked', False) self.contentstore.set_attr(asset_key, 'locked', not prelocked) self.assertEqual( self.contentstore.get_attr(asset_key, 'locked', False), not prelocked) @ddt.data(True, False) def test_copy_assets(self, deprecated): """ copy_all_course_assets """ self.set_up_assets(deprecated) dest_course = CourseLocator('test', 'destination', 'copy') self.contentstore.copy_all_course_assets(self.course1_key, dest_course) for filename in self.course1_files: asset_key = self.course1_key.make_asset_key('asset', filename) dest_key = dest_course.make_asset_key('asset', filename) source = self.contentstore.find(asset_key) copied = self.contentstore.find(dest_key) for propname in ['name', 'content_type', 'length', 'locked']: self.assertEqual(getattr(source, propname), getattr(copied, propname)) __, count = self.contentstore.get_all_content_for_course(dest_course) self.assertEqual(count, len(self.course1_files)) @ddt.data(True, False) def test_delete_assets(self, deprecated): """ delete_all_course_assets """ self.set_up_assets(deprecated) self.contentstore.delete_all_course_assets(self.course1_key) __, count = self.contentstore.get_all_content_for_course( self.course1_key) self.assertEqual(count, 0) # ensure it didn't remove any from other course __, count = self.contentstore.get_all_content_for_course( self.course2_key) self.assertEqual(count, len(self.course2_files))