Skip to content

Commit

Permalink
fixing unit tests (#3168)
Browse files Browse the repository at this point in the history
  • Loading branch information
armandobelardo authored Mar 15, 2024
1 parent eda8c44 commit 44af800
Show file tree
Hide file tree
Showing 270 changed files with 3,340 additions and 1,662 deletions.
34 changes: 16 additions & 18 deletions .pnp.cjs

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

Binary file not shown.
2 changes: 1 addition & 1 deletion fern/apis/public-api/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ groups:
- external
generators:
- name: fernapi/fern-python-sdk
version: 0.7.6
version: 0.12.0
output:
location: pypi
package-name: fern-api
Expand Down
4 changes: 2 additions & 2 deletions fern/fern.config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"organization": "fern",
"version": "0.19.8"
}
"version": "0.19.9"
}
2 changes: 0 additions & 2 deletions generators/python/core_utilities/sdk/base_test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def validate_response(response: typing.Any, json_expectation: typing.Any) -> Non
assert key in response_json
if isinstance(value, dict):
validate_response(response_json[key], value)
elif isinstance(value, list):
assert set(response_json[key]) == set(value)
else:
assert response_json[key] == value

Expand Down
7 changes: 6 additions & 1 deletion generators/python/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.12.0] - 2024-03-06
## [0.12.1] - 2024-03-14

- Improves example generation and snippets for union types, as well as multi-url environments.
- Fix: Stringifies header arguments, HTTPX was previously hard failing for certain types

## [0.12.0] - 2024-03-11

