File: test_struct.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 (36 lines) | stat: -rw-r--r-- 1,265 bytes parent folder | download | duplicates (3)
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
import json

from aristaproto.lib.google.protobuf import Struct
from aristaproto.lib.pydantic.google.protobuf import Struct as StructPydantic


def test_struct_roundtrip():
    data = {
        "foo": "bar",
        "baz": None,
        "quux": 123,
        "zap": [1, {"two": 3}, "four"],
    }
    data_json = json.dumps(data)

    struct_from_dict = Struct().from_dict(data)
    assert struct_from_dict.fields == data
    assert struct_from_dict.to_dict() == data
    assert struct_from_dict.to_json() == data_json

    struct_from_json = Struct().from_json(data_json)
    assert struct_from_json.fields == data
    assert struct_from_json.to_dict() == data
    assert struct_from_json == struct_from_dict
    assert struct_from_json.to_json() == data_json

    struct_pyd_from_dict = StructPydantic(fields={}).from_dict(data)
    assert struct_pyd_from_dict.fields == data
    assert struct_pyd_from_dict.to_dict() == data
    assert struct_pyd_from_dict.to_json() == data_json

    struct_pyd_from_dict = StructPydantic(fields={}).from_json(data_json)
    assert struct_pyd_from_dict.fields == data
    assert struct_pyd_from_dict.to_dict() == data
    assert struct_pyd_from_dict == struct_pyd_from_dict
    assert struct_pyd_from_dict.to_json() == data_json