def test_gridfs_replica_set(self): rsc = self.asyncio_rsc(w=test.env.w, wtimeout=5000, read_preference=ReadPreference.SECONDARY) fs = AsyncIOMotorGridFS(rsc.motor_test) oid = yield from fs.put(b'foo') gridout = yield from fs.get(oid) content = yield from gridout.read() self.assertEqual(b'foo', content)
def test_stream_to_handler(self): fs = AsyncIOMotorGridFS(self.db) for content_length in (0, 1, 100, 100 * 1000): _id = yield from fs.put(b'a' * content_length) gridout = yield from fs.get(_id) handler = test.MockRequestHandler() yield from gridout.stream_to_handler(handler) self.assertEqual(content_length, handler.n_written) yield from fs.delete(_id)
def test_gridfs_replica_set(self): rsc = self.asyncio_rsc( w=test.env.w, wtimeout=5000, read_preference=ReadPreference.SECONDARY) fs = AsyncIOMotorGridFS(rsc.motor_test) oid = yield from fs.put(b'foo') gridout = yield from fs.get(oid) content = yield from gridout.read() self.assertEqual(b'foo', content)
def test_stream_to_handler(self): # TODO: Sort of Tornado-specific, but it does work with asyncio. class MockRequestHandler(object): def __init__(self): self.n_written = 0 def write(self, data): self.n_written += len(data) def flush(self): pass fs = AsyncIOMotorGridFS(self.db) for content_length in (0, 1, 100, 100 * 1000): _id = yield from fs.put(b'a' * content_length) gridout = yield from fs.get(_id) handler = MockRequestHandler() yield from gridout.stream_to_handler(handler) self.assertEqual(content_length, handler.n_written) yield from fs.delete(_id)
class TestAsyncIOGridFS(AsyncIOTestCase): @asyncio.coroutine def _reset(self): yield from self.db.drop_collection("fs.files") yield from self.db.drop_collection("fs.chunks") yield from self.db.drop_collection("alt.files") yield from self.db.drop_collection("alt.chunks") def setUp(self): super().setUp() self.loop.run_until_complete(self._reset()) self.fs = AsyncIOMotorGridFS(self.db) def tearDown(self): self.loop.run_until_complete(self._reset()) super().tearDown() @asyncio_test def test_get_version(self): # new_file creates a MotorGridIn. gin = yield from self.fs.new_file(_id=1, filename='foo', field=0) yield from gin.write(b'a') yield from gin.close() yield from self.fs.put(b'', filename='foo', field=1) yield from self.fs.put(b'', filename='foo', field=2) gout = yield from self.fs.get_version('foo') self.assertEqual(2, gout.field) gout = yield from self.fs.get_version('foo', -3) self.assertEqual(0, gout.field) gout = yield from self.fs.get_last_version('foo') self.assertEqual(2, gout.field) @asyncio_test def test_basic(self): oid = yield from self.fs.put(b"hello world") out = yield from self.fs.get(oid) self.assertEqual(b"hello world", (yield from out.read())) self.assertEqual(1, (yield from self.db.fs.files.count())) self.assertEqual(1, (yield from self.db.fs.chunks.count())) yield from self.fs.delete(oid) with self.assertRaises(NoFile): yield from self.fs.get(oid) self.assertEqual(0, (yield from self.db.fs.files.count())) self.assertEqual(0, (yield from self.db.fs.chunks.count())) with self.assertRaises(NoFile): yield from self.fs.get("foo") self.assertEqual("foo", (yield from self.fs.put(b"hello world", _id="foo"))) gridout = yield from self.fs.get("foo") self.assertEqual(b"hello world", (yield from gridout.read())) @asyncio_test def test_list(self): self.assertEqual([], (yield from self.fs.list())) yield from self.fs.put(b"hello world") self.assertEqual([], (yield from self.fs.list())) yield from self.fs.put(b"", filename="mike") yield from self.fs.put(b"foo", filename="test") yield from self.fs.put(b"", filename="hello world") self.assertEqual(set(["mike", "test", "hello world"]), set((yield from self.fs.list()))) @asyncio_test def test_put_filelike(self): oid = yield from self.fs.put(StringIO(b"hello world"), chunk_size=1) self.assertEqual(11, (yield from self.cx.motor_test.fs.chunks.count())) gridout = yield from self.fs.get(oid) self.assertEqual(b"hello world", (yield from gridout.read())) @asyncio_test def test_put_duplicate(self): oid = yield from self.fs.put(b"hello") with self.assertRaises(FileExists): yield from self.fs.put(b"world", _id=oid) @asyncio_test def test_put_unacknowledged(self): client = self.asyncio_client(w=0) fs = AsyncIOMotorGridFS(client.motor_test) with self.assertRaises(ConfigurationError): yield from fs.put(b"hello") client.close() @asyncio_test def test_gridfs_find(self): yield from self.fs.put(b"test2", filename="two") yield from self.fs.put(b"test2+", filename="two") yield from self.fs.put(b"test1", filename="one") yield from self.fs.put(b"test2++", filename="two") cursor = self.fs.find().sort("_id", -1).skip(1).limit(2) self.assertTrue((yield from cursor.fetch_next)) grid_out = cursor.next_object() self.assertTrue(isinstance(grid_out, AsyncIOMotorGridOut)) self.assertEqual(b"test1", (yield from grid_out.read())) self.assertRaises(TypeError, self.fs.find, {}, {"_id": True})
class TestAsyncIOGridFS(AsyncIOTestCase): @asyncio.coroutine def _reset(self): yield from self.db.drop_collection("fs.files") yield from self.db.drop_collection("fs.chunks") yield from self.db.drop_collection("alt.files") yield from self.db.drop_collection("alt.chunks") def setUp(self): super().setUp() self.loop.run_until_complete(self._reset()) self.fs = AsyncIOMotorGridFS(self.db) def tearDown(self): self.loop.run_until_complete(self._reset()) super().tearDown() @asyncio_test def test_get_version(self): # new_file creates a MotorGridIn. gin = yield from self.fs.new_file(_id=1, filename='foo', field=0) yield from gin.write(b'a') yield from gin.close() yield from self.fs.put(b'', filename='foo', field=1) yield from self.fs.put(b'', filename='foo', field=2) gout = yield from self.fs.get_version('foo') self.assertEqual(2, gout.field) gout = yield from self.fs.get_version('foo', -3) self.assertEqual(0, gout.field) gout = yield from self.fs.get_last_version('foo') self.assertEqual(2, gout.field) @asyncio_test def test_basic(self): oid = yield from self.fs.put(b"hello world") out = yield from self.fs.get(oid) self.assertEqual(b"hello world", (yield from out.read())) self.assertEqual(1, (yield from self.db.fs.files.count())) self.assertEqual(1, (yield from self.db.fs.chunks.count())) yield from self.fs.delete(oid) with self.assertRaises(NoFile): yield from self.fs.get(oid) self.assertEqual(0, (yield from self.db.fs.files.count())) self.assertEqual(0, (yield from self.db.fs.chunks.count())) with self.assertRaises(NoFile): yield from self.fs.get("foo") self.assertEqual( "foo", (yield from self.fs.put(b"hello world", _id="foo"))) gridout = yield from self.fs.get("foo") self.assertEqual(b"hello world", (yield from gridout.read())) @asyncio_test def test_list(self): self.assertEqual([], (yield from self.fs.list())) yield from self.fs.put(b"hello world") self.assertEqual([], (yield from self.fs.list())) yield from self.fs.put(b"", filename="mike") yield from self.fs.put(b"foo", filename="test") yield from self.fs.put(b"", filename="hello world") self.assertEqual(set(["mike", "test", "hello world"]), set((yield from self.fs.list()))) @asyncio_test(timeout=30) def test_put_filelike(self): oid = yield from self.fs.put(StringIO(b"hello world"), chunk_size=1) self.assertEqual(11, (yield from self.cx.motor_test.fs.chunks.count())) gridout = yield from self.fs.get(oid) self.assertEqual(b"hello world", (yield from gridout.read())) @asyncio_test def test_put_duplicate(self): oid = yield from self.fs.put(b"hello") with self.assertRaises(FileExists): yield from self.fs.put(b"world", _id=oid) @asyncio_test def test_put_unacknowledged(self): client = self.asyncio_client(w=0) with self.assertRaises(ConfigurationError): AsyncIOMotorGridFS(client.motor_test) client.close() @asyncio_test def test_gridfs_find(self): yield from self.fs.put(b"test2", filename="two") yield from self.fs.put(b"test2+", filename="two") yield from self.fs.put(b"test1", filename="one") yield from self.fs.put(b"test2++", filename="two") cursor = self.fs.find().sort("_id", -1).skip(1).limit(2) self.assertTrue((yield from cursor.fetch_next)) grid_out = cursor.next_object() self.assertTrue(isinstance(grid_out, AsyncIOMotorGridOut)) self.assertEqual(b"test1", (yield from grid_out.read())) self.assertRaises(TypeError, self.fs.find, {}, {"_id": True})