def commit_library_and_verify(library_key): """ Commit library changes, and verify that there are no uncommited changes anymore """ last_published = ContentLibraryIndexer.get_libraries([library_key])[0]['last_published'] self._commit_library_changes(str(library_key)) response = ContentLibraryIndexer.get_libraries([library_key])[0] self.assertEqual(response['has_unpublished_changes'], False) self.assertEqual(response['has_unpublished_deletes'], False) self.assertGreaterEqual(response['last_published'], last_published) return response
def verify_uncommitted_libraries(library_key, has_unpublished_changes, has_unpublished_deletes): """ Verify uncommitted changes and deletes in the index """ response = ContentLibraryIndexer.get_libraries([library_key])[0] self.assertEqual(response['has_unpublished_changes'], has_unpublished_changes) self.assertEqual(response['has_unpublished_deletes'], has_unpublished_deletes) return response
def test_update_libraries(self): """ Test if indexes are updated when libraries are updated """ lib = self._create_library(slug="test-lib-update", title="Title", description="Description") library_key = LibraryLocatorV2.from_string(lib['id']) self._update_library(lib['id'], title="New Title", description="New Title") response = ContentLibraryIndexer.get_libraries([library_key])[0] self.assertEqual(response['id'], lib['id']) self.assertEqual(response['title'], "New Title") self.assertEqual(response['description'], "New Title") self.assertEqual(response['uuid'], lib['bundle_uuid']) self.assertEqual(response['num_blocks'], 0) self.assertEqual(response['version'], lib['version']) self.assertEqual(response['last_published'], None) self.assertEqual(response['has_unpublished_changes'], False) self.assertEqual(response['has_unpublished_deletes'], False) self._delete_library(lib['id']) with self.assertRaises(LibraryNotIndexedException): ContentLibraryIndexer.get_libraries([library_key])
def get_metadata_from_index(queryset): """ Take a list of ContentLibrary objects and return metadata stored in ContentLibraryIndex. """ metadata = None if ContentLibraryIndexer.indexing_is_enabled(): try: library_keys = [lib.library_key for lib in queryset] metadata = ContentLibraryIndexer.get_libraries(library_keys) except (LibraryNotIndexedException, KeyError, ElasticConnectionError) as e: log.exception(e) # If ContentLibraryIndex is not available, we query blockstore for a limited set of metadata if metadata is None: uuids = [lib.bundle_uuid for lib in queryset] bundles = get_bundles(uuids) bundle_dict = { bundle.uuid: { 'uuid': bundle.uuid, 'title': bundle.title, 'description': bundle.description, 'version': bundle.latest_version, } for bundle in bundles } metadata = [bundle_dict[uuid] for uuid in uuids] libraries = [ ContentLibraryMetadata( key=lib.library_key, bundle_uuid=metadata[i]['uuid'], title=metadata[i]['title'], description=metadata[i]['description'], version=metadata[i]['version'], allow_public_learning=queryset[i].allow_public_learning, allow_public_read=queryset[i].allow_public_read, num_blocks=metadata[i].get('num_blocks'), last_published=metadata[i].get('last_published'), has_unpublished_changes=metadata[i].get('has_unpublished_changes'), has_unpublished_deletes=metadata[i].get('has_unpublished_deletes'), ) for i, lib in enumerate(queryset) ] return libraries
def test_schema_updates(self): """ Test that outdated indexes aren't retrieved """ result = self._create_library(slug="test-lib-schemaupdates-1", title="Title 1", description="Description") library_key = LibraryLocatorV2.from_string(result['id']) ContentLibraryIndexer.get_libraries([library_key]) with patch("openedx.core.djangoapps.content_libraries.libraries_index.ContentLibraryIndexer.SCHEMA_VERSION", new=1): with self.assertRaises(LibraryNotIndexedException): ContentLibraryIndexer.get_libraries([library_key]) call_command("reindex_content_library", all=True, quiet=True) ContentLibraryIndexer.get_libraries([library_key])
def test_index_libraries(self): """ Test if libraries are being indexed correctly """ result1 = self._create_library(slug="test-lib-index-1", title="Title 1", description="Description") result2 = self._create_library(slug="test-lib-index-2", title="Title 2", description="Description") response = self.searcher.search(doc_type=ContentLibraryIndexer.LIBRARY_DOCUMENT_TYPE, filter_dictionary={}) self.assertEqual(response['total'], 2) for result in [result1, result2]: library_key = LibraryLocatorV2.from_string(result['id']) response = ContentLibraryIndexer.get_libraries([library_key])[0] self.assertEqual(response['id'], result['id']) self.assertEqual(response['title'], result['title']) self.assertEqual(response['description'], result['description']) self.assertEqual(response['uuid'], result['bundle_uuid']) self.assertEqual(response['num_blocks'], 0) self.assertEqual(response['version'], result['version']) self.assertEqual(response['last_published'], None) self.assertEqual(response['has_unpublished_changes'], False) self.assertEqual(response['has_unpublished_deletes'], False)