def test_bundle_cache(self): """ Test caching data related to a bundle (no draft) """ cache = BundleCache(self.bundle.uuid) key1 = ("some", "key", "1") key2 = ("key2", ) value1 = "value1" cache.set(key1, value1) value2 = {"this is": "a dict", "for": "key2"} cache.set(key2, value2) self.assertEqual(cache.get(key1), value1) self.assertEqual(cache.get(key2), value2) # Now publish a new version of the bundle: api.write_draft_file( self.draft.uuid, "test.txt", "we need a changed file in order to publish a new version") api.commit_draft(self.draft.uuid) # Now the cache should be invalidated # (immediately since we set MAX_BLOCKSTORE_CACHE_DELAY to 0) self.assertEqual(cache.get(key1), None) self.assertEqual(cache.get(key2), None)
def test_drafts_and_files(self): """ Test creating, reading, writing, committing, and reverting drafts and files. """ coll = api.create_collection("Test Collection") bundle = api.create_bundle(coll.uuid, title="Earth 🗿 Bundle", slug="earth", description="another test bundle") # Create a draft draft = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") self.assertEqual(draft.bundle_uuid, bundle.uuid) self.assertEqual(draft.name, "test-draft") self.assertGreaterEqual(draft.updated_at.year, 2019) # And retrieve it again: draft2 = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") self.assertEqual(draft, draft2) # Also test retrieving using get_draft draft3 = api.get_draft(draft.uuid) self.assertEqual(draft, draft3) # Write a file into the bundle: api.write_draft_file(draft.uuid, "test.txt", "initial version") # Now the file should be visible in the draft: draft_contents = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) self.assertEqual(draft_contents, "initial version") api.commit_draft(draft.uuid) # Write a new version into the draft: api.write_draft_file(draft.uuid, "test.txt", "modified version") published_contents = api.get_bundle_file_data(bundle.uuid, "test.txt") self.assertEqual(published_contents, "initial version") draft_contents2 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) self.assertEqual(draft_contents2, "modified version") # Now delete the draft: api.delete_draft(draft.uuid) draft_contents3 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) # Confirm the file is now reset: self.assertEqual(draft_contents3, "initial version") # Finaly, test the get_bundle_file* methods: file_info1 = api.get_bundle_file_metadata(bundle.uuid, "test.txt") self.assertEqual(file_info1.path, "test.txt") self.assertEqual(file_info1.size, len("initial version")) self.assertEqual(file_info1.hash_digest, "a45a5c6716276a66c4005534a51453ab16ea63c4") self.assertEqual(api.get_bundle_files(bundle.uuid), [file_info1]) self.assertEqual(api.get_bundle_files_dict(bundle.uuid), { "test.txt": file_info1, })
def test_drafts_and_files(self): """ Test creating, reading, writing, committing, and reverting drafts and files. """ coll = api.create_collection("Test Collection") bundle = api.create_bundle(coll.uuid, title="Earth 🗿 Bundle", slug="earth", description="another test bundle") # Create a draft draft = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") assert draft.bundle_uuid == bundle.uuid assert draft.name == 'test-draft' assert draft.updated_at.year >= 2019 # And retrieve it again: draft2 = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") assert draft == draft2 # Also test retrieving using get_draft draft3 = api.get_draft(draft.uuid) assert draft == draft3 # Write a file into the bundle: api.write_draft_file(draft.uuid, "test.txt", b"initial version") # Now the file should be visible in the draft: draft_contents = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) assert draft_contents == b'initial version' api.commit_draft(draft.uuid) # Write a new version into the draft: api.write_draft_file(draft.uuid, "test.txt", b"modified version") published_contents = api.get_bundle_file_data(bundle.uuid, "test.txt") assert published_contents == b'initial version' draft_contents2 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) assert draft_contents2 == b'modified version' # Now delete the draft: api.delete_draft(draft.uuid) draft_contents3 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) # Confirm the file is now reset: assert draft_contents3 == b'initial version' # Finaly, test the get_bundle_file* methods: file_info1 = api.get_bundle_file_metadata(bundle.uuid, "test.txt") assert file_info1.path == 'test.txt' assert file_info1.size == len(b'initial version') assert file_info1.hash_digest == 'a45a5c6716276a66c4005534a51453ab16ea63c4' assert list(api.get_bundle_files(bundle.uuid)) == [file_info1] assert api.get_bundle_files_dict(bundle.uuid) == { 'test.txt': file_info1 }
def publish_changes(library_key): """ Publish all pending changes to the specified library. """ ref = ContentLibrary.objects.get_by_key(library_key) bundle = get_bundle(ref.bundle_uuid) if DRAFT_NAME in bundle.drafts: # pylint: disable=unsupported-membership-test draft_uuid = bundle.drafts[DRAFT_NAME] # pylint: disable=unsubscriptable-object commit_draft(draft_uuid) else: return # If there is no draft, no action is needed. LibraryBundle(library_key, ref.bundle_uuid).cache.clear() LibraryBundle(library_key, ref.bundle_uuid, draft_name=DRAFT_NAME).cache.clear()
def test_bundle_cache_clear(self): """ Test the cache clear() method """ cache = BundleCache(self.bundle.uuid) key1 = ("some", "key", "1") value1 = "value1" cache.set(key1, value1) assert cache.get(key1) == value1 # Now publish a new version of the bundle: api.write_draft_file(self.draft.uuid, "test.txt", "we need a changed file in order to publish a new version") api.commit_draft(self.draft.uuid) # Now the cache will not be immediately invalidated; it takes up to MAX_BLOCKSTORE_CACHE_DELAY seconds. # Since this is a new bundle and we _just_ accessed the cache for the first time, we can be confident # it won't yet be automatically invalidated. assert cache.get(key1) == value1 # Now "clear" the cache, forcing the check of the new version: cache.clear() assert cache.get(key1) is None
def test_links(self): """ Test operations involving bundle links. """ coll = api.create_collection("Test Collection") # Create two library bundles and a course bundle: lib1_bundle = api.create_bundle(coll.uuid, title="Library 1", slug="lib1") lib1_draft = api.get_or_create_bundle_draft(lib1_bundle.uuid, draft_name="test-draft") lib2_bundle = api.create_bundle(coll.uuid, title="Library 1", slug="lib2") lib2_draft = api.get_or_create_bundle_draft(lib2_bundle.uuid, draft_name="other-draft") course_bundle = api.create_bundle(coll.uuid, title="Library 1", slug="course") course_draft = api.get_or_create_bundle_draft(course_bundle.uuid, draft_name="test-draft") # To create links, we need valid BundleVersions, which requires having committed at least one change: api.write_draft_file(lib1_draft.uuid, "lib1-data.txt", "hello world") api.commit_draft(lib1_draft.uuid) # Creates version 1 api.write_draft_file(lib2_draft.uuid, "lib2-data.txt", "hello world") api.commit_draft(lib2_draft.uuid) # Creates version 1 # Lib2 has no links: self.assertFalse(api.get_bundle_links(lib2_bundle.uuid)) # Create a link from lib2 to lib1 link1_name = "lib2_to_lib1" api.set_draft_link(lib2_draft.uuid, link1_name, lib1_bundle.uuid, version=1) # Now confirm the link exists in the draft: lib2_draft_links = api.get_bundle_links(lib2_bundle.uuid, use_draft=lib2_draft.name) self.assertIn(link1_name, lib2_draft_links) self.assertEqual(lib2_draft_links[link1_name].direct.bundle_uuid, lib1_bundle.uuid) self.assertEqual(lib2_draft_links[link1_name].direct.version, 1) # Now commit the change to lib2: api.commit_draft(lib2_draft.uuid) # Creates version 2 # Now create a link from course to lib2 link2_name = "course_to_lib2" api.set_draft_link(course_draft.uuid, link2_name, lib2_bundle.uuid, version=2) api.commit_draft(course_draft.uuid) # And confirm the link exists in the resulting bundle version: course_links = api.get_bundle_links(course_bundle.uuid) self.assertIn(link2_name, course_links) self.assertEqual(course_links[link2_name].direct.bundle_uuid, lib2_bundle.uuid) self.assertEqual(course_links[link2_name].direct.version, 2) # And since the links go course->lib2->lib1, course has an indirect link to lib1: self.assertEqual(course_links[link2_name].indirect[0].bundle_uuid, lib1_bundle.uuid) self.assertEqual(course_links[link2_name].indirect[0].version, 1) # Finally, test deleting a link from course's draft: api.set_draft_link(course_draft.uuid, link2_name, None, None) self.assertFalse(api.get_bundle_links(course_bundle.uuid, use_draft=course_draft.name))