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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
from io import StringIO
import pytest
from numpy import float32, float64
from numpy.testing import assert_equal
from brian2 import amp, restore_initial_state, volt
from brian2.core.preferences import (
BrianGlobalPreferences,
BrianGlobalPreferencesView,
BrianPreference,
DefaultValidator,
PreferenceError,
)
@pytest.mark.codegen_independent
def test_defaultvalidator():
# Test that the default validator checks the class
validator = DefaultValidator(5)
assert validator(3)
assert not validator("3")
validator = DefaultValidator("astring")
assert validator("another")
assert not validator(3)
# test that the default validator checks the units
validator = DefaultValidator(3 * volt)
assert validator(2 * volt)
assert not validator(1 * amp)
@pytest.mark.codegen_independent
def test_brianpreference():
# check default args
pref = BrianPreference(1.0 / 3, "docs")
assert not pref.validator(1)
assert pref.docs == "docs"
assert pref.default == 1.0 / 3
assert pref.representor(pref.default) == repr(1.0 / 3)
@pytest.mark.codegen_independent
def test_preference_name_checking():
"""
Test that you cannot set illegal preference names.
"""
gp = BrianGlobalPreferences()
# Name that starts with an underscore
with pytest.raises(PreferenceError):
gp.register_preferences(
"dummy",
"dummy doc",
_notalegalname=BrianPreference(True, "some preference"),
)
# Name that clashes with a method name
with pytest.raises(PreferenceError):
gp.register_preferences(
"dummy", "dummy doc", update=BrianPreference(True, "some preference")
)
gp.register_preferences(
"a", "dummy doc", b=BrianPreference(True, "some preference")
)
# Trying to register a subcategory that would shadow a preference
with pytest.raises(PreferenceError):
gp.register_preferences(
"a.b", "dummy doc", name=BrianPreference(True, "some preference")
)
gp.register_preferences(
"b.c", "dummy doc", name=BrianPreference(True, "some preference")
)
# Trying to register a preference that clashes with an existing category
with pytest.raises(PreferenceError):
gp.register_preferences(
"b", "dummy doc", c=BrianPreference(True, "some preference")
)
@pytest.mark.codegen_independent
def test_brianglobalpreferences():
# test that pre-setting a nonexistent preference in a subsequently
# existing base name raises an error at the correct point
gp = BrianGlobalPreferences()
# This shouldn't work, in user code only registered preferences can be set
with pytest.raises(PreferenceError):
gp.__setitem__("a.b", 5)
# This uses the method that is used when reading preferences from a file
gp._set_preference("a.b", 5)
gp._set_preference("a.c", 5)
with pytest.raises(PreferenceError):
gp.register_preferences("a", "docs for a", b=BrianPreference(5, "docs for b"))
# test that post-setting a nonexistent preference in an existing base
# name raises an error
gp = BrianGlobalPreferences()
gp.register_preferences("a", "docs for a", b=BrianPreference(5, "docs for b"))
with pytest.raises(PreferenceError):
gp.__setitem__("a.c", 5)
# Test pre and post-setting some correct names but valid and invalid values
gp = BrianGlobalPreferences()
gp._set_preference("a.b", 5)
gp.register_preferences(
"a",
"docs for a",
b=BrianPreference(5, "docs for b"),
c=BrianPreference(1 * volt, "docs for c"),
d=BrianPreference(0, "docs for d", validator=lambda x: x >= 0),
e=BrianPreference(float64, "docs for e", representor=lambda x: x.__name__),
)
assert gp["a.c"] == 1 * volt
gp["a.c"] = 2 * volt
with pytest.raises(PreferenceError):
gp.__setitem__("a.c", 3 * amp)
gp["a.d"] = 2.0
with pytest.raises(PreferenceError):
gp.__setitem__("a.d", -1)
gp["a.e"] = float32
with pytest.raises(PreferenceError):
gp.__setitem__("a.e", 0)
# test backup and restore
gp._backup()
gp["a.d"] = 10
assert gp["a.d"] == 10
gp._restore()
assert gp["a.d"] == 2.0
# test that documentation and as_file generation runs without error, but
# don't test for values because we might change the organisation of it
assert len(gp.get_documentation())
gp.as_file
gp.defaults_as_file
# test that reading a preference file works as expected
pref_file = StringIO(
"""
# a comment
a.b = 10
[a]
c = 5*volt
d = 1
e = float64
"""
)
gp.read_preference_file(pref_file)
assert gp["a.b"] == 10
assert gp["a.c"] == 5 * volt
assert gp["a.d"] == 1
assert gp["a.e"] == float64
# test that reading a badly formatted prefs file fails
pref_file = StringIO(
"""
[a
b = 10
"""
)
with pytest.raises(PreferenceError):
gp.read_preference_file(pref_file)
# test that reading a well formatted prefs file with an invalid value fails
pref_file = StringIO(
"""
a.b = 'oh no, not a string'
"""
)
with pytest.raises(PreferenceError):
gp.read_preference_file(pref_file)
# assert that writing the prefs to a file and loading them gives the
# same values
gp = BrianGlobalPreferences()
gp.register_preferences(
"a",
"docs for a",
b=BrianPreference(5, "docs for b"),
)
gp._backup()
gp["a.b"] = 10
str_modified = gp.as_file
str_defaults = gp.defaults_as_file
gp["a.b"] = 15
gp.read_preference_file(StringIO(str_modified))
assert gp["a.b"] == 10
gp.read_preference_file(StringIO(str_defaults))
assert gp["a.b"] == 5
# check that load_preferences works, but nothing about its values
gp = BrianGlobalPreferences()
gp.load_preferences()
# Check that resetting to default preferences works
gp = BrianGlobalPreferences()
gp.register_preferences("a", "docs for a", b=BrianPreference(5, "docs for b"))
assert gp["a.b"] == 5
gp["a.b"] = 7
assert gp["a.b"] == 7
gp.reset_to_defaults()
assert gp["a.b"] == 5
@pytest.mark.codegen_independent
def test_preference_name_access():
"""
Test various ways of accessing preferences
"""
gp = BrianGlobalPreferences()
gp.register_preferences(
"main", "main category", name=BrianPreference(True, "some preference")
)
gp.register_preferences(
"main.sub", "subcategory", name2=BrianPreference(True, "some preference")
)
gp.register_preferences("main.sub_no_pref", "subcategory without preference")
gp.register_preferences(
"main.sub_no_pref.sub",
"deep subcategory",
name=BrianPreference(True, "some preference"),
)
# Keyword based access
assert gp["main.name"]
assert gp["main.sub.name2"]
assert gp["main.sub_no_pref.sub.name"]
gp["main.name"] = False
gp["main.sub.name2"] = False
gp["main.sub_no_pref.sub.name"] = False
# Attribute based access
assert not gp.main.name # we set it to False above
assert not gp.main.sub.name2
assert not gp.main.sub_no_pref.sub.name
gp.main.name = True
gp.main.sub.name2 = True
gp.main.sub_no_pref.sub.name = True
# Mixed access
assert gp.main["name"]
assert gp["main"].name
assert gp.main["sub"].name2
assert gp["main"].sub["name2"]
# Accessing categories
assert isinstance(gp["main"], BrianGlobalPreferencesView)
assert isinstance(gp["main.sub"], BrianGlobalPreferencesView)
assert isinstance(gp.main, BrianGlobalPreferencesView)
assert isinstance(gp.main.sub, BrianGlobalPreferencesView)
# Setting categories shouldn't work
with pytest.raises(PreferenceError):
gp.__setitem__("main", None)
with pytest.raises(PreferenceError):
gp.__setattr__("main", None)
with pytest.raises(PreferenceError):
gp.main.__setitem__("sub", None)
with pytest.raises(PreferenceError):
gp.main.__setattr__("sub", None)
# Neither should deleting categories or preferences
with pytest.raises(PreferenceError):
gp.__delitem__("main")
with pytest.raises(PreferenceError):
gp.__delattr__("main")
with pytest.raises(PreferenceError):
gp.main.__delitem__("name")
with pytest.raises(PreferenceError):
gp.main.__delattr__("name")
with pytest.raises(PreferenceError):
gp.main.__delitem__("sub")
with pytest.raises(PreferenceError):
gp.main.__delattr__("sub")
# Errors for accessing non-existing preferences
with pytest.raises(KeyError):
gp["main.doesnotexist"]
with pytest.raises(KeyError):
gp["nonexisting.name"]
# Note that it is important to raise AttributeError here, since some tools like
# Google Collab check for a `.shape` attribute to display debugging information
# for numpy arrays. More generally, a call like `hasattr(gp, "main.doesnotexist")`
# would fail with an error instead of returning `False`
with pytest.raises(AttributeError):
gp.main.doesnotexist
with pytest.raises(AttributeError):
gp.nonexisting.name
# Check dictionary functionality
for name, value in gp.items():
assert gp[name] == value
for name, value in gp.main.items():
assert gp.main[name] == value
assert len(gp) == 3 # three preferences in total
assert len(gp["main"]) == 3 # all preferences are in the main category
assert len(gp["main.sub"]) == 1 # one preference in main.sub
assert "main.name" in gp
assert "name" in gp["main"]
assert "name2" in gp["main.sub"]
assert not "name" in gp["main.sub"]
gp["main.name"] = True
gp.update({"main.name": False})
assert not gp["main.name"]
gp.main.update({"name": True})
assert gp["main.name"]
# Class based functionality
assert "main" in dir(gp)
assert "sub" in dir(gp.main)
assert "name" in dir(gp.main)
# Check that the fiddling with getattr and setattr did not destroy the
# access to standard attributes
assert len(gp.prefs)
assert gp.main._basename == "main"
# Check that acessing internal attributes work
assert len(gp.prefs.__doc__)
@pytest.mark.codegen_independent
def test_str_repr():
# Just test whether str and repr do not throw an error and return something
gp = BrianGlobalPreferences()
gp.register_preferences(
"main", "main category", name=BrianPreference(True, "some preference")
)
assert len(str(gp))
assert len(repr(gp))
assert len(str(gp.main))
assert len(repr(gp.main))
if __name__ == "__main__":
for t in [
test_defaultvalidator,
test_brianpreference,
test_brianglobalpreferences,
test_preference_name_checking,
test_preference_name_access,
]:
t()
restore_initial_state()
|