Пример #1
0
    async def test_iter_gridfs(self):
        gfs = AsyncIOMotorGridFS(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()
Пример #2
0
    async def test_iter_gridfs(self):
        gfs = AsyncIOMotorGridFS(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()
Пример #3
0
    async def test_iter_gridfs(self):
        gfs = AsyncIOMotorGridFS(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)
Пример #4
0
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})
Пример #5
0
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})