diff --git a/src/encoded/schemas/changelogs/treatment.md b/src/encoded/schemas/changelogs/treatment.md index f7f121a8408..06f280dad78 100644 --- a/src/encoded/schemas/changelogs/treatment.md +++ b/src/encoded/schemas/changelogs/treatment.md @@ -1,5 +1,8 @@ ## Changelog for treatment.json +### Schema version 14 +* Restricted *amount* and *duration* properties to have a minimum value of 0. + ### Schema version 13 * Modified regex pattern for *product_id* to disallow blank strings. diff --git a/src/encoded/schemas/treatment.json b/src/encoded/schemas/treatment.json index 0460ddc5875..e4fa9e9c5c5 100644 --- a/src/encoded/schemas/treatment.json +++ b/src/encoded/schemas/treatment.json @@ -28,7 +28,7 @@ }, "properties": { "schema_version": { - "default": "13" + "default": "14" }, "documents": { "description": "Documents that describe the treatment protocol." @@ -50,7 +50,8 @@ }, "amount": { "title": "Amount", - "type": "number" + "type": "number", + "minimum": 0 }, "amount_units": { "title": "Amount units", @@ -76,7 +77,8 @@ }, "duration": { "title": "Duration", - "type": "number" + "type": "number", + "minimum": 0 }, "duration_units": { "title": "Duration units", diff --git a/src/encoded/tests/fixtures/schemas/treatment.py b/src/encoded/tests/fixtures/schemas/treatment.py index 2ee89823828..695d570e244 100644 --- a/src/encoded/tests/fixtures/schemas/treatment.py +++ b/src/encoded/tests/fixtures/schemas/treatment.py @@ -149,3 +149,16 @@ def treatment_12(testapp): 'treatment_term_id': 'CHEBI:23965' } return testapp.post_json('/treatment', item).json['@graph'][0] + + +@pytest.fixture +def treatment_with_negative_duration_amount_units(testapp, organism): + item = { + 'treatment_term_name': 'ethanol', + 'treatment_type': 'chemical', + 'duration': -9, + 'duration_units': 'day', + 'amount': -100, + 'amount_units': 'mg' + } + return item \ No newline at end of file diff --git a/src/encoded/tests/test_upgrade_treatment.py b/src/encoded/tests/test_upgrade_treatment.py index e5e49f4fa90..e13efbb7795 100644 --- a/src/encoded/tests/test_upgrade_treatment.py +++ b/src/encoded/tests/test_upgrade_treatment.py @@ -78,3 +78,14 @@ def test_treatment_upgrade_12_13(upgrader, treatment_12): value = upgrader.upgrade('treatment', treatment_12, current_version='12', target_version='13') assert value['schema_version'] == '13' assert 'product_id' not in value + + +def test_treatment_upgrade_13_14(upgrader, treatment_with_negative_duration_amount_units): + value = upgrader.upgrade('treatment', treatment_with_negative_duration_amount_units, current_version='13', target_version='14') + assert value['amount'] == 0 + assert value['duration'] == 0 + assert value['notes'] == ( + 'This treatment erroneously had a negative amount of -100 ' + 'and was upgraded to 0. This treatment erroneously had a ' + 'negative duration of -9 and was upgraded to 0.' + ) diff --git a/src/encoded/upgrade/treatment.py b/src/encoded/upgrade/treatment.py index 3977e2de50a..c757cb27658 100644 --- a/src/encoded/upgrade/treatment.py +++ b/src/encoded/upgrade/treatment.py @@ -112,3 +112,26 @@ def treatment_12_13(value, system): # https://encodedcc.atlassian.net/browse/ENCD-6102 if 'product_id' in value and value['product_id'] == '': value.pop('product_id') + + +@upgrade_step('treatment', '13', '14') +def treatment_13_14(value, system): + # https://encodedcc.atlassian.net/browse/ENCD-6124 + if value.get('amount') < 0: + current_notes = '' + if 'notes' in value: + current_notes = value['notes'] + value['notes'] = ( + f'{current_notes} This treatment erroneously had a negative' + f' amount of {value["amount"]} and was upgraded to 0.'.strip() + ) + value['amount'] = 0 + if value.get('duration') < 0: + current_notes = '' + if 'notes' in value: + current_notes = value['notes'] + value['notes'] = ( + f'{current_notes} This treatment erroneously had a negative' + f' duration of {value["duration"]} and was upgraded to 0.'.strip() + ) + value['duration'] = 0