def test_which_one_of_returns_second_field_when_set():
    message = Test()
    message.from_json(get_test_case_json_data("oneof_enum"))
    assert message.move == Move(x=2, y=3)
    assert message.signal == 0
    assert betterproto.which_one_of(message,
                                    "action") == ("move", Move(x=2, y=3))
Exemple #2
0
def test_data(request):
    test_case_name = request.param

    # Reset the internal symbol database so we can import the `Test` message
    # multiple times. Ugh.
    sym = symbol_database.Default()
    sym.pool = DescriptorPool()

    reference_module_root = os.path.join(*reference_output_package.split("."),
                                         test_case_name)
    sys.path.append(reference_module_root)

    plugin_module = importlib.import_module(
        f"{plugin_output_package}.{test_case_name}")

    plugin_module_entry_point = find_module(plugin_module,
                                            module_has_entry_point)

    if not plugin_module_entry_point:
        raise Exception(
            f"Test case {repr(test_case_name)} has no entry point. "
            "Please add a proto message or service called Test and recompile.")

    yield (TestData(
        plugin_module=plugin_module_entry_point,
        reference_module=lambda: importlib.import_module(
            f"{reference_output_package}.{test_case_name}.{test_case_name}_pb2"
        ),
        json_data=get_test_case_json_data(test_case_name),
    ))

    sys.path.remove(reference_module_root)
def test_which_one_of_returns_enum_with_non_default_value():
    """
    returns first field when it is enum and set with non default value
    """
    message = Test()
    message.from_json(
        get_test_case_json_data("oneof_enum", "oneof_enum-enum-1.json"))
    assert message.move is None
    assert message.signal == Signal.PASS
    assert betterproto.which_one_of(message,
                                    "action") == ("signal", Signal.RESIGN)
def test_which_one_of_returns_enum_with_non_default_value():
    """
    returns first field when it is enum and set with non default value
    """
    message = Test()
    message.from_json(
        get_test_case_json_data("oneof_enum", "oneof_enum-enum-1.json"))
    assert message.move == Move(
        x=0, y=0)  # Proto3 will default this as there is no null
    assert message.signal == Signal.RESIGN
    assert betterproto.which_one_of(message,
                                    "action") == ("signal", Signal.RESIGN)
Exemple #5
0
    def __init__(self, path, services: Set[str], xfail: Set[str]):
        _all = set(get_directories(path)) - {"__pycache__"}
        _services = services
        _messages = (_all - services) - {"__pycache__"}
        _messages_with_json = {
            test
            for test in _messages if get_test_case_json_data(test)
        }

        unknown_xfail_tests = xfail - _all
        if unknown_xfail_tests:
            raise Exception(
                f"Unknown test(s) in config.py: {unknown_xfail_tests}")

        self.all = self.apply_xfail_marks(_all, xfail)
        self.services = self.apply_xfail_marks(_services, xfail)
        self.messages = self.apply_xfail_marks(_messages, xfail)
        self.messages_with_json = self.apply_xfail_marks(
            _messages_with_json, xfail)
Exemple #6
0
def test_which_count():
    message = Test()
    message.from_json(get_test_case_json_data("oneof"))
    assert betterproto.which_one_of(message, "foo") == ("count", 100)
Exemple #7
0
def test_which_name():
    message = Test()
    message.from_json(get_test_case_json_data("oneof", "oneof-name.json"))
    assert betterproto.which_one_of(message, "foo") == ("name", "foobar")
def test_which_name():
    message = Test()
    message.from_json(
        get_test_case_json_data("oneof", "oneof_name.json")[0].json)
    assert betterproto.which_one_of(message, "foo") == ("pitier", "Mr. T")