def test_update_doc(mocker, app): ''' This function tests the simplest case for migration.tasks.update_doc function ''' mocker.patch('elasticsearch.Elasticsearch.index') collection = app.data_db['personalities'] with app.app_context(): # make sure the document does not exist in mongo assert not collection.find_one({'UnitId': 1000}) # this will create the document in mongo update_doc(collection, deepcopy(THE_TESTER)) doc = collection.find_one({'UnitId': 1000}) assert doc['UnitText1']['En'] == 'The Tester' assert doc["UnitId"] == 1000 # check in elasticsearch expected_elasticsearch_body = dict(deepcopy(THE_TESTER), Header={ "En": "Nik Nikos", "He": "_" }, Slug={"En": "luminary_nik-nikos"}, related=["place_some"]) elasticsearch.Elasticsearch.index.assert_called_once_with( body=expected_elasticsearch_body, doc_type='personalities', id=1000, index='bhdata', )
def test_update_person(app): persons = app.data_db['persons'] with app.app_context(): # first, create a tree update_tree(first_tree, app.data_db) # and now the tree update_doc(persons, { 'id': 'I1', 'tree_num': 1, 'tree_file_id': 'initial' }) doc = persons.find_one({'id': 'I1'}) assert doc['tree_version'] == 0 # ensure we keep the old version with app.app_context(): update_tree(dict(num=1, file_id='2', persons=16, date='right now'), app.data_db) # reset the cache app.redis.delete('tree_vers_1') update_doc(persons, {'id': 'I1', 'tree_num': 1, 'tree_file_id': '2'}) assert persons.count({'id': 'I1'}) == 2 # eensure the old I1 is archived doc = persons.find_one({'id': 'I1', 'tree_version': 0}) assert doc['archived'] == True doc = persons.find_one({'id': 'I1', 'tree_version': 1}) assert 'archived' not in doc
def test_updated_doc(mocker, app): ''' testing a creation and an update, ensuring uniquness ''' mocker.patch('elasticsearch.Elasticsearch.index') mocker.patch('elasticsearch.Elasticsearch.update') collection = app.data_db['personalities'] with app.app_context(): the_tester = deepcopy(THE_TESTER) update_doc(collection, the_tester) assert collection.find_one({'UnitId':1000})['Slug']['En'] == "luminary_nik-nikos" elasticsearch.Elasticsearch.index.assert_called_once_with( index="bhdata", doc_type="personalities", id=1000, body = dict(deepcopy(THE_TESTER), Header={"En": "Nik Nikos", "He": "_"}, Slug={"En": "luminary_nik-nikos"}, related=["place_some"]) ) updated_tester = deepcopy(the_tester) updated_tester['Header']['En'] = 'Nikos Nikolveich' updated_tester['UnitText1']['En'] = 'The Great Tester' update_doc(collection, updated_tester) elasticsearch.Elasticsearch.update.assert_called_once_with( index = 'bhdata', doc_type = 'personalities', id = 1000, body = dict(deepcopy(THE_TESTER), Header={"En": "Nikos Nikolveich", "He": "_"}, Slug={"En": "luminary_nik-nikos"}, related=["place_some"], UnitText1={"En": "The Great Tester"}) )
def test_update_person(app): persons = app.data_db['persons'] with app.app_context(): # first, create a tree update_tree(first_tree, app.data_db) # and now the tree update_doc(persons, { 'id': 'I1', 'tree_num': 1, 'tree_file_id': 'initial' } ) doc = persons.find_one({'id':'I1'}) assert doc['tree_version'] == 0 # ensure we keep the old version with app.app_context(): update_tree(dict(num=1, file_id='2', persons=16, date='right now'), app.data_db) # reset the cache app.redis.delete('tree_vers_1') update_doc(persons, { 'id': 'I1', 'tree_num': 1, 'tree_file_id': '2' }) assert persons.count({'id':'I1'}) == 2 # eensure the old I1 is archived doc = persons.find_one({'id':'I1', 'tree_version': 0}) assert doc['archived'] == True doc = persons.find_one({'id':'I1', 'tree_version': 1}) assert 'archived' not in doc
def test_updated_doc(mocker, app): mocker.patch('elasticsearch.Elasticsearch.index') collection = app.data_db['personalities'] es_body = the_tester.copy() del es_body["_id"] with app.app_context(): update_doc(collection, the_tester) original_slug = collection.find_one({'UnitId': 1000})['Slug']['En'] id = collection.find_one({'UnitId': 1000})['_id'] elasticsearch.Elasticsearch.index.assert_called_once_with( body=es_body, doc_type='personalities', id=id, index='db', ) elasticsearch.Elasticsearch.index.reset_mock() updated_tester = the_tester.copy() updated_tester['Header']['En'] = 'Nikos Nikolveich' updated_tester['UnitText1']['En'] = 'The Great Tester' update_doc(collection, updated_tester) del updated_tester['_id'] elasticsearch.Elasticsearch.index.assert_called_once_with( body=updated_tester, doc_type='personalities', id=id, index='db', ) assert original_slug == collection.find_one({'UnitId': 1000})['Slug']['En'] assert collection.count({'UnitId': 1000}) == 1
def test_update_place(mocker, app): mocker.patch('elasticsearch.Elasticsearch.index') mocker.patch('requests.get') resp = requests.Response() resp.status_code = 200 resp._content = json.dumps({'features': [{'geometry': 'geo'}]}) requests.get.return_value = resp collection = app.data_db['places'] with app.app_context(): update_doc(collection, { 'UnitId': 2000, 'UnitText1': {'En': "Oh la la!"}, 'Header': {'En': 'Paris'}, }) doc = collection.find_one({'UnitId':2000}) assert doc['geometry'] == 'geo'
def test_update_place(mocker, app): mocker.patch('elasticsearch.Elasticsearch.index') mocker.patch('requests.get') resp = requests.Response() resp.status_code = 200 resp._content = json.dumps({'features': [{'geometry': 'geo'}]}) requests.get.return_value = resp collection = app.data_db['places'] with app.app_context(): update_doc( collection, { 'UnitId': 2000, 'UnitText1': { 'En': "Oh la la!" }, 'Header': { 'En': 'Paris' }, }) doc = collection.find_one({'UnitId': 2000}) assert doc['geometry'] == 'geo'
def test_update_doc(mocker, app): ''' This function tests the simplest case for migration.tasks.update_doc function ''' mocker.patch('elasticsearch.Elasticsearch.index') collection = app.data_db['personalities'] with app.app_context(): # make sure the document does not exist in mongo assert not collection.find_one({'UnitId':1000}) # this will create the document in mongo update_doc(collection, deepcopy(THE_TESTER)) doc = collection.find_one({'UnitId':1000}) assert doc['UnitText1']['En'] == 'The Tester' assert doc["UnitId"] == 1000 # check in elasticsearch expected_elasticsearch_body = dict(deepcopy(THE_TESTER), Header={"En": "Nik Nikos", "He": "_"}, Slug={"En": "luminary_nik-nikos"}, related=["place_some"]) elasticsearch.Elasticsearch.index.assert_called_once_with(body=expected_elasticsearch_body, doc_type='personalities', id=1000, index='bhdata',)
def test_updated_doc(mocker, app): ''' testing a creation and an update, ensuring uniquness ''' mocker.patch('elasticsearch.Elasticsearch.index') mocker.patch('elasticsearch.Elasticsearch.update') collection = app.data_db['personalities'] with app.app_context(): the_tester = deepcopy(THE_TESTER) update_doc(collection, the_tester) assert collection.find_one({'UnitId': 1000 })['Slug']['En'] == "luminary_nik-nikos" elasticsearch.Elasticsearch.index.assert_called_once_with( index="bhdata", doc_type="personalities", id=1000, body=dict(deepcopy(THE_TESTER), Header={ "En": "Nik Nikos", "He": "_" }, Slug={"En": "luminary_nik-nikos"}, related=["place_some"])) updated_tester = deepcopy(the_tester) updated_tester['Header']['En'] = 'Nikos Nikolveich' updated_tester['UnitText1']['En'] = 'The Great Tester' update_doc(collection, updated_tester) elasticsearch.Elasticsearch.update.assert_called_once_with( index='bhdata', doc_type='personalities', id=1000, body=dict(deepcopy(THE_TESTER), Header={ "En": "Nikos Nikolveich", "He": "_" }, Slug={"En": "luminary_nik-nikos"}, related=["place_some"], UnitText1={"En": "The Great Tester"}))
def test_update_doc(mocker, app): mocker.patch('elasticsearch.Elasticsearch.index') collection = app.data_db['personalities'] with app.app_context(): r = update_doc(collection, the_tester) doc = collection.find_one({'UnitId': 1000}) assert doc['UnitText1']['En'] == 'The Tester' assert doc['_id'] == 1000 body = the_tester.copy() del body["_id"] elasticsearch.Elasticsearch.index.assert_called_once_with( body=body, doc_type='personalities', id=doc['_id'], index='db', ) assert doc['related'] == ['place_some']