Improve get_int_value() and its tests #25
2 changed files with 41 additions and 22 deletions
|
|
@ -480,9 +480,10 @@ def get_string_value(value: str):
|
|||
def get_int_value(value: str):
|
||||
if len(value) == 0:
|
||||
return None
|
||||
float_value = float(value)
|
||||
int_value = int(float_value)
|
||||
return None if float_value != int_value else int_value
|
||||
number = float(value)
|
||||
if number != int(number):
|
||||
raise ValueError(f"Not an integer: {value}")
|
||||
return int(number)
|
||||
|
||||
|
||||
def get_float_value(value: str):
|
||||
|
|
@ -705,21 +706,21 @@ def protected_application(environ, request_body):
|
|||
template_directory = Path(environ[TEMPLATE_DIRECTORY_KEY])
|
||||
|
||||
# Parse data and check value structure
|
||||
sent_data = parse_qs(request_body)
|
||||
for value in sent_data.values():
|
||||
received_data = parse_qs(request_body)
|
||||
for value in received_data.values():
|
||||
if not isinstance(value, list) or not len(value) == 1:
|
||||
raise ValueError(f"expected list of length one, got: {repr(value)}")
|
||||
|
||||
# Add auto fields to the sent data
|
||||
add_auto_fields(sent_data)
|
||||
# Add auto fields to the received data
|
||||
add_auto_fields(received_data)
|
||||
|
||||
# Correct the optional checkbox field in the sent data
|
||||
correct_optional_checkbox_fields(sent_data)
|
||||
# Correct the optional checkbox field in the received data
|
||||
correct_optional_checkbox_fields(received_data)
|
||||
|
||||
# Read the mandatory keys into the result dictionary
|
||||
entered_data_object, missing_keys = read_mandatory_fields(
|
||||
required_fields,
|
||||
sent_data)
|
||||
received_data)
|
||||
|
||||
if missing_keys:
|
||||
return create_missing_key_result(missing_keys)
|
||||
|
|
@ -727,7 +728,7 @@ def protected_application(environ, request_body):
|
|||
if entered_data_object["subject-group"] == "patient":
|
||||
entered_patient_data, missing_patient_keys = read_mandatory_fields(
|
||||
required_patient_fields,
|
||||
sent_data)
|
||||
received_data)
|
||||
|
||||
entered_data_object.update(entered_patient_data)
|
||||
missing_keys.extend(missing_patient_keys)
|
||||
|
|
@ -736,15 +737,15 @@ def protected_application(environ, request_body):
|
|||
return create_missing_key_result(missing_keys)
|
||||
|
||||
# Check the hash value
|
||||
local_hash_string = get_canonic_content_string(sent_data)
|
||||
if local_hash_string != sent_data["hashed-string"][0]:
|
||||
local_hash_string = get_canonic_content_string(received_data)
|
||||
if local_hash_string != received_data["hashed-string"][0]:
|
||||
return create_bad_request_result([
|
||||
"Local hash input-string does not match submitted values\n",
|
||||
"LOCAL: " + local_hash_string + "\n",
|
||||
"SENT: " + sent_data["hashed-string"][0] + "\n"])
|
||||
"SENT: " + received_data["hashed-string"][0] + "\n"])
|
||||
|
||||
local_hash_value = hashlib.sha256(local_hash_string.encode()).hexdigest()
|
||||
if local_hash_value != sent_data["hash-value"][0]:
|
||||
if local_hash_value != received_data["hash-value"][0]:
|
||||
return create_bad_request_result([
|
||||
"Server side hash value does not match submitted hash value\n"])
|
||||
|
||||
|
|
@ -753,14 +754,14 @@ def protected_application(environ, request_body):
|
|||
result_object = {
|
||||
"source": {
|
||||
"time_stamp": time_stamp,
|
||||
"version": sent_data["form-data-version"][0],
|
||||
"version": received_data["form-data-version"][0],
|
||||
"remote_address": environ["REMOTE_ADDR"],
|
||||
"hashed-string": sent_data["hashed-string"][0],
|
||||
"hash-value": sent_data["hash-value"][0],
|
||||
"hashed-string": received_data["hashed-string"][0],
|
||||
"hash-value": received_data["hash-value"][0],
|
||||
"signature-data": (
|
||||
None
|
||||
if sent_data["signature-data"][0] == ""
|
||||
else sent_data["signature-data"][0]
|
||||
if received_data["signature-data"][0] == ""
|
||||
else received_data["signature-data"][0]
|
||||
)
|
||||
},
|
||||
"data": entered_data_object
|
||||
|
|
|
|||
|
|
@ -17,14 +17,20 @@ template_dir = Path(__file__).parents[2] / "templates"
|
|||
|
||||
sys.path.insert(0, str(server_dir))
|
||||
|
||||
|
||||
import store_data
|
||||
from store_data import DATASET_ROOT_KEY, HOME_KEY, TEMPLATE_DIRECTORY_KEY
|
||||
from store_data import (
|
||||
get_int_value,
|
||||
DATASET_ROOT_KEY,
|
||||
HOME_KEY,
|
||||
TEMPLATE_DIRECTORY_KEY,
|
||||
)
|
||||
|
||||
|
||||
minimal_form_data = """form-data-version=2.2&data-entry-domain=de.sfb1451.z03&data-entry-employee=cm-test&project-code=b4&subject-pseudonym=test-111&date-of-birth=2000-01-01&sex=male&date-of-test=2020-01-01&subject-group=healthy&patient-year-first-symptom=&patient-month-first-symptom=&patient-day-first-symptom=&patient-year-diagnosis=&patient-month-diagnosis=&patient-day-diagnosis=&additional-remarks=&hashed-string=form-data-version%3A2.2%3Bdata-entry-domain%3Ade.sfb1451.z03%3Bdata-entry-employee%3Acm-test%3Bproject-code%3Ab4%3Bsubject-pseudonym%3Atest-111%3Bdate-of-birth%3A2000-01-01%3Bsex%3Amale%3Bdate-of-test%3A2020-01-01%3Brepeated-test%3AFalse%3Bpatient-year-first-symptom%3A%3Bpatient-month-first-symptom%3A%3Bpatient-day-first-symptom%3A%3Bpatient-year-diagnosis%3A%3Bpatient-month-diagnosis%3A%3Bpatient-day-diagnosis%3A%3Bpatient-main-disease%3A%3Bpatient-stronger-impacted-hand%3A%3Blaterality-quotient%3A%3Bmaximum-ftf-left%3A%3Bmaximum-ftf-right%3A%3Bmaximum-gs-left%3A%3Bmaximum-gs-right%3A%3Bpurdue-pegboard-left%3A%3Bpurdue-pegboard-right%3A%3Bturn-cards-left%3A%3Bturn-cards-right%3A%3Bsmall-things-left%3A%3Bsmall-things-right%3A%3Bsimulated-feeding-left%3A%3Bsimulated-feeding-right%3A%3Bcheckers-left%3A%3Bcheckers-right%3A%3Blarge-light-things-left%3A%3Blarge-light-things-right%3A%3Blarge-heavy-things-left%3A%3Blarge-heavy-things-right%3A%3Bjtt-incorrectly-executed%3A%3Barat-left%3A%3Barat-right%3A%3Btug-executed%3A%3Btug-a-incorrectly-executed%3A%3Btug-a-tools-required%3A%3Btug-imagined%3A%3Bgo-nogo-block-count%3A%3Bgo-nogo-total-errors%3A%3Bgo-nogo-wrong-errors%3A%3Bgo-nogo-recognized-errors%3A%3Bgo-nogo-correct-answer-time%3A%3Bgo-nogo-recognized-error-time%3A%3Bgo-nogo-incorrectly-executed%3A%3Bkas-pantomime-bukko-facial%3A%3Bkas-pantomime-arm-hand%3A%3Bkas-imitation-bukko-facial%3A%3Bkas-imitation-arm-hand%3A%3Bkopss-orientation%3A%3Bkopss-speech%3A%3Bkopss-praxie%3A%3Bkopss-visual-spatial-performance%3A%3Bkopss-calculating%3A%3Bkopss-executive-performance%3A%3Bkopss-memory%3A%3Bkopss-affect%3A%3Bkopss-behavior-observation%3A%3Bacl-k-loud-reading%3A%3Bacl-k-color-form-test%3A%3Bacl-k-supermarket-task%3A%3Bacl-k-communication-ability%3A%3Bbdi-ii-score%3A%3Bmadrs-score%3A%3Bdemtect-wordlist%3A%3Bdemtect-convert-numbers%3A%3Bdemtect-supermarket-task%3A%3Bdemtect-numbers-reverse%3A%3Bdemtect-wordlist-recall%3A%3Btime-tmt-a%3A%3Btmt-a-incorrectly-executed%3A%3Btime-tmt-b%3A%3Btmt-b-incorrectly-executed%3A%3Bmrs-score%3A%3Beuroqol-code%3A%3Beuroqol-vas%3A%3Bisced-value%3A%3Badditional-mrt-url%3A%3Badditional-mrt-resting-state%3A%3Badditional-mrt-tapping-task%3A%3Badditional-mrt-anatomical-representation%3A%3Badditional-mrt-dti%3A%3Badditional-eeg-url%3A%3Badditional-blood-sampling-url%3A%3Badditional-remarks%3A&hash-value=3bb998f5acf11ad82a17b3cef2c14258712a3b50c5efe7261e7792158e058ebe&signature-data="""
|
||||
|
||||
|
||||
class TestFileTree(unittest.TestCase):
|
||||
class TestStoreData(unittest.TestCase):
|
||||
|
||||
def _test_exception_caught(self,
|
||||
app_tester,
|
||||
|
|
@ -116,3 +122,15 @@ class TestFileTree(unittest.TestCase):
|
|||
with expected_path.open() as f:
|
||||
json_object = json.load(f)
|
||||
print(json_object)
|
||||
|
||||
def test_get_int(self):
|
||||
self.assertEqual(get_int_value(".0"), 0)
|
||||
self.assertEqual(get_int_value("1.0"), 1)
|
||||
self.assertEqual(get_int_value("2"), 2)
|
||||
|
||||
# Empty fields should be mapped to None
|
||||
self.assertEqual(get_int_value(""), None)
|
||||
|
||||
# Non-int should raise a ValueError
|
||||
self.assertRaises(ValueError, get_int_value, "3.4")
|
||||
self.assertRaises(ValueError, get_int_value, "a")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue