File: test_mutable_overlay_mapping.py

package info (click to toggle)
matrix-synapse 1.143.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 79,852 kB
  • sloc: python: 258,912; javascript: 7,330; sql: 4,733; sh: 1,281; perl: 626; makefile: 207
file content (189 lines) | stat: -rw-r--r-- 5,881 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
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
#
# This file is licensed under the Affero General Public License (AGPL) version 3.
#
# Copyright (C) 2025 New Vector, Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# See the GNU Affero General Public License for more details:
# <https://www.gnu.org/licenses/agpl-3.0.html>.
#

import unittest

from synapse.util import MutableOverlayMapping


class TestMutableOverlayMapping(unittest.TestCase):
    """Tests for the MutableOverlayMapping class."""

    def test_init(self) -> None:
        """Test initialization with different input types."""
        # Test with empty dict
        empty_dict: dict[str, int] = {}
        mapping = MutableOverlayMapping(empty_dict)
        self.assertEqual(len(mapping), 0)

        # Test with populated dict
        populated_dict = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(populated_dict)
        self.assertEqual(len(mapping), 3)
        self.assertEqual(mapping["a"], 1)

    def test_get_item(self) -> None:
        """Test getting items from the mapping."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        # Get from underlying map
        self.assertEqual(mapping["a"], 1)
        self.assertEqual(mapping["b"], 2)

        # Check KeyError for non-existent key
        with self.assertRaises(KeyError):
            mapping["d"]

    def test_set_item(self) -> None:
        """Test setting items in the mapping."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        # Set new key
        mapping["d"] = 4
        self.assertEqual(mapping["d"], 4)

        # Override existing key
        mapping["a"] = 10
        self.assertEqual(mapping["a"], 10)

        # Original map should be unchanged
        self.assertEqual(underlying["a"], 1)
        self.assertNotIn("d", underlying)

    def test_del_item(self) -> None:
        """Test deleting items from the mapping."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        # Delete a key
        del mapping["a"]
        with self.assertRaises(KeyError):
            mapping["a"]

        # Original map should be unchanged
        self.assertEqual(underlying["a"], 1)

        # Delete non-existent key
        with self.assertRaises(KeyError):
            del mapping["d"]

    def test_len(self) -> None:
        """Test the len() function."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        self.assertEqual(len(mapping), 3)

        # Add a new key
        mapping["d"] = 4
        self.assertEqual(len(mapping), 4)

        # Override an existing key
        mapping["a"] = 10
        self.assertEqual(len(mapping), 4)

        # Delete a key
        del mapping["b"]
        self.assertEqual(len(mapping), 3)

        # Delete a key in mutable map
        del mapping["d"]
        self.assertEqual(len(mapping), 2)

    def test_iteration(self) -> None:
        """Test iteration over the mapping."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        # Add a new key and override an existing one
        mapping["d"] = 4
        mapping["a"] = 10

        # Delete a key
        del mapping["c"]

        iterated_keys = set()
        for k in mapping:
            iterated_keys.add(k)

        # Expected keys: a, b, d (c is deleted)
        self.assertEqual(iterated_keys, {"a", "b", "d"})

        iterated_items = dict(mapping.items())
        self.assertDictEqual(iterated_items, {"a": 10, "b": 2, "d": 4})

    def test_clear(self) -> None:
        """Test the clear method."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        # Add a new key and override an existing one
        mapping["d"] = 4
        mapping["a"] = 10

        # Clear the mapping
        mapping.clear()
        self.assertEqual(len(mapping), 0)

        # All keys should be gone
        with self.assertRaises(KeyError):
            mapping["a"]

        with self.assertRaises(KeyError):
            mapping["d"]

        # Adding a new key after clearing
        mapping["b"] = 2
        self.assertEqual(mapping["b"], 2)
        self.assertEqual(len(mapping), 1)

        # The underlying map should remain unchanged
        self.assertDictEqual(underlying, {"a": 1, "b": 2, "c": 3})

    def test_dict_methods(self) -> None:
        """Test standard dict methods."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        # Test keys, values, and items
        self.assertEqual(set(mapping.keys()), {"a", "b", "c"})
        self.assertEqual(set(mapping.values()), {1, 2, 3})
        self.assertEqual(set(mapping.items()), {("a", 1), ("b", 2), ("c", 3)})

        # Modify, then test again
        mapping["d"] = 4
        mapping["a"] = 10
        del mapping["c"]

        self.assertEqual(set(mapping.keys()), {"a", "b", "d"})
        self.assertEqual(set(mapping.values()), {10, 2, 4})
        self.assertEqual(set(mapping.items()), {("a", 10), ("b", 2), ("d", 4)})

    def test_key_presence(self) -> None:
        """Test checking if keys exist in the mapping."""
        underlying = {"a": 1, "b": 2, "c": 3}
        mapping = MutableOverlayMapping(underlying)

        mapping["d"] = 4
        mapping["a"] = 10
        del mapping["c"]

        # Test key presence
        self.assertIn("a", mapping)
        self.assertIn("b", mapping)
        self.assertNotIn("c", mapping)
        self.assertIn("d", mapping)
        self.assertNotIn("e", mapping)