From 8c423b3d06c67f02b87127665883ff366f2ac454 Mon Sep 17 00:00:00 2001 From: pthierry Date: Fri, 12 Jul 2024 14:29:00 +0200 Subject: [PATCH 1/2] pytest: increase coverage to more than 50 percent --- tests/test_dump.py | 19 +++++++++++ tests/test_filters.py | 79 +++++++++++++++++++++++++++++++++++++++++++ tests/test_node.py | 19 +++++++++++ 3 files changed, 117 insertions(+) create mode 100644 tests/test_dump.py create mode 100644 tests/test_filters.py create mode 100644 tests/test_node.py diff --git a/tests/test_dump.py b/tests/test_dump.py new file mode 100644 index 0000000..b685f06 --- /dev/null +++ b/tests/test_dump.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2023 - 2024 Ledger SAS +# +# SPDX-License-Identifier: Apache-2.0 + +# flake8: noqa + +import pathlib +import dts_utils +import dts_utils.dump +import dts_utils.filters + + +def _get_dtsload() -> dts_utils.Dts: + return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") + + +def test_dump(): + dts = _get_dtsload() + usart1_info = dts_utils.dump.dump(dts, "usart1", True) diff --git a/tests/test_filters.py b/tests/test_filters.py new file mode 100644 index 0000000..204f1a6 --- /dev/null +++ b/tests/test_filters.py @@ -0,0 +1,79 @@ +# SPDX-FileCopyrightText: 2023 - 2024 Ledger SAS +# +# SPDX-License-Identifier: Apache-2.0 + +# flake8: noqa + +import pathlib +import dts_utils +import dts_utils.filters + + +def _get_dtsload() -> dts_utils.Dts: + return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") + + +def test_filter_enabled(): + dts = _get_dtsload() + pinctrl_list = dts_utils.filters.f_peripherals(dts.soc.pinctrl) + enabled_gpios = dts_utils.filters.f_enabled(pinctrl_list) + assert len(enabled_gpios) == 7 + + +def test_filter_enabled_exceptions(): + dts = _get_dtsload() + try: + enabled_gpios = dts_utils.filters.f_enabled(dts) + assert False + except dts_utils.exceptions.InvalidTemplateValueType: + assert True + + +def test_filter_owner(): + dts = _get_dtsload() + i2c1 = dts.i2c1 + assert dts_utils.filters.f_owner(i2c1) == 0xBABE + + +def test_filter_owner_exceptions(): + dts = _get_dtsload() + try: + enabled_gpios = dts_utils.filters.f_owner(dts) + assert False + except dts_utils.exceptions.InvalidTemplateValueType: + assert True + + +def test_filter_has_property(): + dts = _get_dtsload() + i2c1 = dts.i2c1 + assert dts_utils.filters.f_has_property(i2c1, "outpost,owner") + + +def test_filter_has_property_exception(): + dts = _get_dtsload() + try: + dts_utils.filters.f_has_property(dts, "outpost,owner") + assert False + except dts_utils.exceptions.InvalidTemplateValueType: + assert True + + +def test_filter_with_property(): + dts = _get_dtsload() + dev_list = dts_utils.filters.f_peripherals(dts.root) + assert len(dts_utils.filters.f_with_property(dev_list, "outpost,owner")) == 1 + + +def test_filter_with_property_exception(): + dts = _get_dtsload() + try: + dts_utils.filters.f_with_property(dts, "outpost,owner") + assert False + except dts_utils.exceptions.InvalidTemplateValueType: + assert True + try: + dts_utils.filters.f_with_property(dts.usart1, "outpost,owner") + assert False + except dts_utils.exceptions.InvalidTemplateValueType: + assert True diff --git a/tests/test_node.py b/tests/test_node.py new file mode 100644 index 0000000..b637507 --- /dev/null +++ b/tests/test_node.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2023 - 2024 Ledger SAS +# +# SPDX-License-Identifier: Apache-2.0 + +# flake8: noqa + +import pathlib +import dts_utils + + +def _get_dtsload() -> dts_utils.Dts: + return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") + + +def test_node_id(): + dts = _get_dtsload() + usart1 = dts.usart1 + assert usart1.label == "usart1" + assert usart1.name == "serial@40013800" From e64fc62bd4a9f31fb2ef86303ff0bc9938606ece Mon Sep 17 00:00:00 2001 From: pthierry Date: Fri, 12 Jul 2024 14:52:23 +0200 Subject: [PATCH 2/2] pytest: using module global fixture --- tests/conftest.py | 19 +++++++++++++++++ tests/test_dump.py | 11 ++-------- tests/test_filters.py | 47 ++++++++++++++++--------------------------- tests/test_load.py | 11 +++------- tests/test_node.py | 10 ++------- 5 files changed, 43 insertions(+), 55 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..598968e --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,19 @@ +# SPDX-FileCopyrightText: 2023 - 2024 Ledger SAS +# +# SPDX-License-Identifier: Apache-2.0 + +# flake8: noqa + +import pathlib +import dts_utils +import pytest + + +class Dts: + def __init__(self): + self.dts = dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") + + +@pytest.fixture(scope="module") +def dts_file(): + return Dts() diff --git a/tests/test_dump.py b/tests/test_dump.py index b685f06..a65e708 100644 --- a/tests/test_dump.py +++ b/tests/test_dump.py @@ -4,16 +4,9 @@ # flake8: noqa -import pathlib import dts_utils import dts_utils.dump -import dts_utils.filters -def _get_dtsload() -> dts_utils.Dts: - return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") - - -def test_dump(): - dts = _get_dtsload() - usart1_info = dts_utils.dump.dump(dts, "usart1", True) +def test_dump(dts_file): + usart1_info = dts_utils.dump.dump(dts_file.dts, "usart1", True) diff --git a/tests/test_filters.py b/tests/test_filters.py index 204f1a6..4c49afc 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -4,76 +4,63 @@ # flake8: noqa -import pathlib import dts_utils import dts_utils.filters -def _get_dtsload() -> dts_utils.Dts: - return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") - - -def test_filter_enabled(): - dts = _get_dtsload() - pinctrl_list = dts_utils.filters.f_peripherals(dts.soc.pinctrl) +def test_filter_enabled(dts_file): + pinctrl_list = dts_utils.filters.f_peripherals(dts_file.dts.soc.pinctrl) enabled_gpios = dts_utils.filters.f_enabled(pinctrl_list) assert len(enabled_gpios) == 7 -def test_filter_enabled_exceptions(): - dts = _get_dtsload() +def test_filter_enabled_exceptions(dts_file): try: - enabled_gpios = dts_utils.filters.f_enabled(dts) + enabled_gpios = dts_utils.filters.f_enabled(dts_file.dts) assert False except dts_utils.exceptions.InvalidTemplateValueType: assert True -def test_filter_owner(): - dts = _get_dtsload() - i2c1 = dts.i2c1 +def test_filter_owner(dts_file): + i2c1 = dts_file.dts.i2c1 assert dts_utils.filters.f_owner(i2c1) == 0xBABE -def test_filter_owner_exceptions(): - dts = _get_dtsload() +def test_filter_owner_exceptions(dts_file): try: - enabled_gpios = dts_utils.filters.f_owner(dts) + enabled_gpios = dts_utils.filters.f_owner(dts_file.dts) assert False except dts_utils.exceptions.InvalidTemplateValueType: assert True -def test_filter_has_property(): - dts = _get_dtsload() - i2c1 = dts.i2c1 +def test_filter_has_property(dts_file): + i2c1 = dts_file.dts.i2c1 assert dts_utils.filters.f_has_property(i2c1, "outpost,owner") -def test_filter_has_property_exception(): - dts = _get_dtsload() +def test_filter_has_property_exception(dts_file): try: - dts_utils.filters.f_has_property(dts, "outpost,owner") + dts_utils.filters.f_has_property(dts_file.dts, "outpost,owner") assert False except dts_utils.exceptions.InvalidTemplateValueType: assert True -def test_filter_with_property(): - dts = _get_dtsload() - dev_list = dts_utils.filters.f_peripherals(dts.root) +def test_filter_with_property(dts_file): + dev_list = dts_utils.filters.f_peripherals(dts_file.dts.root) assert len(dts_utils.filters.f_with_property(dev_list, "outpost,owner")) == 1 -def test_filter_with_property_exception(): - dts = _get_dtsload() +def test_filter_with_property_exception(dts_file): try: - dts_utils.filters.f_with_property(dts, "outpost,owner") + dts_utils.filters.f_with_property(dts_file.dts, "outpost,owner") assert False except dts_utils.exceptions.InvalidTemplateValueType: assert True try: - dts_utils.filters.f_with_property(dts.usart1, "outpost,owner") + dts_utils.filters.f_with_property(dts_file.dts.usart1, "outpost,owner") assert False except dts_utils.exceptions.InvalidTemplateValueType: assert True diff --git a/tests/test_load.py b/tests/test_load.py index c23f2ef..0c5404f 100644 --- a/tests/test_load.py +++ b/tests/test_load.py @@ -4,14 +4,9 @@ # flake8: noqa -import pathlib import dts_utils -def test_dtsload(): - dts = dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") - - -def test_socload(): - dts = dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") - soc = dts.soc +def test_socload(dts_file): + soc = dts_file.dts.soc + assert soc != None diff --git a/tests/test_node.py b/tests/test_node.py index b637507..23618b2 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -4,16 +4,10 @@ # flake8: noqa -import pathlib import dts_utils -def _get_dtsload() -> dts_utils.Dts: - return dts_utils.Dts(pathlib.Path(__file__).parent.absolute() / "dts/sample.dts") - - -def test_node_id(): - dts = _get_dtsload() - usart1 = dts.usart1 +def test_node_id(dts_file): + usart1 = dts_file.dts.usart1 assert usart1.label == "usart1" assert usart1.name == "serial@40013800"