From 4a24e471e579d849b979b11eaa77dfd2f84a99d0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Jul 2023 12:00:59 -0400 Subject: [PATCH] Updates tests to reflect new pydantic 2.0 --- pyproject.toml | 3 ++- tests/settings/test_applicationsettings.py | 2 +- tests/settings/test_filesystemschema.py | 16 +++++------ tests/settings/test_settingsschema.py | 31 ---------------------- 4 files changed, 11 insertions(+), 41 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 4735810..5f6df0d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,7 +28,8 @@ notifier = "quota_notifier.cli:Application.execute" [tool.poetry.dependencies] python = ">=3.8" -pydantic = "2.0.2" +#pydantic = "2.0.2" +pydantic-settings = "2.0.1" sqlalchemy = "2.0.17" [tool.poetry.group.tests] diff --git a/tests/settings/test_applicationsettings.py b/tests/settings/test_applicationsettings.py index 249341e..191c4ae 100644 --- a/tests/settings/test_applicationsettings.py +++ b/tests/settings/test_applicationsettings.py @@ -62,7 +62,7 @@ def test_invalid_file(self) -> None: with path_obj.open('w') as io: json.dump(settings, io) - with self.assertRaisesRegex(ValidationError, 'extra fields not permitted'): + with self.assertRaisesRegex(Exception, 'Extra inputs are not permitted'): ApplicationSettings.set_from_file(path_obj) diff --git a/tests/settings/test_filesystemschema.py b/tests/settings/test_filesystemschema.py index ba608df..2ee5bff 100644 --- a/tests/settings/test_filesystemschema.py +++ b/tests/settings/test_filesystemschema.py @@ -17,11 +17,11 @@ class NameValidation(DefaultSetupTeardown, TestCase): def test_blank_name_error(self) -> None: """Test a ``ValueError`` is raised for empty/blank names""" - with self.assertRaisesRegex(ValueError, 'File system name cannot be blank'): + with self.assertRaisesRegex(Exception, 'File system name cannot be blank'): FileSystemSchema(name='') for char in string.whitespace: - with self.assertRaisesRegex(ValueError, 'File system name cannot be blank'): + with self.assertRaisesRegex(Exception, 'File system name cannot be blank'): FileSystemSchema(name=char) def test_whitespace_is_stripped(self) -> None: @@ -61,7 +61,7 @@ def test_valid_types_pass() -> None: def test_invalid_type_error(self) -> None: """Test a ``ValueError`` is raised for invalid types""" - with self.assertRaisesRegex(ValidationError, 'type\n unexpected value;'): + with self.assertRaisesRegex(Exception, 'type\n Input should be '): FileSystemSchema(type='fake_type') @@ -78,29 +78,29 @@ def test_intermediate_values_pass(self) -> None: def test_empty_list_fails(self) -> None: """Test an empty collection of thresholds fails validation""" - with self.assertRaisesRegex(ValidationError, 'At least one threshold must be specified'): + with self.assertRaisesRegex(Exception, 'At least one threshold must be specified'): FileSystemSchema(thresholds=[]) def test_zero_percent(self) -> None: """Test the value ``0`` fails validation""" - with self.assertRaisesRegex(ValidationError, 'must be greater than 0 and less than 100'): + with self.assertRaisesRegex(Exception, 'must be greater than 0 and less than 100'): FileSystemSchema(thresholds=[0, 50]) def test_100_percent(self) -> None: """Test the value ``100`` fails validation""" - with self.assertRaisesRegex(ValidationError, 'must be greater than 0 and less than 100'): + with self.assertRaisesRegex(Exception, 'must be greater than 0 and less than 100'): FileSystemSchema(thresholds=[50, 100]) def test_negative_percent(self) -> None: """Test negative values fail validation""" - with self.assertRaisesRegex(ValidationError, 'must be greater than 0 and less than 100'): + with self.assertRaisesRegex(Exception, 'must be greater than 0 and less than 100'): FileSystemSchema(thresholds=[-1, 50]) def test_over_100_percent(self) -> None: """Test values over ``100`` fail validation""" - with self.assertRaisesRegex(ValidationError, 'must be greater than 0 and less than 100'): + with self.assertRaisesRegex(Exception, 'must be greater than 0 and less than 100'): FileSystemSchema(thresholds=[50, 101]) diff --git a/tests/settings/test_settingsschema.py b/tests/settings/test_settingsschema.py index 3592347..99b0d03 100644 --- a/tests/settings/test_settingsschema.py +++ b/tests/settings/test_settingsschema.py @@ -8,37 +8,6 @@ from tests.base import DefaultSetupTeardown -class BlacklistValidation(DefaultSetupTeardown, TestCase): - """Test validation for the ``uid_blacklist`` and ``gid_blacklist`` fields""" - - def test_error_on_id_range_len_1(self) -> None: - """Test a ``ValueError`` is raised for UID and GID ranges with 1 element""" - - with self.assertRaisesRegex(ValueError, 'actual_length=1; expected_length=2'): - SettingsSchema(uid_blacklist=[[1], ]) - - with self.assertRaisesRegex(ValueError, 'actual_length=1; expected_length=2'): - SettingsSchema(gid_blacklist=[[1], ]) - - def test_error_on_id_range_len_3(self) -> None: - """Test a ``ValueError`` is raised for UID and GID ranges with 3 elements""" - - with self.assertRaisesRegex(ValueError, 'actual_length=3; expected_length=2'): - SettingsSchema(uid_blacklist=[[1, 2, 3], ]) - - with self.assertRaisesRegex(ValueError, 'actual_length=3; expected_length=2'): - SettingsSchema(gid_blacklist=[[1, 2, 3], ]) - - def test_error_on_account_names(self) -> None: - """Test a useful error message is raised when users provide account names instead of IDs""" - - with self.assertRaisesRegex(ValueError, 'value is not a valid integer'): - SettingsSchema(uid_blacklist=['root', ]) - - with self.assertRaisesRegex(ValueError, 'value is not a valid integer'): - SettingsSchema(gid_blacklist=['root', ]) - - class FileSystemValidation(DefaultSetupTeardown, TestCase): """Test validation for the ``file_systems`` field"""