2018-09-03 11:52:38 +00:00
|
|
|
import pytest
|
|
|
|
|
2018-12-20 10:31:55 +00:00
|
|
|
from corrscope.gui.data_bind import rgetattr, rsetattr, rhasattr
|
2018-09-03 11:52:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_rgetattr():
|
|
|
|
""" Test to ensure recursive model access works.
|
|
|
|
GUI elements are named "prefix__" "recursive__attr" and bind to recursive.attr.
|
|
|
|
|
|
|
|
https://stackoverflow__com/a/31174427/
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Person(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.pet = Pet()
|
|
|
|
self.residence = Residence()
|
|
|
|
|
|
|
|
class Pet(object):
|
2019-01-03 08:57:30 +00:00
|
|
|
def __init__(self, name="Fido", species="Dog"):
|
2018-09-03 11:52:38 +00:00
|
|
|
self.name = name
|
|
|
|
self.species = species
|
|
|
|
|
|
|
|
class Residence(object):
|
2019-01-03 08:57:30 +00:00
|
|
|
def __init__(self, type="House", sqft=None):
|
2018-09-03 11:52:38 +00:00
|
|
|
self.type = type
|
|
|
|
self.sqft = sqft
|
|
|
|
|
|
|
|
p = Person()
|
|
|
|
|
|
|
|
# Test rgetattr(present)
|
2019-01-03 08:57:30 +00:00
|
|
|
assert rgetattr(p, "pet__species") == "Dog"
|
|
|
|
assert rgetattr(p, "pet__species", object()) == "Dog"
|
2018-09-03 11:52:38 +00:00
|
|
|
|
|
|
|
# Test rgetattr(missing)
|
2019-01-03 08:57:30 +00:00
|
|
|
assert rgetattr(p, "pet__ghost__species", "calico") == "calico"
|
2018-09-03 11:52:38 +00:00
|
|
|
with pytest.raises(AttributeError):
|
|
|
|
# Without a default argument, `rgetattr`, like `getattr`, raises
|
|
|
|
# AttributeError when the dotted attribute is missing
|
2019-01-03 08:57:30 +00:00
|
|
|
print(rgetattr(p, "pet__ghost__species"))
|
2018-09-03 11:52:38 +00:00
|
|
|
|
|
|
|
# Test rsetattr()
|
2019-01-03 08:57:30 +00:00
|
|
|
rsetattr(p, "pet__name", "Sparky")
|
|
|
|
rsetattr(p, "residence__type", "Apartment")
|
|
|
|
assert p.pet.name == "Sparky"
|
|
|
|
assert p.residence.type == "Apartment"
|
2018-09-03 11:52:38 +00:00
|
|
|
|
|
|
|
# Test rhasattr()
|
2019-01-03 08:57:30 +00:00
|
|
|
assert rhasattr(p, "pet")
|
|
|
|
assert rhasattr(p, "pet__name")
|
2018-09-03 11:52:38 +00:00
|
|
|
|
|
|
|
# Test rhasattr(levels of missing)
|
2019-01-03 08:57:30 +00:00
|
|
|
assert not rhasattr(p, "pet__ghost")
|
|
|
|
assert not rhasattr(p, "pet__ghost__species")
|
|
|
|
assert not rhasattr(p, "ghost")
|
|
|
|
assert not rhasattr(p, "ghost__species")
|
2019-01-25 06:45:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.xfail(
|
|
|
|
reason="rgetattr copied from Stack Overflow and subtly broken", strict=True
|
|
|
|
)
|
|
|
|
def test_rgetattr_broken():
|
|
|
|
"""
|
|
|
|
rgetattr(default) fails to short-circuit/return on the first missing attribute.
|
|
|
|
I never use rgetattr(default) so I won't bother fixing the bug.
|
|
|
|
|
|
|
|
Wrong answer:
|
|
|
|
- None.foo AKA 1
|
|
|
|
- 1.bar AKA 1
|
|
|
|
- 1.imag == 0
|
|
|
|
|
|
|
|
Right answer:
|
|
|
|
- None.foo AKA return 1 to caller
|
|
|
|
"""
|
|
|
|
|
|
|
|
result = rgetattr(None, "foo__bar__imag", 1)
|
|
|
|
assert result == 1, result
|