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
|
# --------------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# --------------------------------------------------------------------------------------
"""Test post_validate/get/set behaviors
All of them have the following handlers:
no_op_handler: Do nothing
delegate_handler: not tested here (see test_delegate.py)
object_method_old_new_handler: method defined on the Atom object
object_method_name_old_new_handler: method defined on the Atom object
taking the member name as first arg
member_method_object_old_new_handler: Method defined on a Member subclass
"""
import pytest
from atom.api import Int, PostGetAttr, PostSetAttr, PostValidate
GET_MEMBER_METHOD_SRC = """
from atom.api import Atom
class TrackedInt(Int):
def __init__(self):
super(TrackedInt, self).__init__()
mode = PostGetAttr.{mode}
self.set_post_getattr_mode(mode, 'post_getattr')
def post_getattr(self, obj, value):
obj.counter += 1
class PostAtom(Atom):
mi = TrackedInt()
counter = Int()
"""
GET_OBJECT_METHOD_SRC = """
from atom.api import Atom
class PostAtom(Atom):
mi = Int()
counter = Int()
def _post_getattr_mi(self, value):
self.counter += 1
"""
GET_OBJECT_METHOD_NAME_SRC = """
from atom.api import Atom
class PostAtom(Atom):
mi = Int()
mi.set_post_getattr_mode(getattr(PostGetAttr, 'ObjectMethod_NameValue'),
'post_getattr_mi')
counter = Int()
def post_getattr_mi(self, name, value):
self.counter += 1
"""
@pytest.mark.parametrize(
"mode",
(
"NoOp",
"ObjectMethod_Value",
"ObjectMethod_NameValue",
"MemberMethod_ObjectValue",
),
)
def test_post_getattr(mode):
"""Test the post_getattr_behaviors."""
namespace = {}
namespace.update(globals())
src = (
GET_MEMBER_METHOD_SRC
if mode in ("MemberMethod_ObjectValue", "NoOp")
else GET_OBJECT_METHOD_SRC
if mode == "ObjectMethod_Value"
else GET_OBJECT_METHOD_NAME_SRC
)
src = src.format(mode=mode)
print(src)
exec(src, namespace)
PostAtom = namespace["PostAtom"]
# Test subclassed member
pot = PostAtom()
mi = pot.get_member("mi")
assert mi.post_getattr_mode[0] == getattr(PostGetAttr, mode)
pot.mi
assert pot.counter == (1 if mode != "NoOp" else 0)
# Test do_post_*** method
func = getattr(mi, "do_post_getattr")
func(pot, 2)
assert pot.counter == (2 if mode != "NoOp" else 0)
MEMBER_METHOD_SRC = """
from atom.api import Atom
class TrackedInt(Int):
def __init__(self):
super(TrackedInt, self).__init__()
mode = enum.{mode}
self.set_post_{operation}_mode(mode, 'post_{operation}')
def post_{operation}(self, obj, old, new):
obj.counter += 1
class PostAtom(Atom):
mi = TrackedInt()
counter = Int()
"""
OBJECT_METHOD_SRC = """
from atom.api import Atom
class PostAtom(Atom):
mi = Int()
counter = Int()
def _post_{operation}_mi(self, old, new):
self.counter += 1
"""
OBJECT_METHOD_NAME_SRC = """
from atom.api import Atom
class PostAtom(Atom):
mi = Int()
mi.set_post_{operation}_mode(getattr(enum, 'ObjectMethod_NameOldNew'),
'post_{operation}_mi')
counter = Int()
def post_{operation}_mi(self, name, old, new):
self.counter += 1
"""
@pytest.mark.parametrize(
"operation, enum", [("setattr", PostSetAttr), ("validate", PostValidate)]
)
@pytest.mark.parametrize(
"mode",
(
"NoOp",
"ObjectMethod_OldNew",
"ObjectMethod_NameOldNew",
"MemberMethod_ObjectOldNew",
),
)
def test_post_setattr_validate(operation, enum, mode):
"""Test the post_setattr/validate behaviors."""
namespace = {"enum": enum}
namespace.update(globals())
src = (
MEMBER_METHOD_SRC
if mode in ("MemberMethod_ObjectOldNew", "NoOp")
else OBJECT_METHOD_SRC
if mode == "ObjectMethod_OldNew"
else OBJECT_METHOD_NAME_SRC
)
src = src.format(operation=operation, mode=mode)
print(src)
exec(src, namespace)
PostAtom = namespace["PostAtom"]
# Test subclassed member
pot = PostAtom()
mi = pot.get_member("mi")
assert getattr(mi, "post_{}_mode".format(operation))[0] == getattr(enum, mode)
pot.mi = 2
assert pot.counter == (1 if mode != "NoOp" else 0)
# Test do_post_*** method
func = getattr(mi, "do_post_{}".format(operation))
func(pot, 2, 3)
assert pot.counter == (2 if mode != "NoOp" else 0)
@pytest.mark.parametrize(
"operation, mode, msg",
[
("getattr", PostGetAttr.ObjectMethod_Value, "str"),
("getattr", PostGetAttr.ObjectMethod_NameValue, "str"),
("getattr", PostGetAttr.MemberMethod_ObjectValue, "str"),
("setattr", PostSetAttr.ObjectMethod_OldNew, "str"),
("setattr", PostSetAttr.ObjectMethod_NameOldNew, "str"),
("setattr", PostSetAttr.MemberMethod_ObjectOldNew, "str"),
("validate", PostValidate.ObjectMethod_OldNew, "str"),
("validate", PostValidate.ObjectMethod_NameOldNew, "str"),
("validate", PostValidate.MemberMethod_ObjectOldNew, "str"),
],
)
def test_wrong_argument_in_mode_setting(operation, mode, msg):
"""Test handling wrong argument types when setting mode."""
m = Int()
with pytest.raises(TypeError) as excinfo:
getattr(m, "set_post_{}_mode".format(operation))(mode, 1)
assert msg in excinfo.exconly()
|