def test_unambig(): """Test that properties are mirrored in a top level dic""" pif = System() pif.properties = [Property(name="foo", scalars=1.0), Property(name="bar", scalars=2.0)] r = ReadView(pif) assert r["foo"].scalars[0].value == 1.0 assert r["bar"].scalars[0].value == 2.0
def datacite_to_pif(dc): """ Parse a top-level datacite dictionary into a PIF :param dc: dictionary containing datacite metadata :return: System containing metadata from datacite """ system = System() system.references = [datacite_to_pif_reference(dc)] return system
def test_unambig(): """Test that properties are mirrored in a top level dic""" pif = System() pif.properties = [ Property(name="foo", scalars=[Scalar(value=1.0)]), Property(name="bar", scalars=[Scalar(value=2.0)]) ] r = ReadView(pif) assert r["foo"].scalars[0].value == 1.0 assert r["bar"].scalars[0].value == 2.0
def test_multiple_instances(): """Test that keys that show up in different places with the same value are kept""" pif = System() pif.uid = "10245" pif.properties = [Property(name="foo", scalars=1.0), Property(name="bar", scalars=2.0)] pif2 = System(sub_systems = [pif,], properties=[Property(name="bar", scalars=2.0)]) r = ReadView(pif2) assert r.properties["bar"].scalars[0].value == 2.0 assert r.sub_systems["10245"].properties["bar"].scalars[0].value == 2.0 assert r["bar"].scalars[0].value == 2.0
def test_read_view(): """Test that properties are passed through to the readview""" pif = System() pif.uid = "10245" pif.names = ["example", "ex"] pif.properties = [Property(name="foo", scalars=1.0), Property(name="bar", scalars=2.0)] r = ReadView(pif) assert r.uid == pif.uid assert r.names == pif.names assert r.properties["foo"].scalars[0].value == 1.0 assert r.properties["bar"].scalars[0].value == 2.0
def test_ambiguity(): """Test that ambiguous keys are removed from the top level dict""" pif = System() pif.uid = "10245" pif.properties = [Property(name="foo", scalars=1.0), Property(name="bar", scalars=2.0)] pif2 = System(sub_systems = [pif,], properties=[Property(name="foo", scalars=10.0)]) r = ReadView(pif2) assert r.properties["foo"].scalars[0].value == 10.0 assert r.sub_systems["10245"].properties["foo"].scalars[0].value == 1.0 assert "foo" not in r.keys() assert r.sub_systems["10245"]["foo"].scalars[0].value == 1.0 assert r["bar"].scalars[0].value == 2.0
def test_nested_read_view(): """Test that nested Pios (system here) are recursively processed""" pif = System() pif.uid = "10245" pif.properties = [Property(name="foo", scalars=1.0), Property(name="bar", scalars=2.0)] pif2 = System(sub_systems=[pif]) r = ReadView(pif2) assert r.sub_systems["10245"].uid == pif.uid assert r["10245"].uid == pif.uid assert r.sub_systems["10245"].properties["foo"].scalars[0].value == 1.0 assert r.sub_systems["10245"].properties["bar"].scalars[0].value == 2.0 assert r["foo"].scalars[0].value == 1.0 assert r["bar"].scalars[0].value == 2.0
def test_read_view(): """Test that properties are passed through to the readview""" pif = System() pif.uid = "10245" pif.names = ["example", "ex"] pif.properties = [ Property(name="foo", scalars=[Scalar(value=1.0)]), Property(name="bar", scalars=[Scalar(value=2.0)]) ] r = ReadView(pif) assert r.uid == pif.uid assert r.names == pif.names assert r.properties["foo"].scalars[0].value == 1.0 assert r.properties["bar"].scalars[0].value == 2.0
def test_nested_read_view(): """Test that nested Pios (system here) are recursively processed""" pif = System() pif.uid = "10245" pif.properties = [ Property(name="foo", scalars=[Scalar(value=1.0)]), Property(name="bar", scalars=[Scalar(value=2.0)]) ] pif2 = System(sub_systems=[pif]) r = ReadView(pif2) assert r.sub_systems["10245"].uid == pif.uid assert r["10245"].uid == pif.uid assert r.sub_systems["10245"].properties["foo"].scalars[0].value == 1.0 assert r.sub_systems["10245"].properties["bar"].scalars[0].value == 2.0 assert r["foo"].scalars[0].value == 1.0 assert r["bar"].scalars[0].value == 2.0
def test_multiple_instances(): """Test that keys that show up in different places with the same value are kept""" pif = System() pif.uid = "10245" pif.properties = [ Property(name="foo", scalars=[Scalar(value=1.0)]), Property(name="bar", scalars=[Scalar(value=2.0)]) ] pif2 = System( sub_systems=[ pif, ], properties=[Property(name="bar", scalars=[Scalar(value=2.0)])]) r = ReadView(pif2) assert r.properties["bar"].scalars[0].value == 2.0 assert r.sub_systems["10245"].properties["bar"].scalars[0].value == 2.0 assert r["bar"].scalars[0].value == 2.0
def test_method_software(): """Testing that method and software names are elevated""" method = Method(name="spam", software=[Software(name="magic")]) pif = System(properties=[ Property(name="foo", scalars=[Scalar(value="bar")], methods=[method]) ]) r = ReadView(pif) assert r["foo"].scalars[0].value == "bar", "Didn't elevate property key" assert "spam" in r.keys(), "Expected spam in keys" assert "magic" in r.keys(), "Expected magic in keys"
def test_condition_elevation(): """Test that read views elevate conditions""" condition = Value(name="spam", scalars=[Scalar(value="eggs")]) pif = System(properties=[ Property( name="foo", scalars=[Scalar(value="bar")], conditions=[condition]) ]) r = ReadView(pif) assert r["foo"].scalars[0].value == "bar", "Didn't elevate property key" assert r["spam"].scalars[0].value == "eggs", "Didn't elevate condition key"
def test_ambiguity(): """Test that ambiguous keys are removed from the top level dict""" pif = System() pif.uid = "10245" pif.properties = [ Property(name="foo", scalars=[Scalar(value=1.0)]), Property(name="bar", scalars=[Scalar(value=2.0)]) ] pif2 = System( sub_systems=[ pif, ], properties=[Property(name="foo", scalars=[Scalar(value=10.0)])]) r = ReadView(pif2) assert r.properties["foo"].scalars[0].value == 10.0 assert r.sub_systems["10245"].properties["foo"].scalars[0].value == 1.0 assert "foo" not in r.keys() assert r.sub_systems["10245"]["foo"].scalars[0].value == 1.0 assert r["bar"].scalars[0].value == 2.0
def test_update_no_conflicts(): """Test that update works when there are no conflicts""" no_conflict_pif = System( names=["gas", "methane"] ) combined = update(test_pif, no_conflict_pif) expected = {"foo": "bar", "spam": 2.7} assert set(x.name for x in combined.properties) == set(expected.keys()) assert set(x.scalars[0].value for x in combined.properties) == set(expected.values()) for prop in combined.properties: assert expected[prop.name] == prop.scalars[0].value assert combined.names == ["gas", "methane"]
def test_update_extend(): """Test that update works when extending with no dups""" more_pif = System( properties=[ Property(name="band gap", scalars=[Scalar(value=1.3)]), Property(name="phase", scalars=[Scalar(value="gas")]), ] ) combined = update(test_pif, more_pif, extend=True) expected = {"band gap": 1.3, "phase": "gas", "foo": "bar", "spam": 2.7} assert set(x.name for x in combined.properties) == set(expected.keys()) assert set(x.scalars[0].value for x in combined.properties) == set(expected.values()) for prop in combined.properties: assert expected[prop.name] == prop.scalars[0].value
def test_update_conflict(): """Test that update works when not extending and a field is redefined""" more_pif = System( properties=[ Property(name="band gap", scalars=[Scalar(value=1.3)]), Property(name="phase", scalars=[Scalar(value="gas")]), ] ) combined = update(test_pif, more_pif) expected = {"band gap": 1.3, "phase": "gas"} assert set(x.name for x in combined.properties) == set(expected.keys()) assert set(x.scalars[0].value for x in combined.properties) == set(expected.values()) for prop in combined.properties: assert expected[prop.name] == prop.scalars[0].value
def test_update_conflicts(): """Test that update works when there are conflicts""" conflict_pif = System( properties=[ Property(name="band gap", scalars=[Scalar(value=1.3)]), Property(name="phase", scalars=[Scalar(value="gas")]), Property(name="spam", scalars=[Scalar(value=2.0)]), ] ) combined = update(test_pif, conflict_pif, extend=True) expected = {"band gap": 1.3, "phase": "gas", "foo": "bar", "spam": 2.0} assert set(x.name for x in combined.properties) == set(expected.keys()) assert set(x.scalars[0].value for x in combined.properties) == set(expected.values()) for prop in combined.properties: assert expected[prop.name] == prop.scalars[0].value
from pypif_sdk.func import update from pypif.obj import System, Property, Scalar test_pif = System( properties=[ Property(name="foo", scalars=[Scalar(value="bar")]), Property(name="spam", scalars=[Scalar(value=2.7)]), ] ) def test_update_no_conflicts(): """Test that update works when there are no conflicts""" no_conflict_pif = System( names=["gas", "methane"] ) combined = update(test_pif, no_conflict_pif) expected = {"foo": "bar", "spam": 2.7} assert set(x.name for x in combined.properties) == set(expected.keys()) assert set(x.scalars[0].value for x in combined.properties) == set(expected.values()) for prop in combined.properties: assert expected[prop.name] == prop.scalars[0].value assert combined.names == ["gas", "methane"] def test_update_conflict(): """Test that update works when not extending and a field is redefined""" more_pif = System(
def test_convert_to_dictionary(): pif = System() pif.uid = "10245" pif.names = ["example", "ex"] d = pif._convert_to_dictionary(pif)
from pypif_sdk.func import replace_by_key, copy from pypif_sdk.accessor import get_property_by_name from pypif.obj import System, Property, Scalar, Value, FileReference test_pif = System(names=["methane"], properties=[ Property(name="foo", scalars=[Scalar(value="bar")]), Property(name="spam", units="eV", scalars=[Scalar(value=2.7)], conditions=[ Value(name="tomato", units="eV", scalars=[Scalar(value=1.0)]) ]), Property( name="image", files=[FileReference(relative_path="/tmp/file.png")]) ]) def test_simple_replace(): """Test replace a single field with default arguments""" prop = get_property_by_name(copy(test_pif), "image") assert prop.files[ 0].relative_path == "/tmp/file.png", "Didn't shorten file name" new_pif = replace_by_key(test_pif, "relative_path", {"/tmp/file.png": "file.png"}) prop = get_property_by_name(new_pif, "image") assert prop.files[ 0].relative_path == "file.png", "Didn't shorten file name"
def test_create_system(): pif = System() pif.uid = "10245" pif.names = ["example", "ex"]