Skip to content

Commit

Permalink
Add tests of toplevel metadata keyed with float
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Dec 2, 2024
1 parent 0b94eed commit 3bf6954
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/test_tskit_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import demes
import fwdpy11
import pytest
import tskit

from dataclasses import dataclass


@pytest.fixture
Expand Down Expand Up @@ -117,3 +120,64 @@ def test_seed(pop):

with pytest.raises(ValueError):
_ = pop.dump_tables_to_tskit(seed=-333)


def test_toplevel_metadata_0():
tc = tskit.TableCollection(10.0)
tc.metadata_schema = fwdpy11.tskit_tools.metadata_schema.TopLevelMetadata
md = {"generation": 100, "data": {0.0: [0, 1, 2]}}
tc.metadata = md
data = tc.metadata["data"]
assert 0.0 not in data
assert "0.0" in data


def test_toplevel_metadata_1():
# Same as previous test, but w/no schema
# and we just shove raw bytes in there
tc = tskit.TableCollection(10.0)
md = {"generation": 100, "data": {0.0: [0, 1, 2]}}
tc.metadata = str(md).encode()
decoded = eval(tc.metadata)
data = decoded["data"]
assert 0.0 in data


def test_toplevel_metadata_2():
# This time w/a JSON codec
tc = tskit.TableCollection(10.0)
schema = tskit.metadata.MetadataSchema(
{
"codec": "json",
}
)
tc.metadata_schema = schema
md = {"generation": 100, "data": {0.0: [0, 1, 2]}}

tc.metadata = md
decoded = tc.metadata
data = decoded["data"]
assert 0.0 not in data
assert "0.0" in data


@dataclass(eq=True, frozen=True)
class FloatWrap:
value: float


def test_toplevel_metadata_3():

# This time w/a JSON codec
tc = tskit.TableCollection(10.0)
schema = tskit.metadata.MetadataSchema(
{
"codec": "json",
}
)
tc.metadata_schema = schema
md = {"data": {FloatWrap(0.0): [0, 1, 2]}}
tc.metadata = str(md)
decoded = eval(tc.metadata)
data = decoded["data"]
assert FloatWrap(0.0) in data

0 comments on commit 3bf6954

Please sign in to comment.