Commit f2985901 authored by Marie FU's avatar Marie FU

add method POST terms validation along with tests and utility functions

parent 3fab29e7
import petl
from gargantools.utils.utils import check_unacceptedCharactersQuote, check_unacceptedCharactersTab
def check_statusCell(cell):
return (cell in ["MapTerm", "CandidateTerm", "StopTerm"]) == True
def check_fileContent(fileContent):
header = ("status", "label")
constraints = [
dict(
name = "acceptedVariable", field = "status", assertion = lambda x : check_statusCell(x)
),
dict(
name = "str_formatQuote", field = "label", assertion = lambda x : check_unacceptedCharactersQuote(x)
),
dict(
name = "str_formatTab", field = "label", assertion = lambda x : check_unacceptedCharactersTab(x)
)
]
dataTable = fileContent.values.tolist()
dataTable.insert(0, fileContent.columns.to_list())
problemCells = petl.validate(dataTable, constraints, header)
if problemCells.len() > 1:
return False, problemCells
else:
return True, None
status label
MapTerm a
CandidateTerm b
StopTerm c
status label
MapTerm a"""
CandidateTerm b
StopTerm "c "
status,label
MapTerm,a
CandidateTerm,b
StopTerm,c
status labels
MapTerm a
CandidateTerm b
StopTerm c
status label
MapTerm
CandidateTerm b
StopTerm s c
from io import BytesIO
from itertools import product
from werkzeug.datastructures import FileStorage
import pytest
from tests.conftest import id_files
def test_missingFile(client):
response = client.post('/terms')
assert response.data == b"Bad request, missing file\n"
assert response.status_code == 400
@pytest.mark.parametrize("test_file_copy", product(["incorrect.csv", "malformed.csv", "incorrectDelimiter.csv", "incorrectHeader.csv", "incorrectExtension"], ["terms"]), indirect=True, ids=lambda param : id_files(param))
def test_incorrectFile(client, test_file_copy):
with open(test_file_copy, "rb") as f:
f_data = BytesIO(f.read())
file_storage = FileStorage(f_data, filename=test_file_copy)
response = client.post('/terms', data={'file': file_storage}, content_type='multipart/form-data')
print(test_file_copy)
if "incorrect.csv" in test_file_copy:
assert response.data == b'Incorrect file - File is not compatible with GarganText\n'
elif "malformed.csv" in test_file_copy:
assert response.data == b"Error while getting file delimiter.\n Can be the following : \n - File delimiter not found \n - Incorrect file delimiter, should be a tabulation \n - File is malformed\n"
elif "incorrectDelimiter.csv" in test_file_copy:
assert response.data == b"Error while getting file delimiter.\n Can be the following : \n - File delimiter not found \n - Incorrect file delimiter, should be a tabulation \n - File is malformed\n"
elif "incorrectHeader.csv" in test_file_copy:
assert b"Some column names were not found" in response.data
elif "incorrectExtension" in test_file_copy:
assert response.data == b"Incorrect file format or file format not found\n"
assert response.status_code == 400
else:
assert False
if "incorrectExtension" not in test_file_copy:
assert response.status_code == 422
@pytest.mark.parametrize("test_file_copy", product(["correct.csv"], ["terms"]), indirect=True, ids=lambda param : id_files(param))
def test_correctFile(client, test_file_copy):
with open(test_file_copy, "rb") as f:
f_data = BytesIO(f.read())
file_storage = FileStorage(f_data, filename=test_file_copy)
response = client.post('/terms', data={'file': file_storage}, content_type='multipart/form-data')
assert response.data == b"Correct file\n"
assert response.status_code == 200
from io import BytesIO
from itertools import product
import pytest
from gargantools.utils.terms_utils import check_fileContent
from gargantools.utils.utils import get_fileContent
from tests.conftest import id_files
@pytest.mark.parametrize("test_file_copy", product(["correct.csv"], ["terms"]), indirect=True, ids=lambda param : id_files(param))
def test_check_fileContent(test_file_copy):
with open(test_file_copy, "r") as f:
f_data = BytesIO(f.read().encode("utf-8"))
assert check_fileContent(get_fileContent(f_data, '\t')) == (True, None)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment