def testCreateCollectionRemote(self): # drive RESTful API via wrapper tca = TreeCollectionsAPI(self.domains, get_from='api') # remove any prior clones of our tests collection? or let them pile up for now? cl = tca.collection_list test_collection_name = 'My test collection' test_collection_id_base = 'jimallman/my-test-collection' expected_id = test_collection_id_base while expected_id in cl: # keep generating ids until we find a new one expected_id = increment_slug(expected_id) # generate a new collection and name it cjson = get_empty_collection() cjson['name'] = test_collection_name # N.B. this name already exists! should force a new, serial id cslug = slugify(cjson['name']) cid = 'jimallman/{}'.format(cslug) # TODO: generate a unique URL based on this json, and modify it internally? commit_msg = 'Test of creating collections via API wrapper' result = tca.post_collection(cjson, cid, commit_msg) cl = tca.collection_list self.assertEqual(result['error'], 0) self.assertEqual(result['merge_needed'], False) self.assertEqual(result['resource_id'], expected_id) self.assertTrue(expected_id in cl)
def testCreateCollectionRemote(self): # drive RESTful API via wrapper tca = TreeCollectionsAPI(self.domains, get_from='api') # remove any prior clones of our tests collection? or let them pile up for now? cl = tca.collection_list test_collection_name = 'My test collection' test_collection_id_base = 'jimallman/my-test-collection' expected_id = test_collection_id_base while expected_id in cl: # keep generating ids until we find a new one expected_id = increment_slug(expected_id) # generate a new collection and name it cjson = get_empty_collection() cjson['name'] = test_collection_name # N.B. this name already exists! should force a new, serial id cslug = slugify(cjson['name']) cid = 'jimallman/{}'.format(cslug) # TODO: generate a unique URL based on this json, and modify it internally? commit_msg = 'Test of creating collections via API wrapper' result = tca.post_collection(cjson, cid, commit_msg) cl = tca.collection_list self.assertEqual(result['error'], 0) self.assertEqual(result['merge_needed'], False) self.assertEqual(result['resource_id'], expected_id) self.assertTrue(expected_id in cl)
def add_new_collection(self, owner_id, json_repr, auth_info, collection_id=None, commit_msg=''): """Validate and save this JSON. Ensure (and return) a unique collection id""" collection = self._coerce_json_to_collection(json_repr) if collection is None: msg = "File failed to parse as JSON:\n{j}".format(j=json_repr) raise ValueError(msg) if not self._is_valid_collection_json(collection): msg = "JSON is not a valid collection:\n{j}".format(j=json_repr) raise ValueError(msg) if collection_id: # try to use this id found_owner_id, slug = collection_id.split('/') assert found_owner_id == owner_id else: # extract a working title and "slugify" it slug = self._slugify_internal_collection_name(json_repr) collection_id = '{i}/{s}'.format(i=owner_id, s=slug) # Check the proposed id for uniqueness in any case. Increment until # we have a new id, then "reserve" it using a placeholder value. with self._index_lock: while collection_id in self._doc2shard_map: collection_id = increment_slug(collection_id) self._doc2shard_map[collection_id] = None # pass the id and collection JSON to a proper git action new_collection_id = None r = None try: # assign the new id to a shard (important prep for commit_and_try_merge2master) gd_id_pair = self.create_git_action_for_new_collection( new_collection_id=collection_id) new_collection_id = gd_id_pair[1] try: # let's remove the 'url' field; it will be restored when the doc is fetched (via API) del collection['url'] # keep it simple (collection is already validated! no annotations needed!) r = self.commit_and_try_merge2master(file_content=collection, doc_id=new_collection_id, auth_info=auth_info, parent_sha=None, commit_msg=commit_msg, merged_sha=None) except: self._growing_shard.delete_doc_from_index(new_collection_id) raise except: with self._index_lock: if new_collection_id in self._doc2shard_map: del self._doc2shard_map[new_collection_id] raise with self._index_lock: self._doc2shard_map[new_collection_id] = self._growing_shard return new_collection_id, r
def add_new_collection(self, owner_id, json_repr, auth_info, collection_id=None, commit_msg=''): """Validate and save this JSON. Ensure (and return) a unique collection id""" collection = self._coerce_json_to_collection(json_repr) if collection is None: msg = "File failed to parse as JSON:\n{j}".format(j=json_repr) raise ValueError(msg) if not self._is_valid_collection_json(collection): msg = "JSON is not a valid collection:\n{j}".format(j=json_repr) raise ValueError(msg) if collection_id: # try to use this id found_owner_id, slug = collection_id.split('/') assert found_owner_id == owner_id else: # extract a working title and "slugify" it slug = self._slugify_internal_collection_name(json_repr) collection_id = '{i}/{s}'.format(i=owner_id, s=slug) # Check the proposed id for uniqueness in any case. Increment until # we have a new id, then "reserve" it using a placeholder value. with self._index_lock: while collection_id in self._doc2shard_map: collection_id = increment_slug(collection_id) self._doc2shard_map[collection_id] = None # pass the id and collection JSON to a proper git action new_collection_id = None r = None try: # assign the new id to a shard (important prep for commit_and_try_merge2master) gd_id_pair = self.create_git_action_for_new_collection(new_collection_id=collection_id) new_collection_id = gd_id_pair[1] try: # let's remove the 'url' field; it will be restored when the doc is fetched (via API) del collection['url'] # keep it simple (collection is already validated! no annotations needed!) r = self.commit_and_try_merge2master(file_content=collection, doc_id=new_collection_id, auth_info=auth_info, parent_sha=None, commit_msg=commit_msg, merged_sha=None) except: self._growing_shard.delete_doc_from_index(new_collection_id) raise except: with self._index_lock: if new_collection_id in self._doc2shard_map: del self._doc2shard_map[new_collection_id] raise with self._index_lock: self._doc2shard_map[new_collection_id] = self._growing_shard return new_collection_id, r