From 8e4aa62865226866e63df611b2e84c86bc357217 Mon Sep 17 00:00:00 2001 From: William Jamieson Date: Thu, 16 Nov 2023 11:18:43 -0500 Subject: [PATCH] Add tests for dq flags. --- src/roman_datamodels/dqflags.py | 4 +- tests/test_dqflags.py | 68 +++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 tests/test_dqflags.py diff --git a/src/roman_datamodels/dqflags.py b/src/roman_datamodels/dqflags.py index 5410c7ffc..9d4469954 100644 --- a/src/roman_datamodels/dqflags.py +++ b/src/roman_datamodels/dqflags.py @@ -18,10 +18,11 @@ which provides 32 bits. Bits of an integer are most easily referred to using the formula `2**bit_number` where `bit_number` is the 0-index bit of interest. """ -from enum import IntEnum +from enum import IntEnum, unique # fmt: off +@unique class pixel(IntEnum): """Pixel-specific data quality flags""" @@ -59,6 +60,7 @@ class pixel(IntEnum): REFERENCE_PIXEL = 2**31 # Pixel is a reference pixel +@unique class group(IntEnum): """Group-specific data quality flags Once groups are combined, these flags are equivalent to the pixel-specific flags. diff --git a/tests/test_dqflags.py b/tests/test_dqflags.py new file mode 100644 index 000000000..21b1a4405 --- /dev/null +++ b/tests/test_dqflags.py @@ -0,0 +1,68 @@ +from math import log10 + +import pytest + +from roman_datamodels import dqflags + + +def _is_power_of_two(x): + return (log10(x) / log10(2)) % 1 == 0 + + +def test_pixel_uniqueness(): + """ + Test that there are no duplicate names in dqflags.pixel + + Note: The @unique decorator should ensure that no flag names have the + same value as another in the enum raising an error at first import + of this module. However, this test is just a sanity check on this. + """ + + assert len(dqflags.pixel) == len(dqflags.pixel.__members__) + + +@pytest.mark.parametrize("flag", dqflags.pixel) +def test_pixel_flags(flag): + """Test that each pixel flag follows the defined rules""" + # Test that the pixel flags are dqflags.pixel instances + assert isinstance(flag, dqflags.pixel) + + # Test that the pixel flags are ints + assert isinstance(flag, int) + + # Test that the pixel flags are dict accessible + assert dqflags.pixel[flag.name] is flag + + # Test that the pixel flag is a power of 2 + if flag.name == "GOOD": + # GOOD is the only non-power-of-two flag (it is 0) + assert flag.value == 0 + else: + assert _is_power_of_two(flag.value) + + +def test_group_uniqueness(): + """ + Test that there are no duplicate names in dqflags.group + + Note: The @unique decorator should ensure that no flag names have the + same value as another in the enum raising an error at first import + of this module. However, this test is just a sanity check on this. + """ + assert len(dqflags.group) == len(dqflags.group.__members__) + + +@pytest.mark.parametrize("flag", dqflags.group) +def test_group_flags(flag): + """Test that each group flag follows the defined rules""" + # Test that the group flags are dqflags.group instances + assert isinstance(flag, dqflags.group) + + # Test that the group flags are ints + assert isinstance(flag, int) + + # Test that the group flags are dict accessible + assert dqflags.group[flag.name] is flag + + # Test that each group flag matches a pixel flag of the same name + assert dqflags.pixel[flag.name] == flag