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
import unittest
class KeyValueStore(object):
''' A key-value store example '''
def __init__(self):
self._store = {}
def put(self, key, value):
''' Puts a value in the store '''
self._store[key] = value
def get(self, key):
''' Gets a value from the store by its key '''
return self._store.get(key)
class TestKeyValueStore(unittest.TestCase):
''' A unit test for KeyValueStore '''
def setUp(self):
''' Runs before each test method '''
self.store = KeyValueStore()
def test_init(self):
''' Tests __init__ '''
self.assertEqual(self.store.get('key'), None)
self.assertEqual(len(self.store._store), 0)
def test_put_with_value(self):
''' Tests put '''
self.store.put('key', 'value')
self.assertEqual(self.store.get('key'), 'value')
self.assertEqual(len(self.store._store), 1)
def test_put_with_none_value(self):
''' Tests put with none value '''
self.store.put('key', None)
self.assertEqual(self.store.get('key'), None)
self.assertEqual(len(self.store._store), 1)
if __name__ == '__main__':
unittest.main()
Enter to Rename, Shift+Enter to Preview