Skip to content

Commit

Permalink
move tests to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
oguzhan-yilmaz committed Jul 15, 2024
1 parent 14ce568 commit 1145406
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 53 deletions.
54 changes: 1 addition & 53 deletions aws_jmespath_utils/filter_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,10 @@
class FilterTagsCustomFunction(functions.Functions):
@functions.signature({'types': ["array"]}, {'types': ["array", "object"]})
def _func_exclude_tags(self, expression_list: List, data: Union[Dict, List[Dict]]):


result_list = None
# logging.debug(f"_func_filter_tags data type is: {type(data)}")

if type(data)==list:
# print("data type is list")
# apply for each element on the list
result_list = []
denied_indices_map = {
str(i): False
Expand Down Expand Up @@ -51,21 +47,11 @@ def _func_exclude_tags(self, expression_list: List, data: Union[Dict, List[Dict]
raise Exception(f"Unimplemented data type: {type(data)} :: {data}")




@functions.signature({'types': ["array"]}, {'types': ["array", "object"]})
def _func_filter_tags(self, expression_list: List, data: Union[Dict, List[Dict]]):

# print(f"{type(expression_list)}: expression_list is:", expression_list)
# print(f"{type(data)}: data is:",data)
# print("-----------")

result_list = None
# logging.debug(f"_func_filter_tags data type is: {type(data)}")

if type(data)==list:
# print("data type is list")
# apply for each element on the list
result_list = []
accepted_indices_map = {
str(i): False
Expand All @@ -92,42 +78,4 @@ def _func_filter_tags(self, expression_list: List, data: Union[Dict, List[Dict]]
if is_accepted(expression, data) and data:
return data
return None
raise Exception(f"Unimplemented data type: {type(data)} :: {data}")



def test():
_jmespath_options = jmespath.Options(custom_functions=FilterTagsCustomFunction())
from pprint import pprint
import json
data = None
with open('tests/data/ec2_describe_instances.json', 'r') as file:
data = json.load(file)

# 'DescribeInstances[*].Reservations[*].Instances[].filter_tags(`["=CHAT*"]`, @)[].exclude_tags(`["="]`, @)',
# 'DescribeInstances[*].Reservations[*].Instances[].exclude_tags(`["Name=*"]`, @)[].Tags',
# 'DescribeInstances[*].Reservations[*].Instances[].filter_tags(`["=can*"]`, @)',

print('Filter test')
print(
json.dumps(
jmespath.search(
'DescribeInstances[*].Reservations[*].Instances[].filter_tags(`["=can*"]`, @)[].Tags',
data,
options=_jmespath_options),
indent=2
)
)
print('Exclude test')
print(
json.dumps(
jmespath.search(
'DescribeInstances[*].Reservations[*].Instances[].exclude_tags(`["=can*"]`, @)[].Tags',
data,
options=_jmespath_options),
indent=2
)
)

if __name__ == '__main__':
test()
raise Exception(f"Unimplemented data type: {type(data)} :: {data}")
35 changes: 35 additions & 0 deletions tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from pprint import pprint
import json
from aws_jmespath_utils import FilterTagsCustomFunction
import jmespath

_jmespath_options = jmespath.Options(custom_functions=FilterTagsCustomFunction())

data = None
with open('tests/data/ec2_describe_instances.json', 'r') as file:
data = json.load(file)

# 'DescribeInstances[*].Reservations[*].Instances[].filter_tags(`["=CHAT*"]`, @)[].exclude_tags(`["="]`, @)',
# 'DescribeInstances[*].Reservations[*].Instances[].exclude_tags(`["Name=*"]`, @)[].Tags',
# 'DescribeInstances[*].Reservations[*].Instances[].filter_tags(`["=can*"]`, @)',

print('Filter test')
print(
json.dumps(
jmespath.search(
'DescribeInstances[*].Reservations[*].Instances[].filter_tags(`["=can*"]`, @)[].Tags',
data,
options=_jmespath_options),
indent=2
)
)
print('Exclude test')
print(
json.dumps(
jmespath.search(
'DescribeInstances[*].Reservations[*].Instances[].exclude_tags(`["=can*"]`, @)[].Tags',
data,
options=_jmespath_options),
indent=2
)
)

0 comments on commit 1145406

Please sign in to comment.