Skip to content
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

Pydantic v2 upgrade #13

Merged
merged 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,5 @@ You can customize name and location of the fixture, but you will need to include
identifier: my_fixture_name # optional; default = interface_tester
```

## Upgrading from v1
As `pytest-interface-tester` v2 is using `pydantic` v2 that introduces breaking changes to their API, you might need to adjust your tested charm to also support v2. See [migration guide](https://docs.pydantic.dev/latest/migration/) for more information.
5 changes: 0 additions & 5 deletions interface_tester/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,6 @@ def load_schema_module(schema_path: Path) -> types.ModuleType:
if module_name in sys.modules:
del sys.modules[module_name]

# Otherwise we'll get an error when we re-run @validator
# fixme: is there a better way to do this?
logger.debug("Clearing pydantic.class_validators._FUNCS")
pydantic.class_validators._FUNCS.clear() # noqa

try:
module = importlib.import_module(module_name)
except ImportError:
Expand Down
4 changes: 2 additions & 2 deletions interface_tester/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,14 @@ def assert_schema_valid(self, schema: Optional["DataBagSchema"] = None):
errors = []
for relation in self._relations:
try:
databag_schema.validate(
databag_schema.model_validate(
{
"unit": relation.local_unit_data,
"app": relation.local_app_data,
}
)
except ValidationError as e:
errors.append(e.args[0])
errors.append(e.errors()[0])
if errors:
raise SchemaValidationError(errors)

Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ build-backend = "setuptools.build_meta"
[project]
name = "pytest-interface-tester"

version = "1.0.7"
version = "2.0.0"
authors = [
{ name = "Pietro Pasotti", email = "[email protected]" },
]
Expand All @@ -20,7 +20,7 @@ license.text = "Apache-2.0"
keywords = ["juju", "relation interfaces"]

dependencies = [
"pydantic>= 1.10.7, <2",
"pydantic>=2",
"typer==0.7.0",
"ops-scenario>=5.2",
"pytest"
Expand Down
22 changes: 11 additions & 11 deletions tests/unit/test_collect_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_load_schema_module(tmp_path):
pth.write_text(
dedent(
"""
FOO = 1
FOO: int = 1
"""
)
)
Expand All @@ -35,7 +35,7 @@ def test_collect_schemas(tmp_path):
"""from interface_tester.schema_base import DataBagSchema

class RequirerSchema(DataBagSchema):
foo = 1"""
foo: int = 1"""
)
)

Expand All @@ -54,7 +54,7 @@ def test_collect_schemas_multiple(tmp_path):
"""from interface_tester.schema_base import DataBagSchema

class RequirerSchema(DataBagSchema):
foo = 1"""
foo: int = 1"""
)
)

Expand All @@ -65,13 +65,13 @@ class RequirerSchema(DataBagSchema):
"""from interface_tester.schema_base import DataBagSchema

class RequirerSchema(DataBagSchema):
foo = 2"""
foo: int = 2"""
)
)

tests = collect_tests(root)
assert tests["mytestinterfacea"]["v0"]["requirer"]["schema"].__fields__["foo"].default == 1
assert tests["mytestinterfaceb"]["v0"]["requirer"]["schema"].__fields__["foo"].default == 2
assert tests["mytestinterfacea"]["v0"]["requirer"]["schema"].model_fields["foo"].default == 1
assert tests["mytestinterfaceb"]["v0"]["requirer"]["schema"].model_fields["foo"].default == 2


def test_collect_invalid_schemas(tmp_path):
Expand All @@ -84,7 +84,7 @@ def test_collect_invalid_schemas(tmp_path):
dedent(
"""from interface_tester.schema_base import DataBagSchema
class ProviderSchema(DataBagSchema):
foo = 2"""
foo: int = 2"""
)
)

Expand All @@ -95,9 +95,9 @@ class ProviderSchema(DataBagSchema):
@pytest.mark.parametrize(
"schema_source, schema_name",
(
(dedent("""Foo2=1"""), "Foo2"),
(dedent("""Bar='baz'"""), "Bar"),
(dedent("""Baz=[1,2,3]"""), "Baz"),
(dedent("""Foo2: int=1"""), "Foo2"),
(dedent("""Bar: str='baz'"""), "Bar"),
(dedent("""Baz: list=[1,2,3]"""), "Baz"),
PietroPasotti marked this conversation as resolved.
Show resolved Hide resolved
),
)
def test_get_schema_from_module_wrong_type(tmp_path, schema_source, schema_name):
Expand All @@ -114,7 +114,7 @@ def test_get_schema_from_module_wrong_type(tmp_path, schema_source, schema_name)
@pytest.mark.parametrize("schema_name", ("foo", "bar", "baz"))
def test_get_schema_from_module_bad_name(tmp_path, schema_name):
pth = Path(tmp_path) / "bar3.py"
pth.write_text("dead='beef'")
pth.write_text("dead: str='beef'")
module = load_schema_module(pth)

# fails because it's not found in the module
Expand Down
Loading