Skip to content

Commit

Permalink
Add tests for dq flags.
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJamieson committed Nov 24, 2023
1 parent fe37dec commit 8e4aa62
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/roman_datamodels/dqflags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down Expand Up @@ -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.
Expand Down
68 changes: 68 additions & 0 deletions tests/test_dqflags.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8e4aa62

Please sign in to comment.