コード例 #1
0
    async def test_grid_in_custom_opts(self):
        self.assertRaises(TypeError, motor_asyncio.AsyncIOMotorGridIn, "foo")
        a = motor_asyncio.AsyncIOMotorGridIn(
            self.db.fs,
            _id=5,
            filename="my_file",
            contentType="text/html",
            chunkSize=1000,
            aliases=["foo"],
            metadata={
                "foo": 1,
                "bar": 2
            },
            bar=3,
            baz="hello",
        )

        self.assertEqual(5, a._id)
        self.assertEqual("my_file", a.filename)
        self.assertEqual("text/html", a.content_type)
        self.assertEqual(1000, a.chunk_size)
        self.assertEqual(["foo"], a.aliases)
        self.assertEqual({"foo": 1, "bar": 2}, a.metadata)
        self.assertEqual(3, a.bar)
        self.assertEqual("hello", a.baz)
        self.assertRaises(AttributeError, getattr, a, "mike")

        b = motor_asyncio.AsyncIOMotorGridIn(self.db.fs,
                                             content_type="text/html",
                                             chunk_size=1000,
                                             baz=100)

        self.assertEqual("text/html", b.content_type)
        self.assertEqual(1000, b.chunk_size)
        self.assertEqual(100, b.baz)
コード例 #2
0
    async def test_write_file_like(self):
        one = motor_asyncio.AsyncIOMotorGridIn(self.db.fs)
        await one.write(b"hello world")
        await one.close()

        two = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, one._id)
        three = motor_asyncio.AsyncIOMotorGridIn(self.db.fs)
        await three.write(two)
        await three.close()

        four = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, three._id)
        self.assertEqual(b"hello world", (await four.read()))
コード例 #3
0
    async def test_attributes(self):
        f = motor_asyncio.AsyncIOMotorGridIn(self.db.fs,
                                             filename="test",
                                             foo="bar",
                                             content_type="text")

        await f.close()

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, f._id)
        attr_names = (
            "_id",
            "filename",
            "name",
            "name",
            "content_type",
            "length",
            "chunk_size",
            "upload_date",
            "aliases",
            "metadata",
        )

        for attr_name in attr_names:
            self.assertRaises(InvalidOperation, getattr, g, attr_name)

        await g.open()
        for attr_name in attr_names:
            getattr(g, attr_name)
コード例 #4
0
    async def test_set_after_close(self):
        f = motor_asyncio.AsyncIOMotorGridIn(self.db.fs, _id="foo", bar="baz")

        self.assertEqual("foo", f._id)
        self.assertEqual("baz", f.bar)
        self.assertRaises(AttributeError, getattr, f, "baz")
        self.assertRaises(AttributeError, getattr, f, "uploadDate")
        self.assertRaises(AttributeError, setattr, f, "_id", 5)

        f.bar = "foo"
        f.baz = 5

        self.assertEqual("foo", f.bar)
        self.assertEqual(5, f.baz)
        self.assertRaises(AttributeError, getattr, f, "uploadDate")

        await f.close()

        self.assertEqual("foo", f._id)
        self.assertEqual("foo", f.bar)
        self.assertEqual(5, f.baz)
        self.assertTrue(f.uploadDate)

        self.assertRaises(AttributeError, setattr, f, "_id", 5)
        await f.set("bar", "a")
        await f.set("baz", "b")
        self.assertRaises(AttributeError, setattr, f, "upload_date", 5)

        g = await motor_asyncio.AsyncIOMotorGridOut(self.db.fs, f._id).open()
        self.assertEqual("a", g.bar)
        self.assertEqual("b", g.baz)
