def test_basic(self): f = motor.MotorGridIn(self.db.fs, filename="test") yield f.write(b"hello world") yield f.close() self.assertEqual(1, (yield self.db.fs.files.find().count())) self.assertEqual(1, (yield self.db.fs.chunks.find().count())) g = motor.MotorGridOut(self.db.fs, f._id) self.assertEqual(b"hello world", (yield g.read())) f = motor.MotorGridIn(self.db.fs, filename="test") yield f.close() self.assertEqual(2, (yield self.db.fs.files.find().count())) self.assertEqual(1, (yield self.db.fs.chunks.find().count())) g = motor.MotorGridOut(self.db.fs, f._id) self.assertEqual(b"", (yield g.read()))
async def test_grid_out_file_document(self): one = motor.MotorGridIn(self.db.fs) await one.write(b"foo bar") await one.close() file_document = await self.db.fs.files.find_one() two = motor.MotorGridOut(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.MotorGridOut(self.db.fs, 5, file_document) self.assertEqual(b"foo bar", (await three.read())) gridout = motor.MotorGridOut(self.db.fs, file_document={}) with self.assertRaises(NoFile): await gridout.open()
def test_grid_out_file_document(self): one = motor.MotorGridIn(self.db.fs) yield one.write(b"foo bar") yield one.close() file_document = yield self.db.fs.files.find_one() two = motor.MotorGridOut(self.db.fs, file_document=file_document) self.assertEqual(b"foo bar", (yield two.read())) file_document = yield self.db.fs.files.find_one() three = motor.MotorGridOut(self.db.fs, 5, file_document) self.assertEqual(b"foo bar", (yield three.read())) gridout = motor.MotorGridOut(self.db.fs, file_document={}) with self.assertRaises(NoFile): yield gridout.open()
async def test_basic(self): f = motor.MotorGridIn(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.MotorGridOut(self.db.fs, f._id) self.assertEqual(b"hello world", (await g.read())) f = motor.MotorGridIn(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.MotorGridOut(self.db.fs, f._id) self.assertEqual(b"", (await g.read()))
def test_grid_out_file_document(self): db = self.cx.pymongo_test one = yield motor.MotorGridIn(db.fs).open() yield one.write(b("foo bar")) yield one.close() two = yield motor.MotorGridOut( db.fs, file_document=(yield db.fs.files.find_one())).open() self.assertEqual(b("foo bar"), (yield two.read())) three = yield motor.MotorGridOut( db.fs, 5, file_document=(yield db.fs.files.find_one())).open() self.assertEqual(b("foo bar"), (yield three.read())) with assert_raises(NoFile): yield motor.MotorGridOut(db.fs, file_document={}).open()
async def test_gridout_open_exc_info(self): g = motor.MotorGridOut(self.db.fs, "_id that doesn't exist") try: await g.open() except NoFile: _, _, tb = sys.exc_info() # The call tree should include PyMongo code we ran on a thread. formatted = '\n'.join(traceback.format_tb(tb)) self.assertTrue('_ensure_file' in formatted)
def test_grid_out_default_opts(self): self.assertRaises(TypeError, motor.MotorGridOut, "foo") gout = motor.MotorGridOut(self.db.fs, 5) with assert_raises(NoFile): yield gout.open() a = motor.MotorGridIn(self.db.fs) yield a.close() b = yield motor.MotorGridOut(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)
def test_gridout_open_exc_info(self): if sys.version_info < (3, ): raise SkipTest("Requires Python 3") g = motor.MotorGridOut(self.db.fs, "_id that doesn't exist") try: yield g.open() except NoFile: _, _, tb = sys.exc_info() # The call tree should include PyMongo code we ran on a thread. formatted = '\n'.join(traceback.format_tb(tb)) self.assertTrue('_ensure_file' in formatted)
def test_basic(self): db = self.cx.pymongo_test f = yield motor.MotorGridIn(db.fs, filename="test").open() yield f.write(b("hello world")) yield f.close() self.assertEqual(1, (yield db.fs.files.find().count())) self.assertEqual(1, (yield db.fs.chunks.find().count())) g = yield motor.MotorGridOut(db.fs, f._id).open() self.assertEqual(b("hello world"), (yield g.read())) # make sure it's still there... g = yield motor.MotorGridOut(db.fs, f._id).open() self.assertEqual(b("hello world"), (yield g.read())) f = yield motor.MotorGridIn(db.fs, filename="test").open() yield f.close() self.assertEqual(2, (yield db.fs.files.find().count())) self.assertEqual(1, (yield db.fs.chunks.find().count())) g = yield motor.MotorGridOut(db.fs, f._id).open() self.assertEqual(b(""), (yield g.read()))
async def test_alternate_collection(self): await self.db.alt.files.delete_many({}) await self.db.alt.chunks.delete_many({}) f = motor.MotorGridIn(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.MotorGridOut(self.db.alt, f._id) self.assertEqual(b"hello world", (await g.read()))
def __init__( self, root_collection, file_id=None, file_document=None, session=None, delegate=None ): """Can be created with collection and kwargs like a PyMongo GridOut, or with a 'delegate' keyword arg, where delegate is a MotorGridOut. """ if delegate: self.delegate = delegate else: if not isinstance(root_collection, Collection): raise TypeError("Expected Collection, got %s" % repr(root_collection)) self.delegate = motor.MotorGridOut( root_collection.delegate, file_id, file_document, session=session )
async def test_alternate_collection(self): await self.db.alt.files.delete_many({}) await self.db.alt.chunks.delete_many({}) f = motor.MotorGridIn(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.MotorGridOut(self.db.alt, f._id) self.assertEqual(b"hello world", (await g.read())) # test that md5 still works... self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
def test_alternate_collection(self): yield self.db.alt.files.remove() yield self.db.alt.chunks.remove() f = motor.MotorGridIn(self.db.alt) yield f.write(b"hello world") yield f.close() self.assertEqual(1, (yield self.db.alt.files.find().count())) self.assertEqual(1, (yield self.db.alt.chunks.find().count())) g = motor.MotorGridOut(self.db.alt, f._id) self.assertEqual(b"hello world", (yield g.read())) # test that md5 still works... self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
def test_alternate_collection(self): db = self.cx.pymongo_test yield db.alt.files.remove() yield db.alt.chunks.remove() f = yield motor.MotorGridIn(db.alt).open() yield f.write(b("hello world")) yield f.close() self.assertEqual(1, (yield db.alt.files.find().count())) self.assertEqual(1, (yield db.alt.chunks.find().count())) g = yield motor.MotorGridOut(db.alt, f._id).open() self.assertEqual(b("hello world"), (yield g.read())) # test that md5 still works... self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
async def test_grid_out_default_opts(self): self.assertRaises(TypeError, motor.MotorGridOut, "foo") gout = motor.MotorGridOut(self.db.fs, 5) with self.assertRaises(NoFile): await gout.open() a = motor.MotorGridIn(self.db.fs) await a.close() b = await motor.MotorGridOut(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)
def test_attributes(self): f = motor.MotorGridIn(self.db.fs, filename="test", foo="bar", content_type="text") yield f.close() g = motor.MotorGridOut(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 g.open() for attr_name in attr_names: getattr(g, attr_name)
def test_readchunk(self): in_data = b'a' * 10 f = motor.MotorGridIn(self.db.fs, chunkSize=3) yield f.write(in_data) yield f.close() g = motor.MotorGridOut(self.db.fs, f._id) # This is starting to look like Lisp. self.assertEqual(3, len((yield g.readchunk()))) self.assertEqual(2, len((yield g.read(2)))) self.assertEqual(1, len((yield g.readchunk()))) self.assertEqual(3, len((yield g.read(3)))) self.assertEqual(1, len((yield g.readchunk()))) self.assertEqual(0, len((yield g.readchunk())))
def test_grid_out_custom_opts(self): one = motor.MotorGridIn( 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 one.write(b"hello world") yield one.close() two = yield motor.MotorGridOut(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)
def test_grid_out_custom_opts(self): db = self.cx.pymongo_test one = yield motor.MotorGridIn(db.fs, _id=5, filename="my_file", contentType="text/html", chunkSize=1000, aliases=["foo"], metadata={ "foo": 1, "bar": 2 }, bar=3, baz="hello").open() yield one.write(b("hello world")) yield one.close() two = yield motor.MotorGridOut(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) for attr in [ "_id", "name", "content_type", "length", "chunk_size", "upload_date", "aliases", "metadata", "md5" ]: self.assertRaises(AttributeError, setattr, two, attr, 5)