Pull request #17: RED-3463: Add tests
Merge in RR/pyinfra from add_tests to master
Squashed commit of the following:
commit aa99d15c0487304a06bb36d990765fdca28bb651
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Mar 4 13:05:27 2022 +0100
RED-3463: Add some important tests
commit 666bb067ae19ee221ee9a4437f5a2c0bc749b3a6
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Mar 4 09:57:03 2022 +0100
blackkky
commit f6ce3be25e32f0c612b76a88e83b473a4d9ce605
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Fri Mar 4 09:56:25 2022 +0100
added tests for processor and analyzer
commit b08f2742742c52400d9b8c984025c66f25c2bede
Author: cdietrich <clarissa.dietrich@iqser.com>
Date: Fri Mar 4 08:19:55 2022 +0100
add tests
commit 7a37b73db0567d6bbb5d854c73c7bdf57fa34043
Merge: a3056c0 cc0c1bb
Author: cdietrich <clarissa.dietrich@iqser.com>
Date: Thu Mar 3 11:58:03 2022 +0100
Merge branch 'master' of ssh://git.iqser.com:2222/rr/pyinfra
commit a3056c0df73091ee766491331f07e3a0377e7887
Author: cdietrich <clarissa.dietrich@iqser.com>
Date: Thu Mar 3 10:24:54 2022 +0100
refactor save response as file
This commit is contained in:
parent
cc0c1bb999
commit
be0a4fe58f
1
.gitignore
vendored
1
.gitignore
vendored
@ -7,3 +7,4 @@ pyinfra.egg-info
|
||||
bamboo-specs/target
|
||||
.pytest_cache
|
||||
/.coverage
|
||||
.idea
|
||||
@ -13,3 +13,11 @@ storage:
|
||||
connection_string: "DefaultEndpointsProtocol=https;AccountName=iqserdevelopment;AccountKey=4imAbV9PYXaztSOMpIyAClg88bAZCXuXMGJG0GA1eIBpdh2PlnFGoRBnKqLy2YZUSTmZ3wJfC7tzfHtuC6FEhQ==;EndpointSuffix=core.windows.net"
|
||||
|
||||
bucket: "pyinfra-test-bucket"
|
||||
|
||||
webserver:
|
||||
host: $SERVER_HOST|"127.0.0.1" # webserver address
|
||||
port: $SERVER_PORT|5000 # webserver port
|
||||
mode: $SERVER_MODE|production # webserver mode: {development, production}
|
||||
|
||||
|
||||
mock_analysis_endpoint: "http://127.0.0.1:5000"
|
||||
522843
pyinfra/test/test_data/test_data.TEXT.json
Normal file
522843
pyinfra/test/test_data/test_data.TEXT.json
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,73 @@
|
||||
import json
|
||||
import logging
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
|
||||
from pyinfra.exceptions import UnknownClient
|
||||
from pyinfra.locations import TEST_DIR
|
||||
from pyinfra.storage.adapters.azure import AzureStorageAdapter
|
||||
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.test.config import CONFIG
|
||||
from pyinfra.test.storage.adapter_mock import StorageAdapterMock
|
||||
from pyinfra.test.storage.client_mock import StorageClientMock
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def bucket_name():
|
||||
return "pyinfra-test-bucket"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def storage_data():
|
||||
with open(f"{TEST_DIR}/test_data/test_data.TEXT.json", "r") as f:
|
||||
data = json.load(f)
|
||||
return data
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_response(storage_data):
|
||||
response = Mock(status_code=200)
|
||||
response.json.return_value = storage_data
|
||||
return response
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_payload():
|
||||
return json.dumps({"dossierId": "test", "fileId": "test"})
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_make_load_data():
|
||||
def load_data(payload):
|
||||
return storage_data
|
||||
|
||||
return load_data
|
||||
|
||||
|
||||
@pytest.fixture(params=["minio", "aws"])
|
||||
def storage(client_name, bucket_name, request):
|
||||
logger.debug("Setup for storage")
|
||||
storage = Storage(get_adapter(client_name, request.param))
|
||||
storage.make_bucket(bucket_name)
|
||||
storage.clear_bucket(bucket_name)
|
||||
yield storage
|
||||
logger.debug("Teardown for storage")
|
||||
storage.clear_bucket(bucket_name)
|
||||
|
||||
|
||||
def get_adapter(client_name, s3_backend):
|
||||
if client_name == "mock":
|
||||
return StorageAdapterMock(StorageClientMock())
|
||||
if client_name == "azure":
|
||||
return AzureStorageAdapter(get_azure_client(CONFIG.storage.azure.connection_string))
|
||||
if client_name == "s3":
|
||||
return S3StorageAdapter(get_s3_client(CONFIG.storage[s3_backend]))
|
||||
else:
|
||||
raise UnknownClient(client_name)
|
||||
|
||||
32
pyinfra/test/unit_tests/core_test.py
Normal file
32
pyinfra/test/unit_tests/core_test.py
Normal file
@ -0,0 +1,32 @@
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
|
||||
from pyinfra.core import make_analyzer, make_payload_processor
|
||||
from pyinfra.test.config import CONFIG
|
||||
|
||||
|
||||
@patch("requests.post")
|
||||
def test_analyse_returns_analysis(mock_post, storage_data, mock_response):
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
analyze = make_analyzer(CONFIG.mock_analysis_endpoint)
|
||||
response = analyze(storage_data)
|
||||
|
||||
assert response == storage_data
|
||||
|
||||
|
||||
@patch("requests.post")
|
||||
def test_process_returns_dossier_id_file_id_predictions(
|
||||
mock_post, mock_make_load_data, storage_data, mock_response, mock_payload
|
||||
):
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
analyze = make_analyzer(CONFIG.mock_analysis_endpoint)
|
||||
mock_load_data = mock_make_load_data
|
||||
|
||||
process = make_payload_processor(mock_load_data, analyze)
|
||||
dossier_id, file_id, predictions = process(mock_payload)
|
||||
|
||||
assert dossier_id == json.loads(mock_payload)["dossierId"]
|
||||
assert file_id == json.loads(mock_payload)["fileId"]
|
||||
assert predictions == storage_data
|
||||
@ -1 +0,0 @@
|
||||
|
||||
@ -2,16 +2,8 @@ import logging
|
||||
|
||||
import pytest
|
||||
|
||||
from pyinfra.exceptions import UnknownClient
|
||||
from pyinfra.storage.adapters.azure import AzureStorageAdapter
|
||||
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.storage.storages import get_azure_storage, get_s3_storage
|
||||
from pyinfra.test.config import CONFIG
|
||||
from pyinfra.test.storage.adapter_mock import StorageAdapterMock
|
||||
from pyinfra.test.storage.client_mock import StorageClientMock
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
@ -51,28 +43,6 @@ class TestStorage:
|
||||
assert {(bucket_name, "file1"), (bucket_name, "file2")} == {*full_names_received}
|
||||
|
||||
|
||||
def get_adapter(client_name, s3_backend):
|
||||
if client_name == "mock":
|
||||
return StorageAdapterMock(StorageClientMock())
|
||||
if client_name == "azure":
|
||||
return AzureStorageAdapter(get_azure_client(CONFIG.storage.azure.connection_string))
|
||||
if client_name == "s3":
|
||||
return S3StorageAdapter(get_s3_client(CONFIG.storage[s3_backend]))
|
||||
else:
|
||||
raise UnknownClient(client_name)
|
||||
|
||||
|
||||
@pytest.fixture(params=["minio", "aws"])
|
||||
def storage(client_name, bucket_name, request):
|
||||
logger.debug("Setup for storage")
|
||||
storage = Storage(get_adapter(client_name, request.param))
|
||||
storage.make_bucket(bucket_name)
|
||||
storage.clear_bucket(bucket_name)
|
||||
yield storage
|
||||
logger.debug("Teardown for storage")
|
||||
storage.clear_bucket(bucket_name)
|
||||
|
||||
|
||||
def test_get_azure_storage_yields_storage():
|
||||
assert isinstance(get_azure_storage(), Storage)
|
||||
|
||||
|
||||
@ -11,3 +11,6 @@ requests==2.27.1
|
||||
docker-compose==1.29.2
|
||||
tqdm==4.62.3
|
||||
dependency-check
|
||||
|
||||
pyinfra~=0.0.1
|
||||
pytest~=7.0.1
|
||||
Loading…
x
Reference in New Issue
Block a user