Esempio n. 1
0
    def test_grid_in_custom_opts(self):
        self.assertRaises(TypeError, AsyncIOMotorGridIn, "foo")
        a = 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 = 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)
Esempio n. 2
0
    def test_set_after_close(self):
        f = 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")

        yield from 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)
        yield from f.set("bar", "a")
        yield from f.set("baz", "b")
        self.assertRaises(AttributeError, setattr, f, "upload_date", 5)

        g = yield from AsyncIOMotorGridOut(self.db.fs, f._id).open()
        self.assertEqual("a", g.bar)
        self.assertEqual("b", g.baz)
Esempio n. 3
0
    def test_grid_out_custom_opts(self):
        one = 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")

        yield from one.write(b"hello world")
        yield from one.close()

        two = yield from 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)
        self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", two.md5)
Esempio n. 4
0
    def test_attributes(self):
        f = AsyncIOMotorGridIn(
            self.db.fs,
            filename="test",
            foo="bar",
            content_type="text")

        yield from f.close()

        g = 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)

        yield from g.open()
        for attr_name in attr_names:
            getattr(g, attr_name)
Esempio n. 5
0
    def test_attributes(self):
        f = AsyncIOMotorGridIn(
            self.db.fs,
            filename="test",
            foo="bar",
            content_type="text")

        yield from f.close()

        g = 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)

        yield from g.open()
        for attr_name in attr_names:
            getattr(g, attr_name)
Esempio n. 6
0
    def test_attributes(self):
        f = AsyncIOMotorGridIn(self.db.fs, filename="test", foo="bar", content_type="text")

        yield from f.close()

        g = 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)

        yield from g.open()
        for attr_name in attr_names:
            getattr(g, attr_name)
Esempio n. 7
0
    def test_grid_out_custom_opts(self):
        one = 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",
        )

        yield from one.write(b"hello world")
        yield from one.close()

        two = yield from 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)
        self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", two.md5)
Esempio n. 8
0
    def test_alternate_collection(self):
        yield from self.db.alt.files.remove()
        yield from self.db.alt.chunks.remove()

        f = AsyncIOMotorGridIn(self.db.alt)
        yield from f.write(b"hello world")
        yield from f.close()

        self.assertEqual(1, (yield from self.db.alt.files.find().count()))
        self.assertEqual(1, (yield from self.db.alt.chunks.find().count()))

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

        # test that md5 still works...
        self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
Esempio n. 9
0
    def test_alternate_collection(self):
        yield from self.db.alt.files.delete_many({})
        yield from self.db.alt.chunks.delete_many({})

        f = AsyncIOMotorGridIn(self.db.alt)
        yield from f.write(b"hello world")
        yield from f.close()

        self.assertEqual(1, (yield from self.db.alt.files.find().count()))
        self.assertEqual(1, (yield from self.db.alt.chunks.find().count()))

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

        # test that md5 still works...
        self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
Esempio n. 10
0
    def test_grid_out_file_document(self):
        one = AsyncIOMotorGridIn(self.db.fs)
        yield from one.write(b"foo bar")
        yield from one.close()

        file_document = yield from self.db.fs.files.find_one()
        two = AsyncIOMotorGridOut(self.db.fs, file_document=file_document)

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

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

        gridout = AsyncIOMotorGridOut(self.db.fs, file_document={})
        with self.assertRaises(NoFile):
            yield from gridout.open()
Esempio n. 11
0
    def test_grid_out_file_document(self):
        one = AsyncIOMotorGridIn(self.db.fs)
        yield from one.write(b"foo bar")
        yield from one.close()

        file_document = yield from self.db.fs.files.find_one()
        two = AsyncIOMotorGridOut(self.db.fs, file_document=file_document)

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

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

        gridout = AsyncIOMotorGridOut(self.db.fs, file_document={})
        with self.assertRaises(NoFile):
            yield from gridout.open()
Esempio n. 12
0
    def test_write_file_like(self):
        one = AsyncIOMotorGridIn(self.db.fs)
        yield from one.write(b"hello world")
        yield from one.close()

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

        four = AsyncIOMotorGridOut(self.db.fs, three._id)
        self.assertEqual(b"hello world", (yield from four.read()))
Esempio n. 13
0
    def test_grid_out_default_opts(self):
        self.assertRaises(TypeError, AsyncIOMotorGridOut, "foo")
        gout = AsyncIOMotorGridOut(self.db.fs, 5)
        with self.assertRaises(NoFile):
            yield from gout.open()

        a = AsyncIOMotorGridIn(self.db.fs)
        yield from a.close()

        b = yield from 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)
        self.assertEqual("d41d8cd98f00b204e9800998ecf8427e", b.md5)
Esempio n. 14
0
    def test_grid_out_default_opts(self):
        self.assertRaises(TypeError, AsyncIOMotorGridOut, "foo")
        gout = AsyncIOMotorGridOut(self.db.fs, 5)
        with self.assertRaises(NoFile):
            yield from gout.open()

        a = AsyncIOMotorGridIn(self.db.fs)
        yield from a.close()

        b = yield from 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)
        self.assertEqual("d41d8cd98f00b204e9800998ecf8427e", b.md5)
Esempio n. 15
0
    def test_set_after_close(self):
        f = 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")

        yield from 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)
        yield from f.set("bar", "a")
        yield from f.set("baz", "b")
        self.assertRaises(AttributeError, setattr, f, "upload_date", 5)

        g = yield from AsyncIOMotorGridOut(self.db.fs, f._id).open()
        self.assertEqual("a", g.bar)
        self.assertEqual("b", g.baz)
Esempio n. 16
0
    def test_write_file_like(self):
        one = AsyncIOMotorGridIn(self.db.fs)
        yield from one.write(b"hello world")
        yield from one.close()

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

        four = AsyncIOMotorGridOut(self.db.fs, three._id)
        self.assertEqual(b"hello world", (yield from four.read()))