def test_load_from_string() -> None:
    load_int_dict = yatiml.load_function(Dict[str, int])
    data = load_int_dict('x: 1')
    assert data['x'] == 1

    load_float_list = yatiml.load_function(List[float])
    data2 = load_float_list('[7.8, 9.1]')
    assert data2[1] == 9.1

    with pytest.raises(yatiml.RecognitionError):
        load_float_list('x: 1')
Beispiel #2
0
def test_load_date() -> None:
    load = yatiml.load_function(date)
    data = load('2018-10-27T06:05:23Z')
    assert isinstance(data, datetime)
    assert data.year == 2018
    assert data.second == 23

    load = yatiml.load_function(date)
    data = load('2020-11-15')
    assert isinstance(data, date)
    assert data.year == 2020
    assert data.month == 11
    assert data.day == 15
Beispiel #3
0
def test_optional() -> None:
    load = yatiml.load_function(Optional[str])  # type: ignore
    data = load('test')
    assert data == 'test'

    data = load('!!null')
    assert data is None
def test_load_settings() -> None:
    load_settings = yatiml.load_function(Settings, Identifier, Reference)

    text = ('domain1._muscle_grain: [0.01]\n'
            'domain1._muscle_extent: [1.5]\n'
            'submodel1._muscle_timestep: 0.001\n'
            'submodel1._muscle_total_time: 100.0\n'
            'test_str: value\n'
            'test_int: 13\n'
            'test_bool: true\n'
            'test_list: [12.3, 1.3]\n')

    settings = load_settings(text)
    assert len(settings) == 8
    assert str(settings.ordered_items()[0][0]) == 'domain1._muscle_grain'
    assert cast(List[float], settings['domain1._muscle_grain'])[0] == 0.01
    assert settings['submodel1._muscle_total_time'] == 100.0

    assert str(settings.ordered_items()[4][0]) == 'test_str'
    assert ([str(s[0]) for s in settings.ordered_items()] == [
        'domain1._muscle_grain', 'domain1._muscle_extent',
        'submodel1._muscle_timestep', 'submodel1._muscle_total_time',
        'test_str', 'test_int', 'test_bool', 'test_list'
    ])
    assert settings['test_bool'] is True
    assert settings['test_list'] == [12.3, 1.3]
Beispiel #5
0
def test_load_model_reference() -> None:
    load = yatiml.load_function(ModelReference, Component, Conduit, Identifier,
                                Model, ModelReference, Reference)

    text = 'name: test_model\n'
    model = load(text)
    assert isinstance(model, ModelReference)
    assert str(model.name) == 'test_model'

    text = ('name: test_model\n'
            'components:\n'
            '  ic: isr2d.initial_conditions\n'
            '  smc: isr2d.smc\n'
            '  bf: isr2d.blood_flow\n'
            '  smc2bf: isr2d.smc2bf\n'
            '  bf2smc: isr2d.bf2smc\n'
            'conduits:\n'
            '  ic.out: smc.initial_state\n'
            '  smc.cell_positions: smc2bf.in\n'
            '  smc2bf.out: bf.initial_domain\n'
            '  bf.wss_out: bf2smc.in\n'
            '  bf2smc.out: smc.wss_in\n')
    model = load(text)
    assert isinstance(model, Model)
    assert str(model.name) == 'test_model'
Beispiel #6
0
def test_extra_attribute() -> None:
    load = yatiml.load_function(Vector2D)
    with pytest.raises(yatiml.RecognitionError):
        load(
                'x: 12.3\n'
                'y: 45.6\n'
                'z: 78.9\n')
Beispiel #7
0
def test_load_mixed_dict_list() -> None:
    load = yatiml.load_function(List[Dict[str, int]])
    data = load('[{key1: 10},{key2: 11, key3: 13}]')
    assert data == [{'key1': 10}, {'key2': 11, 'key3': 13}]

    with pytest.raises(yatiml.RecognitionError):
        load('[{key1: string}]')
Beispiel #8
0
def test_load_complex_document() -> None:
    load = yatiml.load_function(
            Document2, Color2, Shape, Rectangle, Circle, Vector2D)
    text = ('cursor_at:\n'
            '  x: 3.0\n'
            '  y: 4.0\n'
            'shapes:\n'
            '- center:\n'
            '    x: 5.0\n'
            '    y: 6.0\n'
            '  radius: 12.0\n'
            '- center:\n'
            '    x: -2.0\n'
            '    y: -5.0\n'
            '  width: 3.0\n'
            '  height: 7.0\n'
            'color: blue\n'
            'extra_shape:\n'
            '  center:\n'
            '    x: 7.0\n'
            '    y: 8.0\n'
            '  radius: 2.0\n'
            )
    doc = load(text)
    assert isinstance(doc, Document2)
    assert isinstance(doc.shapes, list)
    assert doc.color == Color2.BLUE
