diff --git a/tests/test_objects.py b/tests/test_objects.py index ccea3d9..5fe84ca 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -12,16 +12,17 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. -import os import time from StringIO import StringIO +import os from bson import objectid, timestamp import txmongo from txmongo import database from txmongo import collection from txmongo.gridfs import GridFS, GridIn, GridOut, GridOutIterator, errors from txmongo import filter as qf +from txmongo._gridfs.errors import NoFile from twisted.trial import unittest from twisted.internet import base, defer from twisted import _version @@ -146,7 +147,9 @@ def test_GridFileObjects(self): 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", @@ -163,6 +166,7 @@ def test_GridFileObjects(self): yield grid_in_file.write("0xDEADBEEF") yield grid_in_file.write("0xDEADBEEF"*1048576) fake_doc = {"_id": "test_id", "length": 1048576} + self.assertRaises(TypeError, GridOut, None, None) grid_out_file = GridOut(db.fs, fake_doc) if _version.version.major >= 15: with self.assertRaises(AttributeError): @@ -273,6 +277,10 @@ def test_GridFsOperations(self): try: # Tests writing to a new gridfs file gfs = GridFS(db) # Default collection + if _version.version.major >= 15: + with self.assertRaises(NoFile): + yield gfs.get_last_version("optest") + g_in = gfs.new_file(filename="optest", contentType="text/plain", chunk_size=65536) # non-default chunk size used # yielding to ensure writes complete before we close and close before we try to read diff --git a/txmongo/_gridfs/__init__.py b/txmongo/_gridfs/__init__.py index a47eb1b..5b766e4 100644 --- a/txmongo/_gridfs/__init__.py +++ b/txmongo/_gridfs/__init__.py @@ -115,6 +115,7 @@ def get(self, file_id): defer.returnValue(GridOut(self.__collection, doc)) + @defer.inlineCallbacks def get_last_version(self, filename): """Get a file from GridFS by ``"filename"``. @@ -132,23 +133,15 @@ def get_last_version(self, filename): .. versionadded:: 1.6 """ - self.__files.ensure_index(filter.sort(ASCENDING("filename") + - DESCENDING("uploadDate"))) - - d = self.__files.find({"filename": filename}, - filter=filter.sort(DESCENDING("uploadDate"))) - d.addCallback(self._cb_get_last_version, filename) - return d - - # cursor.limit(-1).sort("uploadDate", -1)#DESCENDING) + self.__files.ensure_index(filter.sort(ASCENDING("filename") + DESCENDING("uploadDate"))) - def _cb_get_last_version(self, docs, filename): - try: - grid_file = docs[0] - return GridOut(self.__collection, grid_file) - except IndexError: + doc = yield self.__files.find_one({"filename": filename}, + filter=filter.sort(DESCENDING("uploadDate"))) + if doc is None: raise NoFile("no file in gridfs with filename %r" % filename) + defer.returnValue(GridOut(self.__collection, doc)) + # TODO add optional safe mode for chunk removal? def delete(self, file_id): """Delete a file from GridFS by ``"_id"``.