I'm have a function that's applying an additional tag to an aws instance based on a dict of tags that are passed in.
Expected behavior: When more than one TEST_CUSTOMER_ID is passed in, the function should return 'shared'.
The current behavior of the unit test function is returning:
['test_customer_id1', 'test_customer_id2']
How can I fix this?
Parent Function
def get_account_value(tags, customer_id):
if not tags:
return customer_id
tag_value = tags.get("Account")
return customer_id if not tag_value or tag_value == customer_id else "shared"
Unit Test
def test_get_account_value_customer_list():
TAGS_NO_VALUE = {'foo':'bar'}
TEST_CUSTOMER_ID_LIST = ["test_customer_id1", "test_customer_id2"]
response = tagging.get_account_value(TAGS_NO_VALUE, TEST_CUSTOMER_ID_LIST)
assert response == "shared"
Unit test Error
AssertionError: assert ['test_client_id1', 'test_client_id2'] == 'shared'
CodePudding user response:
It raises an error because tag_value ist None because you dont give an Account tag. bool(None) = False. So your test will fail. You have to change you test function to this:
def test_get_account_value_customer_list():
TAGS_NO_VALUE = {'Account': 'bar'}
TEST_CUSTOMER_ID_LIST = ["test_customer_id1", "test_customer_id2"]
response = tagging.get_account_value(TAGS_NO_VALUE, TEST_CUSTOMER_ID_LIST)
assert response == "shared"
I hope this will help.
CodePudding user response:
In your test, tag_value
is None
because there is no value with key 'Account'
in tags
. Therefore, if not tag_value
returns True. I think this would do the trick:
def get_account_value(tags, customer_id):
if not tags:
return customer_id
tag_value = tags.get("Account")
if not tag_value or tag_value == customer_id:
if len(customer_id) > 1:
return "shared"
return customer_id