|
| 1 | +"""Collections tests.""" |
| 2 | + |
| 3 | +import unittest2 |
| 4 | + |
| 5 | +from domain_models import collections |
| 6 | +from domain_models import errors |
| 7 | + |
| 8 | + |
| 9 | +class CollectionTests(unittest2.TestCase): |
| 10 | + """Collection tests.""" |
| 11 | + |
| 12 | + def test_init_empty(self): |
| 13 | + """Test creation of collection.""" |
| 14 | + collection = collections.Collection(int) |
| 15 | + |
| 16 | + self.assertIsInstance(collection, collections.Collection) |
| 17 | + self.assertIsInstance(collection, list) |
| 18 | + |
| 19 | + def test_init_with_correct_values(self): |
| 20 | + """Test creation of collection.""" |
| 21 | + collection = collections.Collection(int, (1, 2, 3)) |
| 22 | + |
| 23 | + self.assertEqual(collection, [1, 2, 3]) |
| 24 | + |
| 25 | + def test_init_with_incorrect_values(self): |
| 26 | + """Test creation of collection.""" |
| 27 | + with self.assertRaises(TypeError): |
| 28 | + collections.Collection(int, ('1', '2', '3')) |
| 29 | + |
| 30 | + def test_init_without_value_type(self): |
| 31 | + """Test creation of collection.""" |
| 32 | + with self.assertRaises(errors.Error): |
| 33 | + collections.Collection(None) |
| 34 | + |
| 35 | + with self.assertRaises(errors.Error): |
| 36 | + collections.Collection(1) |
| 37 | + |
| 38 | + with self.assertRaises(errors.Error): |
| 39 | + collections.Collection(object()) |
| 40 | + |
| 41 | + def test_append_valid_type(self): |
| 42 | + """Test append.""" |
| 43 | + collection = collections.Collection(int) |
| 44 | + |
| 45 | + collection.append(1) |
| 46 | + |
| 47 | + self.assertEqual(collection, [1]) |
| 48 | + |
| 49 | + def test_append_invalid_type(self): |
| 50 | + """Test append.""" |
| 51 | + collection = collections.Collection(int) |
| 52 | + |
| 53 | + with self.assertRaises(TypeError): |
| 54 | + collection.append('1') |
| 55 | + |
| 56 | + def test_extend_valid_type(self): |
| 57 | + """Test extend.""" |
| 58 | + collection = collections.Collection(int) |
| 59 | + |
| 60 | + collection.extend([1]) |
| 61 | + |
| 62 | + self.assertEqual(collection, [1]) |
| 63 | + |
| 64 | + def test_extend_invalid_type(self): |
| 65 | + """Test extend.""" |
| 66 | + collection = collections.Collection(int) |
| 67 | + |
| 68 | + with self.assertRaises(TypeError): |
| 69 | + collection.extend(['1']) |
| 70 | + |
| 71 | + def test_insert_valid_type(self): |
| 72 | + """Test insert.""" |
| 73 | + collection = collections.Collection(int) |
| 74 | + |
| 75 | + collection.insert(0, 1) |
| 76 | + |
| 77 | + self.assertEqual(collection, [1]) |
| 78 | + |
| 79 | + def test_insert_invalid_type(self): |
| 80 | + """Test insert.""" |
| 81 | + collection = collections.Collection(int) |
| 82 | + |
| 83 | + with self.assertRaises(TypeError): |
| 84 | + collection.insert(0, '1') |
| 85 | + |
| 86 | + def test_set_valid_type(self): |
| 87 | + """Test set.""" |
| 88 | + collection = collections.Collection(int, [0]) |
| 89 | + |
| 90 | + collection[0] = 1 |
| 91 | + |
| 92 | + self.assertEqual(collection, [1]) |
| 93 | + |
| 94 | + def test_set_invalid_type(self): |
| 95 | + """Test set.""" |
| 96 | + collection = collections.Collection(int, [0]) |
| 97 | + |
| 98 | + with self.assertRaises(TypeError): |
| 99 | + collection[0] = '1' |
| 100 | + |
| 101 | + def test_set_valid_slice(self): |
| 102 | + """Test set slice.""" |
| 103 | + collection = collections.Collection(int, [1, 2, 3]) |
| 104 | + |
| 105 | + collection[0:3] = [7, 7, 7] |
| 106 | + |
| 107 | + self.assertEqual(collection, [7, 7, 7]) |
| 108 | + |
| 109 | + def test_set_invalid_slice(self): |
| 110 | + """Test set slice.""" |
| 111 | + collection = collections.Collection(int, [1, 2, 3]) |
| 112 | + |
| 113 | + with self.assertRaises(TypeError): |
| 114 | + collection[0:3] = [7, '7', 7] |
| 115 | + |
| 116 | + self.assertEqual(collection, [1, 2, 3]) |
| 117 | + |
| 118 | + def test_set_valid_slice_setitem(self): |
| 119 | + """Test set slice.""" |
| 120 | + collection = collections.Collection(int, [1, 2, 3]) |
| 121 | + |
| 122 | + collection.__setitem__(slice(0, 3), [7, 7, 7]) |
| 123 | + |
| 124 | + self.assertEqual(collection, [7, 7, 7]) |
| 125 | + |
| 126 | + def test_set_invalid_slice_setitem(self): |
| 127 | + """Test set slice.""" |
| 128 | + collection = collections.Collection(int, [1, 2, 3]) |
| 129 | + |
| 130 | + with self.assertRaises(TypeError): |
| 131 | + collection.__setitem__(slice(0, 3), [7, '7', 7]) |
| 132 | + |
| 133 | + self.assertEqual(collection, [1, 2, 3]) |
| 134 | + |
| 135 | + def test_get_item(self): |
| 136 | + """Test getting of item.""" |
| 137 | + collection = collections.Collection(int, [1, 2, 3]) |
| 138 | + |
| 139 | + self.assertEqual(collection[0], 1) |
| 140 | + self.assertEqual(collection[1], 2) |
| 141 | + self.assertEqual(collection[2], 3) |
| 142 | + |
| 143 | + def test_get_slice(self): |
| 144 | + """Test getting of slice.""" |
| 145 | + collection = collections.Collection(int, [1, 2, 3]) |
| 146 | + |
| 147 | + collection_slice = collection[0:2] |
| 148 | + |
| 149 | + self.assertEqual(collection_slice, [1, 2]) |
| 150 | + self.assertIsInstance(collection_slice, collections.Collection) |
| 151 | + self.assertIs(collection.value_type, collection_slice.value_type) |
| 152 | + |
| 153 | + def test_getitem_slice(self): |
| 154 | + """Test getting of slice.""" |
| 155 | + collection = collections.Collection(int, [1, 2, 3]) |
| 156 | + |
| 157 | + collection_slice = collection.__getitem__(slice(0, 2)) |
| 158 | + |
| 159 | + self.assertEqual(collection_slice, [1, 2]) |
| 160 | + self.assertIsInstance(collection_slice, collections.Collection) |
| 161 | + self.assertIs(collection.value_type, collection_slice.value_type) |
0 commit comments