File: test_yaml.py

package info (click to toggle)
python-cobra 0.29.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,512 kB
  • sloc: python: 14,703; xml: 12,841; makefile: 137; sh: 32
file content (34 lines) | stat: -rw-r--r-- 1,121 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""Test functionalities of I/O in YAML format."""

import json
from pathlib import Path
from typing import Callable

import pytest
from ruamel.yaml import YAML

from cobra import Model
from cobra import io as cio


def test_load_yaml_model(
    compare_models: Callable, cobra_data_directory: Path, mini_model: Model
) -> None:
    """Test the reading of YAML model."""
    yaml_model = cio.load_yaml_model(cobra_data_directory.joinpath("mini.yml"))
    assert compare_models(mini_model, yaml_model) is None


@pytest.mark.xfail(reason="schema outdated")
def test_save_yaml_model(tmp_path: Path, mini_model: Model) -> None:
    """Test the writing of YAML model."""
    jsonschema = pytest.importorskip("jsonschema")
    output_file = tmp_path.joinpath("mini.yml")
    cio.save_yaml_model(mini_model, str(output_file), sort=True)
    # validate against JSONSchema
    yaml = YAML(typ="unsafe")
    with open(output_file, "r") as infile:
        yaml_to_dict = yaml.load(infile)
    dict_to_json = json.dumps(yaml_to_dict)
    loaded = json.loads(dict_to_json)
    assert jsonschema.validate(loaded, cio.json.json_schema)