Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix, python): generate multiple examples for query-parameters to make sure wire format is correct #3798

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 70 additions & 51 deletions generators/python/src/fern_python/snippet/snippet_test_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,59 +452,78 @@ def _generate_service_test(self, service: ir_types.HttpService, snippet_writer:
if len(successful_examples) == 0:
continue

example = successful_examples[0]
_path_parameter_names = dict()
for path_parameter in endpoint.all_path_parameters:
_path_parameter_names[path_parameter.name] = path_parameter.name.snake_case.safe_name
endpoint_snippet = self._function_generator(
snippet_writer=snippet_writer,
service=service,
endpoint=endpoint,
)._generate_endpoint_snippet_raw(example=example)

sync_snippet = self._client_snippet(False, package_path, endpoint_snippet)
async_snippet = self._client_snippet(True, package_path, endpoint_snippet)

if async_snippet is None and sync_snippet is None:
continue
example_count = 0
seen_generated = False
for example in successful_examples:
test_name = endpoint_name # Start with endpoint name

maybe_example_name = example.get_as_union().name
if maybe_example_name is not None:
test_name += f"_{maybe_example_name.snake_case.safe_name}"
elif example_count > 0:
if example.get_as_union().example_type == "generated":
if not seen_generated:
test_name += f"_generated"
seen_generated = True
else:
test_name += f"_generated_{example_count}"
else:
test_name += f"_{example_count}"

_path_parameter_names = dict()
for path_parameter in endpoint.all_path_parameters:
_path_parameter_names[path_parameter.name] = path_parameter.name.snake_case.safe_name
endpoint_snippet = self._function_generator(
snippet_writer=snippet_writer,
service=service,
endpoint=endpoint,
)._generate_endpoint_snippet_raw(example=example)

sync_snippet = self._client_snippet(False, package_path, endpoint_snippet)
async_snippet = self._client_snippet(True, package_path, endpoint_snippet)

if async_snippet is None and sync_snippet is None:
continue

response = ir_types.ExampleResponse.visit(
example.get_as_union().response,
ok=lambda _: example.get_as_union().response,
error=lambda _: None,
)
# Add functions to a test function
function_declaration = AST.FunctionDeclaration(
name=f"test_{test_name}",
# All tests will have the sync and async instantiation within them
is_async=True,
signature=AST.FunctionSignature(
# Adds in the parameters for the pytest fixtures to be injected in
parameters=[
AST.FunctionParameter(
name=self.SYNC_CLIENT_FIXTURE_NAME,
type_hint=AST.TypeHint(self._generated_root_client.sync_client.class_reference),
),
AST.FunctionParameter(
name=self.ASYNC_CLIENT_FIXTURE_NAME,
type_hint=AST.TypeHint(self._generated_root_client.async_client.class_reference),
),
],
named_parameters=[],
return_type=AST.TypeHint.none(),
),
body=self._test_body(sync_snippet, async_snippet, response),
)

response = ir_types.ExampleResponse.visit(
example.get_as_union().response,
ok=lambda _: example.get_as_union().response,
error=lambda _: None,
)
# Add functions to a test function
function_declaration = AST.FunctionDeclaration(
name=f"test_{endpoint_name}",
# All tests will have the sync and async instantiation within them
is_async=True,
signature=AST.FunctionSignature(
# Adds in the parameters for the pytest fixtures to be injected in
parameters=[
AST.FunctionParameter(
name=self.SYNC_CLIENT_FIXTURE_NAME,
type_hint=AST.TypeHint(self._generated_root_client.sync_client.class_reference),
),
AST.FunctionParameter(
name=self.ASYNC_CLIENT_FIXTURE_NAME,
type_hint=AST.TypeHint(self._generated_root_client.async_client.class_reference),
),
],
named_parameters=[],
return_type=AST.TypeHint.none(),
),
body=self._test_body(sync_snippet, async_snippet, response),
)
# At least one endpoint has a snippet, now make the file
source_file = source_file or SourceFileFactory.create(
project=self._project,
filepath=filepath,
generator_exec_wrapper=self._generator_exec_wrapper,
from_src=False,
)
# Add function to file
source_file.add_expression(AST.Expression(function_declaration))

# At least one endpoint has a snippet, now make the file
source_file = source_file or SourceFileFactory.create(
project=self._project,
filepath=filepath,
generator_exec_wrapper=self._generator_exec_wrapper,
from_src=False,
)
# Add function to file
source_file.add_expression(AST.Expression(function_declaration))
example_count += 1

if source_file:
self._service_test_files[filepath] = source_file
Expand Down
47 changes: 37 additions & 10 deletions seed/csharp-sdk/query-parameters/.mock/definition/user.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 37 additions & 10 deletions seed/go-sdk/query-parameters/.mock/definition/user.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion seed/go-sdk/query-parameters/snippet.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading