Skip to content

Commit

Permalink
ref: speed up flagpole tests (#74949)
Browse files Browse the repository at this point in the history
```console
$ time pytest tests/flagpole/
/Users/asottile/workspace/sentry/.venv/lib/python3.11/site-packages/phabricator/__init__.py:12: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  __version__ = __import__('pkg_resources') \
============================== test session starts ==============================
platform darwin -- Python 3.11.8, pytest-8.0.0, pluggy-1.4.0
django: version: 5.0.7
rootdir: /Users/asottile/workspace/sentry
configfile: pyproject.toml
plugins: fail-slow-0.3.0, rerunfailures-11.0, json-report-1.5.0, metadata-3.1.1, time-machine-2.13.0, xdist-3.0.2, pytest_sentry-0.3.0, anyio-3.7.1, django-4.8.0, cov-4.0.0
collected 45 items                                                              

tests/flagpole/test_conditions.py ..........................              [ 57%]
tests/flagpole/test_evaluation_context.py .........                       [ 77%]
tests/flagpole/test_feature.py ..........                                 [100%]

============================== 45 passed in 4.22s ===============================

real	0m7.365s
user	0m4.001s
sys	0m0.634s
$ git apply patch
$ time pytest tests/flagpole/
/Users/asottile/workspace/sentry/.venv/lib/python3.11/site-packages/phabricator/__init__.py:12: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  __version__ = __import__('pkg_resources') \
============================== test session starts ==============================
platform darwin -- Python 3.11.8, pytest-8.0.0, pluggy-1.4.0
django: version: 5.0.7
rootdir: /Users/asottile/workspace/sentry
configfile: pyproject.toml
plugins: fail-slow-0.3.0, rerunfailures-11.0, json-report-1.5.0, metadata-3.1.1, time-machine-2.13.0, xdist-3.0.2, pytest_sentry-0.3.0, anyio-3.7.1, django-4.8.0, cov-4.0.0
collected 45 items                                                              

tests/flagpole/test_conditions.py ..........................              [ 57%]
tests/flagpole/test_evaluation_context.py .........                       [ 77%]
tests/flagpole/test_feature.py ..........                                 [100%]

============================== 45 passed in 0.55s ===============================

real	0m3.489s
user	0m2.584s
sys	0m0.549s
```

<!-- Describe your PR here. -->
  • Loading branch information
asottile-sentry authored Jul 25, 2024
1 parent 32b627c commit ba2f759
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions tests/flagpole/test_conditions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import Any

import orjson
import pytest
from pydantic import ValidationError

Expand All @@ -15,11 +16,9 @@
NotInCondition,
create_case_insensitive_set_from_list,
)
from sentry.testutils.cases import TestCase
from sentry.utils import json


class TestCreateCaseInsensitiveSetFromList(TestCase):
class TestCreateCaseInsensitiveSetFromList:
def test_empty_set(self):
assert create_case_insensitive_set_from_list([]) == set()

Expand All @@ -36,7 +35,7 @@ def test_returns_lowercase_string_set(self):
def assert_valid_types(condition: type[ConditionBase], expected_types: list[Any]):
for value in expected_types:
condition_dict = dict(property="test", value=value)
json_condition = json.dumps(condition_dict)
json_condition = orjson.dumps(condition_dict)
try:
parsed_condition = condition.parse_raw(json_condition)
except ValidationError as exc:
Expand All @@ -49,7 +48,7 @@ def assert_valid_types(condition: type[ConditionBase], expected_types: list[Any]
def assert_invalid_types(condition: type[ConditionBase], invalid_types: list[Any]):
for value in invalid_types:
json_dict = dict(value=value)
condition_json = json.dumps(json_dict)
condition_json = orjson.dumps(json_dict)
try:
condition.parse_raw(condition_json)
except ValidationError:
Expand All @@ -60,7 +59,7 @@ def assert_invalid_types(condition: type[ConditionBase], invalid_types: list[Any
)


class TestInConditions(TestCase):
class TestInConditions:
def test_invalid_values(self):
with pytest.raises(ValidationError):
InCondition(property="foo", value="bar")
Expand Down Expand Up @@ -144,7 +143,7 @@ def test_missing_context_property(self):
)


class TestContainsConditions(TestCase):
class TestContainsConditions:
def test_does_contain(self):
condition = ContainsCondition(property="foo", value="bar")
assert condition.match(
Expand Down Expand Up @@ -216,7 +215,7 @@ def test_missing_context_property(self):
)


class TestEqualsConditions(TestCase):
class TestEqualsConditions:
def test_is_equal_string(self):
value = "foo"
condition = EqualsCondition(property="foo", value=value)
Expand Down

0 comments on commit ba2f759

Please sign in to comment.