File size: 5,511 Bytes
e40d9d0 |
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# -*- coding: utf-8 -*-
import io
import os
import pathlib
import pickle
import tempfile
from pathlib import Path
from textwrap import dedent
from typing import Any, Dict, Type
import pytest
from omegaconf import MISSING, DictConfig, OmegaConf
from . import PersonA, PersonD
def save_load_from_file(conf: Any, resolve: bool, expected: Any) -> None:
if expected is None:
expected = conf
try:
with tempfile.NamedTemporaryFile(
mode="wt", delete=False, encoding="utf-8"
) as fp:
OmegaConf.save(conf, fp.file, resolve=resolve) # type: ignore
with io.open(os.path.abspath(fp.name), "rt", encoding="utf-8") as handle:
c2 = OmegaConf.load(handle)
assert c2 == expected
finally:
os.unlink(fp.name)
def save_load_from_filename(
conf: Any, resolve: bool, expected: Any, file_class: Type[Any]
) -> None:
if expected is None:
expected = conf
# note that delete=False here is a work around windows incompetence.
try:
with tempfile.NamedTemporaryFile(delete=False) as fp:
filepath = file_class(fp.name)
OmegaConf.save(conf, filepath, resolve=resolve)
c2 = OmegaConf.load(filepath)
assert c2 == expected
finally:
os.unlink(fp.name)
def test_load_from_invalid() -> None:
with pytest.raises(TypeError):
OmegaConf.load(3.1415) # type: ignore
@pytest.mark.parametrize(
"input_,resolve,expected,file_class",
[
({"a": 10}, False, None, str),
({"foo": 10, "bar": "${foo}"}, False, None, str),
({"foo": 10, "bar": "${foo}"}, False, None, pathlib.Path),
({"foo": 10, "bar": "${foo}"}, False, {"foo": 10, "bar": 10}, str),
([u"שלום"], False, None, str),
],
)
class TestSaveLoad:
def test_save_load__from_file(
self,
input_: Dict[str, Any],
resolve: bool,
expected: Any,
file_class: Type[Any],
) -> None:
cfg = OmegaConf.create(input_)
save_load_from_file(cfg, resolve, expected)
def test_save_load__from_filename(
self,
input_: Dict[str, Any],
resolve: bool,
expected: Any,
file_class: Type[Any],
) -> None:
cfg = OmegaConf.create(input_)
save_load_from_filename(cfg, resolve, expected, file_class)
@pytest.mark.parametrize(
"input_,resolve,expected,file_class",
[
(PersonA, False, {"age": 18, "registered": True}, str),
(PersonD, False, {"age": 18, "registered": True}, str),
(PersonA(), False, {"age": 18, "registered": True}, str),
(PersonD(), False, {"age": 18, "registered": True}, str),
],
)
class TestSaveLoadStructured:
def test_save_load__from_file(
self,
input_: Dict[str, Any],
resolve: bool,
expected: Any,
file_class: Type[Any],
) -> None:
save_load_from_file(input_, resolve, expected)
def test_save_load__from_filename(
self,
input_: Dict[str, Any],
resolve: bool,
expected: Any,
file_class: Type[Any],
) -> None:
save_load_from_filename(input_, resolve, expected, file_class)
def test_save_illegal_type() -> None:
with pytest.raises(TypeError):
OmegaConf.save(OmegaConf.create(), 1000) # type: ignore
def test_pickle_dict() -> None:
with tempfile.TemporaryFile() as fp:
c = OmegaConf.create({"a": "b"})
pickle.dump(c, fp)
fp.flush()
fp.seek(0)
c1 = pickle.load(fp)
assert c == c1
def test_pickle_list() -> None:
with tempfile.TemporaryFile() as fp:
c = OmegaConf.create([1, 2, 3])
pickle.dump(c, fp)
fp.flush()
fp.seek(0)
c1 = pickle.load(fp)
assert c == c1
def test_load_duplicate_keys_top() -> None:
from yaml.constructor import ConstructorError
try:
with tempfile.NamedTemporaryFile(delete=False) as fp:
content = dedent(
"""\
a:
b: 1
a:
b: 2
"""
)
fp.write(content.encode("utf-8"))
with pytest.raises(ConstructorError):
OmegaConf.load(fp.name)
finally:
os.unlink(fp.name)
def test_load_duplicate_keys_sub() -> None:
from yaml.constructor import ConstructorError
try:
with tempfile.NamedTemporaryFile(delete=False) as fp:
content = dedent(
"""\
a:
b: 1
c: 2
b: 3
"""
)
fp.write(content.encode("utf-8"))
with pytest.raises(ConstructorError):
OmegaConf.load(fp.name)
finally:
os.unlink(fp.name)
def test_load_empty_file(tmpdir: str) -> None:
empty = Path(tmpdir) / "test.yaml"
empty.touch()
assert OmegaConf.load(empty) == {}
def test_pickle_missing() -> None:
cfg = DictConfig(content=MISSING)
with tempfile.TemporaryFile() as fp:
import pickle
pickle.dump(cfg, fp)
fp.flush()
fp.seek(0)
cfg2 = pickle.load(fp)
assert cfg == cfg2
def test_pickle_none() -> None:
cfg = DictConfig(content=None)
with tempfile.TemporaryFile() as fp:
import pickle
pickle.dump(cfg, fp)
fp.flush()
fp.seek(0)
cfg2 = pickle.load(fp)
assert cfg == cfg2
|