File: test_get_ref_type.py

package info (click to toggle)
python-aristaproto 1.2%2Breally0.1.4-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,600 kB
  • sloc: python: 6,521; java: 106; xml: 84; makefile: 6
file content (497 lines) | stat: -rw-r--r-- 14,842 bytes parent folder | download | duplicates (2)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
import pytest

from aristaproto.compile.importing import (
    get_type_reference,
    parse_source_type_name,
)
from aristaproto.plugin.typing_compiler import DirectImportTypingCompiler


@pytest.fixture
def typing_compiler() -> DirectImportTypingCompiler:
    """
    Generates a simple Direct Import Typing Compiler for testing.
    """
    return DirectImportTypingCompiler()


@pytest.mark.parametrize(
    ["google_type", "expected_name", "expected_import"],
    [
        (
            ".google.protobuf.Empty",
            '"aristaproto_lib_google_protobuf.Empty"',
            "import aristaproto.lib.google.protobuf as aristaproto_lib_google_protobuf",
        ),
        (
            ".google.protobuf.Struct",
            '"aristaproto_lib_google_protobuf.Struct"',
            "import aristaproto.lib.google.protobuf as aristaproto_lib_google_protobuf",
        ),
        (
            ".google.protobuf.ListValue",
            '"aristaproto_lib_google_protobuf.ListValue"',
            "import aristaproto.lib.google.protobuf as aristaproto_lib_google_protobuf",
        ),
        (
            ".google.protobuf.Value",
            '"aristaproto_lib_google_protobuf.Value"',
            "import aristaproto.lib.google.protobuf as aristaproto_lib_google_protobuf",
        ),
    ],
)
def test_reference_google_wellknown_types_non_wrappers(
    google_type: str,
    expected_name: str,
    expected_import: str,
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type=google_type,
        typing_compiler=typing_compiler,
        pydantic=False,
    )

    assert name == expected_name
    assert imports.__contains__(expected_import), (
        f"{expected_import} not found in {imports}"
    )


@pytest.mark.parametrize(
    ["google_type", "expected_name", "expected_import"],
    [
        (
            ".google.protobuf.Empty",
            '"aristaproto_lib_pydantic_google_protobuf.Empty"',
            "import aristaproto.lib.pydantic.google.protobuf as aristaproto_lib_pydantic_google_protobuf",
        ),
        (
            ".google.protobuf.Struct",
            '"aristaproto_lib_pydantic_google_protobuf.Struct"',
            "import aristaproto.lib.pydantic.google.protobuf as aristaproto_lib_pydantic_google_protobuf",
        ),
        (
            ".google.protobuf.ListValue",
            '"aristaproto_lib_pydantic_google_protobuf.ListValue"',
            "import aristaproto.lib.pydantic.google.protobuf as aristaproto_lib_pydantic_google_protobuf",
        ),
        (
            ".google.protobuf.Value",
            '"aristaproto_lib_pydantic_google_protobuf.Value"',
            "import aristaproto.lib.pydantic.google.protobuf as aristaproto_lib_pydantic_google_protobuf",
        ),
    ],
)
def test_reference_google_wellknown_types_non_wrappers_pydantic(
    google_type: str,
    expected_name: str,
    expected_import: str,
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type=google_type,
        typing_compiler=typing_compiler,
        pydantic=True,
    )

    assert name == expected_name
    assert imports.__contains__(expected_import), (
        f"{expected_import} not found in {imports}"
    )


@pytest.mark.parametrize(
    ["google_type", "expected_name"],
    [
        (".google.protobuf.DoubleValue", "Optional[float]"),
        (".google.protobuf.FloatValue", "Optional[float]"),
        (".google.protobuf.Int32Value", "Optional[int]"),
        (".google.protobuf.Int64Value", "Optional[int]"),
        (".google.protobuf.UInt32Value", "Optional[int]"),
        (".google.protobuf.UInt64Value", "Optional[int]"),
        (".google.protobuf.BoolValue", "Optional[bool]"),
        (".google.protobuf.StringValue", "Optional[str]"),
        (".google.protobuf.BytesValue", "Optional[bytes]"),
    ],
)
def test_referenceing_google_wrappers_unwraps_them(
    google_type: str, expected_name: str, typing_compiler: DirectImportTypingCompiler
):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type=google_type,
        typing_compiler=typing_compiler,
    )

    assert name == expected_name
    assert imports == set()


@pytest.mark.parametrize(
    ["google_type", "expected_name"],
    [
        (
            ".google.protobuf.DoubleValue",
            '"aristaproto_lib_google_protobuf.DoubleValue"',
        ),
        (".google.protobuf.FloatValue", '"aristaproto_lib_google_protobuf.FloatValue"'),
        (".google.protobuf.Int32Value", '"aristaproto_lib_google_protobuf.Int32Value"'),
        (".google.protobuf.Int64Value", '"aristaproto_lib_google_protobuf.Int64Value"'),
        (
            ".google.protobuf.UInt32Value",
            '"aristaproto_lib_google_protobuf.UInt32Value"',
        ),
        (
            ".google.protobuf.UInt64Value",
            '"aristaproto_lib_google_protobuf.UInt64Value"',
        ),
        (".google.protobuf.BoolValue", '"aristaproto_lib_google_protobuf.BoolValue"'),
        (
            ".google.protobuf.StringValue",
            '"aristaproto_lib_google_protobuf.StringValue"',
        ),
        (".google.protobuf.BytesValue", '"aristaproto_lib_google_protobuf.BytesValue"'),
    ],
)
def test_referenceing_google_wrappers_without_unwrapping(
    google_type: str, expected_name: str, typing_compiler: DirectImportTypingCompiler
):
    name = get_type_reference(
        package="",
        imports=set(),
        source_type=google_type,
        typing_compiler=typing_compiler,
        unwrap=False,
    )

    assert name == expected_name


