def test_writebulkjob(mock_dump): """ ensure job def calls json.dump """ bulk = Bulk(test=True) bulk.job = JOB_EXPORTS_CONTACTS mock_dump.return_value = None bulk.write_job('/tmp/pyeloqua_test_bulk_write.json') assert mock_dump.called
def test_start_sync_export_nodef(mock_post): """ except when no job_def """ bulk = Bulk(test=True) bulk.exports('contacts') mock_post.return_value = Mock(ok=True, status_code=200) mock_post.return_value.json.return_value = deepcopy(SYNC_RESPONSE) bulk.start_sync()
def test_option_add_all(): """ add several random options """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.add_options(identifierFieldName='C_EmailAddress', areSystemTimestampsInUTC=True) assert len(bulk.job['options']) == 2
def test_writebulkjob(mock_dump): """ ensure job def calls json.dump """ bulk = Bulk(test=True) bulk.job = JOB_EXPORTS_CONTACTS mock_dump.return_value = None bulk.write_job('/tmp/pyeloqua_test_bulk_write.json') assert mock_dump.called
def test_event_lnk_cntct_fields(mock_get): """ add some linked contact fields to event """ bulk = Bulk(test=True) bulk.exports('events', 1) mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS) bulk.add_linked_fields('contacts', ['C_EmailAddress', 'C_FirstName']) assert bulk.job['fields'] == LINKED_EVENT_CONTACT_FIELDS
def test_cntct_lnk_acct_fields(mock_get): """ add some linked account fields """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS_ACCOUNT) bulk.add_linked_fields('accounts', ['M_CompanyName', 'M_Country']) assert bulk.job['fields'] == LINKED_ACCOUNT_FIELDS
def test_accnt_system_fields_set(mock_get): """ add some account system fields """ bulk = Bulk(test=True) bulk.exports('accounts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS) bulk.add_fields(['accountID', 'createdAt']) assert len(bulk.job['fields']) == 2
def test_add_fields_notfound(mock_get): """ raise exception when field not found """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS) fields = ['C_EmailAddress', 'C_FirstName', 'C_LastName'] bulk.add_fields(fields)
def test_get_fields_specify(mock_get): """ specify elq_object to return fields for """ bulk = Bulk(test=True) bulk.exports('accounts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS) fields = bulk.get_fields(elq_object='contacts') assert fields == ALL_CONTACT_FIELDS
def test_get_fields_cntcts_return(mock_get): """ find all contact fields - return correct items """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS) fields = bulk.get_fields() assert fields == ALL_CONTACT_FIELDS
def test_create_imports_error(mock_post): """ raise exception on bad import def """ bulk = Bulk(test=True) bulk.job = JOB_IMPORTS_CONTACTS_BAD mock_post.return_value = Mock(ok=True, status_code=400) mock_post.return_value.json.return_value = deepcopy( IMPORT_JOB_RESPONSE_BAD) bulk.create_def('test name')
def test_setup_actvty(): """ setup sets obj_id """ bulk = Bulk(test=True) bulk._setup_( 'imports', 'activities', # pylint: disable=W0212 act_type='EmailOpen') assert bulk.job['act_type'] == 'EmailOpen'
def test_get_export_data_rt(mock_data): """ get data from a synced export - return data """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_data.return_value = RETURN_DATA['items'] data = bulk.get_export_data() assert data == RETURN_DATA['items']
def test_get_sync_rejects_rt(mock_data): """ get logs from a sync - return data """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE_SUCCESS mock_data.return_value = RETURN_SYNC_REJECTS['items'] data = bulk.get_sync_rejects() assert data == RETURN_SYNC_REJECTS['items']
def test_create_imports_error(mock_post): """ raise exception on bad import def """ bulk = Bulk(test=True) bulk.job = JOB_IMPORTS_CONTACTS_BAD mock_post.return_value = Mock(ok=True, status_code=400) mock_post.return_value.json.return_value = deepcopy( IMPORT_JOB_RESPONSE_BAD) bulk.create_def('test name')
def test_create_imports_setdef(mock_post): """ set job_def for create import """ bulk = Bulk(test=True) bulk.job = JOB_IMPORTS_CONTACTS mock_post.return_value = Mock(ok=True, status_code=201) mock_post.return_value.json.return_value = deepcopy(IMPORT_JOB_RESPONSE) bulk.create_def('test name') assert bulk.job_def == IMPORT_JOB_RESPONSE
def test_create_imports_setdef(mock_post): """ set job_def for create import """ bulk = Bulk(test=True) bulk.job = JOB_IMPORTS_CONTACTS mock_post.return_value = Mock(ok=True, status_code=201) mock_post.return_value.json.return_value = deepcopy(IMPORT_JOB_RESPONSE) bulk.create_def('test name') assert bulk.job_def == IMPORT_JOB_RESPONSE
def test_get_data_nodata(mock_get): """ get data from an endpoint - no results data """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(RETURN_NORESULTS) return_data = bulk.get_data(endpoint='/dummyurl') assert return_data == []
def test_get_data_return(mock_get): """ get data from an endpoint - return data """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(RETURN_DATA) return_data = bulk.get_data(endpoint='/dummyurl') assert return_data == RETURN_DATA['items']
def test_check_sync_notfinished(mock_get): """ check on status of 'active' sync - return False """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = SYNC_RESPONSE_ACTIVE status = bulk.check_sync() assert status is False
def test_asset_exists_list_id_call(mock_get): """ add exists filter - Shared List by ID, api call """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_LIST_ID) bulk.asset_exists(asset='lists', asset_id=1) mock_get.assert_called_with(url=bulk.bulk_base + '/contacts/lists/1', auth=bulk.auth)
def test_filter_date_end(mock_get): """ add field filter by ending date """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_CONTACT_FIELDS) bulk.filter_date(field='createdAt', end='2017-01-01 00:00:00') assert bulk.job['filters'][ 0] == " '{{Contact.CreatedAt}}' <= '2017-01-01 00:00:00' "
def test_leadscore_fields_name_call(mock_get): """ lead score model by model name api call """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(LEADSCORE_MODEL_NAME) bulk.add_leadscore_fields(name='Model 1') mock_get.assert_called_with(url=bulk.bulk_base + '/contacts/scoring/models?q="name=Model*1"', auth=bulk.auth)
def test_check_sync_error(mock_get): """ check on status of 'error' sync - return True """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = SYNC_RESPONSE_ERROR status = bulk.check_sync() assert status is True
def test_leadscore_fields_id_call(mock_get): """ lead score model by model id api call """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(LEADSCORE_MODEL_ID) bulk.add_leadscore_fields(model_id=1) mock_get.assert_called_with(url=bulk.bulk_base + '/contacts/scoring/models/1', auth=bulk.auth)
def test_add_fields_display(mock_get): """ add contact fields to object by Display Name """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_FIELDS) fields = ['Email Address', 'First Name'] bulk.add_fields(fields) assert bulk.job['fields'] == GOOD_FIELDS['items']
def test_asset_exists_list_nm_call(mock_get): """ add exists filter - Shared List by Name, api call """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_LIST_NAME) bulk.asset_exists(asset='lists', name='Test List 1') mock_get.assert_called_with(url=bulk.bulk_base + '/contacts/lists?q="name=Test*List*1"', auth=bulk.auth)
def test_readbulkjob(): """ import correctly sets Bulk.job """ bulk1 = Bulk(test=True) bulk1.job = JOB_EXPORTS_CONTACTS bulk1.write_job('/tmp/pyeloqua_test_bulk_write.json') bulk2 = Bulk(test=True) bulk2.read_job('/tmp/pyeloqua_test_bulk_write.json') assert bulk2.job == bulk1.job
def test_create_exports_call_sgfilt(mock_post): """ api call for create export - single filter """ bulk = Bulk(test=True) bulk.job = JOB_EXPORTS_CONTACTS_F1 mock_post.return_value = Mock(ok=True, status_code=201) mock_post.return_value.json.return_value = deepcopy(EXPORT_JOB_RESPONSE) bulk.create_def('test name') url = bulk.bulk_base + '/contacts/exports' mock_post.assert_called_with(url=url, auth=bulk.auth, data=DATA_EXPORTS_CONTACTS_F1, headers=POST_HEADERS)
def test_create_imports_call(mock_post): """ api call for create import """ bulk = Bulk(test=True) bulk.job = JOB_IMPORTS_CONTACTS mock_post.return_value = Mock(ok=True, status_code=201) mock_post.return_value.json.return_value = deepcopy(IMPORT_JOB_RESPONSE) bulk.create_def('test name') url = bulk.bulk_base + '/contacts/imports' mock_post.assert_called_with(url=url, auth=bulk.auth, data=DATA_IMPORTS_CONTACTS, headers=POST_HEADERS)
def test_post_data_except_import(mock_post): """ post data to an export definition """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_post.return_value = Mock(ok=True, status_code=201) bulk.post_data(IMPORT_TEST_DATA)
def test_get_export_data_notexp(mock_data): """ get data from a synced export - exception """ bulk = Bulk(test=True) bulk.imports('contacts') bulk.job_def = IMPORT_JOB_DEF mock_data.return_value = RETURN_DATA['items'] bulk.get_export_data()
def test_get_sync_rejects_call(mock_data): """ get logs from a sync - method call """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE_SUCCESS mock_data.return_value = RETURN_SYNC_REJECTS['items'] bulk.get_sync_rejects() mock_data.assert_called_with(endpoint='/syncs/1/rejects')
def test_post_data_except(mock_post): """ post data server error """ bulk = Bulk(test=True) bulk.imports('contacts') bulk.job_def = IMPORT_JOB_DEF mock_post.return_value = Mock(ok=False, status_code=400) mock_post.return_value.json.return_value = {} bulk.post_data(IMPORT_TEST_DATA)
def test_check_sync_update(mock_get): """ check on status of 'active' sync - update job_sync """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = SYNC_RESPONSE_ACTIVE bulk.check_sync() assert bulk.job_sync == SYNC_RESPONSE_ACTIVE
def test_start_sync_export_add(mock_post): """ start syncing an export - add to Bulk """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_post.return_value = Mock(ok=True, status_code=200) mock_post.return_value.json.return_value = deepcopy(SYNC_RESPONSE) bulk.start_sync() assert bulk.job_sync == SYNC_RESPONSE
def test_get_export_data_call(mock_data): """ get data from a synced export - method call """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_data.return_value = RETURN_DATA['items'] bulk.get_export_data() mock_data.assert_called_with(endpoint='/contacts/exports/1/data', max_recs=None, offset=0)
def test_run_sync_timeout(mock_get, mock_post): """ fcn to run a sync end-to-end until finished - timeout """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_post.return_value = Mock(ok=True, status_code=200) mock_post.return_value.json.return_value = deepcopy(SYNC_RESPONSE) mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = SYNC_RESPONSE_ACTIVE bulk.sync(sleeptime=0.03, timeout=0.01)
def test_check_sync_call(mock_get): """ check on status of 'active' sync - api call """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = SYNC_RESPONSE_ACTIVE bulk.check_sync() mock_get.assert_called_with(url=bulk.bulk_base + '/syncs/1', auth=bulk.auth)
def test_leadscore_fields_noparams(mock_get): """ add fields from a lead score model by model name """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(LEADSCORE_MODEL_NAME) bulk.add_leadscore_fields()
def test_asset_exists_list_none(mock_get): """ add exists filter - exception on no params """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_LIST_NAME) bulk.asset_exists(asset='lists')
def test_filter_date_badstr_end(mock_get): """ exception when bad datetime string passed - end """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_CONTACT_FIELDS) bulk.filter_date(field='created', end='2017-01- 00:00:00')
def test_add_fields_actvty(): """ add some activity fields """ bulk = Bulk(test=True) bulk.exports('activities', act_type='EmailOpen') fields = ['ActivityId', 'AssetId'] bulk.add_fields(fields) assert len(bulk.job['fields']) == 2
def test_start_sync_export_nodef(mock_post): """ except when no job_def """ bulk = Bulk(test=True) bulk.exports('contacts') mock_post.return_value = Mock(ok=True, status_code=200) mock_post.return_value.json.return_value = deepcopy(SYNC_RESPONSE) bulk.start_sync()
def test_option_add_all(): """ add several random options """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.add_options(identifierFieldName='C_EmailAddress', areSystemTimestampsInUTC=True) assert len(bulk.job['options']) == 2
def test_get_data_call(mock_get): """ get data from an endpoint - api call """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(RETURN_DATA) bulk.get_data(endpoint='/dummyurl') mock_get.assert_called_with(url=bulk.bulk_base + '/dummyurl?limit=1000&offset=0', auth=bulk.auth)
def test_get_sync_rejects_rt(mock_data): """ get logs from a sync - return data """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_sync = SYNC_RESPONSE_SUCCESS mock_data.return_value = RETURN_SYNC_REJECTS['items'] data = bulk.get_sync_rejects() assert data == RETURN_SYNC_REJECTS['items']
def test_leadscore_fields_name(mock_get): """ add fields from a lead score model by model name """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(LEADSCORE_MODEL_NAME) bulk.add_leadscore_fields(name='Model 1') assert bulk.job['fields'] == LEADSCORE_MODEL_FIELDS
def test_filter_value_equal(mock_get): """ add field filter by value equal """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_CONTACT_FIELDS) bulk.filter_equal(field='contactID', value='12345') assert bulk.job['filters'][0] == " '{{Contact.Id}}' = '12345' "
def test_get_export_data_rt(mock_data): """ get data from a synced export - return data """ bulk = Bulk(test=True) bulk.exports('contacts') bulk.job_def = EXPORT_JOB_DEF mock_data.return_value = RETURN_DATA['items'] data = bulk.get_export_data() assert data == RETURN_DATA['items']
def test_asset_exists_list_id(mock_get): """ add exists filter - Shared List by ID """ bulk = Bulk(test=True) bulk.exports('contacts') mock_get.return_value = Mock(ok=True, status_code=200) mock_get.return_value.json.return_value = deepcopy(GOOD_LIST_ID) bulk.asset_exists(asset='lists', asset_id=1) assert bulk.job['filters'][0] == " EXISTS('{{ContactList[1]}}') "