def test_GridFileObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test db.fs.files.remove({}) # drop all objects there first db.fs.chunks.remove({}) self.assertRaises(TypeError, GridFS, None) _ = GridFS(db) # Default collection self.assertRaises(TypeError, GridIn, None) with GridIn(db.fs, filename="test_with", contentType="text/plain", chunk_size=1024): pass grid_in_file = GridIn(db.fs, filename="test_1", contentType="text/plain", content_type="text/plain", chunk_size=65536, length=1048576, upload_date="20150101") self.assertFalse(grid_in_file.closed) if _version.version.major >= 15: with self.assertRaises(TypeError): yield grid_in_file.write(1) with self.assertRaises(TypeError): yield grid_in_file.write(u"0xDEADBEEF") with self.assertRaises(AttributeError): _ = grid_in_file.test grid_in_file.test = 1 yield grid_in_file.write(b"0xDEADBEEF") yield grid_in_file.write(b"0xDEADBEEF"*1048576) fake_doc = {"_id": "test_id", "length": 1048576, "filename": "test", "upload_date": "20150101"} self.assertRaises(TypeError, GridOut, None, None) grid_out_file = GridOut(db.fs, fake_doc) if _version.version.major >= 15: with self.assertRaises(AttributeError): _ = grid_out_file.testing self.assertEqual("test", grid_out_file.filename) self.assertEqual(0, grid_out_file.tell()) grid_out_file.seek(1024) self.assertEqual(1024, grid_out_file.tell()) grid_out_file.seek(1024, os.SEEK_CUR) self.assertEqual(2048, grid_out_file.tell()) grid_out_file.seek(0, os.SEEK_END) self.assertEqual(1048576, grid_out_file.tell()) self.assertRaises(IOError, grid_out_file.seek, 0, 4) self.assertRaises(IOError, grid_out_file.seek, -1) self.assertTrue("'_id': 'test_id'" in repr(grid_out_file)) self.assertTrue("20150101", grid_in_file.upload_date) yield grid_in_file.writelines([b"0xDEADBEEF", b"0xDEADBEAF"]) yield grid_in_file.close() if _version.version.major >= 15: with self.assertRaises(AttributeError): grid_in_file.length = 1 self.assertEqual(1, grid_in_file.test) if _version.version.major >= 15: with self.assertRaises(AttributeError): _ = grid_in_file.test_none self.assertTrue(grid_in_file.closed) if _version.version.major >= 15: with self.assertRaises(ValueError): yield grid_in_file.write(b"0xDEADBEEF") yield conn.disconnect()
def test_GridFileObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test yield self._drop_gridfs(db) self.assertRaises(TypeError, GridFS, None) _ = GridFS(db) # Default collection self.assertRaises(TypeError, GridIn, None) with GridIn(db.fs, filename="test_with", contentType="text/plain", chunk_size=1024): pass grid_in_file = GridIn(db.fs, filename="test_1", contentType="text/plain", content_type="text/plain", chunk_size=65536, length=1048576, upload_date="20150101") self.assertFalse(grid_in_file.closed) if twisted_version.major >= 15: with self.assertRaises(TypeError): yield grid_in_file.write(1) with self.assertRaises(TypeError): yield grid_in_file.write(u"0xDEADBEEF") with self.assertRaises(AttributeError): _ = grid_in_file.test grid_in_file.test = 1 yield grid_in_file.write(b"0xDEADBEEF") yield grid_in_file.write(b"0xDEADBEEF"*1048576) self.assertTrue("20150101", grid_in_file.upload_date) yield grid_in_file.writelines([b"0xDEADBEEF", b"0xDEADBEEF"]) yield grid_in_file.close() if twisted_version.major >= 15: with self.assertRaises(AttributeError): grid_in_file.length = 1 self.assertEqual(1, grid_in_file.test) if twisted_version.major >= 15: with self.assertRaises(AttributeError): _ = grid_in_file.test_none self.assertTrue(grid_in_file.closed) if twisted_version.major >= 15: with self.assertRaises(ValueError): yield grid_in_file.write(b"0xDEADBEEF") doc = yield db.fs.files.find_one({"_id": grid_in_file._id}) grid_out_file = GridOut(db.fs, doc) grid_out_file.seek(0, os.SEEK_END) self.assertEqual(10 * (1 + 1048576 + 2), grid_out_file.tell()) yield grid_out_file.close() data = b'' for block_dfr in GridOutIterator(grid_out_file, db.fs.chunks): block = yield block_dfr if block: data += block else: break self.assertEqual(data, b"0xDEADBEEF" * (1 + 1048576 + 2)) fake_doc = {"_id": "test_id", "length": 1048576, "filename": "test", "upload_date": "20150101"} self.assertRaises(TypeError, GridOut, None, None) grid_out_file = GridOut(db.fs, fake_doc) if twisted_version.major >= 15: with self.assertRaises(AttributeError): _ = grid_out_file.testing self.assertEqual("test", grid_out_file.filename) self.assertEqual(0, grid_out_file.tell()) grid_out_file.seek(1024) self.assertEqual(1024, grid_out_file.tell()) grid_out_file.seek(1024, os.SEEK_CUR) self.assertEqual(2048, grid_out_file.tell()) grid_out_file.seek(0, os.SEEK_END) self.assertEqual(1048576, grid_out_file.tell()) self.assertRaises(IOError, grid_out_file.seek, 0, 4) self.assertRaises(IOError, grid_out_file.seek, -1) self.assertTrue("'_id': 'test_id'" in repr(grid_out_file)) yield conn.disconnect()
def test_GridFileObjects(self): """ Tests gridfs objects """ conn = yield txmongo.MongoConnection(mongo_host, mongo_port) db = conn.test yield self._drop_gridfs(db) self.assertRaises(TypeError, GridFS, None) _ = GridFS(db) # Default collection self.assertRaises(TypeError, GridIn, None) with GridIn(db.fs, filename="test_with", contentType="text/plain", chunk_size=1024): pass grid_in_file = GridIn(db.fs, filename="test_1", contentType="text/plain", content_type="text/plain", chunk_size=65536, length=1048576, upload_date="20150101") self.assertFalse(grid_in_file.closed) if twisted_version.major >= 15: with self.assertRaises(TypeError): yield grid_in_file.write(1) with self.assertRaises(TypeError): yield grid_in_file.write(u"0xDEADBEEF") with self.assertRaises(AttributeError): _ = grid_in_file.test grid_in_file.test = 1 yield grid_in_file.write(b"0xDEADBEEF") yield grid_in_file.write(b"0xDEADBEEF" * 1048576) self.assertTrue("20150101", grid_in_file.upload_date) yield grid_in_file.writelines([b"0xDEADBEEF", b"0xDEADBEEF"]) yield grid_in_file.close() if twisted_version.major >= 15: with self.assertRaises(AttributeError): grid_in_file.length = 1 self.assertEqual(1, grid_in_file.test) if twisted_version.major >= 15: with self.assertRaises(AttributeError): _ = grid_in_file.test_none self.assertTrue(grid_in_file.closed) if twisted_version.major >= 15: with self.assertRaises(ValueError): yield grid_in_file.write(b"0xDEADBEEF") doc = yield db.fs.files.find_one({"_id": grid_in_file._id}) grid_out_file = GridOut(db.fs, doc) grid_out_file.seek(0, os.SEEK_END) self.assertEqual(10 * (1 + 1048576 + 2), grid_out_file.tell()) yield grid_out_file.close() data = b'' for block_dfr in GridOutIterator(grid_out_file, db.fs.chunks): block = yield block_dfr if block: data += block else: break self.assertEqual(data, b"0xDEADBEEF" * (1 + 1048576 + 2)) fake_doc = { "_id": "test_id", "length": 1048576, "filename": "test", "upload_date": "20150101" } self.assertRaises(TypeError, GridOut, None, None) grid_out_file = GridOut(db.fs, fake_doc) if twisted_version.major >= 15: with self.assertRaises(AttributeError): _ = grid_out_file.testing self.assertEqual("test", grid_out_file.filename) self.assertEqual(0, grid_out_file.tell()) grid_out_file.seek(1024) self.assertEqual(1024, grid_out_file.tell()) grid_out_file.seek(1024, os.SEEK_CUR) self.assertEqual(2048, grid_out_file.tell()) grid_out_file.seek(0, os.SEEK_END) self.assertEqual(1048576, grid_out_file.tell()) self.assertRaises(IOError, grid_out_file.seek, 0, 4) self.assertRaises(IOError, grid_out_file.seek, -1) self.assertTrue("'_id': 'test_id'" in repr(grid_out_file)) yield conn.disconnect()