Esempio n. 1
0
def test_ToFile_FromFile_equivalence(suffix):
    """Test that ToFile() and FromFile() are symmetrical."""
    with tempfile.TemporaryDirectory(prefix='labm8_proto_') as d:
        path = pathlib.Path(d) / f'proto{suffix}'
        proto_in = test_protos_pb2.TestMessage(string='abc', number=1)
        pbutil.ToFile(proto_in, path)
        assert path.is_file()
        proto_out = test_protos_pb2.TestMessage()
        pbutil.FromFile(path, proto_out)
        assert proto_out.string == 'abc'
        assert proto_out.number == 1
        assert proto_in == proto_out
Esempio n. 2
0
def test_AssertFieldConstraint_no_callback_return_value():
    """Field value is returned when no callback and field is set."""
    t = test_protos_pb2.TestMessage()
    t.string = 'foo'
    t.number = 5
    assert 'foo' == pbutil.AssertFieldConstraint(t, 'string')
    assert 5 == pbutil.AssertFieldConstraint(t, 'number')
Esempio n. 3
0
def test_FromFile_FileNotFoundError(suffix):
    """Test that FileNotFoundError raised if file doesn't exist."""
    with tempfile.TemporaryDirectory(prefix='labm8_proto_') as d:
        with pytest.raises(FileNotFoundError):
            pbutil.FromFile(
                pathlib.Path(d) / f'proto{suffix}',
                test_protos_pb2.TestMessage())
Esempio n. 4
0
def test_ToFile_message_missing_required_fields(suffix):
    """Test that EncodeError is raised if required field is not set."""
    with tempfile.NamedTemporaryFile(prefix='labm8_proto_',
                                     suffix=suffix) as f:
        proto = test_protos_pb2.TestMessage(number=1)
        with pytest.raises(pbutil.EncodeError):
            pbutil.ToFile(proto, pathlib.Path(f.name))
Esempio n. 5
0
def test_ToFile_parent_directory_does_not_exist(suffix):
    """Test that FileNotFoundError raised if parent directory doesn't exist."""
    with tempfile.TemporaryDirectory() as d:
        proto = test_protos_pb2.TestMessage(string='abc', number=1)
        with pytest.raises(FileNotFoundError):
            pbutil.ToFile(proto,
                          pathlib.Path(d) / 'notadir' / f'proto{suffix}')
Esempio n. 6
0
def test_ToFile_path_is_directory(suffix):
    """Test that IsADirectoryError raised if path is a directory."""
    with tempfile.TemporaryDirectory(suffix=suffix) as d:
        proto = test_protos_pb2.TestMessage(string='abc', number=1)
        with pytest.raises(IsADirectoryError) as e_info:
            pbutil.ToFile(proto, pathlib.Path(d))
        assert str(e_info.value).endswith(f"Is a directory: '{d}'")
Esempio n. 7
0
def test_AssertFieldIsSet_field_is_set():
    """Field value is returned when field is set."""
    t = test_protos_pb2.TestMessage()
    t.string = 'foo'
    t.number = 5
    assert 'foo' == pbutil.AssertFieldIsSet(t, 'string')
    assert 5 == pbutil.AssertFieldIsSet(t, 'number')
Esempio n. 8
0
def test_AssertFieldConstraint_user_callback_passes():
    """Field value is returned when user callback passes."""
    t = test_protos_pb2.TestMessage()
    t.string = 'foo'
    t.number = 5
    assert 'foo' == pbutil.AssertFieldConstraint(t, 'string',
                                                 lambda x: x == 'foo')
    assert 5 == pbutil.AssertFieldConstraint(t, 'number', lambda x: 1 < x < 10)
Esempio n. 9
0
def test_AssertFieldConstraint_field_not_set():
    """ValueError is raised if the requested field is not set."""
    t = test_protos_pb2.TestMessage()
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldConstraint(t, 'string')
    assert "Field not set: 'TestMessage.string'" == str(e_info.value)
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldConstraint(t, 'number')
    assert "Field not set: 'TestMessage.number'" == str(e_info.value)
Esempio n. 10
0
def test_AssertFieldIsSet_user_callback_custom_fail_message():
    """Test that the requested message is returned on callback fail."""
    t = test_protos_pb2.TestMessage()
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldIsSet(t, 'string', 'Hello, world!')
    assert 'Hello, world!' == str(e_info.value)
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldIsSet(t, 'number', fail_message='Hello, world!')
    assert 'Hello, world!' == str(e_info.value)
