Skip to content

Commit

Permalink
Merge d81649c into 6669ec4
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaSkriblovsky committed Jun 4, 2015
2 parents 6669ec4 + d81649c commit cd24106
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 187 deletions.
3 changes: 1 addition & 2 deletions docs/source/NEWS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ Features
or by URI options.
- Write-concern options can also be set for ``Database`` and ``Collection`` with ``write_concern``
named argument of their constructors. In this case write-concern is specified by instance of
``txmongo.write_concern.WriteConcern`` class which is the same as
``pymongo.write_concern.WriteConcern`` of PyMongo 3.0.
``pymongo.write_concern.WriteConcern``
- ``txmongo.protocol.INSERT_CONTINUE_ON_ERROR`` flag defined for using with ``insert()``
- Replaced all traditional deferred callbacks (and errbacks) to use @defer.inlineCallbacks

Expand Down
2 changes: 1 addition & 1 deletion tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
from pymongo.results import InsertOneResult, InsertManyResult, UpdateResult, \
DeleteResult
from pymongo.collection import ReturnDocument
from pymongo.write_concern import WriteConcern
from twisted.internet import defer
from twisted.trial import unittest
import txmongo
from txmongo.protocol import MongoClientProtocol
from txmongo.write_concern import WriteConcern
import txmongo.filter as qf

mongo_host = "localhost"
Expand Down
51 changes: 1 addition & 50 deletions tests/test_write_concern.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@

from mock import Mock, patch
from pymongo.errors import ConfigurationError
from pymongo.write_concern import WriteConcern
from twisted.internet import defer
from twisted.trial import unittest
from txmongo.connection import MongoConnection, ConnectionPool
from txmongo.protocol import MongoProtocol
from txmongo.database import Database
from txmongo.collection import Collection
from txmongo.write_concern import WriteConcern

mongo_host = "localhost"
mongo_port = 27017
Expand Down Expand Up @@ -151,52 +151,3 @@ def test_ConnectionUrlParams(self):
finally:
yield coll.drop()
yield conn.disconnect()


class TestWriteConcernClass(unittest.TestCase):

def test_wtimeout(self):
wc = WriteConcern(wtimeout=123)
self.assertEqual(wc.document['wtimeout'], 123)

self.assertRaises(TypeError, WriteConcern, wtimeout=123.456)

def test_j(self):
wc = WriteConcern(j=True)
self.assertEqual(wc.document['j'], True)

self.assertRaises(TypeError, WriteConcern, j=1)

def test_fsync(self):
wc = WriteConcern(fsync=True)
self.assertEqual(wc.document['fsync'], True)

self.assertRaises(TypeError, WriteConcern, fsync=1)
# Can't set both j and fsync
self.assertRaises(ConfigurationError, WriteConcern, j=True, fsync=True)

def test_w(self):
WriteConcern(w=0)

# Can't set w=0 with any other options
self.assertRaises(ConfigurationError, WriteConcern, w=0, j=True)
self.assertRaises(ConfigurationError, WriteConcern, w=0, wtimeout=100)
self.assertRaises(ConfigurationError, WriteConcern, w=0, fsync=True)

self.assertRaises(TypeError, WriteConcern, w=1.5)

def test_repr(self):
self.assertEqual(repr(WriteConcern()), "WriteConcern()")
self.assertEqual(repr(WriteConcern(w=2)), "WriteConcern(w=2)")
self.assertEqual(repr(WriteConcern(fsync=True)), "WriteConcern(fsync=True)")

multiopt = repr(WriteConcern(w=2, wtimeout=500, fsync=True))
self.assertTrue(multiopt.startswith("WriteConcern("))
self.assertTrue(multiopt.endswith(')'))
inner = multiopt[13:-1]
self.assertEqual(set(inner.split(", ")), set(["w=2", "wtimeout=500", "fsync=True"]))

def test_cmp(self):
self.assertEqual(WriteConcern(w=2, wtimeout=500), WriteConcern(wtimeout=500, w=2))
self.assertNotEqual(WriteConcern(w=2, wtimeout=500),
WriteConcern(wtimeout=500, w=2, j=True))
12 changes: 3 additions & 9 deletions txmongo/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
from pymongo.common import validate_ok_for_update, validate_ok_for_replace, \
validate_is_mapping, validate_boolean
from pymongo.collection import ReturnDocument
from pymongo.write_concern import WriteConcern
from txmongo import filter as qf
from txmongo.protocol import DELETE_SINGLE_REMOVE, UPDATE_UPSERT, UPDATE_MULTI, \
Query, Getmore, Insert, Update, Delete, KillCursors, INSERT_CONTINUE_ON_ERROR
from twisted.internet import defer
from txmongo.write_concern import WriteConcern


class Collection(object):
Expand Down Expand Up @@ -189,14 +189,8 @@ def after_reply(reply, proto, fetched=0):
docs_count = min(docs_count, limit - fetched)
fetched += docs_count

try:
# as_class is removed from PyMongo >= 3.0
# trying to use CodecOptions instead
options = bson.codec_options.CodecOptions(document_class=as_class)
out = [document.decode(codec_options=options) for document in documents[:docs_count]]
except AttributeError:
# Falling back to as_class for PyMongo < 3.0
out = [document.decode(as_class=as_class) for document in documents[:docs_count]]
options = bson.codec_options.CodecOptions(document_class=as_class)
out = [document.decode(codec_options=options) for document in documents[:docs_count]]

if reply.cursor_id:
if limit == 0:
Expand Down
15 changes: 5 additions & 10 deletions txmongo/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,18 @@
from pymongo.errors import AutoReconnect, ConfigurationError, OperationFailure
from pymongo.uri_parser import parse_uri
from pymongo.read_preferences import ReadPreference
from pymongo.write_concern import WriteConcern

from twisted.internet import defer, reactor, task
from twisted.internet.protocol import ReconnectingClientFactory
from twisted.python import log

from txmongo.database import Database
from txmongo.protocol import MongoProtocol, Query
from txmongo.write_concern import WriteConcern

# PyMongo 2.x defines ReadPreference.XXX as ints while 3.0 defines them
# as objects with `mode` integer attribute
try:
_PRIMARY_READ_PREFERENCES = set([ReadPreference.PRIMARY.mode,
ReadPreference.PRIMARY_PREFERRED.mode])
except AttributeError:
_PRIMARY_READ_PREFERENCES = set([ReadPreference.PRIMARY,
ReadPreference.PRIMARY_PREFERRED])


_PRIMARY_READ_PREFERENCES = set([ReadPreference.PRIMARY.mode,
ReadPreference.PRIMARY_PREFERRED.mode])


class _Connection(ReconnectingClientFactory):
Expand Down
115 changes: 0 additions & 115 deletions txmongo/write_concern.py

This file was deleted.

0 comments on commit cd24106

Please sign in to comment.