File: test_codegen.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (304 lines) | stat: -rw-r--r-- 7,564 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
from pathlib import Path

import pytest
from typer import Typer
from typer.testing import CliRunner

from strawberry.cli.commands.codegen import ConsolePlugin
from strawberry.codegen import CodegenFile, CodegenResult, QueryCodegenPlugin
from strawberry.codegen.types import GraphQLOperation, GraphQLType


class ConsoleTestPlugin(ConsolePlugin):
    def on_end(self, result: CodegenResult):
        result.files[0].path = "renamed.py"

        return super().on_end(result)


class QueryCodegenTestPlugin(QueryCodegenPlugin):
    def generate_code(
        self, types: list[GraphQLType], operation: GraphQLOperation
    ) -> list[CodegenFile]:
        return [
            CodegenFile(
                path="test.py",
                content=f"# This is a test file for {operation.name}",
            )
        ]


class EmptyPlugin(QueryCodegenPlugin):
    def generate_code(
        self, types: list[GraphQLType], operation: GraphQLOperation
    ) -> list[CodegenFile]:
        return [
            CodegenFile(
                path="test.py",
                content="# Empty",
            )
        ]


@pytest.fixture
def query_file_path(tmp_path: Path) -> Path:
    output_path = tmp_path / "query.graphql"
    output_path.write_text(
        """
        query GetUser {
            user {
                name
            }
        }
        """
    )
    return output_path


@pytest.fixture
def query_file_path2(tmp_path: Path) -> Path:
    output_path = tmp_path / "query2.graphql"
    output_path.write_text(
        """
        query GetUser {
            user {
                name
            }
        }
        """
    )
    return output_path


def test_codegen(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "tests.cli.test_codegen:QueryCodegenTestPlugin",
            "-o",
            str(tmp_path),
            "--schema",
            selector,
            str(query_file_path),
        ],
    )

    assert result.exit_code == 0

    code_path = tmp_path / "test.py"

    assert code_path.exists()
    assert code_path.read_text() == "# This is a test file for GetUser"


def test_codegen_multiple_files(
    cli_app: Typer,
    cli_runner: CliRunner,
    query_file_path: Path,
    query_file_path2: Path,
    tmp_path: Path,
):
    expected_paths = [
        tmp_path / "query.py",
        tmp_path / "query2.py",
        tmp_path / "query.ts",
        tmp_path / "query2.ts",
    ]
    for path in expected_paths:
        assert not path.exists()

    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "python",
            "-p",
            "typescript",
            "-o",
            str(tmp_path),
            "--schema",
            selector,
            str(query_file_path),
            str(query_file_path2),
        ],
    )

    assert result.exit_code == 0

    for path in expected_paths:
        assert path.exists()
        assert " GetUserResult" in path.read_text()


def test_codegen_pass_no_query(cli_app: Typer, cli_runner: CliRunner, tmp_path: Path):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "tests.cli.test_codegen:EmptyPlugin",
            "-o",
            str(tmp_path),
            "--schema",
            selector,
        ],
    )

    assert result.exit_code == 0


def test_codegen_passing_plugin_symbol(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "tests.cli.test_codegen:EmptyPlugin",
            "-o",
            str(tmp_path),
            "--schema",
            selector,
            str(query_file_path),
        ],
    )

    assert result.exit_code == 0

    code_path = tmp_path / "test.py"

    assert code_path.exists()
    assert code_path.read_text() == "# Empty"


def test_codegen_returns_error_when_symbol_does_not_exist(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "tests.cli.test_codegen:SomePlugin",
            "--schema",
            selector,
            "-o",
            str(tmp_path),
            str(query_file_path),
        ],
    )

    assert result.exit_code == 1
    assert result.exception
    assert result.exception.args == (
        "module 'tests.cli.test_codegen' has no attribute 'SomePlugin'",
    )


def test_codegen_returns_error_when_module_does_not_exist(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "fake_module_plugin",
            "--schema",
            selector,
            "-o",
            str(tmp_path),
            str(query_file_path),
        ],
    )

    assert result.exit_code == 1
    assert "Error: Plugin fake_module_plugin not found" in result.output


def test_codegen_returns_error_when_does_not_find_plugin(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "tests.cli.test_server",
            "--schema",
            selector,
            "-o",
            str(tmp_path),
            str(query_file_path),
        ],
    )

    assert result.exit_code == 1
    assert "Error: Plugin tests.cli.test_server not found" in result.output


def test_codegen_finds_our_plugins(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "-p",
            "python",
            "--schema",
            selector,
            "-o",
            str(tmp_path),
            str(query_file_path),
        ],
    )

    assert result.exit_code == 0

    code_path = tmp_path / query_file_path.with_suffix(".py").name

    assert code_path.exists()
    assert "class GetUserResult" in code_path.read_text()


def test_can_use_custom_cli_plugin(
    cli_app: Typer, cli_runner: CliRunner, query_file_path: Path, tmp_path: Path
):
    selector = "tests.fixtures.sample_package.sample_module:schema"
    result = cli_runner.invoke(
        cli_app,
        [
            "codegen",
            "--cli-plugin",
            "tests.cli.test_codegen:ConsoleTestPlugin",
            "-p",
            "python",
            "--schema",
            selector,
            "-o",
            str(tmp_path),
            str(query_file_path),
        ],
    )

    assert result.exit_code == 0

    code_path = tmp_path / "renamed.py"

    assert code_path.exists()
    assert "class GetUserResult" in code_path.read_text()