Beispiel #9
0
def test_load_bool_object_bool() -> None:
    # On Python 3.7 and up, Union[bool, int] does not collapse to int.
    # So there, this is supposed to work without the work-around.
    if sys.version_info.major > 3 or sys.version_info.minor > 7:
        load = yatiml.load_function(BoolTester)
        data = load('x: true')
        assert isinstance(data.x, bool)
        assert data.x is True
Beispiel #10
0
def test_load_user_string() -> None:
    load = yatiml.load_function(ConstrainedString)
    data = load('abcd\n')
    assert isinstance(data, ConstrainedString)
    assert data == 'abcd'

    with pytest.raises(ValueError):
        load('efgh\n')
Beispiel #11
0
def test_load_union() -> None:
    load = yatiml.load_function(Union[str, int])  # type: ignore
    data = load('10')
    assert isinstance(data, int)
    assert data == 10
    data = load('test')
    assert isinstance(data, str)
    assert data == 'test'
Beispiel #12
0
def test_enum_list() -> None:
    load = yatiml.load_function(List[Color2], Color2)
    data = load(
            '- blue\n'
            '- yellow\n')
    assert isinstance(data[0], Color2)
    assert data[0] == Color2.BLUE
    assert data[1] == Color2.YELLOW
def test_load_from_path(tmpdir_path: Path) -> None:
    tmp_file = tmpdir_path / 'test_load_from_path.yaml'
    with tmp_file.open('w') as f:
        f.write('y: z')

    load_string_dict = yatiml.load_function(Dict[str, str])
    data = load_string_dict(tmp_file)
    assert data['y'] == 'z'
Beispiel #14
0
def test_load_bool_union_bool() -> None:
    # On Python 3.7 and up, Union[bool, int] does not collapse to int.
    # So there, this is supposed to work without the work-around.
    if sys.version_info.major > 3 or sys.version_info.minor >= 7:
        load = yatiml.load_function(Union[int, bool])  # type: ignore
        data = load('true')
        assert isinstance(data, bool)
        assert data is True
Beispiel #15
0
def test_missing_class() -> None:
    load = yatiml.load_function(Shape, Rectangle, Ellipse, Vector2D)
    with pytest.raises(yatiml.RecognitionError):
        load(
                'center:\n'
                '  x: 1.0\n'
                '  y: 2.0\n'
                'radius: 10.0\n')
Beispiel #16
0
def test_missing_attribute_class() -> None:
    # omitting Vector2D from the list here
    load = yatiml.load_function(
            Document2, Color2, Shape, Rectangle, Circle)
    with pytest.raises(yatiml.RecognitionError):
        load(
                'cursor_at:\n'
                '  x: 42.0\n'
                '  y: 42.1\n')
Beispiel #17
0
def test_recognize_subclass() -> None:
    load = yatiml.load_function(Shape, Rectangle, Circle, Ellipse, Vector2D)
    data = load(
            'center:\n'
            '  x: 10.0\n'
            '  y: 12.3\n')
    assert isinstance(data, Shape)
    assert data.center.x == 10.0
    assert data.center.y == 12.3
Beispiel #18
0
def test_yatiml_extra() -> None:
    load = yatiml.load_function(Extensible)
    data = load(
            'a: 10\n'
            'b: test1\n'
            'c: 42\n')
    assert isinstance(data, Extensible)
    assert data.a == 10
    assert data._yatiml_extra['b'] == 'test1'
    assert data._yatiml_extra['c'] == 42
Beispiel #19
0
def test_load_string_like_key() -> None:
    load = yatiml.load_function(Dict[StringLike, int], StringLike)
    data = load('abcd: 10\nefgh: 20\n')
    assert isinstance(data, dict)
    assert len(data) == 2
    assert isinstance(list(data.keys())[0], StringLike)
    assert isinstance(list(data.keys())[1], StringLike)
    assert StringLike('abcd') in data
    assert data[StringLike('abcd')] == 10
    assert data[StringLike('efgh')] == 20
Beispiel #20
0
def test_dict_attribute() -> None:
    load = yatiml.load_function(DictAttribute)
    data = load(
            'a:\n'
            '  b: 10\n'
            '  c: 20\n')
    assert isinstance(data, DictAttribute)
    assert isinstance(data.a, OrderedDict)
    assert data.a['b'] == 10
    assert data.a['c'] == 20
Beispiel #21
0
def test_enum_class() -> None:
    load = yatiml.load_function(Color)
    data = load('blue\n')
    assert isinstance(data, Color)
    assert data == Color.blue

    with pytest.raises(yatiml.RecognitionError):
        load('yelow\n')

    with pytest.raises(yatiml.RecognitionError):
        load('1\n')
Beispiel #22
0
def test_untyped_attributes() -> None:
    load = yatiml.load_function(Document6)
    text = (
            'attr1: test\n'
            'attr2: [12, 76]')
    data = load(text)
    assert isinstance(data, Document6)
    assert isinstance(data.attr1, str)
    assert data.attr1 == 'test'
    assert isinstance(data.attr2, list)
    assert data.attr2 == [12, 76]
