Refactor dump_json method to use model_dump_json
All checks were successful
CI / lint (pull_request) Successful in 1m0s
CI / test (3.10) (pull_request) Successful in 27s
CI / test (3.11) (pull_request) Successful in 35s
CI / test (3.12) (pull_request) Successful in 36s
CI / test (3.13) (pull_request) Successful in 37s
CI / ci-success (pull_request) Successful in 2s
All checks were successful
CI / lint (pull_request) Successful in 1m0s
CI / test (3.10) (pull_request) Successful in 27s
CI / test (3.11) (pull_request) Successful in 35s
CI / test (3.12) (pull_request) Successful in 36s
CI / test (3.13) (pull_request) Successful in 37s
CI / ci-success (pull_request) Successful in 2s
- Fix the `dump_json` method in the `ModelView` class to call `model_dump_json` instead of `model_dump`, ensuring consistency in JSON serialization. - Added a new test case in `TestListModelView` to verify the functionality of `dump_json` and check for correct IP address serialization.
This commit is contained in:
@@ -23,7 +23,7 @@ class ModelView(Generic[TModel]):
|
|||||||
def dump_json(self) -> str:
|
def dump_json(self) -> str:
|
||||||
if isinstance(self._model, list):
|
if isinstance(self._model, list):
|
||||||
return json.dumps(
|
return json.dumps(
|
||||||
[item.model_dump(by_alias=True) for item in self._model],
|
[item.model_dump_json(by_alias=True) for item in self._model],
|
||||||
ensure_ascii=False,
|
ensure_ascii=False,
|
||||||
)
|
)
|
||||||
return self._model.model_dump_json(by_alias=True)
|
return self._model.model_dump_json(by_alias=True)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
from ipaddress import IPv4Address
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@@ -66,6 +67,10 @@ class TestListModelView:
|
|||||||
def test_dump_uses_aliases(self, interfaces_view):
|
def test_dump_uses_aliases(self, interfaces_view):
|
||||||
dumped = interfaces_view.dump()
|
dumped = interfaces_view.dump()
|
||||||
assert dumped[0]["interface"] == "eth0"
|
assert dumped[0]["interface"] == "eth0"
|
||||||
|
assert dumped[0]["ip_address"] == IPv4Address("192.168.1.1")
|
||||||
|
|
||||||
|
def test_dump_json(self, interfaces_view):
|
||||||
|
assert interfaces_view.dump_json()
|
||||||
|
|
||||||
def test_dump_json_keeps_unicode(self):
|
def test_dump_json_keeps_unicode(self):
|
||||||
view = ModelView([Interfaces(interface="eth0", description="Дом")])
|
view = ModelView([Interfaces(interface="eth0", description="Дом")])
|
||||||
|
|||||||
Reference in New Issue
Block a user