Skip to content

Commit

Permalink
BC for pydantic v1
Browse files Browse the repository at this point in the history
  • Loading branch information
PietroPasotti committed Feb 7, 2024
1 parent 91e7fbb commit 8543e16
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ You can customize name and location of the fixture, but you will need to include
```
## 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.
`pytest-interface-tester` supports both pydantic v1 and v2, but using v2 is recommended.
You might need to adjust your tested charm to also support v2. See [migration guide](https://docs.pydantic.dev/latest/migration/) for more information.
14 changes: 12 additions & 2 deletions interface_tester/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from enum import Enum
from typing import Any, Callable, List, Literal, Optional, Union

import pydantic
from ops.testing import CharmType
from pydantic import ValidationError
from scenario import Context, Event, Relation, State
Expand All @@ -28,6 +29,14 @@

logger = logging.getLogger(__name__)

_has_pydantic_v1 = pydantic.version.VERSION.split(".") <= ["2"]


def _validate(model: pydantic.BaseModel, obj: dict):
if _has_pydantic_v1:
return model.validate(obj)
return model.model_validate(obj)


class InvalidTestCase(RuntimeError):
"""Raised if a function decorated with interface_test_case is invalid."""
Expand Down Expand Up @@ -283,7 +292,8 @@ def assert_schema_valid(self, schema: Optional["DataBagSchema"] = None):
errors = []
for relation in self._relations:
try:
databag_schema.model_validate(
_validate(
databag_schema,
{
"unit": relation.local_unit_data,
"app": relation.local_app_data,
Expand Down Expand Up @@ -441,7 +451,7 @@ def _get_endpoint(supported_endpoints: dict, role: Role, interface_name: str):
return endpoints_for_interface[0]

def _generate_relations_state(
self, state_template: State, input_state: State, supported_endpoints, role: Role
self, state_template: State, input_state: State, supported_endpoints, role: Role
) -> List[Relation]:
"""Merge the relations from the input state and the state template into one.
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/test_collect_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from interface_tester.interface_test import _has_pydantic_v1
from interface_tester.collector import (
collect_tests,
get_schema_from_module,
Expand Down Expand Up @@ -70,8 +71,13 @@ class RequirerSchema(DataBagSchema):
)

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

else:
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 Down

0 comments on commit 8543e16

Please sign in to comment.