async def test_iter_gridfs(self): gfs = MotorGridFS(self.db) async def cleanup(): await self.db.fs.files.remove() await self.db.fs.chunks.remove() await cleanup() # Empty iterator. async for _ in gfs.find({'_id': 1}): self.fail() data = b'data' for n_files in 1, 2, 10: for i in range(n_files): await gfs.put(data, filename='filename') # Force extra batches to test iteration. j = 0 async for _ in gfs.find({'filename': 'filename'}).batch_size(3): j += 1 self.assertEqual(j, n_files) await cleanup()
async def test_stream_to_handler(self): fs = MotorGridFS(self.db) content_length = 1000 await fs.delete(1) self.assertEqual(1, await fs.put(b'a' * content_length, _id=1)) gridout = await fs.get(1) handler = test.MockRequestHandler() await gridout.stream_to_handler(handler) self.assertEqual(content_length, handler.n_written) await fs.delete(1)
def test_gridfs_attrs(self): pymongo_gridfs_only = set([ # Obsolete PyMongo methods. 'open', 'remove']) motor_gridfs_only = set(['collection']).union(motor_only) self.assertEqual( attrs(GridFS(env.sync_cx.test)) - pymongo_gridfs_only, attrs(MotorGridFS(self.cx.test)) - motor_gridfs_only)
async def test_iter_gridfs(self): gfs = MotorGridFS(self.db) async def cleanup(): await self.db.fs.files.delete_many({}) await self.db.fs.chunks.delete_many({}) await cleanup() # Empty iterator. async for _ in gfs.find({'_id': 1}): self.fail() data = b'data' for n_files in 1, 2, 10: for i in range(n_files): await gfs.put(data, filename='filename') # Force extra batches to test iteration. j = 0 async for _ in gfs.find({'filename': 'filename'}).batch_size(3): j += 1 self.assertEqual(j, n_files) await cleanup() async with await gfs.new_file(_id=1, chunk_size=1) as f: await f.write(data) gout = await gfs.find_one({'_id': 1}) chunks = [] async for chunk in gout: chunks.append(chunk) self.assertEqual(len(chunks), len(data)) self.assertEqual(b''.join(chunks), data)
def test_gridout_cursor_attrs(self): self.assertEqual( attrs(self.sync_fs.find()) - pymongo_cursor_only, attrs(MotorGridFS(self.cx.test).find()) - motor_cursor_only)