added tests for processor and analyzer
This commit is contained in:
parent
b08f274274
commit
f6ce3be25e
@ -1,24 +0,0 @@
|
||||
import json
|
||||
from multiprocessing import Process
|
||||
|
||||
from flask import Flask, request, jsonify
|
||||
from pyinfra.test.config import CONFIG
|
||||
|
||||
|
||||
def start_mock_analyzer():
|
||||
app = Flask(__name__)
|
||||
|
||||
@app.route("/", methods=["POST"])
|
||||
def predict_request():
|
||||
|
||||
data = request.data
|
||||
section_text = json.loads(data)
|
||||
|
||||
return jsonify(section_text)
|
||||
|
||||
@app.get("/shut_down")
|
||||
def shut_down():
|
||||
request.environ.get('werkzeug.server.shutdown')
|
||||
|
||||
server = Process(target=app.run(host=CONFIG.webserver.host, port=CONFIG.webserver.port, debug=True))
|
||||
server.start()
|
||||
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,36 @@
|
||||
import json
|
||||
from unittest.mock import Mock
|
||||
|
||||
import pytest
|
||||
from pyinfra.locations import TEST_DIR
|
||||
|
||||
|
||||
@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
|
||||
|
||||
@ -1,20 +1,31 @@
|
||||
import json
|
||||
from time import sleep
|
||||
from unittest.mock import patch, Mock, MagicMock
|
||||
|
||||
from pyinfra.core import make_analyzer
|
||||
from pyinfra.core import make_analyzer, make_payload_processor
|
||||
from pyinfra.test.config import CONFIG
|
||||
from pyinfra.test.mock_analyzer import start_mock_analyzer
|
||||
import requests
|
||||
|
||||
|
||||
def test_analyse():
|
||||
data = {"test": "Bliblablub"}
|
||||
start_mock_analyzer()
|
||||
sleep(5)
|
||||
analyzer = make_analyzer(CONFIG.mock_analysis_endpoint)
|
||||
assert analyzer(data) == data
|
||||
@patch("requests.post")
|
||||
def test_analyse_returns_analysis(mock_post, storage_data, mock_response):
|
||||
mock_post.return_value = mock_response
|
||||
|
||||
requests.get(CONFIG.mock_analysis_endpoint + "/shut_down")
|
||||
analyze = make_analyzer(CONFIG.mock_analysis_endpoint)
|
||||
response = analyze(storage_data)
|
||||
|
||||
assert response == storage_data
|
||||
|
||||
|
||||
def test_make_payload_processor_yields_dossier_id_file_id_predictions():
|
||||
pass
|
||||
@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 @@
|
||||
|
||||
@ -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