Skip to content

Commit c1bdfae

Browse files
fix: removed unused variable
fix: ordered tests andd reset logger after each test
1 parent 8f902a0 commit c1bdfae

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

supertokens_python/supertokens.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ def __init__(
170170
mode,
171171
)
172172
self.supertokens_config = supertokens_config
173-
self.debug = debug
174173
if debug is True:
175174
enable_debug_logging()
176175
self._telemetry_status: str = "NONE"

tests/test_logger.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import importlib
22
import json
3+
import logging
34
import os
45
import sys
56
from datetime import datetime as real_datetime
@@ -10,15 +11,26 @@
1011

1112
from supertokens_python import InputAppInfo, SupertokensConfig, init
1213
from supertokens_python.constants import VERSION
13-
from supertokens_python.logger import log_debug_message, streamFormatter
14+
from supertokens_python.logger import (
15+
log_debug_message,
16+
streamFormatter,
17+
NAMESPACE,
18+
enable_debug_logging,
19+
)
1420
from supertokens_python.recipe import session
1521

1622
from tests.utils import clean_st, reset, setup_st, start_st
1723

1824

1925
class LoggerTests(TestCase):
20-
def setup_method(self, _):
21-
self._caplog.clear()
26+
@pytest.fixture(autouse=True)
27+
def inject_fixtures(self, caplog: pytest.LogCaptureFixture):
28+
# caplog is the pytest fixture to capture all logs
29+
self._caplog = caplog # pylint: disable=attribute-defined-outside-init
30+
31+
def setup_method(self, _): # pylint: disable=no-self-use
32+
# Setting the log level to a higher level so debug logs are not printed
33+
logging.getLogger(NAMESPACE).setLevel(logging.ERROR)
2234
reset()
2335
clean_st()
2436
setup_st()
@@ -28,13 +40,9 @@ def teardown_method(self, _):
2840
reset()
2941
clean_st()
3042

31-
@pytest.fixture(autouse=True)
32-
def inject_fixtures(self, caplog: pytest.LogCaptureFixture):
33-
# caplog is the pytest fixture to capture all logs
34-
self._caplog = caplog # pylint: disable=attribute-defined-outside-init
35-
3643
@patch("supertokens_python.logger.datetime", wraps=real_datetime)
37-
def test_json_msg_format(self, datetime_mock: MagicMock):
44+
def test_1_json_msg_format(self, datetime_mock: MagicMock):
45+
enable_debug_logging()
3846
datetime_mock.utcnow.return_value = real_datetime(2000, 1, 1) # type: ignore
3947

4048
with self.assertLogs(level="DEBUG") as captured:
@@ -47,17 +55,17 @@ def test_json_msg_format(self, datetime_mock: MagicMock):
4755
"t": "2000-01-01T00:00Z",
4856
"sdkVer": VERSION,
4957
"message": "API replied with status 200",
50-
"file": "../tests/test_logger.py:40",
58+
"file": "../tests/test_logger.py:49",
5159
}
5260

5361
@staticmethod
54-
def test_stream_formatter_format():
62+
def test_2_stream_formatter_format():
5563
assert (
5664
streamFormatter._fmt # pylint: disable=protected-access
5765
== "{name} {message}\n"
5866
)
5967

60-
def test_logger_config_with_debug_true(self):
68+
def test_3_logger_config_with_debug_false(self):
6169
start_st()
6270
init(
6371
supertokens_config=SupertokensConfig("http://localhost:3567"),
@@ -69,15 +77,15 @@ def test_logger_config_with_debug_true(self):
6977
),
7078
framework="fastapi",
7179
recipe_list=[session.init(anti_csrf="VIA_CUSTOM_HEADER")],
72-
debug=True,
80+
debug=False,
7381
)
7482

7583
logMsg = "log test - valid log"
7684
log_debug_message(logMsg)
7785

78-
assert logMsg in self._caplog.text
86+
assert logMsg not in self._caplog.text
7987

80-
def test_logger_config_with_debug_false(self):
88+
def test_4_logger_config_with_debug_true(self):
8189
start_st()
8290
init(
8391
supertokens_config=SupertokensConfig("http://localhost:3567"),
@@ -89,15 +97,15 @@ def test_logger_config_with_debug_false(self):
8997
),
9098
framework="fastapi",
9199
recipe_list=[session.init(anti_csrf="VIA_CUSTOM_HEADER")],
92-
debug=False,
100+
debug=True,
93101
)
94102

95103
logMsg = "log test - valid log"
96104
log_debug_message(logMsg)
97105

98-
assert logMsg not in self._caplog.text
106+
assert logMsg in self._caplog.text
99107

100-
def test_logger_config_with_debug_not_set(self):
108+
def test_5_logger_config_with_debug_not_set(self):
101109
start_st()
102110
init(
103111
supertokens_config=SupertokensConfig("http://localhost:3567"),
@@ -116,7 +124,7 @@ def test_logger_config_with_debug_not_set(self):
116124

117125
assert logMsg not in self._caplog.text
118126

119-
def test_logger_config_with_env_var(self):
127+
def test_6_logger_config_with_env_var(self):
120128
os.environ["SUPERTOKENS_DEBUG"] = "1"
121129

122130
# environment variable was already set when the logger module was imported

0 commit comments

Comments
 (0)