thegetty / crom

Python library to make creation of CIDOC CRM easier by mapping classes/predicates to python objects
Apache License 2.0
49 stars 16 forks source link

Fix flaky tests by cleaning a polluted state. #181

Open sturmianseq opened 2 years ago

sturmianseq commented 2 years ago

What is the purpose of the change

This PR is to fix flaky tests tests/test_model.py::TestMagicMethods::test_not_multiple_instance, tests/test_model.py::TestMagicMethods::test_validate_multiplicity after running tests/test_model.py::TestAutoIdentifiers::test_bad_autoid, but pass when they are run in isolation.

Reproduce the test failure

Run the following command:

python -m pytest tests/test_model.py::TestAutoIdentifiers::test_bad_autoid tests/test_model.py::TestMagicMethods::test_not_multiple_instance

python -m pytest tests/test_model.py::TestAutoIdentifiers::test_bad_autoid tests/test_model.py::TestMagicMethods::test_validate_multiplicity

Expexted result

Test tests/test_model.py::TestMagicMethods::test_not_multiple_instance and tests/test_model.py::TestMagicMethods::test_validate_multiplicity should pass after running tests/test_model.py::TestAutoIdentifiers::test_bad_autoid.

Actual result

tests/test_model.py::TestMagicMethods::test_not_multiple_instance and tests/test_model.py::TestMagicMethods::test_validate_multiplicity fail:

        else:
>               raise ConfigurationError("Unknown auto-id type")
E     cromulent.model.ConfigurationError: Unknown auto-id type

cromulent/model.py:281: ConfigurationError

Why they fail

model.factory.auto_id_type is polluted after tests/test_model.py::TestAutoIdentifiers::test_bad_autoid

Fix

Reset model.factory.auto_id_type to int-per-segment at the end of tests/test_model.py::TestAutoIdentifiers::test_bad_autoid to avoid flaky tests.