File: test_del_behaviors.py

package info (click to toggle)
python-atom 0.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,616 kB
  • sloc: cpp: 9,040; python: 6,249; makefile: 123
file content (89 lines) | stat: -rw-r--r-- 2,194 bytes parent folder | download
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
# --------------------------------------------------------------------------------------
# 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 the del behaviors

no_op_handler:  not sure it is used
slot_handler: behavior leading to calling the default factory on next get
constant_handler
read_only_handler
event_handler
signal_handler
delegate_handler: not tested here (see test_delegate.py)
property_handler: not tested here (see test_property.py)

"""

import pytest

from atom.api import Atom, Constant, Event, Int, Member, ReadOnly, Signal
from atom.catom import DelAttr


def test_del_noop():
    """Test the noop handler."""
    member = Member()
    member.set_delattr_mode(DelAttr.NoOp, None)

    class A(Atom):
        m = member

    assert A.m.delattr_mode[0] == DelAttr.NoOp
    a = A()
    a.m = 1
    del a.m
    assert a.m == 1
    assert A.m.do_delattr(a) is None
    assert a.m == 1


@pytest.mark.parametrize(
    "member, mode",
    [
        (Event(), DelAttr.Event),
        (Signal(), DelAttr.Signal),
        (ReadOnly(), DelAttr.ReadOnly),
        (Constant(1), DelAttr.Constant),
    ],
)
def test_undeletable(member, mode):
    """Test that unsettable members do raise the proper error."""

    class Undeletable(Atom):
        m = member

    assert Undeletable.m.delattr_mode[0] == mode
    u = Undeletable()
    with pytest.raises(TypeError):
        del u.m
    with pytest.raises(TypeError):
        Undeletable.m.do_delattr(u)


def test_del_slot():
    """Test deleting a member using the slot handler."""

    class DelSlot(Atom):
        i = Int(10)

    a = DelSlot()
    assert a.i == 10

    # Using del statement
    a.i = 0
    del a.i
    assert a.i == 10

    # Using the member do_delattr
    a.i = 0
    DelSlot.i.do_delattr(a)
    assert a.i == 10

    # Test deleting an improperly indexed slot
    DelSlot.i.set_index(DelSlot.i.index + 1)
    with pytest.raises(AttributeError):
        DelSlot.i.do_delattr(a)