File: test_oneof_default_value_serialization.py

package info (click to toggle)
python-aristaproto 1.2%2Breally0.1.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,600 kB
  • sloc: python: 6,521; java: 106; xml: 84; makefile: 6
file content (73 lines) | stat: -rw-r--r-- 2,230 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
import datetime

import aristaproto
from tests.output_aristaproto.oneof_default_value_serialization import (
    Message,
    NestedMessage,
    Test,
)


def assert_round_trip_serialization_works(message: Test) -> None:
    assert aristaproto.which_one_of(message, "value_type") == aristaproto.which_one_of(
        Test().from_json(message.to_json()), "value_type"
    )


def test_oneof_default_value_serialization_works_for_all_values():
    """
    Serialization from message with oneof set to default -> JSON -> message should keep
    default value field intact.
    """

    test_cases = [
        Test(bool_value=False),
        Test(int64_value=0),
        Test(
            timestamp_value=datetime.datetime(
                year=1970,
                month=1,
                day=1,
                hour=0,
                minute=0,
                tzinfo=datetime.timezone.utc,
            )
        ),
        Test(duration_value=datetime.timedelta(0)),
        Test(wrapped_message_value=Message(value=0)),
        # NOTE: Do NOT use aristaproto.BoolValue here, it will cause JSON serialization
        # errors.
        # TODO: Do we want to allow use of BoolValue directly within a wrapped field or
        # should we simply hard fail here?
        Test(wrapped_bool_value=False),
    ]
    for message in test_cases:
        assert_round_trip_serialization_works(message)


def test_oneof_no_default_values_passed():
    message = Test()
    assert (
        aristaproto.which_one_of(message, "value_type")
        == aristaproto.which_one_of(Test().from_json(message.to_json()), "value_type")
        == ("", None)
    )


def test_oneof_nested_oneof_messages_are_serialized_with_defaults():
    """
    Nested messages with oneofs should also be handled
    """
    message = Test(
        wrapped_nested_message_value=NestedMessage(
            id=0, wrapped_message_value=Message(value=0)
        )
    )
    assert (
        aristaproto.which_one_of(message, "value_type")
        == aristaproto.which_one_of(Test().from_json(message.to_json()), "value_type")
        == (
            "wrapped_nested_message_value",
            NestedMessage(id=0, wrapped_message_value=Message(value=0)),
        )
    )