Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

chore(batch-exports): tier utility tests (#103320)

Paul D'Ambrab91a44d551b8
diff --git a/products/batch_exports/backend/tests/temporal/test_batch_export_utils.py b/products/batch_exports/backend/tests/temporal/test_batch_export_utils.py
index 7294a26a7a34..432483895ab8 100644
--- a/products/batch_exports/backend/tests/temporal/test_batch_export_utils.py
+++ b/products/batch_exports/backend/tests/temporal/test_batch_export_utils.py
@@ -3,17 +3,12 @@ import datetime as dt
import pytest
-import pyarrow as pa
import pytest_asyncio
from posthog.temporal.tests.utils.models import acreate_batch_export, adelete_batch_export
from products.batch_exports.backend.models.batch_export import BatchExportRun
-from products.batch_exports.backend.temporal.utils import (
- JsonType,
- make_retryable_with_exponential_backoff,
- set_status_to_running_task,
-)
+from products.batch_exports.backend.temporal.utils import set_status_to_running_task
pytestmark = [pytest.mark.asyncio, pytest.mark.django_db]
@@ -74,115 +69,3 @@ async def test_batch_export_run_is_set_to_running(ateam, s3_batch_export):
await run.arefresh_from_db()
assert run.status == BatchExportRun.Status.RUNNING
-
-
-async def test_make_retryable_with_exponential_backoff_called_max_attempts():
- """Test function wrapped is called all `max_attempts` times."""
- counter = 0
-
- async def raise_value_error():
- nonlocal counter
- counter += 1
-
- raise ValueError("I failed")
-
- with pytest.raises(ValueError):
- await make_retryable_with_exponential_backoff(raise_value_error, max_retry_delay=1, max_delay_jitter=0)()
-
- assert counter == 5
-
-
-async def test_make_retryable_with_exponential_backoff_called_max_attempts_if_timesout():
- """Test function wrapped is called all `max_attempts` times on a timeout."""
- counter = 0
-
- async def raise_value_error():
- nonlocal counter
- counter += 1
- await asyncio.sleep(10)
-
- with pytest.raises(TimeoutError):
- await make_retryable_with_exponential_backoff(
- raise_value_error, max_retry_delay=1, timeout=1, max_delay_jitter=0
- )()
-
- assert counter == 5
-
-
-async def test_make_retryable_with_exponential_backoff_called_max_attempts_if_func_returns_retryable():
- """Test function wrapped is called all `max_attempts` times if `is_exception_retryable` returns `True`."""
- counter = 0
-
- def is_exception_retryable(err):
- return True
-
- async def raise_value_error():
- nonlocal counter
- counter += 1
-
- raise ValueError("I failed")
-
- with pytest.raises(ValueError):
- await make_retryable_with_exponential_backoff(
- raise_value_error, is_exception_retryable=is_exception_retryable, max_retry_delay=1, max_delay_jitter=0
- )()
-
- assert counter == 5
-
-
-async def test_make_retryable_with_exponential_backoff_raises_if_func_returns_not_retryable():
- """Test function wrapped raises immediately if `is_exception_retryable` returns `False`."""
- counter = 0
-
- def is_exception_retryable(err):
- return False
-
- async def raise_value_error():
- nonlocal counter
- counter += 1
-
- raise ValueError("I failed")
-
- with pytest.raises(ValueError):
- await make_retryable_with_exponential_backoff(
- raise_value_error, is_exception_retryable=is_exception_retryable
- )()
-
- assert counter == 1
-
-
-async def test_make_retryable_with_exponential_backoff_raises_if_not_retryable():
- """Test function wrapped raises immediately if exception not in `retryable_exceptions`."""
- counter = 0
-
- async def raise_value_error():
- nonlocal counter
- counter += 1
-
- raise ValueError("I failed")
-
- with pytest.raises(ValueError):
- await make_retryable_with_exponential_backoff(raise_value_error, retryable_exceptions=(TypeError,))()
-
- assert counter == 1
-
-
- "input,expected",
- [
- ([b'{"asdf": "\udee5\ud83e\udee5\\ud83e"}'], [{"asdf": "????"}]),
- ([b'{"asdf": "\\"Hello\\" \\udfa2"}'], [{"asdf": '"Hello" ?'}]),
- ([b'{"asdf": "\n"}'], [{"asdf": "\n"}]),
- ([b'{"asdf": "\\n"}'], [{"asdf": "\n"}]),
- (
- [b'{"finally": "a", "normal": "json", "thing": 1, "bool": false}'],
- [{"finally": "a", "normal": "json", "thing": 1, "bool": False}],
- ),
- ],
-)
-def test_json_type_as_py(input, expected):
- array = pa.array(input)
- casted_array = array.cast(JsonType())
- result = casted_array.to_pylist()
-
- assert result == expected
diff --git a/products/batch_exports/backend/tests/test_utils.py b/products/batch_exports/backend/tests/test_utils.py
new file mode 100644
index 000000000000..eb466be3993a
--- /dev/null
+++ b/products/batch_exports/backend/tests/test_utils.py
@@ -0,0 +1,119 @@
+import asyncio
+
+import pytest
+
+import pyarrow as pa
+
+from products.batch_exports.backend.temporal.utils import JsonType, make_retryable_with_exponential_backoff
+
+
+async def test_make_retryable_with_exponential_backoff_called_max_attempts():
+ """Test function wrapped is called all `max_attempts` times."""
+ counter = 0
+
+ async def raise_value_error():
+ nonlocal counter
+ counter += 1
+
+ raise ValueError("I failed")
+
+ with pytest.raises(ValueError):
+ await make_retryable_with_exponential_backoff(raise_value_error, max_retry_delay=0, max_delay_jitter=0)()
+
+ assert counter == 5
+
+
+async def test_make_retryable_with_exponential_backoff_called_max_attempts_if_timesout():
+ """Test function wrapped is called all `max_attempts` times on a timeout."""
+ counter = 0
+
+ async def raise_value_error():
+ nonlocal counter
+ counter += 1
+ await asyncio.sleep(10)
+
+ with pytest.raises(TimeoutError):
+ await make_retryable_with_exponential_backoff(
+ raise_value_error, max_retry_delay=0, timeout=0.01, max_delay_jitter=0
+ )()
+
+ assert counter == 5
+
+
+async def test_make_retryable_with_exponential_backoff_called_max_attempts_if_func_returns_retryable():
+ """Test function wrapped is called all `max_attempts` times if `is_exception_retryable` returns `True`."""
+ counter = 0
+
+ def is_exception_retryable(err):
+ return True
+
+ async def raise_value_error():
+ nonlocal counter
+ counter += 1
+
+ raise ValueError("I failed")
+
+ with pytest.raises(ValueError):
+ await make_retryable_with_exponential_backoff(
+ raise_value_error, is_exception_retryable=is_exception_retryable, max_retry_delay=0, max_delay_jitter=0
+ )()
+
+ assert counter == 5
+
+
+async def test_make_retryable_with_exponential_backoff_raises_if_func_returns_not_retryable():
+ """Test function wrapped raises immediately if `is_exception_retryable` returns `False`."""
+ counter = 0
+
+ def is_exception_retryable(err):
+ return False
+
+ async def raise_value_error():
+ nonlocal counter
+ counter += 1
+
+ raise ValueError("I failed")
+
+ with pytest.raises(ValueError):
+ await make_retryable_with_exponential_backoff(
+ raise_value_error, is_exception_retryable=is_exception_retryable
+ )()
+
+ assert counter == 1
+
+
+async def test_make_retryable_with_exponential_backoff_raises_if_not_retryable():
+ """Test function wrapped raises immediately if exception not in `retryable_exceptions`."""
+ counter = 0
+
+ async def raise_value_error():
+ nonlocal counter
+ counter += 1
+
+ raise ValueError("I failed")
+
+ with pytest.raises(ValueError):
+ await make_retryable_with_exponential_backoff(raise_value_error, retryable_exceptions=(TypeError,))()
+
+ assert counter == 1
+
+
+ "input,expected",
+ [
+ ([b'{"asdf": "\udee5\ud83e\udee5\\ud83e"}'], [{"asdf": "????"}]),
+ ([b'{"asdf": "\\"Hello\\" \\udfa2"}'], [{"asdf": '"Hello" ?'}]),
+ ([b'{"asdf": "\n"}'], [{"asdf": "\n"}]),
+ ([b'{"asdf": "\\n"}'], [{"asdf": "\n"}]),
+ (
+ [b'{"finally": "a", "normal": "json", "thing": 1, "bool": false}'],
+ [{"finally": "a", "normal": "json", "thing": 1, "bool": False}],
+ ),
+ ],
+)
+def test_json_type_as_py(input, expected):
+ array = pa.array(input)
+ casted_array = array.cast(JsonType())
+ result = casted_array.to_pylist()
+
+ assert result == expected