Skip to content

Commit

Permalink
use inlinecallbacks, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
psi29a committed Jun 2, 2015
1 parent 1d3e315 commit 297aeb3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
10 changes: 9 additions & 1 deletion tests/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand All @@ -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):
Expand Down Expand Up @@ -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
Expand Down
21 changes: 7 additions & 14 deletions txmongo/_gridfs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"``.
Expand All @@ -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"``.
Expand Down

0 comments on commit 297aeb3

Please sign in to comment.