refactoring: introduced sub-conftest files

This commit is contained in:
Matthias Bisping 2022-04-26 13:13:26 +02:00
parent 01daa634ec
commit b7882d4452
9 changed files with 60 additions and 47 deletions

View File

@ -1,6 +1,6 @@
import io
from itertools import repeat
import logging
from itertools import repeat
from operator import attrgetter
from minio import Minio

View File

@ -1,14 +1,12 @@
import json
import logging
import time
from unittest.mock import Mock
import pika
import pytest
import testcontainers.compose
from pyinfra.exceptions import UnknownClient
from pyinfra.locations import TEST_DIR, COMPOSE_PATH
from pyinfra.locations import TEST_DIR
from pyinfra.queue.queue_manager.pika_queue_manager import PikaQueueManager, get_connection_params
from pyinfra.queue.queue_manager.queue_manager import QueueManager
from pyinfra.storage.adapters.azure import AzureStorageAdapter
@ -16,16 +14,23 @@ from pyinfra.storage.adapters.s3 import S3StorageAdapter
from pyinfra.storage.clients.azure import get_azure_client
from pyinfra.storage.clients.s3 import get_s3_client
from pyinfra.storage.storage import Storage
from pyinfra.visitor import StorageStrategy, ForwardingStrategy, QueueVisitor
from test.config import CONFIG
from test.queue.queue_manager_mock import QueueManagerMock
from test.storage.adapter_mock import StorageAdapterMock
from test.storage.client_mock import StorageClientMock
from pyinfra.visitor import StorageStrategy, ForwardingStrategy, QueueVisitor
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
pytest_plugins = [
"test.fixtures.input",
"test.fixtures.pdf",
"test.fixtures.server",
]
@pytest.fixture(scope="session")
def bucket_name():
return "pyinfra-test-bucket"
@ -71,13 +76,14 @@ def storage(client_name, bucket_name, request, docker_compose):
@pytest.fixture(scope="session", autouse=False)
def docker_compose(sleep_seconds=30):
logger.info(f"Starting docker containers with {COMPOSE_PATH}/docker-compose.yml...")
compose = testcontainers.compose.DockerCompose(COMPOSE_PATH, compose_file_name="docker-compose.yml")
compose.start()
logger.info(f"Sleeping for {sleep_seconds} seconds to wait for containers to finish startup... ")
time.sleep(sleep_seconds)
yield compose
compose.stop()
pass
# logger.info(f"Starting docker containers with {COMPOSE_PATH}/docker-compose.yml...")
# compose = testcontainers.compose.DockerCompose(COMPOSE_PATH, compose_file_name="docker-compose.yml")
# compose.start()
# logger.info(f"Sleeping for {sleep_seconds} seconds to wait for containers to finish startup... ")
# time.sleep(sleep_seconds)
# yield compose
# compose.stop()
def get_pika_connection_params():

View File

@ -1,11 +1,8 @@
import json
from operator import itemgetter
import fpdf
import pytest
from test.server import set_up_processing_server
def processor_fn(payload):
data = payload["data"].encode()
@ -14,18 +11,6 @@ def processor_fn(payload):
return response_payload
@pytest.fixture
def server():
server = set_up_processing_server(processor_fn)
server.config.update({"TESTING": True})
return server
@pytest.fixture
def client(server):
return server.test_client()
def test_server_ready_check(client):
response = client.get("/ready")
assert response.status_code == 200
@ -43,23 +28,3 @@ def test_sending_bytes_through_json(client, data):
assert data_type == "<class 'bytes'>"
assert metadata_type == "<class 'dict'>"
@pytest.fixture
def pdf():
pdf = fpdf.FPDF(unit="pt")
pdf.add_page()
return pdf_stream(pdf)
def pdf_stream(pdf: fpdf.fpdf.FPDF):
return pdf.output(dest="S").encode("latin1")
@pytest.fixture
def data(data_type, pdf):
if data_type == "pdf":
return pdf
elif data_type == "bytestring":
return "content".encode("latin1")

0
test/fixtures/__init__.py vendored Normal file
View File

9
test/fixtures/input.py vendored Normal file
View File

@ -0,0 +1,9 @@
import pytest
@pytest.fixture
def data(data_type, pdf):
if data_type == "pdf":
return pdf
elif data_type == "bytestring":
return "content".encode("latin1")

12
test/fixtures/pdf.py vendored Normal file
View File

@ -0,0 +1,12 @@
import fpdf
import pytest
from test.utils.pdf import pdf_stream
@pytest.fixture
def pdf():
pdf = fpdf.FPDF(unit="pt")
pdf.add_page()
return pdf_stream(pdf)

16
test/fixtures/server.py vendored Normal file
View File

@ -0,0 +1,16 @@
import pytest
from test.exploration_tests.data_json_request_test import processor_fn
from test.server import set_up_processing_server
@pytest.fixture
def server():
server = set_up_processing_server(processor_fn)
server.config.update({"TESTING": True})
return server
@pytest.fixture
def client(server):
return server.test_client()

0
test/utils/__init__.py Normal file
View File

5
test/utils/pdf.py Normal file
View File

@ -0,0 +1,5 @@
import fpdf
def pdf_stream(pdf: fpdf.fpdf.FPDF):
return pdf.output(dest="S").encode("latin1")