コード例 #5
0
    async def test_grid_out_custom_opts(self):
        one = motor_asyncio.AsyncIOMotorGridIn(
            self.db.fs,
            _id=5,
            filename="my_file",
            contentType="text/html",
            chunkSize=1000,
            aliases=["foo"],
            metadata={
                "foo": 1,
                "bar": 2
            },
            bar=3,
            baz="hello",
        )

        await one.write(b"hello world")
        await one.close()

        two = await motor_asyncio.AsyncIOMotorGridOut(self.db.fs, 5).open()

        self.assertEqual(5, two._id)
        self.assertEqual(11, two.length)
        self.assertEqual("text/html", two.content_type)
        self.assertEqual(1000, two.chunk_size)
        self.assertTrue(isinstance(two.upload_date, datetime.datetime))
        self.assertEqual(["foo"], two.aliases)
        self.assertEqual({"foo": 1, "bar": 2}, two.metadata)
        self.assertEqual(3, two.bar)
コード例 #6
0
    async def test_basic(self):
        f = motor_asyncio.AsyncIOMotorGridIn(self.db.fs, filename="test")
        await f.write(b"hello world")
        await f.close()
        self.assertEqual(1, (await self.db.fs.files.count_documents({})))
        self.assertEqual(1, (await self.db.fs.chunks.count_documents({})))

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, f._id)
        self.assertEqual(b"hello world", (await g.read()))

        f = motor_asyncio.AsyncIOMotorGridIn(self.db.fs, filename="test")
        await f.close()
        self.assertEqual(2, (await self.db.fs.files.count_documents({})))
        self.assertEqual(1, (await self.db.fs.chunks.count_documents({})))

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, f._id)
        self.assertEqual(b"", (await g.read()))
コード例 #7
0
    async def test_alternate_collection(self):
        await self.db.alt.files.delete_many({})
        await self.db.alt.chunks.delete_many({})
        f = motor_asyncio.AsyncIOMotorGridIn(self.db.alt)
        await f.write(b"hello world")
        await f.close()

        self.assertEqual(1, (await self.db.alt.files.count_documents({})))
        self.assertEqual(1, (await self.db.alt.chunks.count_documents({})))

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.alt, f._id)
        self.assertEqual(b"hello world", (await g.read()))
コード例 #8
0
    async def test_alternate_collection(self):
        await self.db.alt.files.delete_many({})
        await self.db.alt.chunks.delete_many({})

        f = motor_asyncio.AsyncIOMotorGridIn(self.db.alt)
        await f.write(b"hello world")
        await f.close()

        self.assertEqual(1, (await self.db.alt.files.count_documents({})))
        self.assertEqual(1, (await self.db.alt.chunks.count_documents({})))

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.alt, f._id)
        self.assertEqual(b"hello world", (await g.read()))

        # test that md5 still works...
        self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
コード例 #9
0
    async def test_grid_out_default_opts(self):
        self.assertRaises(TypeError, motor_asyncio.AsyncIOMotorGridOut, "foo")
        gout = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, 5)
        with self.assertRaises(NoFile):
            await gout.open()

        a = motor_asyncio.AsyncIOMotorGridIn(self.db.fs)
        await a.close()

        b = await motor_asyncio.AsyncIOMotorGridOut(self.db.fs, a._id).open()

        self.assertEqual(a._id, b._id)
        self.assertEqual(0, b.length)
        self.assertEqual(None, b.content_type)
        self.assertEqual(255 * 1024, b.chunk_size)
        self.assertTrue(isinstance(b.upload_date, datetime.datetime))
        self.assertEqual(None, b.aliases)
        self.assertEqual(None, b.metadata)
コード例 #10
0
    async def test_grid_out_file_document(self):
        one = motor_asyncio.AsyncIOMotorGridIn(self.db.fs)
        await one.write(b"foo bar")
        await one.close()

        file_document = await self.db.fs.files.find_one()
        two = motor_asyncio.AsyncIOMotorGridOut(self.db.fs,
                                                file_document=file_document)

        self.assertEqual(b"foo bar", (await two.read()))

        file_document = await self.db.fs.files.find_one()
        three = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, 5, file_document)
        self.assertEqual(b"foo bar", (await three.read()))

        gridout = motor_asyncio.AsyncIOMotorGridOut(self.db.fs,
                                                    file_document={})
        with self.assertRaises(NoFile):
            await gridout.open()
