def test_copy_except_deleted_and_reply(wa_list): # insert a reply wa_list.append(make_wa_object( age_in_hours=8, reply_to=wa_list[0]['id'])) # add a deleted wa_list[1]['platform']['deleted'] = True original_total = len(wa_list) # import catcha list import_resp = CRUD.import_annos(wa_list) assert int(import_resp['original_total']) == original_total assert int(import_resp['total_success']) == original_total assert int(import_resp['total_failed']) == 0 anno_list = CRUD.select_annos( context_id=wa_list[0]['platform']['context_id'], collection_id=wa_list[0]['platform']['collection_id'], platform_name=wa_list[0]['platform']['platform_name'] ) select_total = len(anno_list) for x in anno_list: print('search returned ({})'.format(x.anno_id)) # discount the deleted and reply assert select_total == (original_total - 2) copy_resp = CRUD.copy_annos( anno_list, 'another_fake_context', 'collection_x') assert int(copy_resp['original_total']) == (original_total - 2) assert int(copy_resp['total_success']) == (original_total - 2) assert int(copy_resp['total_failed']) == 0
def test_import_deleted_reply_ok(): catcha_dict = {} catcha_dict['c_regular'] = make_wa_object(age_in_hours=10) catcha_dict['c_regular']['id'] = 'regular' catcha_dict['c_parent1'] = make_wa_object(age_in_hours=9) catcha_dict['c_parent1']['id'] = 'parent1' catcha_dict['c_reply1'] = make_wa_object( age_in_hours=8, reply_to=catcha_dict['c_parent1']['id']) catcha_dict['c_reply1']['id'] = 'reply1' catcha_dict['c_deleted'] = make_wa_object(age_in_hours=7) catcha_dict['c_deleted']['platform']['deleted'] = True catcha_dict['c_deleted']['id'] = 'deleted' # import all resp = CRUD.import_annos(catcha_dict.values()) assert resp['total_failed'] == 0 assert len(resp['deleted']) == 1 assert len(resp['reply']) == 1 c_parent1 = Anno._default_manager.get(pk=catcha_dict['c_parent1']['id']) assert c_parent1.total_replies == 1 c_reply1 = Anno._default_manager.get(pk=catcha_dict['c_reply1']['id']) assert c_reply1.anno_reply_to.anno_id == c_parent1.anno_id c_deleted = Anno._default_manager.get(pk=catcha_dict['c_deleted']['id']) assert c_deleted.anno_deleted
def test_import_anno_ok_2(wa_image): catcha = wa_image now = datetime.now(tz.tzutc()) # import first because CRUD.create changes created time in input catcha['id'] = 'naomi-xx-imported' resp = CRUD.import_annos([catcha]) x2 = Anno._default_manager.get(pk=catcha['id']) assert x2 is not None assert Anno._default_manager.count() == 1 # x2 was created more in the past? import preserves created date? delta = timedelta(hours=25) assert x2.created < (now - delta) # about to create catcha['id'] = 'naomi-xx' x1 = CRUD.create_anno(catcha) assert x1 is not None assert Anno._default_manager.count() == 2 # x1 was created less than 1m ago? delta = timedelta(minutes=1) assert (now - delta) < x1.created
def test_copy_ok(wa_list): original_total = len(wa_list) # import catcha list import_resp = CRUD.import_annos(wa_list) assert int(import_resp['original_total']) == original_total assert int(import_resp['total_success']) == original_total assert int(import_resp['total_failed']) == 0 anno_list = CRUD.select_annos( context_id=wa_list[0]['platform']['context_id'], collection_id=wa_list[0]['platform']['collection_id'], platform_name=wa_list[0]['platform']['platform_name'], ) select_total = len(anno_list) assert select_total == original_total copy_resp = CRUD.copy_annos( anno_list, 'another_fake_context', 'collection_x') assert int(copy_resp['original_total']) == original_total assert int(copy_resp['total_success']) == original_total assert int(copy_resp['total_failed']) == 0
def test_import_deleted_parent_ok(): catcha_dict = {} catcha_dict['c_regular'] = make_wa_object(age_in_hours=10) catcha_dict['c_regular']['id'] = 'regular' catcha_dict['c_parent1'] = make_wa_object(age_in_hours=9) catcha_dict['c_parent1']['id'] = 'parent1_deleted' catcha_dict['c_parent1']['platform']['deleted'] = True catcha_dict['c_reply1'] = make_wa_object( age_in_hours=8, reply_to=catcha_dict['c_parent1']['id']) catcha_dict['c_reply1']['id'] = 'reply1' # import all resp = CRUD.import_annos(catcha_dict.values()) assert resp['total_failed'] == 0 assert len(resp['deleted']) == 1 assert len(resp['reply']) == 1 c_parent1 = Anno._default_manager.get(pk=catcha_dict['c_parent1']['id']) #assert c_parent1.total_replies == 1 # have to check that reply is associated, but marked for deletion assert c_parent1.anno_set.count() == 1 assert c_parent1.anno_deleted c_reply1 = Anno._default_manager.get(pk=catcha_dict['c_reply1']['id']) assert c_reply1.anno_reply_to.anno_id == c_parent1.anno_id assert c_reply1.anno_deleted c_regular = Anno._default_manager.get(pk=catcha_dict['c_regular']['id']) assert c_regular.total_replies == 0 assert c_regular.anno_deleted is False
def handle(self, *args, **kwargs): filepath = kwargs['filepath'] with open(filepath, 'r') as f: catcha_list = json.load(f) resp = CRUD.import_annos(catcha_list) print(json.dumps(resp, indent=4))
def test_copy_back_compat(wa_list): original_total = len(wa_list) # import catcha list import_resp = CRUD.import_annos(wa_list) assert int(import_resp['original_total']) == original_total assert int(import_resp['total_success']) == original_total assert int(import_resp['total_failed']) == 0 anno_list = CRUD.select_annos( context_id='fake_context', collection_id='fake_collection', platform_name=CATCH_DEFAULT_PLATFORM_NAME, ) select_total = anno_list.count() assert select_total == original_total # setup copy call to client params = { 'platform_name': CATCH_DEFAULT_PLATFORM_NAME, 'source_context_id': 'fake_context', 'source_collection_id': 'fake_collection', 'target_context_id': 'another_fake_context', 'target_collection_id': 'another_fake_collection', 'back_compat': True, } c = Consumer._default_manager.create() payload = make_jwt_payload(apikey=c.consumer, user='******', override=['CAN_COPY']) token = make_encoded_token(c.secret_key, payload) client = Client() copy_url = reverse('copy_api') response = client.post(copy_url, data=json.dumps(params), HTTP_X_ANNOTATOR_AUTH_TOKEN=token, content_type='application/json') assert response.status_code == 200 resp = json.loads(response.content.decode('utf-8')) assert int(resp['original_total']) == original_total assert int(resp['total_success']) == original_total assert int(resp['total_failed']) == 0 # search via back-compat api compat_search_url = reverse('compat_search') response = client.get(compat_search_url, data={'context_id': 'another_fake_context'}, HTTP_X_ANNOTATOR_AUTH_TOKEN=token) assert response.status_code == 200 content = json.loads(response.content.decode('utf-8')) assert int(content['total']) == original_total assert int(content['size_failed']) == 0
def push_from_file(workdir, filepath): print_db_info() with open(filepath, 'r') as f: catcha_list = json.load(f) # order by id (prevent a reply before associated annotation) ordered_catcha_list = sorted(catcha_list, key=lambda k: k['id']) resp = CRUD.import_annos(ordered_catcha_list) failed_list = resp['failed'] save_to_file(outdir=workdir, filename='fail_to_push_from_file_{}'.format( os.path.basename(filepath)), json_content=failed_list)
def test_import_anno_ok(wa_image): catcha = wa_image catcha_reply = make_wa_object( age_in_hours=1, reply_to=catcha['id']) now = datetime.now(tz.tzutc()) resp = CRUD.import_annos([catcha, catcha_reply]) x2 = Anno._default_manager.get(pk=catcha['id']) assert x2 is not None # preserve replies? assert x2.total_replies == 1 assert x2.replies[0].anno_id == catcha_reply['id'] # import preserve created date? delta = timedelta(hours=25) assert x2.created < (now - delta)
def test_remove_in_2step(wa_list): # insert a reply wa_list.append(make_wa_object( age_in_hours=8, reply_to=wa_list[0]['id'])) # add a deleted wa_list[1]['platform']['deleted'] = True original_total = len(wa_list) # import catcha list import_resp = CRUD.import_annos(wa_list) assert int(import_resp['original_total']) == original_total assert int(import_resp['total_success']) == original_total assert int(import_resp['total_failed']) == 0 # delete annotations (not all soft-deleted) delete_resp = CRUD.delete_annos( context_id=wa_list[1]['platform']['context_id']) assert int(delete_resp['failed']) == 0 # discount the deleted and reply assert int(delete_resp['succeeded']) == (original_total -2) anno_list = CRUD.select_annos( context_id=wa_list[0]['platform']['context_id'], is_copy=False ) # didn't true-delete anything yet assert len(anno_list) == original_total # delete annotations (true-delete) delete2_resp = CRUD.delete_annos( context_id=wa_list[1]['platform']['context_id']) assert int(delete2_resp['failed']) == 0 assert int(delete2_resp['succeeded']) == original_total anno_list = CRUD.select_annos( context_id=wa_list[0]['platform']['context_id'], is_copy=False ) # true-delete all annotations assert len(anno_list) == 0