Skip to content

Commit

Permalink
Merge branch 'master' into hzhang-add-retry
Browse files Browse the repository at this point in the history
  • Loading branch information
HantingZhang2 committed Mar 21, 2024
2 parents 116694d + 22cd378 commit 1637569
Show file tree
Hide file tree
Showing 2,982 changed files with 181,568 additions and 14,926 deletions.
38 changes: 21 additions & 17 deletions .generator/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
untitle_case,
)

from generator.formatter import format_parameters, format_data_with_schema, go_name
from generator.formatter import format_parameters, format_data_with_schema, rust_name


MODIFIED_FEATURES = {pathlib.Path(p).resolve() for p in os.getenv("BDD_MODIFIED_FEATURES", "").split(" ") if p}
Expand Down Expand Up @@ -68,12 +68,12 @@ def lookup(value, path):
JINJA_ENV.filters["snake_case"] = snake_case
JINJA_ENV.filters["camel_case"] = camel_case
JINJA_ENV.filters["untitle_case"] = untitle_case
JINJA_ENV.filters["go_name"] = go_name
JINJA_ENV.filters["rust_name"] = rust_name
JINJA_ENV.globals["format_data_with_schema"] = format_data_with_schema
JINJA_ENV.globals["format_parameters"] = format_parameters
JINJA_ENV.globals["given_variables"] = given_variables

GO_EXAMPLE_J2 = JINJA_ENV.get_template("example.j2")
RUST_EXAMPLE_J2 = JINJA_ENV.get_template("example.j2")


def pytest_bdd_after_scenario(request, feature, scenario):
Expand All @@ -87,7 +87,7 @@ def pytest_bdd_after_scenario(request, feature, scenario):

status_code = context["status_code"]
if status_code >= 300:
warnings.warn(f"do not generate example for {version}:{operation_id}:{status_code}")
# warnings.warn(f"do not generate example for {version}:{operation_id}:{status_code}")
return

operation_spec = operation_specs[version][operation_id]
Expand All @@ -100,14 +100,14 @@ def pytest_bdd_after_scenario(request, feature, scenario):
if scenario_name != scenario.name:
unique_suffix = "_" + str(zlib.adler32(scenario.name.encode("utf-8")))

data = GO_EXAMPLE_J2.render(
data = RUST_EXAMPLE_J2.render(
context=context,
version=version,
scenario=scenario,
operation_spec=operation_spec.spec,
)

output = ROOT_PATH / "examples" / version / group_name / f"{operation_id}{unique_suffix}.go"
output = ROOT_PATH / "examples" / f"{version}_{group_name}_{snake_case(operation_id)}{unique_suffix}.rs"
output.parent.mkdir(parents=True, exist_ok=True)

with output.open("w") as f:
Expand Down Expand Up @@ -141,16 +141,16 @@ def unique(request):


TIME_FORMATTER = {
"now": "time.Now()",
"timestamp": "{sret}.Unix()",
"now": "Utc::now()",
"timestamp": "({sret}).timestamp()",
"isoformat": "{sret}", # .Format(time.RFC3339) we don't need to format it as time.Time{} is expected
"units": {
"s": "{sret}.Add(time.Second*{num})",
"m": "{sret}.Add(time.Minute*{num})",
"h": "{sret}.Add(time.Hour*{num})",
"d": "{sret}.AddDate(0, 0, {num})",
"M": "{sret}.AddDate(0, {num}, 0)",
"y": "{sret}.AddDate({num}, 0, 0)",
"s": "{sret} + Duration::seconds({num})",
"m": "{sret} + Duration::minutes({num})",
"h": "{sret} + Duration::hours({num})",
"d": "{sret} + Duration::days({num})",
"M": "{sret} {sign} Months::new(1)",
"y": "{sret} {sign} Months::new(12)",
},
}

Expand All @@ -159,7 +159,6 @@ def relative_time(imports, calls, freezed_time, iso):
time_re = re.compile(r"now( *([+-]) *(\d+)([smhdMy]))?")

def func(arg):
imports["time"].add(None)
sret = TIME_FORMATTER["now"]
ret = freezed_time
m = time_re.match(arg)
Expand All @@ -182,7 +181,7 @@ def func(arg):
ret += relativedelta(years=num)
else:
raise ValueError(f"Unknown unit {unit}")
sret = TIME_FORMATTER["units"][unit].format(sret=sret, num=num)
sret = TIME_FORMATTER["units"][unit].format(sret=sret, num=num, sign="+" if num > 0 else "-")

if iso:
return (
Expand All @@ -194,7 +193,7 @@ def func(arg):

def store_calls(arg):
result, value = func(arg)
calls[result] = value
# calls[result] = value
return result

return store_calls
Expand Down Expand Up @@ -486,6 +485,11 @@ def expect_response_has_field(context, response_path, field):
"""Check that a response has field."""


@then(parsers.parse('the response "{response_path}" does not have field "{field}"'))
def expect_response_does_not_have_field(context, response_path, field):
"""Check that a response path does not have field."""


@then(parsers.parse("the response {response_path} has item with field {key_path} with value {value}"))
def expect_array_contains_object(context, response_path, key_path, value):
"""Check that a response attribute contains an object with the specified key and value."""
Expand Down
3 changes: 2 additions & 1 deletion .generator/src/generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ def cli(specs, output):
env.filters["parameter_schema"] = openapi.parameter_schema
env.filters["parameters"] = openapi.parameters
env.filters["form_parameter"] = openapi.form_parameter
env.filters["response_type"] = openapi.get_type_for_response
env.filters["return_type"] = openapi.return_type
env.filters["simple_type"] = formatter.simple_type
env.filters["snake_case"] = formatter.snake_case
Expand All @@ -61,7 +60,9 @@ def cli(specs, output):
env.globals["get_deprecated"] = openapi.get_deprecated
env.globals["get_container"] = openapi.get_container
env.globals["get_container_type"] = openapi.get_container_type
env.globals["get_accessors"] = openapi.get_accessors
env.globals["get_type_at_path"] = openapi.get_type_at_path
env.globals["get_schema_for_attribute"] = openapi.get_schema_for_attribute
env.globals["common_package_name"] = COMMON_PACKAGE_NAME
env.globals["module"] = MODULE

Expand Down
Loading

0 comments on commit 1637569

Please sign in to comment.