Skip to content

Commit

Permalink
move settings import tests to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
pierec committed Feb 19, 2024
1 parent 23a93bd commit 7735a27
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 68 deletions.
72 changes: 72 additions & 0 deletions tests/test_settings_import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from __future__ import annotations

import contextlib
import secrets
import sys
import tempfile
import textwrap
import unittest
from pathlib import Path

from saq.worker import import_settings


class TestSettingsImport(unittest.TestCase):
def setUp(self) -> None:
self.cm = cm = contextlib.ExitStack()

tempdir = Path(cm.enter_context(tempfile.TemporaryDirectory()))
root_module_name = "foo" + secrets.token_urlsafe(2)
file_tree = [
tempdir / root_module_name / "__init__.py",
tempdir / root_module_name / "bar" / "__init__.py",
tempdir / root_module_name / "bar" / "settings.py",
]
for path in file_tree:
path.parent.mkdir(exist_ok=True, parents=True)
path.touch()

file_tree[-1].write_text(
textwrap.dedent(
"""
static = {
"functions": ["pretend_its_a_fn"],
"concurrency": 100
}
def factory():
return {
"functions": ["pretend_its_some_other_fn"],
"concurrency": static["concurrency"] + 100
}
"""
).strip()
)
sys.path.append(str(tempdir))

self.module_path = f"{root_module_name}.bar.settings"

def tearDown(self) -> None:
self.cm.close()

def test_imports_settings_from_module_path(self) -> None:
settings = import_settings(self.module_path + ".static")

self.assertDictEqual(
settings,
{
"functions": ["pretend_its_a_fn"],
"concurrency": 100,
},
)

def test_calls_settings_factory(self) -> None:
settings = import_settings(self.module_path + ".factory")

self.assertDictEqual(
settings,
{
"functions": ["pretend_its_some_other_fn"],
"concurrency": 200,
},
)
69 changes: 1 addition & 68 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

import asyncio
import contextvars
import contextlib
import logging
import secrets
import sys
import tempfile
import textwrap
import typing as t
import unittest
from unittest import mock
from pathlib import Path

from saq.job import CronJob, Job, Status
from saq.utils import uuid1
from saq.worker import Worker, import_settings
from saq.worker import Worker
from tests.helpers import cleanup_queue, create_queue

if t.TYPE_CHECKING:
Expand Down Expand Up @@ -325,64 +319,3 @@ async def before_enqueue(job: Job) -> None:
correlation_ids = await self.queue.apply("recurse", n=2)
self.assertEqual(len(correlation_ids), 3)
self.assertTrue(all(cid == correlation_ids[0] for cid in correlation_ids[1:]))


class TestSettingsImport(unittest.TestCase):
def setUp(self) -> None:
self.cm = cm = contextlib.ExitStack()

tempdir = Path(cm.enter_context(tempfile.TemporaryDirectory()))
root_module_name = "foo" + secrets.token_urlsafe(2)
file_tree = [
tempdir / root_module_name / "__init__.py",
tempdir / root_module_name / "bar" / "__init__.py",
tempdir / root_module_name / "bar" / "settings.py",
]
for path in file_tree:
path.parent.mkdir(exist_ok=True, parents=True)
path.touch()

file_tree[-1].write_text(
textwrap.dedent(
"""
static = {
"functions": ["pretend_its_a_fn"],
"concurrency": 100
}
def factory():
return {
"functions": ["pretend_its_some_other_fn"],
"concurrency": static["concurrency"] + 100
}
"""
).strip()
)
sys.path.append(str(tempdir))

self.module_path = f"{root_module_name}.bar.settings"

def tearDown(self) -> None:
self.cm.close()

def test_imports_settings_from_module_path(self) -> None:
settings = import_settings(self.module_path + ".static")

self.assertDictEqual(
settings,
{
"functions": ["pretend_its_a_fn"],
"concurrency": 100,
},
)

def test_calls_settings_factory(self) -> None:
settings = import_settings(self.module_path + ".factory")

self.assertDictEqual(
settings,
{
"functions": ["pretend_its_some_other_fn"],
"concurrency": 200,
},
)

0 comments on commit 7735a27

Please sign in to comment.