Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use inlinecallbacks, added tests #114

Merged
merged 2 commits into from
Jun 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions 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,11 +147,14 @@ 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",
content_type="text/plain", chunk_size=2**2**2**2)
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):
Expand All @@ -162,11 +166,14 @@ def test_GridFileObjects(self):
grid_in_file.test = 1
yield grid_in_file.write("0xDEADBEEF")
yield grid_in_file.write("0xDEADBEEF"*1048576)
fake_doc = {"_id": "test_id", "length": 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())
Expand All @@ -177,11 +184,12 @@ def test_GridFileObjects(self):
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(["0xDEADBEEF", "0xDEADBEAF"])
yield grid_in_file.close()
if _version.version.major >= 15:
with self.assertRaises(AttributeError):
grid_in_file.test = 2
grid_in_file.length = 1
self.assertEqual(1, grid_in_file.test)
if _version.version.major >= 15:
with self.assertRaises(AttributeError):
Expand Down Expand Up @@ -273,6 +281,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
3 changes: 0 additions & 3 deletions tests/test_replicaset.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,10 @@ def setUp(self):

yield master.disconnect()


@defer.inlineCallbacks
def tearDown(self):
yield defer.gatherResults([mongo.stop() for mongo in self.__mongod])


@defer.inlineCallbacks
def test_WriteToMaster(self):
conn = MongoConnection("localhost", self.ports[0])
Expand All @@ -119,7 +117,6 @@ def test_SlaveOk(self):
finally:
yield conn.disconnect()


@defer.inlineCallbacks
def test_SwitchToMasterOnConnect(self):
# Reverse hosts order
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