Skip to content

Commit 243ed0b

Browse files
committed
refactor: extract channel type cleanup into reusable decorator
1 parent 08d4543 commit 243ed0b

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

tests/base.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import datetime
12
import functools
3+
import inspect
24
import time
35
from abc import ABC
46

@@ -90,3 +92,44 @@ def wrapper(*args, **kwargs):
9092
return wrapper
9193

9294
return decorator
95+
96+
97+
_BUILTIN_CHANNEL_TYPES = frozenset(
98+
{"messaging", "livestream", "team", "gaming", "commerce"}
99+
)
100+
_STALE_THRESHOLD = datetime.timedelta(minutes=2)
101+
102+
103+
def cleanup_channel_types(func):
104+
"""Decorator that deletes stale test channel types before the test runs.
105+
106+
Frees slots toward the 50-type limit without interfering with parallel
107+
runners (only removes types older than 2 minutes). Expects a ``client``
108+
pytest fixture parameter.
109+
"""
110+
111+
@functools.wraps(func)
112+
def wrapper(*args, **kwargs):
113+
# Resolve 'client' from pytest kwargs or positional args
114+
client = kwargs.get("client")
115+
if client is None:
116+
sig = inspect.signature(func)
117+
params = list(sig.parameters)
118+
idx = params.index("client") if "client" in params else 0
119+
client = args[idx]
120+
121+
now = datetime.datetime.now(datetime.timezone.utc)
122+
resp = client.chat.list_channel_types()
123+
for name, config in resp.data.channel_types.items():
124+
if name in _BUILTIN_CHANNEL_TYPES:
125+
continue
126+
if config and (now - config.created_at) > _STALE_THRESHOLD:
127+
try:
128+
client.chat.delete_channel_type(name=name)
129+
except Exception:
130+
pass
131+
time.sleep(2)
132+
133+
return func(*args, **kwargs)
134+
135+
return wrapper

tests/test_chat_misc.py

Lines changed: 5 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import datetime
21
import time
32
import uuid
43

@@ -18,7 +17,7 @@
1817
QueryFutureChannelBansPayload,
1918
SortParamRequest,
2019
)
21-
from tests.base import retry_on_transient_error
20+
from tests.base import cleanup_channel_types, retry_on_transient_error
2221

2322

2423
def test_get_app_settings(client: Stream):
@@ -391,42 +390,10 @@ def test_query_future_channel_bans(client: Stream, random_users):
391390
pass
392391

393392

394-
BUILTIN_CHANNEL_TYPES = frozenset(
395-
{
396-
"messaging",
397-
"livestream",
398-
"team",
399-
"gaming",
400-
"commerce",
401-
}
402-
)
403-
404-
_STALE_THRESHOLD = datetime.timedelta(minutes=2)
405-
406-
407-
def _cleanup_test_channel_types(client: Stream):
408-
"""Delete stale non-builtin channel types to free slots for test runs.
409-
410-
Only deletes types older than 2 minutes to avoid interfering with
411-
parallel runners that just created theirs.
412-
"""
413-
now = datetime.datetime.now(datetime.timezone.utc)
414-
resp = client.chat.list_channel_types()
415-
for name, config in resp.data.channel_types.items():
416-
if name in BUILTIN_CHANNEL_TYPES:
417-
continue
418-
if config and (now - config.created_at) > _STALE_THRESHOLD:
419-
try:
420-
client.chat.delete_channel_type(name=name)
421-
except Exception:
422-
pass
423-
time.sleep(2)
424-
425-
393+
@cleanup_channel_types
426394
@retry_on_transient_error()
427395
def test_create_channel_type(client: Stream):
428396
"""Create a channel type with custom settings."""
429-
_cleanup_test_channel_types(client)
430397
type_name = f"testtype{uuid.uuid4().hex[:8]}"
431398

432399
try:
@@ -449,10 +416,10 @@ def test_create_channel_type(client: Stream):
449416
pass
450417

451418

419+
@cleanup_channel_types
452420
@retry_on_transient_error()
453421
def test_update_channel_type_mark_messages_pending(client: Stream):
454422
"""Update a channel type with mark_messages_pending=True."""
455-
_cleanup_test_channel_types(client)
456423
type_name = f"testtype{uuid.uuid4().hex[:8]}"
457424

458425
try:
@@ -483,10 +450,10 @@ def test_update_channel_type_mark_messages_pending(client: Stream):
483450
pass
484451

485452

453+
@cleanup_channel_types
486454
@retry_on_transient_error()
487455
def test_update_channel_type_push_notifications(client: Stream):
488456
"""Update a channel type with push_notifications=False."""
489-
_cleanup_test_channel_types(client)
490457
type_name = f"testtype{uuid.uuid4().hex[:8]}"
491458

492459
try:
@@ -517,10 +484,10 @@ def test_update_channel_type_push_notifications(client: Stream):
517484
pass
518485

519486

487+
@cleanup_channel_types
520488
@retry_on_transient_error()
521489
def test_delete_channel_type(client: Stream):
522490
"""Create and delete a channel type with retry."""
523-
_cleanup_test_channel_types(client)
524491
type_name = f"testdeltype{uuid.uuid4().hex[:8]}"
525492

526493
client.chat.create_channel_type(

0 commit comments

Comments
 (0)