dictionaries: tests

The code in dictionaries/tests/test_dictionaries.py from dictionaries

dictionaries/tests/test_dictionaries.py
 1import unittest
 2
 3
 4class TestDictionaries(unittest.TestCase):
 5
 6    def test_making_a_dictionary(self):
 7        self.assertEqual(dict(), {})
 8        self.assertEqual(dict(key='value'), {'key': 'value'})
 9
10    def test_making_a_dictionary_w_none_as_a_key(self):
11        self.assertEqual({None: 'boom'}, {None: 'boom'})
12
13    def test_making_a_dictionary_w_a_boolean_as_a_key(self):
14        self.assertEqual(
15            {False: 'boom', True: 'bap'},
16            {False: 'boom', True: 'bap'}
17        )
dictionaries/tests/test_dictionaries.py
19    def test_making_a_dictionary_w_a_number_as_a_key(self):
20        self.assertEqual(
21            {0: 'boom', 0.1: 'bap'},
22            {0: 'boom', 0.1: 'bap'}
23        )
24
25    def test_making_a_dictionary_w_a_tuple_as_a_key(self):
26        self.assertEqual(
27            {(0, 1): 'boom'},
28            {(0, 1): 'boom'}
29        )
dictionaries/tests/test_dictionaries.py
31    def test_making_a_dictionary_w_a_list_as_a_key(self):
32        with self.assertRaises(TypeError):
33            {[3, 2, 1]: 'BOOM!!!'}
34
35    def test_making_a_dictionary_w_a_set_as_a_key(self):
36        with self.assertRaises(TypeError):
37            {{3, 2, 1}: 'BOOM!!!'}
38
39    def test_making_a_dictionary_w_a_dictionary_as_a_key(self):
40        a_dictionary = {'key': 'value'}
41        with self.assertRaises(TypeError):
42            {a_dictionary: 'BOOM!!!'}
dictionaries/tests/test_dictionaries.py
44    def test_dir_dict(self):
45        self.maxDiff = None
46        dir_dict = dir(object)
47        dir_dict.extend((
48            '__class_getitem__', '__contains__',
49            '__delitem__', '__getitem__', '__ior__',
50            '__iter__', '__len__', '__or__',
51            '__reversed__', '__ror__', '__setitem__',
52            'clear', 'copy', 'fromkeys',
53            'get', 'items', 'keys',
54            'pop', 'popitem', 'setdefault',
55            'update', 'values'
56        ))
57        dir_dict.sort()
58        self.assertEqual(
59            dir(dict),
60            dir_dict
61        )
dictionaries/tests/test_dictionaries.py
63    def test_clear_empties_a_dictionary(self):
64        a_dictionary = {'key': 'value'}
65        self.assertIsNone(a_dictionary.clear())
66        self.assertEqual(a_dictionary, {})
67
68    def test_copy_a_dictionary(self):
69        a_dictionary = {'key': 'value'}
70        self.assertEqual(
71            a_dictionary.copy(),
72            {'key': 'value'}
73        )
dictionaries/tests/test_dictionaries.py
75    def test_fromkeys_makes_a_dictionary_from_an_iterable(self):
76        self.assertEqual(
77            dict.fromkeys((0, 1)),
78            {0: None, 1: None}
79        )
80        self.assertEqual(
81            dict.fromkeys((0, 1), 'default'),
82            {0: 'default', 1: 'default'}
83        )
84        self.assertEqual(
85            {key: 'value' for key in (0, 1)},
86            {0: 'value', 1: 'value'}
87        )
dictionaries/tests/test_dictionaries.py
 89    def test_get_value_of_a_key_in_a_dictionary(self):
 90        a_dictionary = {'key': 'value'}
 91        self.assertIsNone(
 92            a_dictionary.get('not in dictionary')
 93        )
 94        self.assertEqual(
 95            a_dictionary.get(
 96                'not in dictionary', 'default'
 97            ),
 98            'default'
 99        )
