init_test.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # pylint: disable=missing-module-docstring
  2. import pytest
  3. from freesurfer_volume_reader import (
  4. SubfieldVolumeFile,
  5. VolumeFile,
  6. __version__,
  7. parse_version_string,
  8. remove_group_names_from_regex,
  9. )
  10. def test_module_version():
  11. assert len(__version__) >= len("0.1.0")
  12. @pytest.mark.filterwarnings("ignore:function `parse_version_string` is deprecated")
  13. @pytest.mark.parametrize(
  14. ("version_string", "expected_tuple"),
  15. [
  16. ("0.24.2", (0, 24, 2)),
  17. ("0.21.0", (0, 21, 0)),
  18. ("0.2.2.dev28+g526f05c.d20190504", (0, 2, 2, "dev28+g526f05c", "d20190504")),
  19. ],
  20. )
  21. def test_parse_version_string(version_string, expected_tuple):
  22. assert expected_tuple == parse_version_string(version_string)
  23. @pytest.mark.filterwarnings("ignore:function `parse_version_string` is deprecated")
  24. def test_parse_version_string_comparison():
  25. assert parse_version_string("0.24.2") == (0, 24, 2)
  26. assert parse_version_string("0.24.2") < (0, 25)
  27. assert parse_version_string("0.24.2") < (0, 24, 3)
  28. assert parse_version_string("0.24.2") <= (0, 24, 2)
  29. assert parse_version_string("0.24.2") >= (0, 24, 2)
  30. assert parse_version_string("0.24.2") > (0, 24, 1)
  31. assert parse_version_string("0.24.2") > (0, 24)
  32. assert parse_version_string("0.2.2.dev28+g526f05c.d20190504") > (0, 2, 2)
  33. assert parse_version_string("0.2.2.dev28+g526f05c.d20190504") < (0, 2, 3)
  34. @pytest.mark.parametrize(
  35. ("source_pattern", "expected_pattern"),
  36. [
  37. (r"^(?P<h>[lr])h\.hippoSfVolumes", r"^([lr])h\.hippoSfVolumes"),
  38. (r"(?P<a>a(?P<b>b))", r"(a(b))"),
  39. ],
  40. )
  41. def test_remove_group_names_from_regex(source_pattern, expected_pattern):
  42. assert expected_pattern == remove_group_names_from_regex(
  43. regex_pattern=source_pattern
  44. )
  45. def test_volume_file_abstract():
  46. with pytest.raises(
  47. TypeError,
  48. match=r"^Can't instantiate abstract class VolumeFile with abstract methods? __init__$",
  49. ):
  50. VolumeFile(path="/tmp/test") # pylint: disable=abstract-class-instantiated
  51. class DummyVolumeFile(VolumeFile):
  52. # pylint: disable=useless-super-delegation
  53. def __init__(self, path: str) -> None:
  54. super().__init__(path=path)
  55. class DummySubfieldVolumeFile(SubfieldVolumeFile):
  56. # pylint: disable=useless-super-delegation
  57. def __init__(self, path: str) -> None:
  58. super().__init__(path=path)
  59. def read_volumes_mm3(self):
  60. return super().read_volumes_mm3()
  61. def read_volumes_dataframe(self):
  62. return super().read_volumes_dataframe()
  63. def test_subfield_volume_file_abstractmethod():
  64. volume_file = DummySubfieldVolumeFile(path="subfield-dummy")
  65. with pytest.raises(NotImplementedError):
  66. volume_file.read_volumes_mm3()
  67. with pytest.raises(NotImplementedError):
  68. volume_file.read_volumes_dataframe()