Skip to content

Commit dc056f6

Browse files
committed
Add unit tests and enable slots.
This makes sure we can't accidentally set flags that don't exist, which ought to help downstream code.
1 parent 2e75a52 commit dc056f6

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/labthings_fastapi/feature_flags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from typing import Any
1919

2020

21-
@dataclass
21+
@dataclass(slots=True)
2222
class LabThingsFeatureFlags:
2323
"""Control over optional features of LabThings."""
2424

tests/test_feature_flags.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""Test the feature flags mechanism.
2+
3+
Specific feature flags should be tested by the test code for the relevant feature. This
4+
test module checks that `set_temporarily` works as expected.
5+
"""
6+
7+
import pytest
8+
9+
import labthings_fastapi as lt
10+
11+
12+
@pytest.mark.parametrize("value", [True, False])
13+
def test_set_temporarily(value):
14+
"""Test values may be set and reset."""
15+
value_before = lt.FEATURE_FLAGS.validate_properties_on_set
16+
17+
with lt.FEATURE_FLAGS.set_temporarily(validate_properties_on_set=value):
18+
assert lt.FEATURE_FLAGS.validate_properties_on_set == value
19+
20+
with lt.FEATURE_FLAGS.set_temporarily(validate_properties_on_set=not value):
21+
assert lt.FEATURE_FLAGS.validate_properties_on_set != value
22+
23+
assert lt.FEATURE_FLAGS.validate_properties_on_set == value
24+
25+
assert lt.FEATURE_FLAGS.validate_properties_on_set == value_before
26+
27+
28+
def test_set_bad_setting():
29+
"""Test for errors when bad flags are used."""
30+
with pytest.raises(AttributeError):
31+
with lt.FEATURE_FLAGS.set_temporarily(bogus_name=True):
32+
pass
33+
with pytest.raises(AttributeError):
34+
lt.FEATURE_FLAGS.bogus_name = True

0 commit comments

Comments
 (0)