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

More json encodings #513

Merged
merged 15 commits into from
Feb 28, 2025
Prev Previous commit
Next Next commit
More robust tests.
pythonspeed committed Feb 28, 2025
commit cf62be67012aa49835e43d8b1a15b80aa1fd2f69
23 changes: 16 additions & 7 deletions eliot/tests/test_json.py
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
from unittest import TestCase, skipUnless, skipIf
from json import loads
import sys
from importlib.metadata import PackageNotFoundError, version as package_version

try:
import numpy as np
@@ -19,6 +20,15 @@
)


def package_installed(name: str) -> bool:
"""Return whether the package is installed."""
try:
package_version(name)
return True
except PackageNotFoundError:
return False


class EliotJSONEncoderTests(TestCase):
"""Tests for L{EliotJSONEncoder} and L{json_default}."""

@@ -109,6 +119,7 @@ class TestEnum(Enum):
"counter": Counter(["a", "a", "b"]),
"complex": 1 + 2j,
"enum": TestEnum.A,
"enum2": TestEnum.B,
}

serialized = loads(dumps(test_data, default=json_default))
@@ -123,12 +134,10 @@ class TestEnum(Enum):
self.assertEqual(serialized["ordered_dict"], {"a": 1, "b": 2})
self.assertEqual(serialized["counter"], {"a": 2, "b": 1})
self.assertEqual(serialized["complex"], {"real": 1.0, "imag": 2.0})
self.assertEqual(
serialized["enum"],
{"__enum__": True, "name": "A", "value": 1, "class": "TestEnum"},
)
self.assertEqual(serialized["enum"], 1)
self.assertEqual(serialized["enum2"], "test")

@skipUnless(sys.modules.get("pydantic"), "Pydantic not installed.")
@skipUnless(package_installed("pydantic"), "Pydantic not installed.")
def test_pydantic(self):
"""Test serialization of Pydantic models."""
from pydantic import BaseModel
@@ -141,7 +150,7 @@ class TestModel(BaseModel):
serialized = loads(dumps(model, default=json_default))
self.assertEqual(serialized, {"name": "test", "value": 42})

@skipUnless(sys.modules.get("pandas"), "Pandas not installed.")
@skipUnless(package_installed("pandas"), "Pandas not installed.")
def test_pandas(self):
"""Test serialization of Pandas objects."""
import pandas as pd
@@ -171,7 +180,7 @@ def test_pandas(self):
period = pd.Period("2024-01")
self.assertEqual(loads(dumps(period, default=json_default)), "2024-01")

@skipUnless(sys.modules.get("polars"), "Polars not installed.")
@skipUnless(package_installed("polars"), "Polars not installed.")
def test_polars(self):
"""Test serialization of Polars objects."""
import polars as pl