Package grassyknoll :: Package tests :: Module test_GenericCollection
[hide private]

Source Code for Module grassyknoll.tests.test_GenericCollection

  1  import sys, shutil 
  2  from grassyknoll.tests import * 
  3  from grassyknoll.lib.util import split_dot_name 
  4   
  5  from grassyknoll.collection.Collection import * 
  6   
  7  __all__=['makeTests'] 
  8   
9 -def make_doc(uid, size, color):
10 return uid, CollectionDocument({u'__id__':uid, 11 u'size':size, 12 u'color':color})
13 14 testdocs=dict([make_doc(u'pants', 32, u'blue'), 15 make_doc(u'shirt', u'XL', u'red'), 16 make_doc(u'belt', 38, u'brown'), 17 make_doc(u'shoes', 14, u'black')]) 18 19 testuids=[d.id for d in testdocs.itervalues()] 20
21 -class GenericBase(object):
22
23 - def make_collection(self):
24 raise NotImplementedError
25
26 - def load_data(self):
27 raise NotImplementedError
28
29 - def setup(self):
30 if hasattr(self, 'setup_hook'): self.setup_hook() 31 self.collection=self.make_collection() 32 self.load_data()
33
34 - def teardown(self):
35 self.collection.close() 36 self.collection.cleanUp() 37 del self.collection
38
39 -class GenericEmptyTest(GenericBase):
40
41 - def load_data(self):
42 pass
43
44 - def test_list(self):
45 uids=self.collection.list() 46 assert isinstance(uids, CollectionIds) 47 assert len(uids) == 0
48
49 - def test_retrieve(self):
50 results=self.collection.retrieve(['pants', 'shirts']) 51 assert isinstance(results, CollectionResultSet) 52 assert len(results) == 0
53
54 - def test_retrieve_fields(self):
55 results=self.collection.retrieve(['pants', 'shirts'], fields=['size', 'color']) 56 assert isinstance(results, CollectionResultSet) 57 assert len(results) == 0
58
59 - def test_delete(self):
60 uids=self.collection.delete(['pants', 'shirts']) 61 assert isinstance(uids, CollectionIds) 62 assert_sorted_equals(uids, ['pants', 'shirts'])
63
64 -class GenericLoadBase(GenericBase):
65 - def do_load_data(self):
66 uids=self.collection.create(testdocs.values()) 67 assert isinstance(uids, CollectionIds) 68 assert len(uids) == len(testuids) 69 assert_sorted_equals(uids, testuids)
70
71 -class GenericCreateTest(GenericLoadBase):
72
73 - def load_data(self):
74 pass
75
76 - def test_load_data(self):
77 self.do_load_data()
78
79 -class GenericTest(GenericLoadBase):
80
81 - def load_data(self):
82 self.do_load_data()
83
84 - def test_list(self):
85 uids=self.collection.list() 86 assert isinstance(uids, CollectionIds) 87 assert len(uids) == len(testuids) 88 assert_sorted_equals(uids, testuids)
89
90 - def test_retrieve(self):
91 for uid in testuids: 92 results=self.collection.retrieve([uid]) 93 assert isinstance(results, CollectionResultSet) 94 assert len(results) == 1 95 96 result=results[0] 97 assert isinstance(result, CollectionResult) 98 assert result == testdocs[uid] 99 100 del results 101 del result
102
103 - def test_retrieve_fields(self):
104 fields=(u'size', u'color') 105 for uid in testuids: 106 results=self.collection.retrieve([uid], fields=fields) 107 assert isinstance(results, CollectionResultSet) 108 assert len(results) == 1 109 110 result=results[0] 111 assert isinstance(result, CollectionResult) 112 assert_sorted_equals(fields+('__id__',), result.iterkeys()) 113 for f in fields: 114 assert result[f] == testdocs[uid][f]
115
116 - def test_retrieve_nonesuch(self):
117 results=self.collection.retrieve([u'hat']) 118 assert isinstance(results, CollectionResultSet) 119 assert len(results) == 0
120
121 - def test_delete(self):
122 uids=self.collection.list() 123 assert_sorted_equals(uids, testuids) 124 125 uids=self.collection.delete([u'pants', u'shirt', u'hat']) 126 assert isinstance(uids, CollectionIds) 127 assert len(uids) == 3 128 assert_sorted_equals(uids, [u'hat', u'pants', u'shirt', ]) 129 130 uids=self.collection.list() 131 assert len(uids) == 2 132 assert_sorted_equals(uids, [u'belt', u'shoes'])
133
134 -def makeTests(makeCollection, name=None):
135 """generate tests and inject them into caller's namespace. 136 137 @arg makeCollection: a L{Factory} that returns an empty L{Collection}. 138 This may be a L{Collection} subclass if its C{__init__} can be called 139 without arguments. 140 @type makeCollection: function or class 141 142 @arg name: a name to use for test subclasses of the form 143 'my_module.MyCollection'. If None, a name will be inferred. 144 @type name: str or None 145 """ 146 147 frame=sys._getframe(1) 148 if name is not None: 149 mod, name = split_dot_name(name) 150 elif issubclass(makeCollection, Collection): 151 name=makeCollection.__name__ 152 # XXX use makeCollection.__module__ instead? 153 mod=frame.f_locals['__name__'] 154 else: 155 # XXX add a case for Factory 156 raise ValueError, "must supply name" 157 158 class _empty(GenericEmptyTest): 159 make_collection=makeCollection
160 _empty.__name__='TestEmpty%s'%name 161 162 class _create(GenericCreateTest): 163 make_collection=makeCollection 164 _create.__name__='TestCreate%s'%name 165 166 class _test(GenericTest): 167 make_collection=makeCollection 168 _test.__name__='Test%s'%name 169 170 for cls in (_empty, _create, _test): 171 cls.__module__=mod 172 frame.f_locals[cls.__name__]=cls 173