File size: 5,930 Bytes
d1ceb73 |
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 |
import collections
import pickle
import pytest
import networkx as nx
from networkx.utils.configs import Config
# Define this at module level so we can test pickling
class ExampleConfig(Config):
"""Example configuration."""
x: int
y: str
def _check_config(self, key, value):
if key == "x" and value <= 0:
raise ValueError("x must be positive")
if key == "y" and not isinstance(value, str):
raise TypeError("y must be a str")
class EmptyConfig(Config):
pass
@pytest.mark.parametrize("cfg", [EmptyConfig(), Config()])
def test_config_empty(cfg):
assert dir(cfg) == []
with pytest.raises(AttributeError):
cfg.x = 1
with pytest.raises(KeyError):
cfg["x"] = 1
with pytest.raises(AttributeError):
cfg.x
with pytest.raises(KeyError):
cfg["x"]
assert len(cfg) == 0
assert "x" not in cfg
assert cfg == cfg
assert cfg.get("x", 2) == 2
assert set(cfg.keys()) == set()
assert set(cfg.values()) == set()
assert set(cfg.items()) == set()
cfg2 = pickle.loads(pickle.dumps(cfg))
assert cfg == cfg2
assert isinstance(cfg, collections.abc.Collection)
assert isinstance(cfg, collections.abc.Mapping)
def test_config_subclass():
with pytest.raises(TypeError, match="missing 2 required keyword-only"):
ExampleConfig()
with pytest.raises(ValueError, match="x must be positive"):
ExampleConfig(x=0, y="foo")
with pytest.raises(TypeError, match="unexpected keyword"):
ExampleConfig(x=1, y="foo", z="bad config")
with pytest.raises(TypeError, match="unexpected keyword"):
EmptyConfig(z="bad config")
cfg = ExampleConfig(x=1, y="foo")
assert cfg.x == 1
assert cfg["x"] == 1
assert cfg["y"] == "foo"
assert cfg.y == "foo"
assert "x" in cfg
assert "y" in cfg
assert "z" not in cfg
assert len(cfg) == 2
assert set(iter(cfg)) == {"x", "y"}
assert set(cfg.keys()) == {"x", "y"}
assert set(cfg.values()) == {1, "foo"}
assert set(cfg.items()) == {("x", 1), ("y", "foo")}
assert dir(cfg) == ["x", "y"]
cfg.x = 2
cfg["y"] = "bar"
assert cfg["x"] == 2
assert cfg.y == "bar"
with pytest.raises(TypeError, match="can't be deleted"):
del cfg.x
with pytest.raises(TypeError, match="can't be deleted"):
del cfg["y"]
assert cfg.x == 2
assert cfg == cfg
assert cfg == ExampleConfig(x=2, y="bar")
assert cfg != ExampleConfig(x=3, y="baz")
assert cfg != Config(x=2, y="bar")
with pytest.raises(TypeError, match="y must be a str"):
cfg["y"] = 5
with pytest.raises(ValueError, match="x must be positive"):
cfg.x = -5
assert cfg.get("x", 10) == 2
with pytest.raises(AttributeError):
cfg.z = 5
with pytest.raises(KeyError):
cfg["z"] = 5
with pytest.raises(AttributeError):
cfg.z
with pytest.raises(KeyError):
cfg["z"]
cfg2 = pickle.loads(pickle.dumps(cfg))
assert cfg == cfg2
assert cfg.__doc__ == "Example configuration."
assert cfg2.__doc__ == "Example configuration."
def test_config_defaults():
class DefaultConfig(Config):
x: int = 0
y: int
cfg = DefaultConfig(y=1)
assert cfg.x == 0
cfg = DefaultConfig(x=2, y=1)
assert cfg.x == 2
def test_nxconfig():
assert isinstance(nx.config.backend_priority, list)
assert isinstance(nx.config.backends, Config)
with pytest.raises(TypeError, match="must be a list of backend names"):
nx.config.backend_priority = "nx_loopback"
with pytest.raises(ValueError, match="Unknown backend when setting"):
nx.config.backend_priority = ["this_almost_certainly_is_not_a_backend"]
with pytest.raises(TypeError, match="must be a Config of backend configs"):
nx.config.backends = {}
with pytest.raises(TypeError, match="must be a Config of backend configs"):
nx.config.backends = Config(plausible_backend_name={})
with pytest.raises(ValueError, match="Unknown backend when setting"):
nx.config.backends = Config(this_almost_certainly_is_not_a_backend=Config())
with pytest.raises(TypeError, match="must be True or False"):
nx.config.cache_converted_graphs = "bad value"
def test_not_strict():
class FlexibleConfig(Config, strict=False):
x: int
cfg = FlexibleConfig(x=1)
assert "_strict" not in cfg
assert len(cfg) == 1
assert list(cfg) == ["x"]
assert list(cfg.keys()) == ["x"]
assert list(cfg.values()) == [1]
assert list(cfg.items()) == [("x", 1)]
assert cfg.x == 1
assert cfg["x"] == 1
assert "x" in cfg
assert hasattr(cfg, "x")
assert "FlexibleConfig(x=1)" in repr(cfg)
assert cfg == FlexibleConfig(x=1)
del cfg.x
assert "FlexibleConfig()" in repr(cfg)
assert len(cfg) == 0
assert not hasattr(cfg, "x")
assert "x" not in cfg
assert not hasattr(cfg, "y")
assert "y" not in cfg
cfg.y = 2
assert len(cfg) == 1
assert list(cfg) == ["y"]
assert list(cfg.keys()) == ["y"]
assert list(cfg.values()) == [2]
assert list(cfg.items()) == [("y", 2)]
assert cfg.y == 2
assert cfg["y"] == 2
assert hasattr(cfg, "y")
assert "y" in cfg
del cfg["y"]
assert len(cfg) == 0
assert list(cfg) == []
with pytest.raises(AttributeError, match="y"):
del cfg.y
with pytest.raises(KeyError, match="y"):
del cfg["y"]
with pytest.raises(TypeError, match="missing 1 required keyword-only"):
FlexibleConfig()
# Be strict when first creating the config object
with pytest.raises(TypeError, match="unexpected keyword argument 'y'"):
FlexibleConfig(x=1, y=2)
class FlexibleConfigWithDefault(Config, strict=False):
x: int = 0
assert FlexibleConfigWithDefault().x == 0
assert FlexibleConfigWithDefault(x=1)["x"] == 1
|