def test_reference_child_package_from_package(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="package",
        imports=imports,
        source_type="package.child.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from . import child"}
    assert name == '"child.Message"'


def test_reference_child_package_from_root(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type="child.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from . import child"}
    assert name == '"child.Message"'


def test_reference_camel_cased(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type="child_package.example_message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from . import child_package"}
    assert name == '"child_package.ExampleMessage"'


def test_reference_nested_child_from_root(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type="nested.child.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .nested import child as nested_child"}
    assert name == '"nested_child.Message"'


def test_reference_deeply_nested_child_from_root(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type="deeply.nested.child.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .deeply.nested import child as deeply_nested_child"}
    assert name == '"deeply_nested_child.Message"'


def test_reference_deeply_nested_child_from_package(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="package",
        imports=imports,
        source_type="package.deeply.nested.child.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .deeply.nested import child as deeply_nested_child"}
    assert name == '"deeply_nested_child.Message"'


def test_reference_root_sibling(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="",
        imports=imports,
        source_type="Message",
        typing_compiler=typing_compiler,
    )

    assert imports == set()
    assert name == '"Message"'


def test_reference_nested_siblings(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="foo",
        imports=imports,
        source_type="foo.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == set()
    assert name == '"Message"'


def test_reference_deeply_nested_siblings(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="foo.bar",
        imports=imports,
        source_type="foo.bar.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == set()
    assert name == '"Message"'


def test_reference_parent_package_from_child(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="package.child",
        imports=imports,
        source_type="package.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ... import package as __package__"}
    assert name == '"__package__.Message"'


def test_reference_parent_package_from_deeply_nested_child(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="package.deeply.nested.child",
        imports=imports,
        source_type="package.deeply.nested.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ... import nested as __nested__"}
    assert name == '"__nested__.Message"'


def test_reference_ancestor_package_from_nested_child(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="package.ancestor.nested.child",
        imports=imports,
        source_type="package.ancestor.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .... import ancestor as ___ancestor__"}
    assert name == '"___ancestor__.Message"'


def test_reference_root_package_from_child(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="package.child",
        imports=imports,
        source_type="Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ... import Message as __Message__"}
    assert name == '"__Message__"'


def test_reference_root_package_from_deeply_nested_child(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="package.deeply.nested.child",
        imports=imports,
        source_type="Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ..... import Message as ____Message__"}
    assert name == '"____Message__"'


def test_reference_unrelated_package(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="a",
        imports=imports,
        source_type="p.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .. import p as _p__"}
    assert name == '"_p__.Message"'


def test_reference_unrelated_nested_package(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="a.b",
        imports=imports,
        source_type="p.q.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ...p import q as __p_q__"}
    assert name == '"__p_q__.Message"'


def test_reference_unrelated_deeply_nested_package(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="a.b.c.d",
        imports=imports,
        source_type="p.q.r.s.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .....p.q.r import s as ____p_q_r_s__"}
    assert name == '"____p_q_r_s__.Message"'


def test_reference_cousin_package(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="a.x",
        imports=imports,
        source_type="a.y.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from .. import y as _y__"}
    assert name == '"_y__.Message"'


def test_reference_cousin_package_different_name(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="test.package1",
        imports=imports,
        source_type="cousin.package2.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ...cousin import package2 as __cousin_package2__"}
    assert name == '"__cousin_package2__.Message"'


def test_reference_cousin_package_same_name(
    typing_compiler: DirectImportTypingCompiler,
):
    imports = set()
    name = get_type_reference(
        package="test.package",
        imports=imports,
        source_type="cousin.package.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ...cousin import package as __cousin_package__"}
    assert name == '"__cousin_package__.Message"'


def test_reference_far_cousin_package(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="a.x.y",
        imports=imports,
        source_type="a.b.c.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ...b import c as __b_c__"}
    assert name == '"__b_c__.Message"'


def test_reference_far_far_cousin_package(typing_compiler: DirectImportTypingCompiler):
    imports = set()
    name = get_type_reference(
        package="a.x.y.z",
        imports=imports,
        source_type="a.b.c.d.Message",
        typing_compiler=typing_compiler,
    )

    assert imports == {"from ....b.c import d as ___b_c_d__"}
    assert name == '"___b_c_d__.Message"'


@pytest.mark.parametrize(
    ["full_name", "expected_output"],
    [
        ("package.SomeMessage.NestedType", ("package", "SomeMessage.NestedType")),
        (".package.SomeMessage.NestedType", ("package", "SomeMessage.NestedType")),
        (".service.ExampleRequest", ("service", "ExampleRequest")),
        (".package.lower_case_message", ("package", "lower_case_message")),
    ],
)
def test_parse_field_type_name(full_name, expected_output):
    assert parse_source_type_name(full_name) == expected_output