100        self.assertEqual(
101            a_dictionary.get('key', 'default'),
102            'value'
103        )
dictionaries/tests/test_dictionaries.py
105    def test_items_returns_iterable_of_key_value_pairs_of_a_dictionary(self):
106        a_dictionary = {
107            'key0': 'value0',
108            'keyN': [0, 1, 2, 'n'],
109        }
110        self.assertEqual(
111            list(a_dictionary.items()),
112            [
113                ('key0', 'value0'),
114                ('keyN', [0, 1, 2, 'n']),
115            ]
116        )
dictionaries/tests/test_dictionaries.py
118    def test_keys_of_a_dictionary(self):
119        a_dictionary = {
120            'key0': 'value0',
121            'keyN': [0, 1, 2, 'n'],
122        }
123        self.assertEqual(
124            list(a_dictionary.keys()),
125            ['key0', 'keyN']
126        )
dictionaries/tests/test_dictionaries.py
128    def test_pop_removes_given_key_from_a_dictionary_and_returns_its_value(self):
129        a_dictionary = {'key': 'value'}
130
131        with self.assertRaises(KeyError):
132            a_dictionary.pop('not_in_dictionary')
133
134        self.assertIsNone(
135            a_dictionary.pop('not_in_dictionary',  None)
136        )
137        self.assertEqual(
138            a_dictionary.pop('not_in_dictionary', 'default'),
139            'default'
140        )
141        self.assertEqual(
142            a_dictionary.pop('key', 'default'),
143            'value'
144        )
145        self.assertEqual(a_dictionary, {})
dictionaries/tests/test_dictionaries.py
147    def test_popitem_removes_and_returns_last_key_value_pair_from_a_dictionary(self):
148        a_dictionary = {
149            'key0': 'value0',
150            'keyN': [0, 1, 2, 'n'],
151        }
152        self.assertEqual(
153            a_dictionary.popitem(),
154            ('keyN', [0, 1, 2, 'n'])
155        )
156        self.assertEqual(a_dictionary, {'key0': 'value0'})
dictionaries/tests/test_dictionaries.py
158    def test_setdefault_adds_given_key_to_a_dictionary(self):
159        a_dictionary = {'key': 'value'}
160        self.assertEqual(
161            a_dictionary.setdefault('new_key', 'new value'),
162            'new value'
163        )
164        self.assertEqual(
165            a_dictionary.setdefault('key', 'default'),
166            'value'
167        )
168        self.assertEqual(
169            a_dictionary,
170            {
171                'key': 'value',
172                'new_key': 'new value',
173            }
174        )
dictionaries/tests/test_dictionaries.py
176    def test_update_a_dictionary(self):
177        a_dictionary = {'key': 'value'}
178
179        for key, value in (
180            ('key', 'updated value'),
181            ('new_key', [0, 1, 2, 'n']),
182            ('another_key', {0, 1, 2, 'n'}),
183        ):
184            self.assertIsNone(
185                a_dictionary.update({key: value})
186            )
187
188        self.assertEqual(
189            a_dictionary,
190            {
191                'key': 'updated value',
192                'new_key': [0, 1, 2, 'n'],
193                'another_key': {0, 1, 2, 'n'},
194            }
195        )
dictionaries/tests/test_dictionaries.py
197    def test_values_of_a_dictionary(self):
198        a_dictionary = {
199            'key0': 'value0',
200            'keyN': [0, 1, 2, 'n'],
201        }
202        self.assertEqual(
203            list(a_dictionary.values()),
204            ['value0', [0, 1, 2, 'n']]
205        )
dictionaries/tests/test_dictionaries.py
207    def test_key_error(self):
208        a_dictionary = {'key': 'value'}
209        self.assertEqual(
210            a_dictionary['key'], 'value'
211        )
212
213        with self.assertRaises(KeyError):
214            a_dictionary['not_in_dictionary']
215        self.assertEqual(
216            a_dictionary.get(
217                'not_in_dictionary', 'default'
218            ),
219            'default'
220        )
221
222        with self.assertRaises(KeyError):
223            a_dictionary.pop('not_in_dictionary')
224        self.assertEqual(
225            a_dictionary.pop(
226                'not_in_dictionary', 'default'
227            ),
228            'default'
229        )
230
231        with self.assertRaises(KeyError):
232            {}.popitem()
233
234
235# Exceptions seen
236# AssertionError
237# TypeError
238# NameError
239# KeyError