def test_03_create_articles_fail(self): # if the account is dud with self.assertRaises(Api401Error): data = ArticleFixtureFactory.make_incoming_api_article() dataset = [data] * 10 ids = ArticlesBulkApi.create(dataset, None) # check that the index is empty, as none of them should have been made all = [x for x in models.Article.iterall()] assert len(all) == 0 # if the data is bust with self.assertRaises(Api400Error): account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal( **JournalFixtureFactory.make_journal_source(in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(1) dataset = dataset[:5] + [{"some": {"junk": "data"}}] + dataset[5:] ids = ArticlesBulkApi.create(dataset, account) # check that the index is empty, as none of them should have been made all = [x for x in models.Article.iterall()] assert len(all) == 0
def test_03_create_articles_fail(self): # if the account is dud with self.assertRaises(Api401Error): data = ArticleFixtureFactory.make_incoming_api_article() dataset = [data] * 10 ids = ArticlesBulkApi.create(dataset, None) # check that the index is empty, as none of them should have been made all = [x for x in models.Article.iterall()] assert len(all) == 0 # if the data is bust with self.assertRaises(Api400Error): account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source(in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(1) dataset = dataset[:5] + [{"some" : {"junk" : "data"}}] + dataset[5:] ids = ArticlesBulkApi.create(dataset, account) # check that the index is empty, as none of them should have been made all = [x for x in models.Article.iterall()] assert len(all) == 0
def bulk_article_delete(): # get the data from the request try: data = json.loads(request.data.decode("utf-8")) except: raise Api400Error("Supplied data was not valid JSON") ArticlesBulkApi.delete(data, current_user._get_current_object()) return no_content()
def bulk_article_delete(): # get the data from the request try: data = json.loads(request.data) except: raise Api400Error("Supplied data was not valid JSON") ArticlesBulkApi.delete(data, current_user._get_current_object()) return no_content()
def test_05_delete_articles_fail(self): # set up all the bits we need dataset = [] for i in range(10): data = ArticleFixtureFactory.make_incoming_api_article( doi="10.123/test/" + str(i), fulltext="http://example.com/" + str(i)) dataset.append(data) # create the main account we're going to work as article_owner = models.Account() article_owner.set_id("test") article_owner.set_name("Tester") article_owner.set_email("*****@*****.**") # create another account which will own the articles so the one # above will be "another user" trying to delete our precious articles. somebody_else = models.Account() somebody_else.set_id("somebody_else") somebody_else.set_name("Somebody Else") somebody_else.set_email("*****@*****.**") # add a journal to the article owner account to create that link # between account and articles journal = models.Journal(**JournalFixtureFactory.make_journal_source( in_doaj=True)) journal.set_owner(article_owner.id) journal.save() time.sleep(1) # call create on the objects (which will save it to the index) ids = ArticlesBulkApi.create(dataset, article_owner) # let the index catch up time.sleep(2) # call delete on the object in various context that will fail # without an account with self.assertRaises(Api401Error): ArticlesBulkApi.delete(ids, None) # with the wrong account article_owner.set_id("other") with self.assertRaises(Api400Error): ArticlesBulkApi.delete(ids, somebody_else) # on the wrong id ids.append("adfasdfhwefwef") article_owner.set_id("test") with self.assertRaises(Api400Error): ArticlesBulkApi.delete(ids, article_owner) with self.assertRaises(Api400Error): ArticlesBulkApi.delete(ids, article_owner)
def test_02_create_duplicate_articles(self): # set up all the bits we need - 10 articles data = ArticleFixtureFactory.make_incoming_api_article() dataset = [data] * 10 # create an account that we'll do the create as account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source(in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(2) # call create on the object (which will save it to the index) with self.assertRaises(Api400Error): ids = ArticlesBulkApi.create(dataset, account) time.sleep(2) with self.assertRaises(ESMappingMissingError): all_articles = models.Article.all()
def test_02_create_duplicate_articles(self): # set up all the bits we need - 10 articles data = ArticleFixtureFactory.make_incoming_api_article() dataset = [data] * 10 # create an account that we'll do the create as account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source( in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(2) # call create on the object (which will save it to the index) with self.assertRaises(Api400Error): ids = ArticlesBulkApi.create(dataset, account) time.sleep(2) with self.assertRaises(ESMappingMissingError): all_articles = models.Article.all()
def test_01_create_articles_success(self): def find_dict_in_list(lst, key, value): for i, dic in enumerate(lst): if dic[key] == value: return i return -1 # set up all the bits we need - 10 articles dataset = [] for i in range(1, 11): data = ArticleFixtureFactory.make_incoming_api_article() # change the DOI and fulltext URLs to escape duplicate detection # and try with multiple articles doi_ix = find_dict_in_list(data['bibjson']['identifier'], 'type', 'doi') if doi_ix == -1: data['bibjson']['identifier'].append({"type": "doi"}) data['bibjson']['identifier'][doi_ix][ 'id'] = '10.0000/SOME.IDENTIFIER.{0}'.format(i) fulltext_url_ix = find_dict_in_list(data['bibjson']['link'], 'type', 'fulltext') if fulltext_url_ix == -1: data['bibjson']['link'].append({"type": "fulltext"}) data['bibjson']['link'][fulltext_url_ix][ 'url'] = 'http://www.example.com/article_{0}'.format(i) dataset.append(deepcopy(data)) # create an account that we'll do the create as account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source( in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(2) # call create on the object (which will save it to the index) ids = ArticlesBulkApi.create(dataset, account) # check that we got the right number of ids back assert len(ids) == 10 assert len(list(set(ids))) == 10, len(list( set(ids))) # are they actually 10 unique IDs? # let the index catch up time.sleep(2) # check that each id was actually created for id in ids: s = models.Article.pull(id) assert s is not None
def test_05_delete_articles_fail(self): # set up all the bits we need dataset = [] for i in range(10): data = ArticleFixtureFactory.make_incoming_api_article(doi="10.123/test/" + str(i), fulltext="http://example.com/" + str(i)) dataset.append(data) # create the main account we're going to work as article_owner = models.Account() article_owner.set_id("test") article_owner.set_name("Tester") article_owner.set_email("*****@*****.**") # create another account which will own the articles so the one # above will be "another user" trying to delete our precious articles. somebody_else = models.Account() somebody_else.set_id("somebody_else") somebody_else.set_name("Somebody Else") somebody_else.set_email("*****@*****.**") # add a journal to the article owner account to create that link # between account and articles journal = models.Journal(**JournalFixtureFactory.make_journal_source(in_doaj=True)) journal.set_owner(article_owner.id) journal.save() time.sleep(1) # call create on the objects (which will save it to the index) ids = ArticlesBulkApi.create(dataset, article_owner) # let the index catch up time.sleep(2) # call delete on the object in various context that will fail # without an account with self.assertRaises(Api401Error): ArticlesBulkApi.delete(ids, None) # with the wrong account article_owner.set_id("other") with self.assertRaises(Api400Error): ArticlesBulkApi.delete(ids, somebody_else) # on the wrong id ids.append("adfasdfhwefwef") article_owner.set_id("test") with self.assertRaises(Api400Error): ArticlesBulkApi.delete(ids, article_owner) with self.assertRaises(Api400Error): ArticlesBulkApi.delete(ids, article_owner)
def test_04_delete_article_success(self): # set up all the bits we need dataset = [] for i in range(10): data = ArticleFixtureFactory.make_incoming_api_article( doi="10.123/test/" + str(i), fulltext="http://example.com/" + str(i)) dataset.append(data) # create the account we're going to work as account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source( in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(1) # call create on the objects (which will save it to the index) ids = ArticlesBulkApi.create(dataset, account) # let the index catch up time.sleep(2) # now delete half of them dels = ids[:5] ArticlesBulkApi.delete(dels, account) # let the index catch up time.sleep(2) for id in dels: ap = models.Article.pull(id) assert ap is None for id in ids[5:]: ap = models.Article.pull(id) assert ap is not None
def test_01_create_articles_success(self): def find_dict_in_list(lst, key, value): for i, dic in enumerate(lst): if dic[key] == value: return i return -1 # set up all the bits we need - 10 articles dataset = [] for i in range(1, 11): data = ArticleFixtureFactory.make_incoming_api_article() # change the DOI and fulltext URLs to escape duplicate detection # and try with multiple articles doi_ix = find_dict_in_list(data['bibjson']['identifier'], 'type', 'doi') if doi_ix == -1: data['bibjson']['identifier'].append({"type" : "doi"}) data['bibjson']['identifier'][doi_ix]['id'] = '10.0000/SOME.IDENTIFIER.{0}'.format(i) fulltext_url_ix = find_dict_in_list(data['bibjson']['link'], 'type', 'fulltext') if fulltext_url_ix == -1: data['bibjson']['link'].append({"type" : "fulltext"}) data['bibjson']['link'][fulltext_url_ix]['url'] = 'http://www.example.com/article_{0}'.format(i) dataset.append(deepcopy(data)) # create an account that we'll do the create as account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source(in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(2) # call create on the object (which will save it to the index) ids = ArticlesBulkApi.create(dataset, account) # check that we got the right number of ids back assert len(ids) == 10 assert len(list(set(ids))) == 10, len(list(set(ids))) # are they actually 10 unique IDs? # let the index catch up time.sleep(2) # check that each id was actually created for id in ids: s = models.Article.pull(id) assert s is not None
def test_04_delete_article_success(self): # set up all the bits we need dataset = [] for i in range(10): data = ArticleFixtureFactory.make_incoming_api_article(doi="10.123/test/" + str(i), fulltext="http://example.com/" + str(i)) dataset.append(data) # create the account we're going to work as account = models.Account() account.set_id("test") account.set_name("Tester") account.set_email("*****@*****.**") # add a journal to the account journal = models.Journal(**JournalFixtureFactory.make_journal_source(in_doaj=True)) journal.set_owner(account.id) journal.save() time.sleep(1) # call create on the objects (which will save it to the index) ids = ArticlesBulkApi.create(dataset, account) # let the index catch up time.sleep(2) # now delete half of them dels = ids[:5] ArticlesBulkApi.delete(dels, account) # let the index catch up time.sleep(2) for id in dels: ap = models.Article.pull(id) assert ap is None for id in ids[5:]: ap = models.Article.pull(id) assert ap is not None
def bulk_article_create(): # get the data from the request try: data = json.loads(request.data.decode("utf-8")) except: raise Api400Error("Supplied data was not valid JSON") # delegate to the API implementation ids = ArticlesBulkApi.create(data, current_user._get_current_object()) # get all the locations for the ids inl = [] for id in ids: inl.append((id, url_for("api_v1.retrieve_article", article_id=id))) # respond with a suitable Created response return bulk_created(inl)
def bulk_article_create(): # get the data from the request try: data = json.loads(request.data) except: raise Api400Error("Supplied data was not valid JSON") # delegate to the API implementation ids = ArticlesBulkApi.create(data, current_user._get_current_object()) # get all the locations for the ids inl = [] for id in ids: inl.append((id, url_for("api_v1.retrieve_article", article_id=id))) # respond with a suitable Created response return bulk_created(inl)
ApplicationsBulkApi.delete(data, current_user) return no_content() ######################################### # Article Bulk API @blueprint.route("/bulk/articles", methods=["POST"]) @api_key_required @write_required(api=True) @swag( swag_summary= 'Bulk article creation <span class="red">[Authenticated, not public]</span>', swag_spec=ArticlesBulkApi.create_swag() ) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes. def bulk_article_create(): # get the data from the request try: data = json.loads(request.data) except: raise Api400Error("Supplied data was not valid JSON") # delegate to the API implementation ids = ArticlesBulkApi.create(data, current_user) # get all the locations for the ids inl = [] for id in ids: inl.append((id, url_for("api_v1.retrieve_article", article_id=id)))
data = json.loads(request.data) except: raise Api400Error("Supplied data was not valid JSON") ApplicationsBulkApi.delete(data, current_user._get_current_object()) return no_content() ######################################### # Article Bulk API @blueprint.route("/bulk/articles", methods=["POST"]) @api_key_required @write_required(api=True) @swag(swag_summary='Bulk article creation <span class="red">[Authenticated, not public]</span>', swag_spec=ArticlesBulkApi.create_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes. def bulk_article_create(): # get the data from the request try: data = json.loads(request.data) except: raise Api400Error("Supplied data was not valid JSON") # delegate to the API implementation ids = ArticlesBulkApi.create(data, current_user._get_current_object()) # get all the locations for the ids inl = [] for id in ids: inl.append((id, url_for("api_v1.retrieve_article", article_id=id)))
data = json.loads(request.data.decode("utf-8")) except: raise Api400Error("Supplied data was not valid JSON") ApplicationsBulkApi.delete(data, current_user._get_current_object()) return no_content() ######################################### # Article Bulk API @blueprint.route("/bulk/articles", methods=["POST"]) @api_key_required @write_required(api=True) @swag(swag_summary='Bulk article creation <span class="red">[Authenticated, not public]</span>', swag_spec=ArticlesBulkApi.create_swag()) # must be applied after @api_key_(optional|required) decorators. They don't preserve func attributes. @analytics.sends_ga_event(GA_CATEGORY, GA_ACTIONS.get('bulk_article_create', 'Bulk article create')) def bulk_article_create(): # get the data from the request try: data = json.loads(request.data.decode("utf-8")) except: raise Api400Error("Supplied data was not valid JSON") # delegate to the API implementation ids = ArticlesBulkApi.create(data, current_user._get_current_object()) # get all the locations for the ids inl = [] for id in ids: inl.append((id, url_for("api_v1.retrieve_article", article_id=id)))