Skip to content

Commit

Permalink
Merge branch 'main' of github.com:canonical/pytest-interface-tester i…
Browse files Browse the repository at this point in the history
…nto pydantic-v2
  • Loading branch information
PietroPasotti committed Feb 7, 2024
2 parents a62e58c + 50cd086 commit a121aa0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
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.
2 changes: 1 addition & 1 deletion interface_tester/interface_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ 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,
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.1"
authors = [
{ name = "Pietro Pasotti", email = "[email protected]" },
]
Expand All @@ -20,7 +20,7 @@ license.text = "Apache-2.0"
keywords = ["juju", "relation interfaces"]

dependencies = [
"pydantic>2",
"pydantic>= 1.10.7",
"typer==0.7.0",
"ops-scenario>=5.2",
"pytest"
Expand Down
28 changes: 18 additions & 10 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 Down Expand Up @@ -54,7 +54,7 @@ def test_collect_schemas_multiple(tmp_path):
"""from interface_tester.schema_base import DataBagSchema
class RequirerSchema(DataBagSchema):
foo:int = 1"""
foo: int = 1"""
)
)

Expand All @@ -65,13 +65,13 @@ class RequirerSchema(DataBagSchema):
"""from interface_tester.schema_base import DataBagSchema
class RequirerSchema(DataBagSchema):
foo:int = 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:int = 2"""
foo: int = 2"""
)
)

Expand All @@ -95,9 +95,17 @@ 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(
"""
from typing import List
Baz: List[int]=[1,2,3]"""
),
"Baz",
),
),
)
def test_get_schema_from_module_wrong_type(tmp_path, schema_source, schema_name):
Expand All @@ -114,7 +122,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

0 comments on commit a121aa0

Please sign in to comment.