def bond_with_subs(): """Return a Bond with an existing set of subscribers""" subs = { 'eniesc200': Subscriber(sid='eniesc200', name='Ed', email='*****@*****.**'), 'tfomoo100': Subscriber(sid='tfomoo100', name='Tom', email='*****@*****.**'), 'bfoere300': Subscriber(sid='bfoere300', name='Bill', email='*****@*****.**') } return Bond(bond_id='HostAcctA-SubAcctB', host_account_id='HostAcctA', sub_account_id='SubAcctB', host_cost_center='HostCostCenterA', sub_cost_center='SubCostCenterB', subscribers=subs)
def test_add_subscriber_exists(table, bond_with_subs): """ Try to add a subscriber that is already listed in the bond. It will overwrite what was there, i.e. an update. """ table.put_item( Item={ 'bond_id': bond_with_subs.bond_id, 'host_account_id': bond_with_subs.host_account_id, 'sub_account_id': bond_with_subs.sub_account_id, 'host_cost_center': bond_with_subs.host_cost_center, 'sub_cost_center': bond_with_subs.sub_cost_center, 'subscribers': jsonable_encoder(bond_with_subs.subscribers) }) crud.add_subscriber( bond_with_subs.bond_id, Subscriber(sid='tfomoo100', name='Foo', email='*****@*****.**')) # make sure the subscriber was added. response = table.query( ProjectionExpression="bond_id, host_account_id, sub_account_id, " "host_cost_center, sub_cost_center, subscribers", KeyConditionExpression=Key('bond_id').eq(bond_with_subs.bond_id)) assert response.get("Count") == 1 items = response.get("Items") subs = items[0]["subscribers"] assert len(subs) == 3 assert subs['tfomoo100']['sid'] == 'tfomoo100' assert subs['tfomoo100']['name'] == 'Foo' assert subs['tfomoo100']['email'] == '*****@*****.**'
def test_add_subscriber_full_bond(table, bond_with_subs): """ Add a new subscriber to a bond that already has subscribers. """ table.put_item( Item={ 'bond_id': bond_with_subs.bond_id, 'host_account_id': bond_with_subs.host_account_id, 'sub_account_id': bond_with_subs.sub_account_id, 'host_cost_center': bond_with_subs.host_cost_center, 'sub_cost_center': bond_with_subs.sub_cost_center, 'subscribers': jsonable_encoder(bond_with_subs.subscribers) }) crud.add_subscriber( bond_with_subs.bond_id, Subscriber(sid='jbojo', name='Joe Bojo', email='*****@*****.**')) # make sure the subscriber was added. response = table.query( ProjectionExpression="bond_id, host_account_id, sub_account_id, " "host_cost_center, sub_cost_center, subscribers", KeyConditionExpression=Key('bond_id').eq(bond_with_subs.bond_id)) assert response.get("Count") == 1 items = response.get("Items") subs = items[0]["subscribers"] assert len(subs) == 4 assert subs['jbojo']['sid'] == 'jbojo' assert subs['jbojo']['name'] == 'Joe Bojo' assert subs['jbojo']['email'] == '*****@*****.**'
def test_add_subscriber_as_dupe(bond_with_subs): """Attempt to add a new subscriber that already exists in the subscriber set """ new_sub = Subscriber(sid='tfomoo100', name='Tom', email='*****@*****.**') bond_with_subs.add_subscriber(new_sub) assert len(bond_with_subs.subscribers) == 3 # nothing added assert new_sub.sid in bond_with_subs.subscribers.keys() # still there
def test_add_subscriber_with_subs(bond_with_subs): """Add a new subscriber to a bond with subs""" new_sub = Subscriber(sid='lmoreo200', name='Lynne', email='*****@*****.**') bond_with_subs.add_subscriber(new_sub) assert len(bond_with_subs.subscribers) == 4 assert new_sub.sid in bond_with_subs.subscribers.keys()
def test_add_subscriber_no_bond(populated_table): """ Try to add a subscriber to a non-existent bond. It will throw a ConditionalCheckError as that bond does not exist. """ with pytest.raises(ConditionalCheckError): crud.add_subscriber( "garbage", Subscriber(sid='jbojo', name='Joe Bojo', email='*****@*****.**'))
def populated_table(table): """Pre-loaded DynamoDB table for get tests.""" subs = { 'eniesc200': Subscriber(sid='eniesc200', name='Ed', email='*****@*****.**'), 'tfomoo100': Subscriber(sid='tfomoo100', name='Tom', email='*****@*****.**'), 'bfoere300': Subscriber(sid='bfoere300', name='Bill', email='*****@*****.**'), 'yonou100': Subscriber(sid='yonou100', name='Yofo', email='*****@*****.**'), 'vdingdo200': Subscriber(sid='vdingdo200', name='Vince', email='*****@*****.**'), 'terfo000': Subscriber(sid='terfo000', name='Bob', email='*****@*****.**'), 'ming007': Subscriber(sid='ming007', name='Ming', email='*****@*****.**') } items = [{ 'bond_id': 'H0001-S0001', 'host_account_id': 'H0001', 'sub_account_id': 'S0001', 'host_cost_center': 'CC001', 'sub_cost_center': 'CC010', 'subscribers': {} }, { 'bond_id': 'H0002-S0002', 'host_account_id': 'H0002', 'sub_account_id': 'S0002', 'host_cost_center': 'CC002', 'sub_cost_center': 'CC010', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': 'H0002-S0003', 'host_account_id': 'H0002', 'sub_account_id': 'S0003', 'host_cost_center': 'CC002', 'sub_cost_center': 'CC012', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': 'H0001-S0002', 'host_account_id': 'H0001', 'sub_account_id': 'S0002', 'host_cost_center': 'CC001', 'sub_cost_center': 'CC012', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': 'H0003-S0004', 'host_account_id': 'H0003', 'sub_account_id': 'S0004', 'host_cost_center': 'CC013', 'sub_cost_center': 'CC010', 'subscribers': jsonable_encoder(subs) }] for r in items: table.put_item(Item=r) yield table
def test_add_subscriber_as_overwrite(bond_with_subs): """Attempt to update a subscriber""" new_sub = Subscriber(sid='tfomoo100', name='Thomas', email='*****@*****.**') bond_with_subs.add_subscriber(new_sub) assert len(bond_with_subs.subscribers) == 3 # nothing added assert new_sub.sid in bond_with_subs.subscribers.keys() # still there curSub = bond_with_subs.subscribers.get(new_sub.sid) assert new_sub.sid == curSub.sid assert new_sub.name == curSub.name assert new_sub.email == curSub.email
def test_update_bond(table, bond_with_subs): """Update a bond with changed values and adding a new subscriber.""" table.put_item( Item={ 'bond_id': bond_with_subs.bond_id, 'host_account_id': bond_with_subs.host_account_id, 'sub_account_id': bond_with_subs.sub_account_id, 'host_cost_center': bond_with_subs.host_cost_center, 'sub_cost_center': bond_with_subs.sub_cost_center, 'subscribers': jsonable_encoder(bond_with_subs.subscribers) }) # create the same bond with updated values and with a new subscriber upd_bond = Bond(bond_id=bond_with_subs.bond_id, host_account_id=bond_with_subs.host_account_id, sub_account_id=bond_with_subs.sub_account_id, host_cost_center='Fuz', sub_cost_center='Boo', subscribers=bond_with_subs.subscribers) upd_bond.add_subscriber( Subscriber(sid='jbojo', name='Joe Bojo', email='*****@*****.**')) # update the bond bond = crud.update_bond(upd_bond) response = table.query( ProjectionExpression="bond_id, host_account_id, sub_account_id, " "host_cost_center, sub_cost_center, subscribers", KeyConditionExpression=Key('bond_id').eq('HostAcctA-SubAcctB')) # print(response) assert bond.subscribers.__len__() == 4 assert response.get("Count") == 1 items = response.get("Items") assert items[0]["bond_id"] == 'HostAcctA-SubAcctB' assert items[0]["host_account_id"] == 'HostAcctA' assert items[0]["sub_account_id"] == 'SubAcctB' assert items[0]["host_cost_center"] == 'Fuz' assert items[0]["sub_cost_center"] == 'Boo' assert len(items[0]["subscribers"]) == 4
def table(dynamodb): """Create the mock bond table.""" with open("./localdb/01-create-table.json") as read_file: table_def = json.load(read_file) try: table = dynamodb.create_table( TableName=table_def['TableName'], BillingMode=table_def['BillingMode'], KeySchema=table_def['KeySchema'], AttributeDefinitions=table_def['AttributeDefinitions'], GlobalSecondaryIndexes=table_def['GlobalSecondaryIndexes']) """Pre-loaded DynamoDB table for get tests.""" subs = { 'eniesc200': Subscriber(sid='eniesc200', name='Ed', email='*****@*****.**'), 'tfomoo100': Subscriber(sid='tfomoo100', name='Tom', email='*****@*****.**'), 'bfoere300': Subscriber(sid='bfoere300', name='Bill', email='*****@*****.**') } items = [ {'bond_id': '6cc333cd', 'host_account_id': 'KYOT95889719595091', 'sub_account_id': 'NSSV61341208978885', 'host_cost_center': 'yellow', 'sub_cost_center': 'silver', 'subscribers': {} }, { 'bond_id': 'bf66a510', 'host_account_id': 'HAEP29388232018739', 'sub_account_id': 'WZNH57184416064999', 'host_cost_center': 'yellow', 'sub_cost_center': 'white', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': '3f4436a3', 'host_account_id': 'BULG01950964065116', 'sub_account_id': 'PFUP24464317335973', 'host_cost_center': 'maroon', 'sub_cost_center': 'navy', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': '070840c5', 'host_account_id': 'LXJC36779030364939', 'sub_account_id': 'OYHE81882804630311', 'host_cost_center': 'maroon', 'sub_cost_center': 'olive', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': 'e6d9c05f', 'host_account_id': 'YHRH31548177246824', 'sub_account_id': 'QZUL57771567168857', 'host_cost_center': 'navy', 'sub_cost_center': 'aqua', 'subscribers': jsonable_encoder(subs) }, { 'bond_id': 'deleteme', 'host_account_id': 'YHRH31548175555555', 'sub_account_id': 'QZUL57771567777777', 'host_cost_center': 'navy', 'sub_cost_center': 'aqua', 'subscribers': {} }, { 'bond_id': 'eb5e729a', 'host_account_id': 'YHRH31548177246824', 'sub_account_id': 'EVJS54814803488145', 'host_cost_center': 'navy', 'sub_cost_center': 'orange', 'subscribers': {} }, { 'bond_id': '2e545043', 'host_account_id': 'NAMD04758644119335', 'sub_account_id': 'BVOD03985622038101', 'host_cost_center': 'green', 'sub_cost_center': 'orange', 'subscribers': {} }, { 'bond_id': '402206d5', 'host_account_id': 'ZGDX86819087481638', 'sub_account_id': 'CNUE67550266655258', 'host_cost_center': 'black', 'sub_cost_center': 'orange', 'subscribers': {} } ] for r in items: table.put_item(Item=r) except ClientError as err: print(err) raise err yield table