Skip to content

Commit

Permalink
add more tests for code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasha10 committed Feb 6, 2021
1 parent 3c97fbd commit 71454d1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 10 deletions.
19 changes: 11 additions & 8 deletions omegaconf/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from dataclasses import dataclass
from typing import Any, Optional, get_type_hints

import attr

from omegaconf._utils import (
_resolve_forward,
_resolve_optional,
Expand Down Expand Up @@ -54,24 +52,29 @@ def get_dataclass_ir(obj: Any) -> IRNode:


def get_attr_ir(obj: Any) -> IRNode:
import attr
import attr._make

from omegaconf.omegaconf import MISSING

resolved_hints = get_type_hints(get_type_of(obj))
assert is_attr_class(obj)
obj_type = get_type_of(obj)
children = []
for name in attr.fields_dict(obj).keys():
for name, attrib in attr.fields_dict(obj).items():
# for fld in dataclasses.fields(obj):
# name = fld.name
opt, type_ = _resolve_optional(resolved_hints[name])
type_ = _resolve_forward(type_, obj_type.__module__)

if hasattr(obj, name):
value = getattr(obj, name)
if value == attr.NOTHING:
value = MISSING
else:
assert not hasattr(obj, name) # no test coverage for this case yet
if attrib.default == attr.NOTHING:
value = MISSING
elif isinstance(attrib.default, attr._make.Factory):
assert not attrib.default.takes_self, "not supported yet"
value = attrib.default.factory()
else:
value = attrib.default
ir = IRNode(name=name, type=type_, opt=opt, val=value)
children.append(ir)

Expand Down
19 changes: 19 additions & 0 deletions tests/ir/data/attr.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import attr
from attr import NOTHING as backend_MISSING

from omegaconf import MISSING

Expand All @@ -13,3 +14,21 @@ class User:
class UserWithMissing:
name: str = MISSING
age: int = MISSING


@attr.s(auto_attribs=True)
class UserWithBackendMissing:
name: str = backend_MISSING # type: ignore
age: int = backend_MISSING # type: ignore


@attr.s(auto_attribs=True)
class UserWithDefault:
name: str = "bond"
age: int = 7


@attr.s(auto_attribs=True)
class UserWithDefaultFactory:
name: str = attr.ib(factory=lambda: "bond")
age: int = attr.ib(factory=lambda: 7)
21 changes: 20 additions & 1 deletion tests/ir/data/dataclass.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from dataclasses import dataclass
from dataclasses import MISSING as backend_MISSING
from dataclasses import dataclass, field

from omegaconf import MISSING

Expand All @@ -13,3 +14,21 @@ class User:
class UserWithMissing:
name: str = MISSING
age: int = MISSING


@dataclass
class UserWithBackendMissing:
name: str = backend_MISSING # type: ignore
age: int = backend_MISSING # type: ignore


@dataclass
class UserWithDefault:
name: str = "bond"
age: int = 7


@dataclass
class UserWithDefaultFactory:
name: str = field(default_factory=lambda: "bond")
age: int = field(default_factory=lambda: 7)
44 changes: 43 additions & 1 deletion tests/ir/test_ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from importlib import import_module
from typing import Any

from pytest import fixture
from pytest import fixture, raises

from omegaconf import MISSING
from omegaconf.ir import IRNode, get_structured_config_ir
Expand Down Expand Up @@ -51,6 +51,42 @@ def module(request: Any) -> Any:
],
),
),
(
"UserWithBackendMissing",
IRNode(
name=None,
type="UserWithBackendMissing",
opt=False,
val=[
IRNode(name="name", type=str, opt=False, val=MISSING),
IRNode(name="age", type=int, opt=False, val=MISSING),
],
),
),
(
"UserWithDefault",
IRNode(
name=None,
type="UserWithDefault",
opt=False,
val=[
IRNode(name="name", type=str, opt=False, val="bond"),
IRNode(name="age", type=int, opt=False, val=7),
],
),
),
(
"UserWithDefaultFactory",
IRNode(
name=None,
type="UserWithDefaultFactory",
opt=False,
val=[
IRNode(name="name", type=str, opt=False, val="bond"),
IRNode(name="age", type=int, opt=False, val=7),
],
),
),
],
ids=lambda x: x[0], # type: ignore
)
Expand All @@ -63,3 +99,9 @@ def tested_type(module: Any, request: Any) -> Any:

def test_get_dataclass_ir(tested_type: Any) -> None:
assert get_structured_config_ir(tested_type["type"]) == tested_type["expected"]


def test_get_structured_config_ir_rejects_nonstructured() -> None:
"""`get_structured_config_ir` should reject input that is not structured"""
with raises(ValueError):
get_structured_config_ir(object())

0 comments on commit 71454d1

Please sign in to comment.