Skip to content

Commit 7e8fa95

Browse files
committed
add tests
1 parent 7b68be7 commit 7e8fa95

12 files changed

Lines changed: 281 additions & 1 deletion

File tree

tests/client_generators/package_generator/test_generated_files.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,3 +764,48 @@ def test_generate_creates_client_with_custom_scalars_imports(
764764
f"{generator.client_file_name}.py"
765765
).open() as client_file:
766766
assert "from .abc import ScalarABC" in client_file.read()
767+
768+
769+
def test_generate_models_only(tmp_path, schema, async_base_client_import):
770+
package_name = "test_graphql_client"
771+
generator = PackageGenerator(
772+
package_name=package_name,
773+
target_path=tmp_path.as_posix(),
774+
schema=schema,
775+
init_generator=InitFileGenerator(),
776+
client_generator=ClientGenerator(
777+
base_client_import=async_base_client_import,
778+
arguments_generator=ArgumentsGenerator(schema=schema),
779+
),
780+
enums_generator=EnumsGenerator(schema=schema),
781+
input_types_generator=InputTypesGenerator(schema=schema),
782+
fragments_generator=FragmentsGenerator(schema=schema, fragments_definitions={}),
783+
models_only=True,
784+
)
785+
query_str = """
786+
query CustomQuery($id: ID!) {
787+
query1(id: $id) {
788+
field1
789+
}
790+
}
791+
"""
792+
generator.add_operation(parse(query_str).definitions[0])
793+
generated_files = generator.generate()
794+
795+
package_path = tmp_path / package_name
796+
assert (package_path / "__init__.py").exists()
797+
assert (package_path / "base_model.py").exists()
798+
assert (package_path / f"{generator.enums_module_name}.py").exists()
799+
assert (package_path / f"{generator.input_types_module_name}.py").exists()
800+
assert (package_path / "custom_query.py").exists()
801+
assert "custom_query.py" in generated_files
802+
assert not (package_path / "client.py").exists()
803+
assert not (package_path / generator.base_client_file_path.name).exists()
804+
assert not (package_path / EXCEPTIONS_FILE_PATH.name).exists()
805+
assert "client.py" not in generated_files
806+
assert EXCEPTIONS_FILE_PATH.name not in generated_files
807+
init_content = (package_path / "__init__.py").read_text()
808+
assert "from .base_model import BaseModel, Upload" in init_content
809+
assert "Client" not in init_content
810+
assert "AsyncBaseClient" not in init_content
811+
assert "GraphQLClientError" not in init_content
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .base_model import BaseModel, Upload
2+
from .create_user import CreateUser, CreateUserUserCreate
3+
from .enums import Color
4+
from .input_types import UserCreateInput
5+
from .list_users import ListUsers, ListUsersUsers
6+
7+
__all__ = [
8+
"BaseModel",
9+
"Color",
10+
"CreateUser",
11+
"CreateUserUserCreate",
12+
"ListUsers",
13+
"ListUsersUsers",
14+
"Upload",
15+
"UserCreateInput",
16+
]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from io import IOBase
2+
3+
from pydantic import BaseModel as PydanticBaseModel
4+
from pydantic import ConfigDict
5+
6+
7+
class UnsetType:
8+
def __bool__(self) -> bool:
9+
return False
10+
11+
12+
UNSET = UnsetType()
13+
14+
15+
class BaseModel(PydanticBaseModel):
16+
model_config = ConfigDict(
17+
populate_by_name=True,
18+
validate_assignment=True,
19+
arbitrary_types_allowed=True,
20+
protected_namespaces=(),
21+
)
22+
23+
24+
class Upload:
25+
def __init__(self, filename: str, content: IOBase, content_type: str):
26+
self.filename = filename
27+
self.content = content
28+
self.content_type = content_type
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import Optional
2+
3+
from pydantic import Field
4+
5+
from .base_model import BaseModel
6+
7+
8+
class CreateUser(BaseModel):
9+
user_create: Optional["CreateUserUserCreate"] = Field(alias="userCreate")
10+
11+
12+
class CreateUserUserCreate(BaseModel):
13+
id: str
14+
email: str
15+
16+
17+
CreateUser.model_rebuild()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from enum import Enum
2+
3+
4+
class Color(str, Enum):
5+
BLACK = "BLACK"
6+
WHITE = "WHITE"
7+
RED = "RED"
8+
GREEN = "GREEN"
9+
BLUE = "BLUE"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Optional
2+
3+
from pydantic import Field
4+
5+
from .base_model import BaseModel
6+
from .enums import Color
7+
8+
9+
class UserCreateInput(BaseModel):
10+
first_name: Optional[str] = Field(alias="firstName", default=None)
11+
last_name: Optional[str] = Field(alias="lastName", default=None)
12+
email: str
13+
favourite_color: Optional[Color] = Field(alias="favouriteColor", default=None)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import Optional
2+
3+
from pydantic import Field
4+
5+
from .base_model import BaseModel
6+
from .enums import Color
7+
8+
9+
class ListUsers(BaseModel):
10+
users: list["ListUsersUsers"]
11+
12+
13+
class ListUsersUsers(BaseModel):
14+
id: str
15+
first_name: Optional[str] = Field(alias="firstName")
16+
email: str
17+
favourite_color: Optional[Color] = Field(alias="favouriteColor")
18+
19+
20+
ListUsers.model_rebuild()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[tool.ariadne-codegen]
2+
schema_path = "schema.graphql"
3+
queries_path = "queries.graphql"
4+
include_comments = "none"
5+
target_package_name = "example_client"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
mutation CreateUser($userData: UserCreateInput!) {
2+
userCreate(userData: $userData) {
3+
id
4+
email
5+
}
6+
}
7+
8+
query ListUsers {
9+
users {
10+
id
11+
firstName
12+
email
13+
favouriteColor
14+
}
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
schema {
2+
query: Query
3+
mutation: Mutation
4+
}
5+
6+
type Query {
7+
users(country: String): [User!]!
8+
}
9+
10+
type Mutation {
11+
userCreate(userData: UserCreateInput!): User
12+
}
13+
14+
input UserCreateInput {
15+
firstName: String
16+
lastName: String
17+
email: String!
18+
favouriteColor: Color
19+
}
20+
21+
type User {
22+
id: ID!
23+
firstName: String
24+
lastName: String
25+
email: String!
26+
favouriteColor: Color
27+
}
28+
29+
enum Color {
30+
BLACK
31+
WHITE
32+
RED
33+
GREEN
34+
BLUE
35+
}

0 commit comments

Comments
 (0)