- Beta, feature: The SDK now generates tests leveraging auto-generated data to test typing, as well as wire-formatting (e.g. the SDKs are sending and receiving data as expected). This comes out of the box within the generated github workflow, as well as through the fern cli: `fern test --command "your test command"`.
**Note**: You must be on the enterprise tier to enable this mode.
Expand Down
2 changes: 1 addition & 1 deletion generators/python/sdk/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0
0.12.1
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _get_github_workflow(self, project: Project, output_mode: GithubOutputMode,
- name: Test
run: |
${'fern test --command "poetry run pytest -rP ."' if write_unit_tests else 'poetry run pytest .'}
{'fern test --command "poetry run pytest -rP ."' if write_unit_tests else 'poetry run pytest .'}
"""
project.add_file(".github/workflows/tests.yml", workflow_yaml)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -709,9 +709,9 @@ def _get_headers_for_endpoint(
if literal_header_value is not None and type(literal_header_value) is str:
headers.append((header.name.wire_value, AST.Expression(f'"{literal_header_value}"')))
elif literal_header_value is not None and type(literal_header_value) is bool:
headers.append((header.name.wire_value, AST.Expression(f"{literal_header_value}")))
headers.append((header.name.wire_value, AST.Expression(f'"{str(literal_header_value).lower()}"')))
else:
headers.append((header.name.wire_value, AST.Expression(get_parameter_name(header.name.name))))
headers.append((header.name.wire_value, AST.Expression(f"str({get_parameter_name(header.name.name)})")))

def write_headers_dict_default(writer: AST.NodeWriter) -> None:
writer.write("{")
Expand Down Expand Up @@ -752,8 +752,10 @@ def write_headers_dict(writer: AST.NodeWriter) -> None:

def _get_query_parameter_reference(self, query_parameter: ir_types.QueryParameter) -> AST.Expression:
possible_query_literal = self._context.get_literal_value(query_parameter.value_type)
if possible_query_literal is not None:
if possible_query_literal is not None and type(possible_query_literal) is str:
return AST.Expression(f'"{possible_query_literal}"')
elif possible_query_literal is not None and type(possible_query_literal) is bool:
return AST.Expression(f"{possible_query_literal}")
return self._get_reference_to_query_parameter(query_parameter)

def _get_query_parameters_for_endpoint(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def _handle_success_text(
writer: AST.NodeWriter,
) -> None:
writer.write("return ")
writer.write_node(AST.Expression(f"{EndpointResponseCodeWriter.RESPONSE_VARIABLE}"))
writer.write_node(AST.Expression(f"{EndpointResponseCodeWriter.RESPONSE_VARIABLE}.text"))
writer.write(" # type: ignore ")
writer.write_newline_if_last_line_not()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ class MultipleBaseUrlsEnvironmentGenerator:
def __init__(self, context: SdkGeneratorContext, environments: ir_types.MultipleBaseUrlsEnvironments):
self._context = context
self._environments = environments
self.class_name = self._context.get_class_name_of_environments()
self.args = [get_base_url_property_name(base_url) for base_url in self._environments.base_urls]

def generate(
self,
source_file: SourceFile,
) -> GeneratedEnvironment:
class_name = self._context.get_class_name_of_environments()
environment_class = AST.ClassDeclaration(name=class_name)
environment_class = AST.ClassDeclaration(name=self.class_name)

example_environment = ""
for i, environment in enumerate(self._environments.environments):
Expand All @@ -28,7 +29,9 @@ def generate(
AST.VariableDeclaration(
name=class_var_name,
type_hint=AST.TypeHint(
AST.ClassReference(qualified_name_excluding_import=(class_name,), is_forward_reference=True)
AST.ClassReference(
qualified_name_excluding_import=(self.class_name,), is_forward_reference=True
)
),
docstring=AST.Docstring(environment.docs) if environment.docs is not None else None,
)
Expand Down Expand Up @@ -59,7 +62,7 @@ def generate(
self._context.get_filepath_for_environments_enum().to_module().path
),
),
named_import=class_name,
named_import=self.class_name,
),
)

Expand Down Expand Up @@ -114,13 +117,12 @@ def _write_constructor_body(self, writer: AST.NodeWriter) -> None:
writer.write_line(f"self.{property_name} = {property_name}")

def _write_bottom_statements(self, writer: AST.NodeWriter) -> None:
class_name = self._context.get_class_name_of_environments()
for environment in self._environments.environments:
class_var = self._get_class_var_name(environment)
writer.write(f"{class_name}.{class_var} = ")
writer.write(f"{self.class_name}.{class_var} = ")
writer.write_node(
AST.ClassInstantiation(
class_=AST.ClassReference(qualified_name_excluding_import=(class_name,)),
class_=AST.ClassReference(qualified_name_excluding_import=(self.class_name,)),
kwargs=[
(
get_base_url_property_name(base_url),
Expand Down
39 changes: 28 additions & 11 deletions generators/python/src/fern_python/generators/sdk/sdk_generator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Sequence, Tuple
from typing import Optional, Sequence, Tuple, Union, cast

import fern.ir.resources as ir_types
from fern.generator_exec.resources.config import GeneratorConfig
Expand Down Expand Up @@ -114,10 +114,16 @@ def run(
)

generated_environment: Optional[GeneratedEnvironment] = None
base_environment: Optional[
Union[SingleBaseUrlEnvironmentGenerator, MultipleBaseUrlsEnvironmentGenerator]
] = None
if ir.environments is not None:
base_environment = self._generate_environments_base(
context=context, environments=ir.environments.environments
)
generated_environment = self._generate_environments(
context=context,
environments=ir.environments.environments,
environments=base_environment,
generator_exec_wrapper=generator_exec_wrapper,
project=project,
)
Expand Down Expand Up @@ -174,6 +180,7 @@ def run(
context=context,
generator_exec_wrapper=generator_exec_wrapper,
generated_root_client=generated_root_client,
generated_environment=base_environment,
)

# Only write unit tests if specified in config
Expand All @@ -195,25 +202,35 @@ def run(
if request_sent:
project.set_generate_readme(False)

def _generate_environments(
def _generate_environments_base(
self,
context: SdkGeneratorContext,
environments: ir_types.Environments,
) -> Union[SingleBaseUrlEnvironmentGenerator, MultipleBaseUrlsEnvironmentGenerator]:
return cast(
Union[SingleBaseUrlEnvironmentGenerator, MultipleBaseUrlsEnvironmentGenerator],
environments.visit(
single_base_url=lambda single_base_url_environments: SingleBaseUrlEnvironmentGenerator(
context=context, environments=single_base_url_environments
),
multiple_base_urls=lambda multiple_base_urls_environments: MultipleBaseUrlsEnvironmentGenerator(
context=context, environments=multiple_base_urls_environments
),
),
)

def _generate_environments(
self,
context: SdkGeneratorContext,
environments: Union[SingleBaseUrlEnvironmentGenerator, MultipleBaseUrlsEnvironmentGenerator],
generator_exec_wrapper: GeneratorExecWrapper,
project: Project,
) -> GeneratedEnvironment:
filepath = context.get_filepath_for_environments_enum()
source_file = SourceFileFactory.create(
project=project, filepath=filepath, generator_exec_wrapper=generator_exec_wrapper
)
generated_environment = environments.visit(
single_base_url=lambda single_base_url_environments: SingleBaseUrlEnvironmentGenerator(
context=context, environments=single_base_url_environments
).generate(source_file=source_file),
multiple_base_urls=lambda multiple_base_urls_environments: MultipleBaseUrlsEnvironmentGenerator(
context=context, environments=multiple_base_urls_environments
).generate(source_file=source_file),
)
generated_environment = environments.generate(source_file=source_file)
project.write_source_file(source_file=source_file, filepath=filepath)
return generated_environment

Expand Down
Loading

0 comments on commit 44af800

Please sign in to comment.