This repository was archived by the owner on Jan 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tests.py
executable file
·110 lines (89 loc) · 3.26 KB
/
create_tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
import argparse
import json
import keyword
import random
from pathlib import Path
import black
from jinja2 import Environment, FileSystemLoader
parser = argparse.ArgumentParser(
description="Generates test cases from a service class data file"
)
parser.add_argument(
"--service",
type=str,
required=True,
help="Service to load data from and create tests for",
)
args = parser.parse_args()
DATA_FOLDER = "data"
DATA_FILE_NAME = f"{args.service}_data.json"
UTF_8 = "utf-8"
READ = "r"
WRITE = "w"
TEMPLATE_FOLDER = "templates"
TEMPLATE_FILE_NAME = "tests.py.j2"
TEST_FOLDER = "tests"
TEST_FILE_NAME_PREFIX = f"test_{args.service}"
SAFE_SERVICE_NAME = (
f"{args.service}_" if keyword.iskeyword(args.service) else args.service
)
here = Path(__file__).parent
def get_random_service():
# Since the random service may also need to provide a service resource,
# It's easier to just hard-code the available ones here as they come up.
services = ["s3", "ec2", "dynamodb"]
not_this_service = [item for item in services if item != args.service]
return random.choice(not_this_service)
def map_fixtures(items):
for item in items:
not_this_item = [other for other in items if other is not item]
# Some services (like dynamodb), only have 1 resource.
# Therefore, we must add the fail fixture by hand from another service.
random_item = (
random.choice(not_this_item) if not_this_item else {"fixture_name": ""}
)
item["fail_fixture_name"] = random_item["fixture_name"]
def create_tests(category, items, folder):
output_file = folder.joinpath(f"{TEST_FILE_NAME_PREFIX}_{category}.py")
with output_file.open(WRITE, encoding=UTF_8) as file:
template.stream(service=SAFE_SERVICE_NAME, items=items).dump(file)
black.format_file_in_place(
output_file, fast=False, write_back=black.WriteBack.YES, mode=black.FileMode()
)
templates_folder = here.parent.joinpath(TEMPLATE_FOLDER)
env = Environment(loader=FileSystemLoader(templates_folder))
template = env.get_template(TEMPLATE_FILE_NAME)
data_folder = here.parent.joinpath(DATA_FOLDER)
data_file = data_folder.joinpath(DATA_FILE_NAME)
with data_file.open(READ, encoding=UTF_8) as file:
data = json.load(file)
HAS_RESOURCES = "resources" in data
HAS_WAITERS = "waiters" in data
HAS_PAGINATORS = "paginators" in data
output_folder = here.parent.joinpath(TEST_FOLDER).joinpath(SAFE_SERVICE_NAME)
output_folder.mkdir(parents=True, exist_ok=True)
# Handle clients
random_service = get_random_service()
data["client"]["fail_fixture_name"] = data["client"]["fixture_name"].replace(
args.service, random_service
)
items = [data["client"]]
if HAS_RESOURCES:
data["service_resource"]["fail_fixture_name"] = data["service_resource"][
"fixture_name"
].replace(args.service, random_service)
items.append(data["service_resource"])
create_tests("clients", items, output_folder)
# Handle other classes
categories = []
if HAS_PAGINATORS:
categories.append("paginators")
if HAS_WAITERS:
categories.append("wiaters")
if HAS_RESOURCES:
categories += ["resources", "collections"]
for category in categories:
items = data[category]
map_fixtures(items)
create_tests(category, items, output_folder)