Esempio n. 11
0
def test_FromFile_required_fields_not_set(suffix):
    """Test that DecodeError raised if required fields not set."""
    with tempfile.NamedTemporaryFile(prefix='labm8_proto_',
                                     suffix=suffix) as f:
        pbutil.ToFile(test_protos_pb2.AnotherTestMessage(number=1),
                      pathlib.Path(f.name))
        with pytest.raises(pbutil.DecodeError) as e_info:
            pbutil.FromFile(pathlib.Path(f.name),
                            test_protos_pb2.TestMessage())
        assert f"Required fields not set: '{f.name}'" == str(e_info.value)
Esempio n. 12
0
def test_FromFile_required_fields_not_set_uninitialized_okay(suffix):
    """Test that DecodeError not raised if required fields not set."""
    with tempfile.NamedTemporaryFile(prefix='labm8_proto_',
                                     suffix=suffix) as f:
        proto_in = test_protos_pb2.AnotherTestMessage(number=1)
        pbutil.ToFile(test_protos_pb2.AnotherTestMessage(number=1),
                      pathlib.Path(f.name))
        pbutil.FromFile(pathlib.Path(f.name),
                        test_protos_pb2.TestMessage(),
                        uninitialized_okay=True)
Esempio n. 13
0
def test_AssertFieldConstraint_user_callback_raises_exception():
    """If callback raises exception, it is passed to calling code."""
    t = test_protos_pb2.TestMessage()
    t.string = 'foo'

    def CallbackWhichRaisesException(x):
        """Test callback which raises an exception"""
        raise FileExistsError('foo')

    with pytest.raises(FileExistsError) as e_info:
        pbutil.AssertFieldConstraint(t, 'string', CallbackWhichRaisesException)
    assert str(e_info.value) == 'foo'
Esempio n. 14
0
def test_AssertFieldConstraint_user_callback_fails():
    """ProtoValueError raised when when user callback fails."""
    t = test_protos_pb2.TestMessage()
    t.string = 'foo'
    t.number = 5
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldConstraint(t, 'string', lambda x: x == 'bar')
    assert "Field fails constraint check: 'TestMessage.string'" == str(
        e_info.value)
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldConstraint(t, 'number', lambda x: 10 < x < 100)
    assert "Field fails constraint check: 'TestMessage.number'" == str(
        e_info.value)
Esempio n. 15
0
def test_AssertFieldConstraint_user_callback_custom_fail_message():
    """Test that the requested message is returned on callback fail."""
    t = test_protos_pb2.TestMessage()
    t.string = 'foo'

    # Constraint function fails.
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldConstraint(t, 'string', lambda x: x == 'bar',
                                     'Hello, world!')
    assert 'Hello, world!' == str(e_info.value)

    # Field not set.
    with pytest.raises(pbutil.ProtoValueError) as e_info:
        pbutil.AssertFieldConstraint(t, 'number', fail_message='Hello, world!')
    assert 'Hello, world!' == str(e_info.value)
Esempio n. 16
0
def test_FromFile_IsADirectoryError(suffix):
    """Test that IsADirectoryError raised if path is a directory."""
    with tempfile.TemporaryDirectory(prefix='labm8_proto_',
                                     suffix=suffix) as d:
        with pytest.raises(IsADirectoryError):
            pbutil.FromFile(pathlib.Path(d), test_protos_pb2.TestMessage())
Esempio n. 17
0
def test_FromString_empty_string_required_fields_uninitialized_okay():
    """Test that an empty string raises DecodeError if uninitialized fields."""
    proto = pbutil.FromString('',
                              test_protos_pb2.TestMessage(),
                              uninitialized_okay=True)
    assert not proto.string
Esempio n. 18
0
def test_FromString_empty_string_required_fields():
    """Test that an empty string raises DecodeError if uninitialized fields."""
    with pytest.raises(pbutil.DecodeError):
        pbutil.FromString('', test_protos_pb2.TestMessage())
Esempio n. 19
0
def test_AssFieldIsSet_oneof_field_no_return():
    """Test that no value is returned when a oneof field is set."""
    t = test_protos_pb2.TestMessage()
    t.option_a = 1
    assert pbutil.AssertFieldIsSet(t, 'union_field') is None
    assert 1 == pbutil.AssertFieldIsSet(t, 'option_a')
Esempio n. 20
0
def test_AssertFieldConstraint_invalid_field_name():
    """ValueError is raised if the requested field name does not exist."""
    t = test_protos_pb2.TestMessage()
    with pytest.raises(ValueError):
        pbutil.AssertFieldConstraint(t, 'not_a_real_field')