Skip to content

Commit 760fea9

Browse files
committed
feat(test-tools): SaaS/Enterprise mode markers, update documentation
1 parent 8e2b042 commit 760fea9

File tree

5 files changed

+99
-10
lines changed

5 files changed

+99
-10
lines changed

README.md

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,56 @@ make install-packages opts="--extras 'feature1 feature2'"
4646

4747
#### Installation
4848

49-
1. Make sure `"common.core"` is in the `INSTALLED_APPS` of your settings module.
49+
1. To make use of the `test_tools` Pytest plugin, install the packages with the `test-tools` extra, e.g. `pip install flagsmith-common[test-tools]`.
50+
51+
2. Make sure `"common.core"` is in the `INSTALLED_APPS` of your settings module.
5052
This enables the `manage.py flagsmith` commands.
5153

52-
2. Add `"common.gunicorn.middleware.RouteLoggerMiddleware"` to `MIDDLEWARE` in your settings module.
54+
3. Add `"common.gunicorn.middleware.RouteLoggerMiddleware"` to `MIDDLEWARE` in your settings module.
5355
This enables the `route` label for Prometheus HTTP metrics.
5456

55-
3. To enable the `/metrics` endpoint, set the `PROMETHEUS_ENABLED` setting to `True`.
57+
4. To enable the `/metrics` endpoint, set the `PROMETHEUS_ENABLED` setting to `True`.
58+
59+
#### Test tools
60+
61+
##### Fixtures
62+
63+
###### `assert_metric`
64+
65+
To test your metrics using the `assert_metric` fixture:
66+
67+
```python
68+
from common.test_tools import AssertMetricFixture
69+
70+
def test_my_code__expected_metrics(assert_metric: AssertMetricFixture) -> None:
71+
# When
72+
my_code()
73+
74+
# Then
75+
assert_metric(
76+
name="flagsmith_distance_from_earth_au_sum",
77+
labels={"engine_type": "solar_sail"},
78+
value=1.0,
79+
)
80+
```
81+
82+
###### `saas_mode`
83+
84+
The `saas_mode` fixture makes all `common.core.utils.is_saas` calls return `True`.
85+
86+
###### `enterprise_mode`
87+
88+
The `enterprise_mode` fixture makes all `common.core.utils.is_enterprise` calls return `True`.
89+
90+
##### Markers
91+
92+
###### `pytest.mark.saas_mode`
93+
94+
Use this mark to auto-use the `saas_mode` fixture.
95+
96+
###### `pytest.mark.enterprise_mode`
97+
98+
Use this mark to auto-use the `enterprise_mode` fixture.
5699

57100
#### Metrics
58101

@@ -87,14 +130,18 @@ It's generally a good idea to allow users to define histogram buckets of their o
87130
import prometheus_client
88131
from django.conf import settings
89132

90-
flagsmith_distance_from_earth_au = prometheus.Histogram(
133+
flagsmith_distance_from_earth_au = prometheus_client.Histogram(
91134
"flagsmith_distance_from_earth_au",
92135
"Distance from Earth in astronomical units",
136+
labels=["engine_type"],
93137
buckets=settings.DISTANCE_FROM_EARTH_AU_HISTOGRAM_BUCKETS,
94138
)
95139
```
96140

141+
For testing your metrics, refer to [`assert_metric` documentation][5].
142+
97143
[1]: https://prometheus.io/docs/practices/naming/
98144
[2]: https://github.com/Flagsmith/flagsmith-common/blob/main/src/common/gunicorn/metrics.py
99145
[3]: https://docs.gunicorn.org/en/stable/design.html#server-model
100146
[4]: https://prometheus.github.io/client_python/multiprocess
147+
[5]: #assert_metric

poetry.lock

Lines changed: 9 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies = [
1818
"psycopg2-binary (>=2.9,<3)",
1919
"simplejson (>=3,<4)",
2020
]
21+
optional-dependencies = { test-tools = ["pytest-mock (>=3,<4)"] }
2122
authors = [
2223
{ name = "Matthew Elwell" },
2324
{ name = "Gagan Trivedi" },

src/common/test_tools/plugin.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import prometheus_client
44
import pytest
55
from prometheus_client.metrics import MetricWrapperBase
6+
from pytest_mock import MockerFixture
67

78
from common.test_tools.types import AssertMetricFixture
89

@@ -32,3 +33,22 @@ def _assert_metric(
3233

3334

3435
assert_metric = pytest.fixture(assert_metric_impl)
36+
37+
38+
@pytest.fixture()
39+
def saas_mode(mocker: MockerFixture) -> None:
40+
mocker.patch("common.core.utils.is_saas", return_value=True)
41+
42+
43+
@pytest.fixture()
44+
def enterprise_mode(mocker: MockerFixture) -> None:
45+
mocker.patch("common.core.utils.is_enterprise", return_value=True)
46+
47+
48+
@pytest.fixture(autouse=True)
49+
def flagsmith_markers_marked(request: pytest.FixtureRequest) -> None:
50+
for marker in request.node.iter_markers():
51+
if marker.name == "saas_mode":
52+
request.getfixturevalue("saas_mode")
53+
if marker.name == "enterprise_mode":
54+
request.getfixturevalue("enterprise_mode")

tests/unit/common/test_tools/test_plugin.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,21 @@ def test_assert_metrics__registry_reset_expected(
3737
labels={"test_name": "test_assert_metrics__registry_reset_expected"},
3838
value=1,
3939
)
40+
41+
42+
@pytest.mark.saas_mode
43+
def test_saas_mode_marker__is_saas_returns_expected() -> None:
44+
# Given
45+
from common.core.utils import is_saas
46+
47+
# When & Then
48+
assert is_saas() is True
49+
50+
51+
@pytest.mark.enterprise_mode
52+
def test_enterprise_mode_marker__is_enterprise_returns_expected() -> None:
53+
# Given
54+
from common.core.utils import is_enterprise
55+
56+
# When & Then
57+
assert is_enterprise() is True

0 commit comments

Comments
 (0)