Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivani-gslab committed Apr 13, 2022
1 parent d0bd996 commit 04dcd3d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def convert_dicts(dictionary, primary_key="name", secondary_key="items"):
# Not a nested dictionary but a string, return the original
return dictionary
else:
item = dictionary[key]
item = dictionary[key].copy()
item.update({primary_key: key})
output.append(item)
return output
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible_collections.arista.avd.plugins.filter.convert_dicts import convert_dicts, FilterModule
import pytest

list_of_dict = {'TEST1': [{'type': 'permit', 'extcommunities': '65000:65000'}, {'type': 'deny', 'extcommunities': '65002:65002'}], 'TEST2': [{'type': 'deny', 'extcommunities': '65001:65001'}]}
nested_dict = {'TEST1': {'action': 'permit 1000:1000'}, 'TEST2': {'action': 'permit 2000:3000'}}
list = ['Test1', 'Test2', 'Test3']

f = FilterModule()

class TestConvertDicts():

def test_convert_dicts_with_nested_dict_default(self):
resp = convert_dicts(nested_dict)
assert resp == [{'action': 'permit 1000:1000', 'name': 'TEST1'}, {'action': 'permit 2000:3000', 'name': 'TEST2'}]

def test_convert_dicts_with_nested_dict_primary_key(self):
resp = convert_dicts(nested_dict, 'id')
assert resp == [{'action': 'permit 1000:1000', 'id': 'TEST1'}, {'action': 'permit 2000:3000', 'id': 'TEST2'}]

def test_convert_dicts_with_nested_dict_secondary_key(self):
resp = convert_dicts(nested_dict, secondary_key='types')
assert resp == [{'action': 'permit 1000:1000', 'name': 'TEST1'}, {'action': 'permit 2000:3000', 'name': 'TEST2'}]

def test_convert_dicts_with_nested_dict_primary_and_secondary_key(self):
resp = convert_dicts(nested_dict, 'id', 'types')
assert resp == [{'action': 'permit 1000:1000', 'id': 'TEST1'}, {'action': 'permit 2000:3000', 'id': 'TEST2'}]

def test_convert_dicts_with_listofdict_default(self):
resp = convert_dicts(list_of_dict)
assert resp == [{'name': 'TEST1', 'items': [{'type': 'permit', 'extcommunities': '65000:65000'}, {'type': 'deny', 'extcommunities': '65002:65002'}]}, {'name': 'TEST2', 'items': [{'type': 'deny', 'extcommunities': '65001:65001'}]}]

def test_convert_dicts_with_listofdict_primary_key(self):
resp = convert_dicts(list_of_dict, 'test')
assert resp == [{'test': 'TEST1', 'items': [{'type': 'permit', 'extcommunities': '65000:65000'}, {'type': 'deny', 'extcommunities': '65002:65002'}]}, {'test': 'TEST2', 'items': [{'type': 'deny', 'extcommunities': '65001:65001'}]}]

def test_convert_dicts_with_listofdict_secondary_key(self):
resp = convert_dicts(list_of_dict, secondary_key='types')
assert resp == [{'name': 'TEST1', 'types': [{'type': 'permit', 'extcommunities': '65000:65000'}, {'type': 'deny', 'extcommunities': '65002:65002'}]}, {'name': 'TEST2', 'types': [{'type': 'deny', 'extcommunities': '65001:65001'}]}]

def test_convert_dicts_with_listofdict_primary_and_secondary_key(self):
resp = convert_dicts(list_of_dict, 'id', 'types')
assert resp == [{'id': 'TEST1', 'types': [{'type': 'permit', 'extcommunities': '65000:65000'}, {'type': 'deny', 'extcommunities': '65002:65002'}]}, {'id': 'TEST2', 'types': [{'type': 'deny', 'extcommunities': '65001:65001'}]}]

def test_convert_dicts_with_list_default(self):
resp = convert_dicts(list)
assert resp == list

def test_convert_dicts_with_list_primary_key(self):
resp = convert_dicts(list, 'id')
assert resp == list

def test_convert_dicts_with_list_secondary_key(self):
resp = convert_dicts(list, secondary_key='id')
assert resp == list

def test_convert_dicts_with_list_primary_and_secondary_key(self):
resp = convert_dicts(list, 'id', 'types')
assert resp == list

def test_convert_dicts_filter(self):
resp = f.filters()
assert isinstance(resp, dict)
assert 'convert_dicts' in resp.keys()

0 comments on commit 04dcd3d

Please sign in to comment.