Skip to content

Commit

Permalink
add tests for flask api and the first /analyze endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaykarle committed May 30, 2024
1 parent 56d1452 commit 5c60000
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/analyzer_engine/csv_analyzer_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ def analyze_csv(
csv_list = list(csv.reader(csv_file))
csv_dict = {header: list(map(str, values)) for header, *values in zip(*csv_list)}
analyzer_results = self.analyze_dict(csv_dict, language, keys_to_skip)
return analyzer_results
return list(analyzer_results)
28 changes: 16 additions & 12 deletions app.py → src/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json
import logging
import os
import uuid
from typing import Tuple

from flask import Flask, request, jsonify, Response
Expand Down Expand Up @@ -36,26 +37,29 @@ def analyze() -> Tuple[str, int]:
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400

filepath = f'uploads/{file.filename}'
filepath = f'uploads/{uuid.uuid4()}'
file.save(filepath)
self.logger.info(f"Successfully saved file: {filepath}")

analyzer_result_list = self.engine.analyze_csv(
analyzer_results = self.engine.analyze_csv(
csv_full_path=filepath,
language="en"
)
self.logger.debug(f"Analyzed file with results: {analyzer_results}")
os.remove(filepath)
self.logger.info(f"Successfully removed file: {filepath}")

resp = {}
for result in analyzer_result_list:
resp['key'] = result.key
resp['value'] = result.value
resp['recognizer_results'] = json.dumps(
result.recognizer_results,
default=lambda o: o.to_dict(),
sort_keys=True,
)
analyzer_results_dict = {}
for a in analyzer_results:
recognizer_results = []
for r in a.recognizer_results:
recognizer_results.append([o.to_dict() for o in r])
analyzer_results_dict[a.key] = {
"value": a.value,
"recognizer_results": recognizer_results
}

return jsonify(resp), 200
return jsonify(analyzer_results_dict), 200
except Exception as e:
self.logger.error(
f"A fatal error occurred during execution of "
Expand Down
42 changes: 42 additions & 0 deletions tests/app_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest
import json

from app import Server

@pytest.fixture()
def app():
app = Server().app
app.config.update({
"TESTING": True,
})

yield app


@pytest.fixture()
def client(app):
return app.test_client()


def test_health(client):
response = client.get("/health")
assert response.status_code == 200


def test_analyze_csv_file(client):
expected_response_id = {'value': ['1', '2', '3'], 'recognizer_results': [[], [], []]}

response = client.post("/analyze", data={
"file": open('./tests/analyzer_engine/sample_data.csv', 'rb'),
})

assert response.status_code == 200
data = json.loads(response.get_data(as_text=True))
# No PII in id
assert data['id'] == expected_response_id
# first row has no PII
assert data['comments']['recognizer_results'][0] == []
# second row has PII
assert data['comments']['recognizer_results'][1][0]['entity_type'] == 'US_DRIVER_LICENSE'
assert data['comments']['recognizer_results'][1][0]['start'] == 34
assert data['comments']['recognizer_results'][1][0]['end'] == 42

0 comments on commit 5c60000

Please sign in to comment.