def test_get_output_with_str(self): collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) ret = qs._get_output('outcoll') self.assertEqual(ret, 'outcoll')
def test_inline_map_reduce_with_bad_output(self): mr_kwrags = {'out': 'bla'} collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) with self.assertRaises(OperationError): yield from qs.inline_map_reduce('mapf', 'reducef', **mr_kwrags)
def test_code_with_str(self): code = 'function f(){return false}' collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) ret = qs._get_code(code) self.assertIsInstance(ret, Code)
def test_get_output_with_dict(self): output = {'merge': 'bla', 'db_alias': 'default'} collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) ret = qs._get_output(output) self.assertIsInstance(ret, SON)
def test_get_output_with_son(self): collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) son = SON({'merge': 'bla'}) ret = qs._get_output(son) self.assertEqual(ret, son)
def test_map_reduce_with_inline_output(self): # raises an exception when output is inline collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) with self.assertRaises(OperationError): yield from qs.map_reduce('mapf', 'reducef', output='inline')
def test_get_with_multiple_docs(self): d = self.test_doc(a='a') yield from d.save() d = self.test_doc(a='a') yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) with self.assertRaises(self.test_doc.MultipleObjectsReturned): yield from qs.get(a='a')
def test_get(self): d = self.test_doc(a=str(1)) yield from d.save() dd = self.test_doc(a=str(2)) yield from dd.save() collection = self.test_doc._collection qs = QuerySet(self.test_doc, collection) returned = yield from qs.get(id=d.id) self.assertEqual(d.id, returned.id)
def test_item_frequencies_with_normalize(self): d = self.test_doc(lf=['a', 'b']) yield from d.save() d = self.test_doc(lf=['a', 'c']) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) freq = yield from qs.item_frequencies('lf', normalize=True) self.assertEqual(freq['a'], 0.5)
def test_count_queryset(self): for i in range(5): d = self.test_doc(a=str(i)) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) qs = qs.filter(a='1') count = yield from qs.count() self.assertEqual(count, 1)
def test_delete_queryset(self): d = self.test_doc(a='a') yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) yield from qs.delete() docs = yield from qs.to_list() self.assertEqual(len(docs), 0)
def test_to_list(self): futures = [] for i in range(4): d = self.test_doc(a=str(i)) futures.append(d.save()) yield from asyncio.gather(*futures) collection = self.test_doc._collection qs = QuerySet(self.test_doc, collection) qs = qs.filter(a__in=['1', '2']) docs = yield from qs.to_list() self.assertEqual(len(docs), 2) self.assertTrue(isinstance(docs[0], self.test_doc))
def _qs(self): """ Returns the queryset to use for updating / reloading / deletions """ if not hasattr(self, '__objects'): self.__objects = QuerySet(self, self._get_collection()) return self.__objects
def test_getitem_with_slice(self): for i in range(5): d = self.test_doc(a=str(i)) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) qs = qs[1:3] count = yield from qs.count() self.assertEqual(count, 2) incr = 0 while (yield from qs.fetch_next): qs.next_object() incr += 1 self.assertEqual(incr, 2)
def test_get_map_reduce(self): for i in range(5): d = self.test_doc(a=str(i)) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) mapf = """ function(){ emit(this.a, 1); } """ reducef = """ function(key, values){ return Array.sum(values) } """ ret = yield from qs.map_reduce(mapf, reducef, {'merge': 'bla'}) self.assertEqual(ret['counts']['input'], 5)
def test_iterate_over_queryset(self): """Ensure that we can iterate over the queryset using fetch_next/next_doc. """ for i in range(5): d = self.test_doc(a=str(i)) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) c = 0 while (yield from qs.fetch_next): c += 1 doc = qs.next_object() self.assertIsInstance(doc, self.test_doc) self.assertEqual(c, 5)
def test_getitem_with_int(self): for i in range(5): d = self.test_doc(a=str(i)) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection).order_by('-a') doc = yield from qs[0] self.assertEqual(doc.a, '4')
def test_inline_map_reduce(self): for i in range(5): d = self.test_doc(a=str(i)) yield from d.save() collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) mapf = """ function(){ emit(this.a, 1); } """ reducef = """ function(key, values){ return Array.sum(values) } """ gen = yield from qs.inline_map_reduce(mapf, reducef) ret = list(gen) self.assertEqual(len(ret), 5) self.assertIsInstance(ret[0], MapReduceDocument)
def test_get_with_no_doc(self): collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) with self.assertRaises(self.test_doc.DoesNotExist): yield from qs.get(a='bla')
def test_dereference(self): collection = self.test_doc._collection qs = QuerySet(self.test_doc, collection) self.assertEqual(type(qs._dereference), SanicMongoDeReference)
def test_get_output_with_bad_output_type(self): collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) with self.assertRaises(ConfusionError): qs._get_output([])
def test_get_output_without_action_data(self): collection = self.test_doc._get_collection() qs = QuerySet(self.test_doc, collection) with self.assertRaises(OperationError): qs._get_output({})