示例#1
0
def test_bool():
    test_type = type_test_pb2.TypeTester()
    assert test_type.bool_value is False
    test_type.bool_value = True
    assert test_type.bool_value is True
    del test_type.bool_value
    assert test_type.bool_value is False
示例#2
0
def test_floats(float_type):
    test_type = type_test_pb2.TypeTester()
    assert getattr(test_type, f"{float_type}_value") == 0
    setattr(test_type, f"{float_type}_value", 0.5)
    assert getattr(test_type, f"{float_type}_value") == 0.5
    setattr(test_type, f"{float_type}_value", 1 / 3)
    if float_type == "double":
        assert getattr(test_type, f"{float_type}_value") == 1 / 3
    else:
        assert getattr(test_type, f"{float_type}_value") == 0.3333333432674408
示例#3
0
def test_map_scalar_value():
    test_type = type_test_pb2.TypeTester()
    assert len(test_type.map_to_int32_value) == 0
    assert "lol" not in test_type.map_to_int32_value
    assert test_type.map_to_int32_value["lol"] == 0
    assert "lol" not in test_type.map_to_int32_value
    test_type.map_to_int32_value["lol"] = 42
    assert "lol" in test_type.map_to_int32_value
    assert len(test_type.map_to_int32_value) == 1
    assert test_type.map_to_int32_value["lol"] == 42
    assert list(test_type.map_to_int32_value) == ["lol"]
示例#4
0
def test_repeated_int32():
    test_type = type_test_pb2.TypeTester()
    assert len(test_type.repeated_int32_value) == 0
    test_type.repeated_int32_value.add(42)
    assert len(test_type.repeated_int32_value) == 1
    test_type.repeated_int32_value.add(3.14)
    assert len(test_type.repeated_int32_value) == 2
    assert list(test_type.repeated_int32_value) == [42, 3]
    assert test_type.repeated_int32_value[:] == [42, 3]
    assert test_type.repeated_int32_value[:1] == [42]
    assert test_type.ToJsonString() == '{"repeatedInt32Value":[42,3]}'
示例#5
0
def test_bytes():
    test_type = type_test_pb2.TypeTester()
    assert test_type.bytes_value == b""
    with pytest.raises(TypeError):
        test_type.bytes_value = "not bytes"
    test_type.bytes_value = b"ascii"
    assert test_type.bytes_value == b"ascii"
    test_type.bytes_value = b"\xc3\x28"
    assert test_type.bytes_value == b"\xc3\x28"
    assert len(test_type.bytes_value) == 2
    del test_type.bytes_value
    assert test_type.bytes_value == b""
示例#6
0
def test_repeated_string_slice():
    test_type = type_test_pb2.TypeTester()
    expected = ["x" * i for i in range(100)]
    for i in range(100):
        test_type.repeated_string_value.add(expected[i])
    assert len(test_type.repeated_string_value) == 100
    assert test_type.repeated_string_value[-1] == "x" * 99
    assert test_type.repeated_string_value[-99] == "x"
    assert test_type.repeated_string_value[0:2] == ["", "x"]
    assert test_type.repeated_string_value[:] == expected
    assert test_type.repeated_string_value[::] == expected
    assert test_type.repeated_string_value[-100:1000:] == expected
    assert test_type.repeated_string_value[3:-1:7] == expected[3:-1:7]
示例#7
0
def test_map_message_value():
    test_type = type_test_pb2.TypeTester()
    assert len(test_type.map_to_submessage_value) == 0
    assert "foo" not in test_type.map_to_submessage_value
    test_type.map_to_submessage_value["foo"].value = "hi"
    assert "foo" in test_type.map_to_submessage_value
    assert test_type.map_to_submessage_value["foo"].value == "hi"
    bar = test_type.map_to_submessage_value["bar"]
    test_type.map_to_submessage_value["bar"].value = "raz"
    assert test_type.map_to_submessage_value["bar"].value == "raz"
    bar.value = "meh"
    assert test_type.map_to_submessage_value["bar"].value == "meh"
    assert {(k, m.value) for k, m in test_type.map_to_submessage_value.items()} == {
        ("foo", "hi"),
        ("bar", "meh"),
    }
示例#8
0
def test_int32(int_type, signed, bits):
    test_type = type_test_pb2.TypeTester()
    assert getattr(test_type, f"{int_type}_value") == 0
    setattr(test_type, f"{int_type}_value", 42)
    assert getattr(test_type, f"{int_type}_value") == 42
    setattr(test_type, f"{int_type}_value", (1 << (bits - 1)) - 1)
    assert getattr(test_type, f"{int_type}_value") == (1 << (bits - 1)) - 1
    if signed:
        setattr(test_type, f"{int_type}_value", -1)
        assert getattr(test_type, f"{int_type}_value") == -1
        with pytest.raises(OverflowError):
            setattr(test_type, f"{int_type}_value", (1 << bits) - 1)
    else:
        setattr(test_type, f"{int_type}_value", (1 << bits) - 1)
        assert getattr(test_type, f"{int_type}_value") == (1 << bits) - 1
        with pytest.raises(OverflowError):
            setattr(test_type, f"{int_type}_value", -1)
    delattr(test_type, f"{int_type}_value")
    assert getattr(test_type, f"{int_type}_value") == 0
示例#9
0
def test_repeated_string():
    test_type = type_test_pb2.TypeTester()
    assert len(test_type.repeated_string_value) == 0
    test_type.repeated_string_value.add("hello world")
    assert len(test_type.repeated_string_value) == 1
    assert list(test_type.repeated_string_value) == ["hello world"]