Skip to content

Commit b6567ca

Browse files
authored
Limit required deps and tox tests (#20)
* fixed broken tests, got 1 set of tox tests working * can test mysql * kafka tests * bunch of test permutations * formatting * removed unused code, added documentation * formatting and removing commented code --------- Co-authored-by: Salaah Amin <[email protected]>
1 parent 51ae53a commit b6567ca

17 files changed

+288
-58
lines changed

action_triggers/message_broker/exceptions.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
import json
22

33

4-
class MissingDependenciesError(ImportError):
5-
"""Exception raised when required dependencies are missing.
6-
7-
:param broker_name: The name of the broker that requires the extra.
8-
:param extra_name: The name of the extra installable that is missing.
9-
:param package_name: The name of the package that is missing.
10-
"""
11-
12-
def __init__(self, broker_name: str, extra_name: str, package_name: str):
13-
super().__init__(
14-
f"The `{extra_name}` extra must be installed to use the "
15-
f"{broker_name} broker. Please install the `{extra_name}` extra "
16-
f"by running `pip install action-triggers[{extra_name}]`. "
17-
f"Alternatively, you can install the required packages by running "
18-
f"`pip install {package_name}`."
19-
)
20-
21-
224
class ConnectionValidationError(RuntimeError):
235
"""Exception raised when connection parameters are invalid."""
246

action_triggers/message_broker/kafka.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from action_triggers.message_broker.base import BrokerBase, ConnectionBase
44
from action_triggers.message_broker.enums import BrokerType
5-
from action_triggers.message_broker.exceptions import MissingDependenciesError
5+
from action_triggers.utils.module_import import MissingImportWrapper
66

77
try:
88
from confluent_kafka import Producer # type: ignore[import-untyped]
99
except ImportError: # pragma: no cover
10-
raise MissingDependenciesError("Kafka", "kafka", "confluent-kafka")
10+
kafka = MissingImportWrapper("kafka")
1111

1212

1313
class KafkaConnection(ConnectionBase):

action_triggers/message_broker/rabbitmq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
from action_triggers.message_broker.base import BrokerBase, ConnectionBase
44
from action_triggers.message_broker.enums import BrokerType
5-
from action_triggers.message_broker.exceptions import MissingDependenciesError
5+
from action_triggers.utils.module_import import MissingImportWrapper
66

77
try:
88
import pika # type: ignore[import-untyped]
99
except ImportError: # pragma: no cover
10-
raise MissingDependenciesError("RabbitMQ", "rabbitmq", "pika")
10+
pika = MissingImportWrapper("pika")
1111

1212

1313
class RabbitMQConnection(ConnectionBase):

action_triggers/utils/__init__.py

Whitespace-only changes.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""Utility functions for managing imports."""
2+
3+
import importlib
4+
5+
6+
class MissingImportWrapper:
7+
"""A wrapper to indicate that an import is missing the next time it is
8+
accessed. Judging from the code, that is not at all the case. However, this
9+
is primary used whenever a module is not found, but we do not want to raise
10+
an exception immediately. Instead, we want to raise an exception when the
11+
module is accessed.
12+
"""
13+
14+
def __init__(
15+
self,
16+
import_path: str,
17+
):
18+
self.import_path = import_path
19+
20+
def __getattr__(self, item):
21+
importlib.import_module(self.import_path)[item]

docker-compose.tox.yml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# A dedicated docker-comopose file for bringing up all the services for tox
2+
# to test against.
3+
4+
version: '3.8'
5+
6+
services:
7+
postgres:
8+
image: postgres:14.13-alpine3.20
9+
environment:
10+
POSTGRES_DB: action_triggers
11+
POSTGRES_USER: postgres
12+
POSTGRES_PASSWORD: postgres
13+
ports:
14+
- "5440:5432"
15+
healthcheck:
16+
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
17+
interval: 10s
18+
timeout: 5s
19+
retries: 5
20+
21+
mysql:
22+
image: mysql:oraclelinux9
23+
environment:
24+
MYSQL_ROOT_PASSWORD: root
25+
MYSQL_DATABASE: action_triggers
26+
MYSQL_PASSWORD: root
27+
ports:
28+
- "3307:3306"
29+
healthcheck:
30+
test: [ "CMD-SHELL", "mysqladmin ping -h localhost" ]
31+
interval: 10s
32+
timeout: 5s
33+
retries: 5
34+
35+
rabbitmq:
36+
image: rabbitmq:4.0.0-beta.5-management-alpine
37+
ports:
38+
- "5680:5672"
39+
- "15680:15672"
40+
healthcheck:
41+
test: [ "CMD", "rabbitmq-diagnostics", "ping" ]
42+
interval: 10s
43+
timeout: 5s
44+
retries: 5
45+
46+
zookeeper:
47+
image: confluentinc/cp-zookeeper:7.4.4
48+
environment:
49+
ZOOKEEPER_CLIENT_PORT: 2181
50+
ports:
51+
- "2181:2181"
52+
healthcheck:
53+
test: [ "CMD", "bash", "-c", "echo > /dev/tcp/localhost/2181" ]
54+
interval: 10s
55+
timeout: 5s
56+
retries: 5
57+
58+
kafka:
59+
image: confluentinc/cp-kafka:7.4.4
60+
environment:
61+
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
62+
KAFKA_BROKER_ID: 1
63+
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
64+
ports:
65+
- "9092:9092"
66+
- "29092:29092"
67+
depends_on:
68+
zookeeper:
69+
condition: service_healthy
70+
healthcheck:
71+
test: [ "CMD", "bash", "-c", "echo > /dev/tcp/localhost/9092" ]
72+
interval: 10s
73+
timeout: 5s
74+
retries: 5

docs/source/installation.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ Django Action Triggers can be installed directly from PyPI using pip:
2525
2626
pip install django-action-triggers
2727
28-
## Optional: Install with Extras
28+
Optional: Install with Extras
29+
-----------------------------
2930

3031
If you plan to use specific features (e.g., integration with messaging
3132
brokers), you can install the required dependencies at the same time:

docs/source/modules.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,8 @@ Module Documentation
9191
:members:
9292
:undoc-members:
9393
:show-inheritance:
94+
95+
.. automodule:: action_triggers.utils.module_import
96+
:members:
97+
:undoc-members:
98+
:show-inheritance:

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.1.0"
44
description = ""
55
authors = ["Salaah Amin <[email protected]>"]
66
readme = "README.md"
7+
packages = [{ include = "action_triggers" }]
78

89
[tool.ruff]
910
line-length = 79
@@ -23,6 +24,7 @@ omit = ["action_triggers/migrations/*"]
2324
python = ">=3.8,<3.13"
2425
django = ">=3.2"
2526
djangorestframework = "^3.15.2"
27+
requests = "^2.32.3"
2628

2729
[tool.mypy]
2830
plugins = ["mypy_django_plugin.main"]

0 commit comments

Comments
 (0)