Danger

DANGER WILL ROBINSON! This chapter is still UNDER CONSTRUCTION.

It is DEFINITELY full of mistakes and may be completely different when I am done editing it though most of the code should work

data structures: dictionaries: tests

tests

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

  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        )
 18
 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        )
 30
 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!!!'}
 43
 44    def test_attributes_and_methods_of_dictionaries(self):
 45        self.maxDiff = None
 46        self.assertEqual(
 47            dir(dict),
 48            [
 49                '__class__',
 50                '__class_getitem__',
 51                '__contains__',
 52                '__delattr__',
 53                '__delitem__',
 54                '__dir__',
 55                '__doc__',
 56                '__eq__',
 57                '__format__',
 58                '__ge__',
 59                '__getattribute__',
 60                '__getitem__',
 61                '__getstate__',
 62                '__gt__',
 63                '__hash__',
 64                '__init__',
 65                '__init_subclass__',
 66                '__ior__',
 67                '__iter__',
 68                '__le__',
 69                '__len__',
 70                '__lt__',
 71                '__ne__',
 72                '__new__',
 73                '__or__',
 74                '__reduce__',
 75                '__reduce_ex__',
 76                '__repr__',
 77                '__reversed__',
 78                '__ror__',
 79                '__setattr__',
 80                '__setitem__',
 81                '__sizeof__',
 82                '__str__',
 83                '__subclasshook__',
 84                'clear',
 85                'copy',
 86                'fromkeys',
 87                'get',
 88                'items',
 89                'keys',
 90                'pop',
 91                'popitem',
 92                'setdefault',
 93                'update',
 94                'values'
 95            ]
 96        )
 97
 98    def test_clear_empties_a_dictionary(self):
 99        a_dictionary = {'key': 'value'}
100        self.assertIsNone(a_dictionary.clear())
101        self.assertEqual(a_dictionary, {})
102
103    def test_copy_a_dictionary(self):
104        a_dictionary = {'key': 'value'}
105        self.assertEqual(
106            a_dictionary.copy(),
107            {'key': 'value'}
108        )
109
110    def test_fromkeys_makes_a_dictionary_from_an_iterable(self):
111        self.assertEqual(
112            dict.fromkeys((0, 1), 'default'),
113            {0: 'default', 1: 'default'}
114        )
115
116    def test_get_value_of_a_key_in_a_dictionary(self):
117        a_dictionary = {'key': 'value'}
118        self.assertEqual(
119            a_dictionary.get('not_in_dictionary', 'default'),
120            'default'
121        )
122        self.assertEqual(
123            a_dictionary.get('key', 'default'),
124            'value'
125        )
126
127    def test_items_returns_iterable_of_key_value_pairs_of_a_dictionary(self):
128        a_dictionary = {
129            'key1': 'value1',
130            'keyN': [0, 1, 2, 'n'],
131        }
132        self.assertEqual(
133            list(a_dictionary.items()),
134            [
135                ('key1', 'value1'),
136                ('keyN', [0, 1, 2, 'n']),
137            ]
138        )
139
140    def test_keys_of_a_dictionary(self):
141        a_dictionary = {
142            'key1': 'value1',
143            'keyN': [0, 1, 2, 'n'],
144        }
145        self.assertEqual(
146            list(a_dictionary.keys()),
147            ['key1', 'keyN']
148        )
149
150    def test_pop_removes_given_key_from_a_dictionary_and_returns_its_value(self):
151        a_dictionary = {'key': 'value'}
152
153        with self.assertRaises(KeyError):
154            a_dictionary.pop('not_in_dictionary')
155        self.assertEqual(
156            a_dictionary.pop('not_in_dictionary', 'default'),
157            'default'
158        )
159        self.assertEqual(
160            a_dictionary.pop('key', 'default'),
161            'value'
162        )
163        self.assertEqual(a_dictionary, {})
164
165    def test_popitem_removes_and_returns_last_key_value_pair_from_a_dictionary(self):
166        a_dictionary = {
167            'key1': 'value1',
168            'keyN': [0, 1, 2, 'n'],
169        }
170        self.assertEqual(
171            a_dictionary.popitem(),
172            ('keyN', [0, 1, 2, 'n'])
173        )
174        self.assertEqual(a_dictionary, {'key1': 'value1'})
175
176    def test_setdefault_adds_given_key_to_a_dictionary(self):
177        a_dictionary = {'key': 'value'}
178        self.assertEqual(
179            a_dictionary.setdefault('new_key', 'default'),
180            'default'
181        )
182        self.assertEqual(
183            a_dictionary.setdefault('key', 'default'),
184            'value'
185        )
186        self.assertEqual(
187            a_dictionary,
188            {
189                'key': 'value',
190                'new_key': 'default',
191            }
192        )
193
194    def test_update_a_dictionary(self):
195        a_dictionary = {'key': 'value'}
196        self.assertIsNone(a_dictionary.update(new_key=[0, 1, 2, 'n']))
197        self.assertIsNone(a_dictionary.update(key='updated value'))
198        self.assertIsNone(a_dictionary.update({'another_key': {0, 1, 2, 'n'}}))
199        self.assertEqual(
200            a_dictionary,
201            {
202                'key': 'updated value',
203                'new_key': [0, 1, 2, 'n'],
204                'another_key': {0, 1, 2, 'n'},
205            }
206        )
207
208    def test_values_of_a_dictionary(self):
209        a_dictionary = {
210            'key1': 'value1',
211            'keyN': [0, 1, 2, 'n'],
212        }
213        self.assertEqual(
214            list(a_dictionary.values()),
215            [
216                'value1',
217                [0, 1, 2, 'n'],
218            ]
219        )
220
221    def test_key_error(self):
222        a_dictionary = {'key': 'value'}
223        self.assertEqual(a_dictionary['key'], 'value')
224
225        with self.assertRaises(KeyError):
226            a_dictionary['not_in_dictionary']
227        self.assertEqual(
228            a_dictionary.get('not_in_dictionary', 'default'),
229            'default'
230        )
231
232        with self.assertRaises(KeyError):
233            a_dictionary.pop('not_in_dictionary')
234        self.assertEqual(
235            a_dictionary.pop('not_in_dictionary', 'default'),
236            'default'
237        )
238
239        with self.assertRaises(KeyError):
240            {}.popitem()
241
242
243# Exceptions Encountered
244# AssertionError
245# TypeError
246# KeyError