Beispiel #23
0
def test_enum_dict() -> None:
    load = yatiml.load_function(Dict[str, Color2], Color2)
    data = load(
            'x: red\n'
            'y: orange\n')
    assert 'x' in data
    assert isinstance(data['x'], Color2)
    assert data['x'] == Color2.RED
    assert 'y' in data
    assert isinstance(data['y'], Color2)
    assert data['y'] == Color2.ORANGE
Beispiel #24
0
def test_optional_attribute() -> None:
    load = yatiml.load_function(
            Document2, Color2, Shape, Rectangle, Circle, Vector2D)
    data = load(
            'cursor_at:\n'
            '  x: 42.0\n'
            '  y: 42.1\n')
    assert isinstance(data, Document2)
    assert data.cursor_at.x == 42.0
    assert data.cursor_at.y == 42.1
    assert data.shapes == []
Beispiel #25
0
def test_disambiguated_union() -> None:
    load = yatiml.load_function(
            Union[Super3, Super3Clone], Super3, Super3Clone)
    with pytest.raises(yatiml.RecognitionError):
        load(
                'attr: X\n')

    data = load(
            '!Super3\n'
            'attr: X\n')
    assert isinstance(data, Super3)
    assert not isinstance(data, Super3Clone)
Beispiel #26
0
def test_load_user_string_key() -> None:
    load = yatiml.load_function(
            Dict[ConstrainedString, int], ConstrainedString)
    data = load('abcd: 10\n')
    assert isinstance(data, dict)
    assert len(data) == 1
    assert isinstance(list(data.keys())[0], ConstrainedString)
    assert ConstrainedString('abcd') in data
    assert data[ConstrainedString('abcd')] == 10

    with pytest.raises(ValueError):
        load('efgh: 4\n')
def test_reference_io() -> None:
    load_reference = yatiml.load_function(Reference, Identifier)

    text = 'test[12]'
    doc = load_reference(text)
    assert str(doc[0]) == 'test'
    assert doc[1] == 12

    dump_reference = yatiml.dumps_function(Identifier, Reference)

    doc = Reference('test[12].testing.ok.index[3][5]')
    text = dump_reference(doc)
    assert text == 'test[12].testing.ok.index[3][5]\n...\n'
def test_load_from_stream(tmpdir_path: Path) -> None:
    tmp_file = tmpdir_path / 'test_load_from_stream.yaml'
    with tmp_file.open('w') as f:
        f.write('testing')

    load_string = yatiml.load_function(str)
    with tmp_file.open('r') as f:
        data = load_string(f)
    assert data == 'testing'

    with tmp_file.open('rb') as f2:
        data = load_string(f2)
    assert data == 'testing'
Beispiel #29
0
def test_yatiml_extra_strip() -> None:
    load = yatiml.load_function(Extensible)
    data = load(
            'a: 10\n'
            'b: test1\n'
            'c: !Extensible\n'
            '  a: 12\n'
            '  b: test2\n')
    assert isinstance(data, Extensible)
    assert data.a == 10
    assert data._yatiml_extra['b'] == 'test1'
    assert not isinstance(data._yatiml_extra['c'], Extensible)
    assert isinstance(data._yatiml_extra['c'], OrderedDict)
    assert data._yatiml_extra['c']['a'] == 12
    assert data._yatiml_extra['c']['b'] == 'test2'
Beispiel #30
0
def test_load_reference():
    load = yatiml.load_function(
            pycff.Reference, pycff.Entity, pycff.Person, pycff.Identifier)

    text = (
            'type: conference-paper\n'
            'doi: 10.1234/123-4-567\n'
            'authors:\n'
            '  - family-names: Doe\n'
            '    given-names: John\n'
            'title: Interesting results\n'
            'year: 2020\n'
            'collection-title: Books on Interesting Results\n'
            'volume: 42\n'
            'volume-title: Proceedings of Interesting Results 2020\n'
            'editors:\n'
            '  - family-names: Doe\n'
            '    given-names: Jane\n'
            'start: 1\n'
            'end: 7\n'
            'publisher:\n'
            '  name: Science \'r Us Ltd.\n'
            '  city: Amsterdam\n')

    ref = load(text)
    assert isinstance(ref, pycff.Reference)
    assert ref.typ == 'conference-paper'
    assert ref.doi == '10.1234/123-4-567'
    assert len(ref.authors) == 1
    assert isinstance(ref.authors[0], pycff.Person)
    assert ref.authors[0].family_names == 'Doe'
    assert ref.authors[0].given_names == 'John'
    assert ref.title == 'Interesting results'
    assert ref.year == 2020
    assert ref.collection_title == 'Books on Interesting Results'
    assert ref.volume == 42
    assert ref.volume_title == 'Proceedings of Interesting Results 2020'
    assert len(ref.editors) == 1
    assert isinstance(ref.editors[0], pycff.Person)
    assert ref.editors[0].family_names == 'Doe'
    assert ref.editors[0].given_names == 'Jane'
    assert ref.start == 1
    assert ref.end == 7
    assert isinstance(ref.publisher, pycff.Entity)
    assert ref.publisher.name == 'Science \'r Us Ltd.'
    assert ref.publisher.city == 'Amsterdam'