def test_get_item_not_available(self): dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) get_test_item = { 'Item': { 'id': '0', 'testName': "0000-999-090909-090909090" } } insert_opts = { 'table_struct': test_table, 'item': get_test_item } existing_item = dynamodb_wrapper.get_item(**insert_opts) print('item: %s ' % existing_item) self.assertIsNone(existing_item)
def test_get_item_available(self): if self.item_list is None: self.test_scan_items() print("ID IN GET: %s" % self.item_list) dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) get_test_item = { 'Item': { 'id': self.item_list[0]['id'] } } insert_opts = { 'table_struct': test_table, 'item': get_test_item } print("GET %s" % insert_opts) existing_item = dynamodb_wrapper.get_item(**insert_opts) print('found item: %s ' % existing_item) self.assertIsNotNone(existing_item) self.assertEqual(existing_item['id'], self.item_list[0]['id']) self.assertEqual(int(existing_item['deleted']), 0)
def test_scan_items(self): dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) scan_all_items = { 'Item': { } } insert_opts = { 'table_struct': test_table, 'item': scan_all_items } self.item_list = dynamodb_wrapper.scan_item(**insert_opts) self.assertIsNotNone(self.item_list) self.assertTrue(len(self.item_list) > 0) return self.item_list
def test_remove_item(self): dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) get_test_item = { 'Item': { 'id': self.test_id } } insert_opts = { 'table_struct': test_table, 'item': get_test_item } print("OPTS %s" % insert_opts) removed_item = dynamodb_wrapper.remove_item(**insert_opts) print('item removed: %s ' % removed_item) self.assertIsNotNone(removed_item) self.assertEqual(removed_item['deleted'], 1)
def test_insert_item(self): dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) test_item = { 'Item': { 'id': '0', 'testName': 'uuid', 'secondTestId': "hello", 'thirdTestId': int(11), 'forthTestId': "world" } } insert_opts = { 'table_struct': test_table, 'item': test_item } new_item = dynamodb_wrapper.add_item(**insert_opts) self.test_id = str(new_item['id']) self.assertNotEqual(0, test_table['table'].item_count) self.assertNotEqual('0', new_item['id']) print("ID IN INSERT: %s" % self.test_id)
def create_address(self, address_req_obj): self.log.debug(address_req_obj) print("CRTADR: %s" % address_req_obj) if type(address_req_obj) is not dict: address_req_obj = json.loads(address_req_obj) trns_address_obj = None st_obj = None ctry_obj = None trns_address_obj = address_req_obj['address'] stg = address_req_obj['stage'] if trns_address_obj.get('country') is not None: ctry_obj = self.country_svc.create_country({ 'country_obj': trns_address_obj['country'], 'stage': address_req_obj['stage'] }) if trns_address_obj.get('state') is not None\ and trns_address_obj.get('country') is not None: state_obj = trns_address_obj['state'] state_obj['countryCode'] =\ trns_address_obj['country']['countryCode'] st_obj = self.state_svc.create_state({ 'state_obj': state_obj, 'stage': address_req_obj['stage'] }) if st_obj is not None: self.log.debug("ST_OBJ: %s" % st_obj) trns_address_obj['stateId'] = st_obj['stateId'] if ctry_obj is not None: self.log.debug("CTRY_OBJ: %s" % ctry_obj) trns_address_obj['countryId'] = ctry_obj['countryId'] adr_table = dynamodb_wrapper.create_table(**{ 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': "address", 'stage': stg }) adr_obj = dynamodb_wrapper.add_item(**{ 'table_struct': adr_table, 'item': { 'Item': trns_address_obj } }) trns_address_obj['addressId'] = adr_obj['addressId'] return trns_address_obj
def setUp(self): if self.table is None: dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } self.table = dynamodb_wrapper.create_table(**dynamodb_options) self.defaults_list = self.table['defaults']
def test_create_table(self): dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) self.assertIsNotNone( test_table['table'] )
def get_states(self, state_req_obj): st_table = dynamodb_wrapper.create_table(**{ 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': "state", 'stage': state_req_obj['stage'] }) sts_obj = dynamodb_wrapper.scan_item(**{ 'table_struct': st_table, 'item': { 'Item': { } } }) return sts_obj
def remove_address(self, req_obj): tbl_opts = { 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': 'address', 'stage': req_obj['stage'] } itm_opts = { 'table_struct': dynamodb_wrapper.create_table(**tbl_opts), 'item': { 'Item': { 'addressId': req_obj['id'] } } } return dynamodb_wrapper.remove_item(**itm_opts)
def list_addresses(self, req_obj): tbl_opts = { 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': 'address', 'stage': req_obj['stage'] } itm_opts = { 'table_struct': dynamodb_wrapper.create_table(**tbl_opts), 'item': { 'Item': { } } } return dynamodb_wrapper.scan_item(**itm_opts)
def get_countries(self, country_req_obj): ct_table = dynamodb_wrapper.create_table(**{ 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': 'country', 'stage': country_req_obj['stage'] }) self.country_lst = dynamodb_wrapper.scan_item(**{ 'table_struct': ct_table, 'item': { 'Item': {} } }) return self.country_lst
def get_address(self, req_obj): self.log.debug("GETADR: %s" % req_obj) tbl_opts = { 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': 'address', 'stage': req_obj['stage'] } itm_opts = { 'table_struct': dynamodb_wrapper.create_table(**tbl_opts), 'item': { 'Item': { 'addressId': req_obj['id'] } } } return dynamodb_wrapper.get_item(**itm_opts)
def get_state(self, state_req_obj): st_table = dynamodb_wrapper.create_table(**{ 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': "state", 'stage': state_req_obj['stage'] }) st_obj = dynamodb_wrapper.scan_item(**{ 'table_struct': st_table, 'item': { 'Item': state_req_obj['state_obj'] } }) if type(st_obj) is list: if len(st_obj) > 0: st_obj = st_obj[0] else: st_obj = None return st_obj
def create_country(self, country_req_obj): ct_obj = self.get_country(country_req_obj) self.log.debug("CTROBJ: %s" % ct_obj) if ct_obj is not None: return ct_obj ct_table = dynamodb_wrapper.create_table(**{ 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': 'country', 'stage': country_req_obj['stage'] }) self.country_obj = dynamodb_wrapper.add_item(**{ 'table_struct': ct_table, 'item': { 'Item': country_req_obj['country_obj'] } }) return self.country_obj
def create_state(self, state_req_obj): st_obj = self.get_state(state_req_obj) self.log.debug("STOBJ: %s" % st_obj) if st_obj is not None: return st_obj st_table = dynamodb_wrapper.create_table(**{ 'dynamodb': dynamodb_wrapper.get_resource(), 'table_name': "state", 'stage': state_req_obj['stage'] }) st_obj = dynamodb_wrapper.add_item(**{ 'table_struct': st_table, 'item': { 'Item': state_req_obj['state_obj'] } }) return st_obj
def setUp(self): if self.test_id is None: dynamodb_test = dynamodb_wrapper.get_resource() dynamodb_options = { 'dynamodb': dynamodb_test, 'table_name': 'TestTable', 'stage': 'dev' } test_table = dynamodb_wrapper.create_table(**dynamodb_options) scan_all_items = { 'Item': { } } insert_opts = { 'table_struct': test_table, 'item': scan_all_items } item_list = dynamodb_wrapper.scan_item(**insert_opts) self.test_id = item_list[0]['id']
from sqs_wrapper import send_json, return_all, create_queue from dynamodb_wrapper import create_table, send_data, return_data #prepare enviroment create_table() create_queue() send_json({ 'first_number' : 14, 'second_number' : 20 }) send_json({ 'first_number' : 15, 'second_number' : 20 }) result = return_all() for r in result: send_data(r) res_dynamo = return_data() for r in res_dynamo: print(r)
def update_address(self, address_req_obj): from address import merchant_type import utils st_obj = None ctry_obj = None trns_address_obj = address_req_obj['address'] address_req_obj['id'] = trns_address_obj['addressId'] adr_obj = self.get_address(address_req_obj) self.log.debug("UPDADR: %s" % adr_obj) if trns_address_obj.get('country') is not None: cntry_obj = trns_address_obj['country'] ctry_obj = self.country_svc.create_country({ 'country_obj': { 'countryName': cntry_obj['countryName'], 'countryCode': cntry_obj.get('countryCode') }, 'stage': address_req_obj['stage'] }) if trns_address_obj.get('state') is not None: state_obj = trns_address_obj['state'] if trns_address_obj['state'].get('countryCode') is None \ and trns_address_obj.get('country') is not None: state_obj['countryCode'] =\ trns_address_obj['country']['countryCode'] self.log.debug("CRTST: %s" % state_obj) st_obj = self.state_svc.create_state({ 'state_obj': { 'stateName': state_obj['stateName'], 'countryCode': state_obj['countryCode'] }, 'stage': address_req_obj['stage'] }) self.log.debug("STC: %s" % st_obj) if st_obj is not None: self.log.debug("ST_OBJ: %s" % st_obj) trns_address_obj['stateId'] = st_obj['stateId'] if ctry_obj is not None: self.log.debug("CTRY_OBJ: %s" % ctry_obj) trns_address_obj['countryId'] = ctry_obj['countryId'] adr_obj = utils.update_old_with_new( merchant_type.TRANSPIRE['address'] if type(merchant_type.TRANSPIRE) is dict else merchant_type.TRANSPIRE.value['address'], adr_obj, trns_address_obj ) address_table_opts = {} address_item_opts = {} address_table_opts['dynamodb'] = dynamodb_wrapper.get_resource() address_table_opts['table_name'] = "address" address_table_opts['stage'] = address_req_obj['stage'] address_item_opts['table_struct'] = dynamodb_wrapper.create_table( **address_table_opts) address_item_opts['item'] = {'Item': adr_obj} self.log.debug("UPDARDOBJ %s" % adr_obj) upd_adr = dynamodb_wrapper.add_item(**address_item_opts) self.log.debug("UPDARD_ITM: %s" % upd_adr) return upd_adr