def test_instrument_find(self): find = pymongo.collection.Collection.find FindWrapper.wrap() try: self.assertNotEqual(pymongo.collection.Collection.find, find) self.assertIsInstance(pymongo.collection.Collection.find, FindWrapper) finally: FindWrapper.unwrap() self.assertEqual(pymongo.collection.Collection.find, find) self.assertNotIsInstance(pymongo.collection.Collection.find, FindWrapper)
def test_instrument_insert(self): find = pymongo.collection.Collection.find FindWrapper.wrap() try: self.assertNotEqual(pymongo.collection.Collection.find, find) self.assertIsInstance(pymongo.collection.Collection.find, FindWrapper) finally: FindWrapper.unwrap() self.assertEqual(pymongo.collection.Collection.find, find) self.assertNotIsInstance(pymongo.collection.Collection.find, FindWrapper)
def test_distinct(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): names = self.db.foo.find().distinct('name') self.assertItemsEqual(names, ['alice', 'bob', 'zed', 'yohan']) self.assertEqual(push_mock.call_count, 2)
def test_get_source(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): doc = self.db.foo.find_one({'name': 'bob'}) frame_info = inspect.getframeinfo(inspect.currentframe()) source = '%s:%d' % (frame_info[0], frame_info[1] - 1) self.assertEqual(push_mock.call_args[0][0]['source'], source)
def test_count(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): num_records = self.db.foo.find().count() self.assertEqual(num_records, 4) # find + $cmd self.assertEqual(push_mock.call_count, 2)
def test_find_push(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): doc = self.db.foo.find_one({'name': 'bob'}) self.assertEqual(doc, {'_id': 1, 'name': 'bob'}) self.assertEqual(push_mock.call_count, 1) self.assertIn('allPlans', push_mock.call_args[0][0]['explain']) self.assertNotIsInstance(pymongo.collection.Collection.find, FindWrapper) self.assertNotIsInstance(self.db.foo.find, FindWrapper)
def test_or_query(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): docs = [d for d in self.db.foo.find({'$or': [{'name': 'bob'}, {'name': 'alice'}]}, {'name': 1})] self.assertEqual(len(docs), 2) self.assertItemsEqual(docs, [{'_id': 1, 'name': 'bob'}, {'_id': 2, 'name': 'alice'}]) self.assertEqual(push_mock.call_count, 1)
def test_cursor_terminator_query(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): q = {'name': 'bob'} curs = self.db.foo.find(q) self.assertIn('_mongodrums', curs.__dict__) curs.next() self.assertNotIn('_mongodrums', curs.__dict__) self.assertDictEqual( json.loads(push_mock.call_args[0][0]['query']), q)
def test_chained_call(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): # chain call curs = self.db.foo.find({'name': 'bob'}).limit(1) self.assertEqual(push_mock.call_count, 0) self.assertTrue(hasattr(curs.next, 'func')) self.assertIsInstance(curs.next.func, _CursorNextWrapper) # use iter instead of direct call to next doc = [d for d in curs][0] self.assertEqual(push_mock.call_count, 1) self.assertEqual(doc, {'_id': 1, 'name': 'bob'}) self.assertIn('allPlans', push_mock.call_args[0][0]['explain']) self.assertNotIsInstance(pymongo.collection.Collection.find, FindWrapper) self.assertNotIsInstance(self.db.foo.find, FindWrapper)
def test_or_query(self): update({'instrument': {'sample_frequency': 1}}) with patch('mongodrums.instrument.push') as push_mock, \ FindWrapper.instrument(): docs = [ d for d in self.db.foo.find( {'$or': [{ 'name': 'bob' }, { 'name': 'alice' }]}, {'name': 1}) ] self.assertEqual(len(docs), 2) self.assertItemsEqual(docs, [{ '_id': 1, 'name': 'bob' }, { '_id': 2, 'name': 'alice' }]) self.assertEqual(push_mock.call_count, 1)