コード例 #11
0
    async def test_readchunk(self):
        in_data = b"a" * 10
        f = motor_asyncio.AsyncIOMotorGridIn(self.db.fs, chunkSize=3)
        await f.write(in_data)
        await f.close()

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, f._id)

        # This is starting to look like Lisp.
        self.assertEqual(3, len((await g.readchunk())))

        self.assertEqual(2, len((await g.read(2))))
        self.assertEqual(1, len((await g.readchunk())))

        self.assertEqual(3, len((await g.read(3))))

        self.assertEqual(1, len((await g.readchunk())))

        self.assertEqual(0, len((await g.readchunk())))
コード例 #12
0
    async def test_attributes(self):
        f = motor_asyncio.AsyncIOMotorGridIn(self.db.fs,
                                             filename="test",
                                             foo="bar",
                                             content_type="text")

        await f.close()

        g = motor_asyncio.AsyncIOMotorGridOut(self.db.fs, f._id)
        attr_names = ('_id', 'filename', 'name', 'name', 'content_type',
                      'length', 'chunk_size', 'upload_date', 'aliases',
                      'metadata', 'md5')

        for attr_name in attr_names:
            self.assertRaises(InvalidOperation, getattr, g, attr_name)

        await g.open()
        for attr_name in attr_names:
            getattr(g, attr_name)
コード例 #13
0
    async def test_grid_in_default_opts(self):
        self.assertRaises(TypeError, motor_asyncio.AsyncIOMotorGridIn, "foo")

        a = motor_asyncio.AsyncIOMotorGridIn(self.db.fs)

        self.assertTrue(isinstance(a._id, ObjectId))
        self.assertRaises(AttributeError, setattr, a, "_id", 5)

        self.assertEqual(None, a.filename)

        # This raises AttributeError because you can't directly set properties
        # in Motor, have to use set()
        def setter():
            a.filename = "my_file"

        self.assertRaises(AttributeError, setter)

        # This method of setting attributes works in Motor
        await a.set("filename", "my_file")
        self.assertEqual("my_file", a.filename)

        self.assertEqual(None, a.content_type)
        await a.set("content_type", "text/html")
        self.assertEqual("text/html", a.content_type)

        self.assertRaises(AttributeError, getattr, a, "length")
        self.assertRaises(AttributeError, setattr, a, "length", 5)

        self.assertEqual(255 * 1024, a.chunk_size)
        self.assertRaises(AttributeError, setattr, a, "chunk_size", 5)

        self.assertRaises(AttributeError, getattr, a, "upload_date")
        self.assertRaises(AttributeError, setattr, a, "upload_date", 5)

        self.assertRaises(AttributeError, getattr, a, "aliases")
        await a.set("aliases", ["foo"])
        self.assertEqual(["foo"], a.aliases)

        self.assertRaises(AttributeError, getattr, a, "metadata")
        await a.set("metadata", {"foo": 1})
        self.assertEqual({"foo": 1}, a.metadata)

        await a.close()

        self.assertTrue(isinstance(a._id, ObjectId))
        self.assertRaises(AttributeError, setattr, a, "_id", 5)

        self.assertEqual("my_file", a.filename)

        self.assertEqual("text/html", a.content_type)

        self.assertEqual(0, a.length)
        self.assertRaises(AttributeError, setattr, a, "length", 5)

        self.assertEqual(255 * 1024, a.chunk_size)
        self.assertRaises(AttributeError, setattr, a, "chunk_size", 5)

        self.assertTrue(isinstance(a.upload_date, datetime.datetime))
        self.assertRaises(AttributeError, setattr, a, "upload_date", 5)

        self.assertEqual(["foo"], a.aliases)

        self.assertEqual({"foo": 1}, a.metadata)