def test_flush_close(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) file = GridFile({"filename": "test"}, self.db, "w") file.flush() file.close() file.close() self.assertRaises(ValueError, file.write, "test") file = GridFile({}, self.db) self.assertEqual(file.read(), "") file.close() file = GridFile({"filename": "test"}, self.db, "w") file.write("mike") file.flush() file.write("test") file.flush() file.write("huh") file.flush() file.flush() file.close() file.close() self.assertRaises(ValueError, file.write, "test") file = GridFile({}, self.db) self.assertEqual(file.read(), "miketesthuh") file.close()
def test_basic(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) self.assertEqual(self.db.fs.files.find().count(), 0) self.assertEqual(self.db.fs.chunks.find().count(), 0) file = GridFile({"filename": "test"}, self.db, "w") file.write("hello world") file.close() self.assertEqual(self.db.fs.files.find().count(), 1) self.assertEqual(self.db.fs.chunks.find().count(), 1) file = GridFile({"filename": "test"}, self.db) self.assertEqual(file.read(), "hello world") file.close() # make sure it's still there... file = GridFile({"filename": "test"}, self.db) self.assertEqual(file.read(), "hello world") file.close() file = GridFile({"filename": "test"}, self.db, "w") file.close() self.assertEqual(self.db.fs.files.find().count(), 1) self.assertEqual(self.db.fs.chunks.find().count(), 0) file = GridFile({"filename": "test"}, self.db) self.assertEqual(file.read(), "") file.close()
def test_alternate_collection(self): self.db.pymongo_test.files.remove({}) self.db.pymongo_test.chunks.remove({}) self.assertEqual(self.db.pymongo_test.files.find().count(), 0) self.assertEqual(self.db.pymongo_test.chunks.find().count(), 0) file = GridFile({"filename": "test"}, self.db, "w", collection="pymongo_test") file.write("hello world") file.close() self.assertEqual(self.db.pymongo_test.files.find().count(), 1) self.assertEqual(self.db.pymongo_test.chunks.find().count(), 1) file = GridFile({"filename": "test"}, self.db, collection="pymongo_test") self.assertEqual(file.read(), "hello world") file.close() # test that md5 still works... self.assertEqual(file.md5, "5eb63bbbe01eeed093cb22bb8f5acdc3") # make sure it's still there... file = GridFile({"filename": "test"}, self.db, collection="pymongo_test") self.assertEqual(file.read(), "hello world") file.close() file = GridFile({"filename": "test"}, self.db, "w", collection="pymongo_test") file.close() self.assertEqual(self.db.pymongo_test.files.find().count(), 1) self.assertEqual(self.db.pymongo_test.chunks.find().count(), 0) file = GridFile({"filename": "test"}, self.db, collection="pymongo_test") self.assertEqual(file.read(), "") file.close()
def test_multiple_reads(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) file = GridFile({"filename": "test"}, self.db, "w") file.write("hello world") file.close() file = GridFile({"filename": "test"}, self.db, "r") self.assertEqual(file.read(2), "he") self.assertEqual(file.read(2), "ll") self.assertEqual(file.read(2), "o ") self.assertEqual(file.read(2), "wo") self.assertEqual(file.read(2), "rl") self.assertEqual(file.read(2), "d") self.assertEqual(file.read(2), "") file.close()
def test_modes(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) file = GridFile({"filename": "test"}, self.db, "w") self.assertRaises(ValueError, file.read) file.write("hello") file.close() self.assertRaises(ValueError, file.read) self.assertRaises(ValueError, file.write, "hello") file = GridFile({"filename": "test"}, self.db, "r") self.assertRaises(ValueError, file.write, "hello") file.read() file.close() self.assertRaises(ValueError, file.read) self.assertRaises(ValueError, file.write, "hello")
def helper(data): filename = qcheck.gen_printable_string(qcheck.lift(20))() f = GridFile({"filename": filename, "chunkSize": 1}, self.db, "w") f.write(data) f.close() self.files += 1 self.chunks += len(data) self.assertEqual(self.db.fs.files.find().count(), self.files) self.assertEqual(self.db.fs.chunks.find().count(), self.chunks) f = GridFile({"filename": filename}, self.db) self.assertEqual(f.read(), data) f.close() f = GridFile({"filename": filename}, self.db) self.assertEqual(f.read(10) + f.read(10), data) f.close() return True
def test_seek(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) file = GridFile({"filename": "test", "chunkSize": 3}, self.db, "w") file.write("hello world") self.assertRaises(ValueError, file.seek, 0) file.close() file = GridFile({"filename": "test"}, self.db, "r") self.assertEqual(file.read(), "hello world") file.seek(0) self.assertEqual(file.read(), "hello world") file.seek(1) self.assertEqual(file.read(), "ello world") self.assertRaises(IOError, file.seek, -1) file.seek(-3, _SEEK_END) self.assertEqual(file.read(), "rld") file.seek(0, _SEEK_END) self.assertEqual(file.read(), "") self.assertRaises(IOError, file.seek, -100, _SEEK_END) file.seek(3) file.seek(3, _SEEK_CUR) self.assertEqual(file.read(), "world") self.assertRaises(IOError, file.seek, -100, _SEEK_CUR) file.close()
def test_overwrite(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) file = GridFile({"filename": "test"}, self.db, "w") file.write("test") file.close() file = GridFile({"filename": "test"}, self.db, "w") file.write("mike") file.close() f = GridFile({}, self.db) self.assertEqual(f.read(), "mike") f.close()
def test_multi_chunk_file(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) random_string = qcheck.gen_string(qcheck.lift(300000))() file = GridFile({"filename": "test"}, self.db, "w") file.write(random_string) file.close() self.assertEqual(self.db.fs.files.find().count(), 1) self.assertEqual(self.db.fs.chunks.find().count(), 2) f = GridFile({}, self.db) self.assertEqual(f.read(), random_string) f.close()
def test_tell(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) file = GridFile({"filename": "test", "chunkSize": 3}, self.db, "w") file.write("hello world") self.assertRaises(ValueError, file.tell) file.close() file = GridFile({"filename": "test"}, self.db, "r") self.assertEqual(file.tell(), 0) file.read(0) self.assertEqual(file.tell(), 0) file.read(1) self.assertEqual(file.tell(), 1) file.read(2) self.assertEqual(file.tell(), 3) file.read() self.assertEqual(file.tell(), file.length) file.close()
def test_read_chunks_unaligned_buffer_size(self): self.db.fs.files.remove({}) self.db.fs.chunks.remove({}) in_data = "This is a text that doesn't quite fit in a single 16-byte chunk." f = GridFile({"filename":"test", "chunkSize":16}, self.db, "w") f.write(in_data) f.close() f = GridFile({"filename":"test"}, self.db) out_data = '' while 1: s = f.read(13) if not s: break out_data += s f.close() self.assertEqual(in_data, out_data)