-
Notifications
You must be signed in to change notification settings - Fork 8
Add Pydantic models for configuration validation #510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
19aa45f
443d16a
49eb154
98867e5
a5b23a0
4c9b071
0291f12
b4d01c5
747e1d3
e4448ba
6345890
7399922
a7c64e5
143d18b
c211d1f
d125bdf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ dependencies: | |
| - tqdm =4.67.3 | ||
| - hatchling =1.29.0 | ||
| - hatch-vcs =0.5.0 | ||
| - pydantic =2.12.5 | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||
| from typing import Optional, Union | ||||||
|
|
||||||
| from pydantic import BaseModel, ConfigDict | ||||||
|
|
||||||
|
|
||||||
| class QueueModel(BaseModel): | ||||||
| """ | ||||||
| Pydantic model for a single queue configuration. | ||||||
| """ | ||||||
|
|
||||||
| model_config = ConfigDict(extra="allow") | ||||||
|
|
||||||
| script: Optional[str] = None | ||||||
| cores_min: Optional[int] = None | ||||||
| cores_max: Optional[int] = None | ||||||
| run_time_max: Optional[int] = None | ||||||
| memory_max: Optional[Union[int, str]] = None | ||||||
|
||||||
| memory_max: Optional[Union[int, str]] = None | |
| memory_max: Optional[Union[int, float, str]] = None |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,70 @@ | ||
| import unittest | ||
| from pysqa.base.config import QueueAdapterWithConfig | ||
| import sys | ||
| from unittest.mock import patch | ||
|
|
||
| try: | ||
| import pydantic | ||
|
|
||
| skip_pydantic_test = False | ||
| except ImportError: | ||
| skip_pydantic_test = True | ||
|
|
||
|
|
||
| class TestConfig(unittest.TestCase): | ||
| def test_bad_queue_type(self): | ||
| from pysqa.base.config import QueueAdapterWithConfig | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| QueueAdapterWithConfig(config={"queue_type": "error", "queues": {}}) | ||
|
|
||
| @unittest.skipIf(skip_pydantic_test, "pydantic not installed") | ||
| def test_pydantic_validation_missing_queues(self): | ||
| from pysqa.base.config import QueueAdapterWithConfig | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| QueueAdapterWithConfig(config={"queue_type": "SLURM"}) | ||
|
|
||
| @unittest.skipIf(skip_pydantic_test, "pydantic not installed") | ||
| def test_pydantic_validation_wrong_type(self): | ||
| from pysqa.base.config import QueueAdapterWithConfig | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| QueueAdapterWithConfig( | ||
| config={ | ||
| "queue_type": "SLURM", | ||
| "queues": {"sq": {"script": "slurm.sh", "cores_min": "not_an_int"}}, | ||
| } | ||
| ) | ||
|
|
||
| def test_pydantic_validation_extra_fields(self): | ||
| from pysqa.base.config import QueueAdapterWithConfig | ||
|
|
||
| config = { | ||
| "queue_type": "SLURM", | ||
| "queue_primary": "sq", | ||
| "queues": {"sq": {"extra_field": "allowed"}}, | ||
| "extra_top_level": "also_allowed", | ||
| } | ||
| qa = QueueAdapterWithConfig(config=config) | ||
| self.assertEqual(qa.config["queues"]["sq"]["extra_field"], "allowed") | ||
| self.assertEqual(qa.config["extra_top_level"], "also_allowed") | ||
|
|
||
| def test_no_pydantic_validation_extra_fields(self): | ||
| with patch.dict('sys.modules', {'pydantic': None}): | ||
| if 'pysqa.base.models' in sys.modules: | ||
| del sys.modules['pysqa.base.models'] | ||
|
|
||
| if 'pysqa.base.config' in sys.modules: | ||
| del sys.modules['pysqa.base.config'] | ||
|
|
||
| from pysqa.base.config import QueueAdapterWithConfig | ||
|
|
||
| config = { | ||
| "queue_type": "SLURM", | ||
| "queue_primary": "sq", | ||
| "queues": {"sq": {"extra_field": "allowed"}}, | ||
| "extra_top_level": "also_allowed", | ||
| } | ||
| qa = QueueAdapterWithConfig(config=config) | ||
| self.assertEqual(qa.config["queues"]["sq"]["extra_field"], "allowed") | ||
| self.assertEqual(qa.config["extra_top_level"], "also_allowed") |
Uh oh!
There was an error while loading. Please reload this page.