Chemical-Curation / chemcurator_django

Backend services for chemical curation
https://api.chemreg.epa.gov
MIT License
1 stars 0 forks source link

149 synonym type validations #217

Closed michael-barbour closed 4 years ago

michael-barbour commented 4 years ago

closes #149

This is the SynonymType portion of validating SynonymType.validation_regular_expression. Updating any SynonymType's validation_regex should first check that there is no conflict with any associated Synonym.identifiers.

This ticket is not responsible for checking if updated Synonyms are valid on upload.

Tests: validation_regular_expression="^.*$ allows all strings. validation_regular_expression="^[0-9]*$" only allows synonym identifiers with numeric strings. Updating from the less restrictive regex to the more restrictive regex should throw an error containing any violations.

Below is a simple test that could be run in manage.py shell

synonym_type = SynonymTypeFactory(validation_regular_expression="^.*$").instance
SynonymFactory(identifier="Foobar", synonym_type={"type": "synonymType", "id": synonym_type.pk})

from chemreg.substance.serializers import SynonymTypeSerializer
# partial updates require pulling the SynonymTypeSerializer.
synonym_type = SynonymTypeSerializer(synonym_type, data={"validation_regular_expression": "^[0-9]*$"},partial=True)
assert not synonym_type.is_valid()
print(synonym_type.errors)

Working update test

synonym_type = SynonymTypeFactory(validation_regular_expression="^.*$").instance
SynonymFactory(identifier="123", synonym_type={"type": "synonymType", "id": synonym_type.pk})

from chemreg.substance.serializers import SynonymTypeSerializer
# partial updates require pulling the SynonymTypeSerializer.
synonym_type = SynonymTypeSerializer(synonym_type, data={"validation_regular_expression": "^[0-9]*$"},partial=True)
assert synonym_type.is_valid()