PYTHON-676 python 2/3 single-source for the test module
This commit is contained in:
parent
69ce42c5a0
commit
4d6d1e067b
1
.gitignore
vendored
1
.gitignore
vendored
@ -12,6 +12,5 @@ tools/settings.py
|
||||
pymongo.egg-info/
|
||||
*.so
|
||||
nosetests.xml
|
||||
setup.cfg
|
||||
*.egg
|
||||
.tox
|
||||
|
||||
@ -20,7 +20,9 @@ PY3 = sys.version_info[0] == 3
|
||||
|
||||
if PY3:
|
||||
import codecs
|
||||
import _thread as thread
|
||||
from io import BytesIO as StringIO
|
||||
MAXSIZE = sys.maxsize
|
||||
|
||||
def b(s):
|
||||
# BSON and socket operations deal in binary data. In
|
||||
@ -60,13 +62,13 @@ else:
|
||||
from cStringIO import StringIO
|
||||
except ImportError:
|
||||
from StringIO import StringIO
|
||||
import thread
|
||||
MAXSIZE = sys.maxint
|
||||
|
||||
def b(s):
|
||||
# See comments above. In python 2.x b('foo') is just 'foo'.
|
||||
return s
|
||||
|
||||
import codecs
|
||||
|
||||
def u(s):
|
||||
"""Replacement for unicode literal prefix."""
|
||||
return unicode(s.replace('\\', '\\\\'), 'unicode_escape')
|
||||
|
||||
71
setup.py
71
setup.py
@ -1,4 +1,3 @@
|
||||
import glob
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
@ -12,12 +11,6 @@ try:
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
try:
|
||||
from ConfigParser import SafeConfigParser
|
||||
except ImportError:
|
||||
# PY3
|
||||
from configparser import SafeConfigParser
|
||||
|
||||
# Don't force people to install setuptools unless
|
||||
# we have to.
|
||||
try:
|
||||
@ -44,8 +37,6 @@ try:
|
||||
finally:
|
||||
f.close()
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
# PYTHON-654 - Clang doesn't support -mno-fused-madd but the pythons Apple
|
||||
# ships are built with it. This is a problem starting with Xcode 5.1
|
||||
# since clang 3.4 errors out when it encounters unrecognized compiler
|
||||
@ -62,31 +53,6 @@ if sys.platform == 'darwin' and 'clang' in platform.python_compiler().lower():
|
||||
flags = re.sub('-mno-fused-madd', '', flags)
|
||||
res[key] = flags
|
||||
|
||||
nose_config_options = {
|
||||
'with-xunit': '1', # Write out nosetests.xml for CI.
|
||||
'py3where': 'build', # Tell nose where to find tests under PY3.
|
||||
}
|
||||
|
||||
def write_nose_config():
|
||||
"""Write out setup.cfg. Since py3where has to be set
|
||||
for tests to run correctly in Python 3 we create this
|
||||
on the fly.
|
||||
"""
|
||||
config = SafeConfigParser()
|
||||
config.add_section('nosetests')
|
||||
for opt, val in nose_config_options.items():
|
||||
config.set('nosetests', opt, val)
|
||||
try:
|
||||
cf = open('setup.cfg', 'w')
|
||||
config.write(cf)
|
||||
finally:
|
||||
cf.close()
|
||||
|
||||
|
||||
should_run_tests = False
|
||||
if "test" in sys.argv or "nosetests" in sys.argv:
|
||||
should_run_tests = True
|
||||
|
||||
|
||||
class doc(Command):
|
||||
|
||||
@ -191,24 +157,11 @@ http://api.mongodb.org/python/current/installation.html#osx
|
||||
"your platform configuration"
|
||||
" - see above."))
|
||||
|
||||
def set_nose_options(self):
|
||||
# Under python 3 we need to tell nose where to find the
|
||||
# proper tests. if we built the C extensions this will be
|
||||
# someplace like build/lib.<os>-<arch>-<python version>
|
||||
if PY3:
|
||||
ver = '.'.join(map(str, sys.version_info[:2]))
|
||||
lib_dirs = glob.glob(os.path.join('build', 'lib*' + ver))
|
||||
if lib_dirs:
|
||||
nose_config_options['py3where'] = lib_dirs[0]
|
||||
write_nose_config()
|
||||
|
||||
def build_extension(self, ext):
|
||||
name = ext.name
|
||||
if sys.version_info[:3] >= (2, 6, 0):
|
||||
try:
|
||||
build_ext.build_extension(self, ext)
|
||||
if should_run_tests:
|
||||
self.set_nose_options()
|
||||
except build_errors:
|
||||
e = sys.exc_info()[1]
|
||||
sys.stdout.write('%s\n' % str(e))
|
||||
@ -262,30 +215,6 @@ Performance may be degraded.\n
|
||||
else:
|
||||
extra_opts['ext_modules'] = ext_modules
|
||||
|
||||
if PY3:
|
||||
extra_opts["use_2to3"] = True
|
||||
if should_run_tests:
|
||||
# Distribute isn't smart enough to copy the
|
||||
# tests and run 2to3 on them. We don't want to
|
||||
# install the test suite so only do this if we
|
||||
# are testing.
|
||||
# https://bitbucket.org/tarek/distribute/issue/233
|
||||
extra_opts["packages"].append("test")
|
||||
extra_opts['package_data'] = {"test": ["certificates/ca.pem",
|
||||
"certificates/client.pem"]}
|
||||
# Hack to make "python3.x setup.py nosetests" work in python 3
|
||||
# otherwise it won't run 2to3 before running the tests.
|
||||
if "nosetests" in sys.argv:
|
||||
sys.argv.remove("nosetests")
|
||||
sys.argv.append("test")
|
||||
# All "nosetests" does is import and run nose.main.
|
||||
extra_opts["test_suite"] = "nose.main"
|
||||
|
||||
# This may be called a second time if
|
||||
# we are testing with C extensions.
|
||||
if should_run_tests:
|
||||
write_nose_config()
|
||||
|
||||
setup(
|
||||
name="pymongo",
|
||||
version=version,
|
||||
|
||||
@ -18,20 +18,21 @@
|
||||
import os
|
||||
import warnings
|
||||
|
||||
from bson.py3compat import _unicode
|
||||
import pymongo
|
||||
from pymongo.errors import ConnectionFailure
|
||||
|
||||
# hostnames retrieved by MongoReplicaSetClient from isMaster will be of unicode
|
||||
# type in Python 2, so ensure these hostnames are unicodes, too. It makes tests
|
||||
# like `test_repr` predictable.
|
||||
host = unicode(os.environ.get("DB_IP", 'localhost'))
|
||||
host = _unicode(os.environ.get("DB_IP", 'localhost'))
|
||||
port = int(os.environ.get("DB_PORT", 27017))
|
||||
pair = '%s:%d' % (host, port)
|
||||
|
||||
host2 = unicode(os.environ.get("DB_IP2", 'localhost'))
|
||||
host2 = _unicode(os.environ.get("DB_IP2", 'localhost'))
|
||||
port2 = int(os.environ.get("DB_PORT2", 27018))
|
||||
|
||||
host3 = unicode(os.environ.get("DB_IP3", 'localhost'))
|
||||
host3 = _unicode(os.environ.get("DB_IP3", 'localhost'))
|
||||
port3 = int(os.environ.get("DB_PORT3", 27019))
|
||||
|
||||
# Make sure warnings are always raised, regardless of
|
||||
|
||||
@ -58,7 +58,7 @@ def kill_members(members, sig, hosts=nodes):
|
||||
for member in sorted(members):
|
||||
try:
|
||||
if ha_tools_debug:
|
||||
print('killing %s' % (member,)),
|
||||
print('killing %s' % (member,))
|
||||
proc = hosts[member]['proc']
|
||||
if 'java' in sys.platform:
|
||||
# _process is a wrapped java.lang.UNIXProcess.
|
||||
@ -279,7 +279,7 @@ def get_client():
|
||||
read_preference=ReadPreference.PRIMARY_PREFERRED,
|
||||
use_greenlets=use_greenlets)
|
||||
except pymongo.errors.ConnectionFailure:
|
||||
if i == len(nodes.keys()) - 1:
|
||||
if i == len(nodes) - 1:
|
||||
raise
|
||||
|
||||
|
||||
@ -419,7 +419,7 @@ def add_member(auth=False):
|
||||
cmd += ['--keyFile', key_file]
|
||||
|
||||
if ha_tools_debug:
|
||||
print 'starting', ' '.join(cmd)
|
||||
print('starting %s' % ' '.join(cmd))
|
||||
|
||||
proc = subprocess.Popen(cmd,
|
||||
stdout=subprocess.PIPE,
|
||||
@ -433,11 +433,11 @@ def add_member(auth=False):
|
||||
config['version'] += 1
|
||||
|
||||
if ha_tools_debug:
|
||||
print {'replSetReconfig': config}
|
||||
print({'replSetReconfig': config})
|
||||
|
||||
response = c.admin.command({'replSetReconfig': config})
|
||||
if ha_tools_debug:
|
||||
print response
|
||||
print(response)
|
||||
|
||||
if not res:
|
||||
return None
|
||||
|
||||
@ -165,7 +165,7 @@ class TestDirectConnection(HATestCase):
|
||||
# should do the same for unacknowledged writes.
|
||||
try:
|
||||
client.pymongo_test.test.insert({}, w=0)
|
||||
except AutoReconnect, e:
|
||||
except AutoReconnect as e:
|
||||
self.assertEqual('not master', e.args[0])
|
||||
else:
|
||||
self.fail(
|
||||
@ -181,7 +181,7 @@ class TestDirectConnection(HATestCase):
|
||||
# See explanation above
|
||||
try:
|
||||
client.pymongo_test.test.insert({}, w=0)
|
||||
except AutoReconnect, e:
|
||||
except AutoReconnect as e:
|
||||
self.assertEqual('not master', e.args[0])
|
||||
else:
|
||||
self.fail(
|
||||
@ -486,7 +486,7 @@ class TestReadWithFailover(HATestCase):
|
||||
|
||||
db.read_preference = SECONDARY_PREFERRED
|
||||
cursor = db.test.find().batch_size(5)
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
self.assertEqual(5, cursor._Cursor__retrieved)
|
||||
self.assertTrue(cursor._Cursor__connection_id in c.secondaries)
|
||||
ha_tools.kill_primary()
|
||||
@ -975,7 +975,7 @@ class TestReplicaSetRequest(HATestCase):
|
||||
for _ in range(patience_seconds):
|
||||
try:
|
||||
if ha_tools.ha_tools_debug:
|
||||
print 'Waiting for failover'
|
||||
print('Waiting for failover')
|
||||
if ha_tools.get_primary():
|
||||
# We have a new primary
|
||||
break
|
||||
|
||||
@ -16,13 +16,18 @@
|
||||
"""
|
||||
|
||||
import sys
|
||||
import urllib2
|
||||
import thread
|
||||
import threading
|
||||
import time
|
||||
|
||||
from optparse import OptionParser
|
||||
|
||||
from bson.py3compat import PY3, thread
|
||||
|
||||
if PY3:
|
||||
from urllib.request import urlopen
|
||||
else:
|
||||
from urllib2 import urlopen
|
||||
|
||||
|
||||
def parse_args():
|
||||
parser = OptionParser("""usage: %prog [options] mode url
|
||||
@ -65,7 +70,7 @@ def parse_args():
|
||||
|
||||
|
||||
def get(url):
|
||||
urllib2.urlopen(url).read().strip()
|
||||
urlopen(url).read().strip()
|
||||
|
||||
|
||||
class URLGetterThread(threading.Thread):
|
||||
@ -84,8 +89,8 @@ class URLGetterThread(threading.Thread):
|
||||
for i in range(self.nrequests_per_thread):
|
||||
try:
|
||||
get(url)
|
||||
except Exception, e:
|
||||
print e
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
if not options.continue_:
|
||||
thread.interrupt_main()
|
||||
@ -101,7 +106,7 @@ class URLGetterThread(threading.Thread):
|
||||
should_print = options.verbose and not counter % 1000
|
||||
|
||||
if should_print:
|
||||
print counter
|
||||
print(counter)
|
||||
|
||||
|
||||
def main(options, mode, url):
|
||||
@ -130,33 +135,33 @@ def main(options, mode, url):
|
||||
errors = sum([t.errors for t in threads])
|
||||
nthreads_with_errors = len([t for t in threads if t.errors])
|
||||
if nthreads_with_errors:
|
||||
print '%d threads had errors! %d errors in total' % (
|
||||
nthreads_with_errors, errors)
|
||||
print('%d threads had errors! %d errors in total' % (
|
||||
nthreads_with_errors, errors))
|
||||
else:
|
||||
assert mode == 'serial'
|
||||
if options.verbose:
|
||||
print 'Getting %s %s times in one thread' % (
|
||||
print('Getting %s %s times in one thread' % (
|
||||
url, options.nrequests
|
||||
)
|
||||
))
|
||||
|
||||
for i in range(1, options.nrequests + 1):
|
||||
try:
|
||||
get(url)
|
||||
except Exception, e:
|
||||
print e
|
||||
except Exception as e:
|
||||
print(e)
|
||||
if not options.continue_:
|
||||
sys.exit(1)
|
||||
|
||||
errors += 1
|
||||
|
||||
if options.verbose and not i % 1000:
|
||||
print i
|
||||
print(i)
|
||||
|
||||
if errors:
|
||||
print '%d errors!' % errors
|
||||
print('%d errors!' % errors)
|
||||
|
||||
if options.verbose:
|
||||
print 'Completed in %.2f seconds' % (time.time() - start_time)
|
||||
print('Completed in %.2f seconds' % (time.time() - start_time))
|
||||
|
||||
if errors:
|
||||
# Failure
|
||||
|
||||
@ -22,15 +22,16 @@ sys.path[0:0] = [""]
|
||||
from bson.binary import Binary
|
||||
from bson.dbref import DBRef
|
||||
from bson.objectid import ObjectId
|
||||
from bson.py3compat import b, binary_type
|
||||
from bson.py3compat import MAXSIZE, PY3, binary_type, iteritems, u
|
||||
from bson.son import SON
|
||||
|
||||
if PY3:
|
||||
unichr = chr
|
||||
|
||||
gen_target = 100
|
||||
reduction_attempts = 10
|
||||
examples = 5
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
|
||||
def lift(value):
|
||||
return lambda: value
|
||||
@ -58,7 +59,7 @@ def gen_int():
|
||||
|
||||
|
||||
def gen_float():
|
||||
return lambda: (random.random() - 0.5) * sys.maxint
|
||||
return lambda: (random.random() - 0.5) * MAXSIZE
|
||||
|
||||
|
||||
def gen_boolean():
|
||||
@ -82,7 +83,7 @@ else:
|
||||
|
||||
|
||||
def gen_string(gen_length):
|
||||
return lambda: b("").join(gen_list(gen_char(), gen_length)())
|
||||
return lambda: b"".join(gen_list(gen_char(), gen_length)())
|
||||
|
||||
|
||||
def gen_unichar():
|
||||
@ -90,9 +91,9 @@ def gen_unichar():
|
||||
|
||||
|
||||
def gen_unicode(gen_length):
|
||||
return lambda: u"".join([x for x in
|
||||
gen_list(gen_unichar(), gen_length)() if
|
||||
x not in ".$"])
|
||||
return lambda: u("").join([x for x in
|
||||
gen_list(gen_unichar(), gen_length)() if
|
||||
x not in ".$"])
|
||||
|
||||
|
||||
def gen_list(generator, gen_length):
|
||||
@ -123,7 +124,7 @@ def gen_regexp(gen_length):
|
||||
# TODO our patterns only consist of one letter.
|
||||
# this is because of a bug in CPython's regex equality testing,
|
||||
# which I haven't quite tracked down, so I'm just ignoring it...
|
||||
pattern = lambda: u"".join(gen_list(choose_lifted(u"a"), gen_length)())
|
||||
pattern = lambda: u("").join(gen_list(choose_lifted(u("a")), gen_length)())
|
||||
|
||||
def gen_flags():
|
||||
flags = 0
|
||||
@ -188,15 +189,17 @@ def simplify(case): # TODO this is a hack
|
||||
simplified = SON(case) # make a copy!
|
||||
if random.choice([True, False]):
|
||||
# delete
|
||||
if not len(simplified.keys()):
|
||||
simplified_keys = list(simplified)
|
||||
if not len(simplified_keys):
|
||||
return (False, case)
|
||||
del simplified[random.choice(simplified.keys())]
|
||||
simplified.pop(random.choice(simplified_keys))
|
||||
return (True, simplified)
|
||||
else:
|
||||
# simplify a value
|
||||
if not len(simplified.items()):
|
||||
simplified_items = list(iteritems(simplified))
|
||||
if not len(simplified_items):
|
||||
return (False, case)
|
||||
(key, value) = random.choice(simplified.items())
|
||||
(key, value) = random.choice(simplified_items)
|
||||
(success, value) = simplify(value)
|
||||
simplified[key] = value
|
||||
return (success, success and simplified or case)
|
||||
|
||||
@ -19,7 +19,11 @@ import sys
|
||||
import threading
|
||||
import unittest
|
||||
|
||||
from urllib import quote_plus
|
||||
try:
|
||||
from urllib.parse import quote_plus
|
||||
except ImportError:
|
||||
# Python 2
|
||||
from urllib import quote_plus
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
@ -128,7 +132,7 @@ class TestGSSAPI(unittest.TestCase):
|
||||
mechanism='GSSAPI'))
|
||||
|
||||
threads = []
|
||||
for _ in xrange(4):
|
||||
for _ in range(4):
|
||||
threads.append(AutoAuthenticateThread(client.foo))
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
@ -147,7 +151,7 @@ class TestGSSAPI(unittest.TestCase):
|
||||
self.assertTrue(client.foo.command('dbstats'))
|
||||
|
||||
threads = []
|
||||
for _ in xrange(4):
|
||||
for _ in range(4):
|
||||
threads.append(AutoAuthenticateThread(client.foo))
|
||||
for thread in threads:
|
||||
thread.start()
|
||||
|
||||
@ -30,7 +30,7 @@ sys.path[0:0] = [""]
|
||||
import bson
|
||||
|
||||
from bson.binary import *
|
||||
from bson.py3compat import b, binary_type
|
||||
from bson.py3compat import binary_type, u
|
||||
from bson.son import SON
|
||||
from nose.plugins.skip import SkipTest
|
||||
from test.test_client import get_client
|
||||
@ -39,81 +39,81 @@ from pymongo.mongo_client import MongoClient
|
||||
class TestBinary(unittest.TestCase):
|
||||
def test_binary(self):
|
||||
a_string = "hello world"
|
||||
a_binary = Binary(b("hello world"))
|
||||
self.assertTrue(a_binary.startswith(b("hello")))
|
||||
self.assertTrue(a_binary.endswith(b("world")))
|
||||
a_binary = Binary(b"hello world")
|
||||
self.assertTrue(a_binary.startswith(b"hello"))
|
||||
self.assertTrue(a_binary.endswith(b"world"))
|
||||
self.assertTrue(isinstance(a_binary, Binary))
|
||||
self.assertFalse(isinstance(a_string, Binary))
|
||||
|
||||
def test_exceptions(self):
|
||||
self.assertRaises(TypeError, Binary, None)
|
||||
self.assertRaises(TypeError, Binary, u"hello")
|
||||
self.assertRaises(TypeError, Binary, u("hello"))
|
||||
self.assertRaises(TypeError, Binary, 5)
|
||||
self.assertRaises(TypeError, Binary, 10.2)
|
||||
self.assertRaises(TypeError, Binary, b("hello"), None)
|
||||
self.assertRaises(TypeError, Binary, b("hello"), "100")
|
||||
self.assertRaises(ValueError, Binary, b("hello"), -1)
|
||||
self.assertRaises(ValueError, Binary, b("hello"), 256)
|
||||
self.assertTrue(Binary(b("hello"), 0))
|
||||
self.assertTrue(Binary(b("hello"), 255))
|
||||
self.assertRaises(TypeError, Binary, b"hello", None)
|
||||
self.assertRaises(TypeError, Binary, b"hello", "100")
|
||||
self.assertRaises(ValueError, Binary, b"hello", -1)
|
||||
self.assertRaises(ValueError, Binary, b"hello", 256)
|
||||
self.assertTrue(Binary(b"hello", 0))
|
||||
self.assertTrue(Binary(b"hello", 255))
|
||||
|
||||
def test_subtype(self):
|
||||
one = Binary(b("hello"))
|
||||
one = Binary(b"hello")
|
||||
self.assertEqual(one.subtype, 0)
|
||||
two = Binary(b("hello"), 2)
|
||||
two = Binary(b"hello", 2)
|
||||
self.assertEqual(two.subtype, 2)
|
||||
three = Binary(b("hello"), 100)
|
||||
three = Binary(b"hello", 100)
|
||||
self.assertEqual(three.subtype, 100)
|
||||
|
||||
def test_equality(self):
|
||||
two = Binary(b("hello"))
|
||||
three = Binary(b("hello"), 100)
|
||||
two = Binary(b"hello")
|
||||
three = Binary(b"hello", 100)
|
||||
self.assertNotEqual(two, three)
|
||||
self.assertEqual(three, Binary(b("hello"), 100))
|
||||
self.assertEqual(two, Binary(b("hello")))
|
||||
self.assertNotEqual(two, Binary(b("hello ")))
|
||||
self.assertNotEqual(b("hello"), Binary(b("hello")))
|
||||
self.assertEqual(three, Binary(b"hello", 100))
|
||||
self.assertEqual(two, Binary(b"hello"))
|
||||
self.assertNotEqual(two, Binary(b"hello "))
|
||||
self.assertNotEqual(b"hello", Binary(b"hello"))
|
||||
|
||||
# Explicitly test inequality
|
||||
self.assertFalse(three != Binary(b("hello"), 100))
|
||||
self.assertFalse(two != Binary(b("hello")))
|
||||
self.assertFalse(three != Binary(b"hello", 100))
|
||||
self.assertFalse(two != Binary(b"hello"))
|
||||
|
||||
def test_repr(self):
|
||||
one = Binary(b("hello world"))
|
||||
one = Binary(b"hello world")
|
||||
self.assertEqual(repr(one),
|
||||
"Binary(%s, 0)" % (repr(b("hello world")),))
|
||||
two = Binary(b("hello world"), 2)
|
||||
"Binary(%s, 0)" % (repr(b"hello world"),))
|
||||
two = Binary(b"hello world", 2)
|
||||
self.assertEqual(repr(two),
|
||||
"Binary(%s, 2)" % (repr(b("hello world")),))
|
||||
three = Binary(b("\x08\xFF"))
|
||||
"Binary(%s, 2)" % (repr(b"hello world"),))
|
||||
three = Binary(b"\x08\xFF")
|
||||
self.assertEqual(repr(three),
|
||||
"Binary(%s, 0)" % (repr(b("\x08\xFF")),))
|
||||
four = Binary(b("\x08\xFF"), 2)
|
||||
"Binary(%s, 0)" % (repr(b"\x08\xFF"),))
|
||||
four = Binary(b"\x08\xFF", 2)
|
||||
self.assertEqual(repr(four),
|
||||
"Binary(%s, 2)" % (repr(b("\x08\xFF")),))
|
||||
five = Binary(b("test"), 100)
|
||||
"Binary(%s, 2)" % (repr(b"\x08\xFF"),))
|
||||
five = Binary(b"test", 100)
|
||||
self.assertEqual(repr(five),
|
||||
"Binary(%s, 100)" % (repr(b("test")),))
|
||||
"Binary(%s, 100)" % (repr(b"test"),))
|
||||
|
||||
def test_legacy_java_uuid(self):
|
||||
if not should_test_uuid:
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
# Generated by the Java driver
|
||||
from_java = b('bAAAAAdfaWQAUCBQxkVm+XdxJ9tOBW5ld2d1aWQAEAAAAAMIQkfACFu'
|
||||
'Z/0RustLOU/G6Am5ld2d1aWRzdHJpbmcAJQAAAGZmOTk1YjA4LWMwND'
|
||||
'ctNDIwOC1iYWYxLTUzY2VkMmIyNmU0NAAAbAAAAAdfaWQAUCBQxkVm+'
|
||||
'XdxJ9tPBW5ld2d1aWQAEAAAAANgS/xhRXXv8kfIec+dYdyCAm5ld2d1'
|
||||
'aWRzdHJpbmcAJQAAAGYyZWY3NTQ1LTYxZmMtNGI2MC04MmRjLTYxOWR'
|
||||
'jZjc5Yzg0NwAAbAAAAAdfaWQAUCBQxkVm+XdxJ9tQBW5ld2d1aWQAEA'
|
||||
'AAAAPqREIbhZPUJOSdHCJIgaqNAm5ld2d1aWRzdHJpbmcAJQAAADI0Z'
|
||||
'DQ5Mzg1LTFiNDItNDRlYS04ZGFhLTgxNDgyMjFjOWRlNAAAbAAAAAdf'
|
||||
'aWQAUCBQxkVm+XdxJ9tRBW5ld2d1aWQAEAAAAANjQBn/aQuNfRyfNyx'
|
||||
'29COkAm5ld2d1aWRzdHJpbmcAJQAAADdkOGQwYjY5LWZmMTktNDA2My'
|
||||
'1hNDIzLWY0NzYyYzM3OWYxYwAAbAAAAAdfaWQAUCBQxkVm+XdxJ9tSB'
|
||||
'W5ld2d1aWQAEAAAAAMtSv/Et1cAQUFHUYevqxaLAm5ld2d1aWRzdHJp'
|
||||
'bmcAJQAAADQxMDA1N2I3LWM0ZmYtNGEyZC04YjE2LWFiYWY4NzUxNDc'
|
||||
'0MQAA')
|
||||
from_java = (b'bAAAAAdfaWQAUCBQxkVm+XdxJ9tOBW5ld2d1aWQAEAAAAAMIQkfACFu'
|
||||
b'Z/0RustLOU/G6Am5ld2d1aWRzdHJpbmcAJQAAAGZmOTk1YjA4LWMwND'
|
||||
b'ctNDIwOC1iYWYxLTUzY2VkMmIyNmU0NAAAbAAAAAdfaWQAUCBQxkVm+'
|
||||
b'XdxJ9tPBW5ld2d1aWQAEAAAAANgS/xhRXXv8kfIec+dYdyCAm5ld2d1'
|
||||
b'aWRzdHJpbmcAJQAAAGYyZWY3NTQ1LTYxZmMtNGI2MC04MmRjLTYxOWR'
|
||||
b'jZjc5Yzg0NwAAbAAAAAdfaWQAUCBQxkVm+XdxJ9tQBW5ld2d1aWQAEA'
|
||||
b'AAAAPqREIbhZPUJOSdHCJIgaqNAm5ld2d1aWRzdHJpbmcAJQAAADI0Z'
|
||||
b'DQ5Mzg1LTFiNDItNDRlYS04ZGFhLTgxNDgyMjFjOWRlNAAAbAAAAAdf'
|
||||
b'aWQAUCBQxkVm+XdxJ9tRBW5ld2d1aWQAEAAAAANjQBn/aQuNfRyfNyx'
|
||||
b'29COkAm5ld2d1aWRzdHJpbmcAJQAAADdkOGQwYjY5LWZmMTktNDA2My'
|
||||
b'1hNDIzLWY0NzYyYzM3OWYxYwAAbAAAAAdfaWQAUCBQxkVm+XdxJ9tSB'
|
||||
b'W5ld2d1aWQAEAAAAAMtSv/Et1cAQUFHUYevqxaLAm5ld2d1aWRzdHJp'
|
||||
b'bmcAJQAAADQxMDA1N2I3LWM0ZmYtNGEyZC04YjE2LWFiYWY4NzUxNDc'
|
||||
b'0MQAA')
|
||||
|
||||
data = base64.b64decode(from_java)
|
||||
|
||||
@ -135,21 +135,21 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
# Test encoding
|
||||
encoded = b('').join([bson.BSON.encode(doc,
|
||||
uuid_subtype=OLD_UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc,
|
||||
uuid_subtype=OLD_UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b('').join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b('').join([bson.BSON.encode(doc, uuid_subtype=CSHARP_LEGACY)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=CSHARP_LEGACY)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b('').join([bson.BSON.encode(doc, uuid_subtype=JAVA_LEGACY)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=JAVA_LEGACY)
|
||||
for doc in docs])
|
||||
self.assertEqual(data, encoded)
|
||||
|
||||
# Test insert and find
|
||||
@ -173,19 +173,19 @@ class TestBinary(unittest.TestCase):
|
||||
raise SkipTest("No uuid module")
|
||||
|
||||
# Generated by the .net driver
|
||||
from_csharp = b('ZAAAABBfaWQAAAAAAAVuZXdndWlkABAAAAAD+MkoCd/Jy0iYJ7Vhl'
|
||||
'iF3BAJuZXdndWlkc3RyaW5nACUAAAAwOTI4YzlmOC1jOWRmLTQ4Y2'
|
||||
'ItOTgyNy1iNTYxOTYyMTc3MDQAAGQAAAAQX2lkAAEAAAAFbmV3Z3V'
|
||||
'pZAAQAAAAA9MD0oXQe6VOp7mK4jkttWUCbmV3Z3VpZHN0cmluZwAl'
|
||||
'AAAAODVkMjAzZDMtN2JkMC00ZWE1LWE3YjktOGFlMjM5MmRiNTY1A'
|
||||
'ABkAAAAEF9pZAACAAAABW5ld2d1aWQAEAAAAAPRmIO2auc/Tprq1Z'
|
||||
'oQ1oNYAm5ld2d1aWRzdHJpbmcAJQAAAGI2ODM5OGQxLWU3NmEtNGU'
|
||||
'zZi05YWVhLWQ1OWExMGQ2ODM1OAAAZAAAABBfaWQAAwAAAAVuZXdn'
|
||||
'dWlkABAAAAADISpriopuTEaXIa7arYOCFAJuZXdndWlkc3RyaW5nA'
|
||||
'CUAAAA4YTZiMmEyMS02ZThhLTQ2NGMtOTcyMS1hZWRhYWQ4MzgyMT'
|
||||
'QAAGQAAAAQX2lkAAQAAAAFbmV3Z3VpZAAQAAAAA98eg0CFpGlPihP'
|
||||
'MwOmYGOMCbmV3Z3VpZHN0cmluZwAlAAAANDA4MzFlZGYtYTQ4NS00'
|
||||
'ZjY5LThhMTMtY2NjMGU5OTgxOGUzAAA=')
|
||||
from_csharp = (b'ZAAAABBfaWQAAAAAAAVuZXdndWlkABAAAAAD+MkoCd/Jy0iYJ7Vhl'
|
||||
b'iF3BAJuZXdndWlkc3RyaW5nACUAAAAwOTI4YzlmOC1jOWRmLTQ4Y2'
|
||||
b'ItOTgyNy1iNTYxOTYyMTc3MDQAAGQAAAAQX2lkAAEAAAAFbmV3Z3V'
|
||||
b'pZAAQAAAAA9MD0oXQe6VOp7mK4jkttWUCbmV3Z3VpZHN0cmluZwAl'
|
||||
b'AAAAODVkMjAzZDMtN2JkMC00ZWE1LWE3YjktOGFlMjM5MmRiNTY1A'
|
||||
b'ABkAAAAEF9pZAACAAAABW5ld2d1aWQAEAAAAAPRmIO2auc/Tprq1Z'
|
||||
b'oQ1oNYAm5ld2d1aWRzdHJpbmcAJQAAAGI2ODM5OGQxLWU3NmEtNGU'
|
||||
b'zZi05YWVhLWQ1OWExMGQ2ODM1OAAAZAAAABBfaWQAAwAAAAVuZXdn'
|
||||
b'dWlkABAAAAADISpriopuTEaXIa7arYOCFAJuZXdndWlkc3RyaW5nA'
|
||||
b'CUAAAA4YTZiMmEyMS02ZThhLTQ2NGMtOTcyMS1hZWRhYWQ4MzgyMT'
|
||||
b'QAAGQAAAAQX2lkAAQAAAAFbmV3Z3VpZAAQAAAAA98eg0CFpGlPihP'
|
||||
b'MwOmYGOMCbmV3Z3VpZHN0cmluZwAlAAAANDA4MzFlZGYtYTQ4NS00'
|
||||
b'ZjY5LThhMTMtY2NjMGU5OTgxOGUzAAA=')
|
||||
|
||||
data = base64.b64decode(from_csharp)
|
||||
|
||||
@ -207,21 +207,21 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(d['newguid'], uuid.UUID(d['newguidstring']))
|
||||
|
||||
# Test encoding
|
||||
encoded = b('').join([bson.BSON.encode(doc,
|
||||
uuid_subtype=OLD_UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc,
|
||||
uuid_subtype=OLD_UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b('').join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=UUID_SUBTYPE)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b('').join([bson.BSON.encode(doc, uuid_subtype=JAVA_LEGACY)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=JAVA_LEGACY)
|
||||
for doc in docs])
|
||||
self.assertNotEqual(data, encoded)
|
||||
|
||||
encoded = b('').join([bson.BSON.encode(doc, uuid_subtype=CSHARP_LEGACY)
|
||||
for doc in docs])
|
||||
encoded = b''.join([bson.BSON.encode(doc, uuid_subtype=CSHARP_LEGACY)
|
||||
for doc in docs])
|
||||
self.assertEqual(data, encoded)
|
||||
|
||||
# Test insert and find
|
||||
@ -267,7 +267,7 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(0, coll.find({'uuid': uu}).count())
|
||||
cur = coll.find({'uuid': UUIDLegacy(uu)})
|
||||
self.assertEqual(1, cur.count())
|
||||
retrieved = cur.next()
|
||||
retrieved = next(cur)
|
||||
self.assertEqual(uu, retrieved['uuid'])
|
||||
|
||||
# Test regular UUID queries (using subtype 4).
|
||||
@ -275,7 +275,7 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(2, coll.count())
|
||||
cur = coll.find({'uuid': uu})
|
||||
self.assertEqual(1, cur.count())
|
||||
retrieved = cur.next()
|
||||
retrieved = next(cur)
|
||||
self.assertEqual(uu, retrieved['uuid'])
|
||||
|
||||
# Test both.
|
||||
@ -284,22 +284,22 @@ class TestBinary(unittest.TestCase):
|
||||
coll.drop()
|
||||
|
||||
def test_pickle(self):
|
||||
b1 = Binary(b('123'), 2)
|
||||
b1 = Binary(b'123', 2)
|
||||
|
||||
# For testing backwards compatibility with pre-2.4 pymongo
|
||||
if PY3:
|
||||
p = b("\x80\x03cbson.binary\nBinary\nq\x00C\x03123q\x01\x85q"
|
||||
"\x02\x81q\x03}q\x04X\x10\x00\x00\x00_Binary__subtypeq"
|
||||
"\x05K\x02sb.")
|
||||
p = (b"\x80\x03cbson.binary\nBinary\nq\x00C\x03123q\x01\x85q"
|
||||
b"\x02\x81q\x03}q\x04X\x10\x00\x00\x00_Binary__subtypeq"
|
||||
b"\x05K\x02sb.")
|
||||
else:
|
||||
p = b("ccopy_reg\n_reconstructor\np0\n(cbson.binary\nBinary\np1\nc"
|
||||
"__builtin__\nstr\np2\nS'123'\np3\ntp4\nRp5\n(dp6\nS'_Binary"
|
||||
"__subtype'\np7\nI2\nsb.")
|
||||
p = (b"ccopy_reg\n_reconstructor\np0\n(cbson.binary\nBinary\np1\nc"
|
||||
b"__builtin__\nstr\np2\nS'123'\np3\ntp4\nRp5\n(dp6\nS'_Binary"
|
||||
b"__subtype'\np7\nI2\nsb.")
|
||||
|
||||
if not sys.version.startswith('3.0'):
|
||||
self.assertEqual(b1, pickle.loads(p))
|
||||
|
||||
for proto in xrange(pickle.HIGHEST_PROTOCOL + 1):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
self.assertEqual(b1, pickle.loads(pickle.dumps(b1, proto)))
|
||||
|
||||
if should_test_uuid:
|
||||
@ -309,7 +309,7 @@ class TestBinary(unittest.TestCase):
|
||||
self.assertEqual(uul, copy.copy(uul))
|
||||
self.assertEqual(uul, copy.deepcopy(uul))
|
||||
|
||||
for proto in xrange(pickle.HIGHEST_PROTOCOL + 1):
|
||||
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
self.assertEqual(uul, pickle.loads(pickle.dumps(uul, proto)))
|
||||
|
||||
|
||||
|
||||
@ -39,7 +39,7 @@ from bson.binary import Binary, UUIDLegacy
|
||||
from bson.code import Code
|
||||
from bson.objectid import ObjectId
|
||||
from bson.dbref import DBRef
|
||||
from bson.py3compat import b
|
||||
from bson.py3compat import PY3, u, text_type, iteritems
|
||||
from bson.son import SON
|
||||
from bson.timestamp import Timestamp
|
||||
from bson.errors import (InvalidBSON,
|
||||
@ -52,7 +52,8 @@ from bson.tz_util import (FixedOffset,
|
||||
|
||||
from test import qcheck
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
if PY3:
|
||||
long = int
|
||||
|
||||
|
||||
class TestBSON(unittest.TestCase):
|
||||
@ -61,106 +62,106 @@ class TestBSON(unittest.TestCase):
|
||||
|
||||
def test_basic_validation(self):
|
||||
self.assertRaises(TypeError, is_valid, 100)
|
||||
self.assertRaises(TypeError, is_valid, u"test")
|
||||
self.assertRaises(TypeError, is_valid, u("test"))
|
||||
self.assertRaises(TypeError, is_valid, 10.4)
|
||||
|
||||
self.assertInvalid(b("test"))
|
||||
self.assertInvalid(b"test")
|
||||
|
||||
# the simplest valid BSON document
|
||||
self.assertTrue(is_valid(b("\x05\x00\x00\x00\x00")))
|
||||
self.assertTrue(is_valid(BSON(b("\x05\x00\x00\x00\x00"))))
|
||||
self.assertTrue(is_valid(b"\x05\x00\x00\x00\x00"))
|
||||
self.assertTrue(is_valid(BSON(b"\x05\x00\x00\x00\x00")))
|
||||
|
||||
# failure cases
|
||||
self.assertInvalid(b("\x04\x00\x00\x00\x00"))
|
||||
self.assertInvalid(b("\x05\x00\x00\x00\x01"))
|
||||
self.assertInvalid(b("\x05\x00\x00\x00"))
|
||||
self.assertInvalid(b("\x05\x00\x00\x00\x00\x00"))
|
||||
self.assertInvalid(b("\x07\x00\x00\x00\x02a\x00\x78\x56\x34\x12"))
|
||||
self.assertInvalid(b("\x09\x00\x00\x00\x10a\x00\x05\x00"))
|
||||
self.assertInvalid(b("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"))
|
||||
self.assertInvalid(b("\x13\x00\x00\x00\x02foo\x00"
|
||||
"\x04\x00\x00\x00bar\x00\x00"))
|
||||
self.assertInvalid(b("\x18\x00\x00\x00\x03foo\x00\x0f\x00\x00"
|
||||
"\x00\x10bar\x00\xff\xff\xff\x7f\x00\x00"))
|
||||
self.assertInvalid(b("\x15\x00\x00\x00\x03foo\x00\x0c"
|
||||
"\x00\x00\x00\x08bar\x00\x01\x00\x00"))
|
||||
self.assertInvalid(b("\x1c\x00\x00\x00\x03foo\x00"
|
||||
"\x12\x00\x00\x00\x02bar\x00"
|
||||
"\x05\x00\x00\x00baz\x00\x00\x00"))
|
||||
self.assertInvalid(b("\x10\x00\x00\x00\x02a\x00"
|
||||
"\x04\x00\x00\x00abc\xff\x00"))
|
||||
self.assertInvalid(b"\x04\x00\x00\x00\x00")
|
||||
self.assertInvalid(b"\x05\x00\x00\x00\x01")
|
||||
self.assertInvalid(b"\x05\x00\x00\x00")
|
||||
self.assertInvalid(b"\x05\x00\x00\x00\x00\x00")
|
||||
self.assertInvalid(b"\x07\x00\x00\x00\x02a\x00\x78\x56\x34\x12")
|
||||
self.assertInvalid(b"\x09\x00\x00\x00\x10a\x00\x05\x00")
|
||||
self.assertInvalid(b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00")
|
||||
self.assertInvalid(b"\x13\x00\x00\x00\x02foo\x00"
|
||||
b"\x04\x00\x00\x00bar\x00\x00")
|
||||
self.assertInvalid(b"\x18\x00\x00\x00\x03foo\x00\x0f\x00\x00"
|
||||
b"\x00\x10bar\x00\xff\xff\xff\x7f\x00\x00")
|
||||
self.assertInvalid(b"\x15\x00\x00\x00\x03foo\x00\x0c"
|
||||
b"\x00\x00\x00\x08bar\x00\x01\x00\x00")
|
||||
self.assertInvalid(b"\x1c\x00\x00\x00\x03foo\x00"
|
||||
b"\x12\x00\x00\x00\x02bar\x00"
|
||||
b"\x05\x00\x00\x00baz\x00\x00\x00")
|
||||
self.assertInvalid(b"\x10\x00\x00\x00\x02a\x00"
|
||||
b"\x04\x00\x00\x00abc\xff\x00")
|
||||
|
||||
def test_bad_string_lengths(self):
|
||||
self.assertInvalid(
|
||||
b("\x0c\x00\x00\x00\x02\x00"
|
||||
"\x00\x00\x00\x00\x00\x00"))
|
||||
b"\x0c\x00\x00\x00\x02\x00"
|
||||
b"\x00\x00\x00\x00\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x12\x00\x00\x00\x02\x00"
|
||||
"\xff\xff\xff\xfffoobar\x00\x00"))
|
||||
b"\x12\x00\x00\x00\x02\x00"
|
||||
b"\xff\xff\xff\xfffoobar\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x0c\x00\x00\x00\x0e\x00"
|
||||
"\x00\x00\x00\x00\x00\x00"))
|
||||
b"\x0c\x00\x00\x00\x0e\x00"
|
||||
b"\x00\x00\x00\x00\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x12\x00\x00\x00\x0e\x00"
|
||||
"\xff\xff\xff\xfffoobar\x00\x00"))
|
||||
b"\x12\x00\x00\x00\x0e\x00"
|
||||
b"\xff\xff\xff\xfffoobar\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x18\x00\x00\x00\x0c\x00"
|
||||
"\x00\x00\x00\x00\x00RY\xb5j"
|
||||
"\xfa[\xd8A\xd6X]\x99\x00"))
|
||||
b"\x18\x00\x00\x00\x0c\x00"
|
||||
b"\x00\x00\x00\x00\x00RY\xb5j"
|
||||
b"\xfa[\xd8A\xd6X]\x99\x00")
|
||||
self.assertInvalid(
|
||||
b("\x1e\x00\x00\x00\x0c\x00"
|
||||
"\xff\xff\xff\xfffoobar\x00"
|
||||
"RY\xb5j\xfa[\xd8A\xd6X]\x99\x00"))
|
||||
b"\x1e\x00\x00\x00\x0c\x00"
|
||||
b"\xff\xff\xff\xfffoobar\x00"
|
||||
b"RY\xb5j\xfa[\xd8A\xd6X]\x99\x00")
|
||||
self.assertInvalid(
|
||||
b("\x0c\x00\x00\x00\r\x00"
|
||||
"\x00\x00\x00\x00\x00\x00"))
|
||||
b"\x0c\x00\x00\x00\r\x00"
|
||||
b"\x00\x00\x00\x00\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x0c\x00\x00\x00\r\x00"
|
||||
"\xff\xff\xff\xff\x00\x00"))
|
||||
b"\x0c\x00\x00\x00\r\x00"
|
||||
b"\xff\xff\xff\xff\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x1c\x00\x00\x00\x0f\x00"
|
||||
"\x15\x00\x00\x00\x00\x00"
|
||||
"\x00\x00\x00\x0c\x00\x00"
|
||||
"\x00\x02\x00\x01\x00\x00"
|
||||
"\x00\x00\x00\x00"))
|
||||
b"\x1c\x00\x00\x00\x0f\x00"
|
||||
b"\x15\x00\x00\x00\x00\x00"
|
||||
b"\x00\x00\x00\x0c\x00\x00"
|
||||
b"\x00\x02\x00\x01\x00\x00"
|
||||
b"\x00\x00\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x1c\x00\x00\x00\x0f\x00"
|
||||
"\x15\x00\x00\x00\xff\xff"
|
||||
"\xff\xff\x00\x0c\x00\x00"
|
||||
"\x00\x02\x00\x01\x00\x00"
|
||||
"\x00\x00\x00\x00"))
|
||||
b"\x1c\x00\x00\x00\x0f\x00"
|
||||
b"\x15\x00\x00\x00\xff\xff"
|
||||
b"\xff\xff\x00\x0c\x00\x00"
|
||||
b"\x00\x02\x00\x01\x00\x00"
|
||||
b"\x00\x00\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x1c\x00\x00\x00\x0f\x00"
|
||||
"\x15\x00\x00\x00\x01\x00"
|
||||
"\x00\x00\x00\x0c\x00\x00"
|
||||
"\x00\x02\x00\x00\x00\x00"
|
||||
"\x00\x00\x00\x00"))
|
||||
b"\x1c\x00\x00\x00\x0f\x00"
|
||||
b"\x15\x00\x00\x00\x01\x00"
|
||||
b"\x00\x00\x00\x0c\x00\x00"
|
||||
b"\x00\x02\x00\x00\x00\x00"
|
||||
b"\x00\x00\x00\x00")
|
||||
self.assertInvalid(
|
||||
b("\x1c\x00\x00\x00\x0f\x00"
|
||||
"\x15\x00\x00\x00\x01\x00"
|
||||
"\x00\x00\x00\x0c\x00\x00"
|
||||
"\x00\x02\x00\xff\xff\xff"
|
||||
"\xff\x00\x00\x00"))
|
||||
b"\x1c\x00\x00\x00\x0f\x00"
|
||||
b"\x15\x00\x00\x00\x01\x00"
|
||||
b"\x00\x00\x00\x0c\x00\x00"
|
||||
b"\x00\x02\x00\xff\xff\xff"
|
||||
b"\xff\x00\x00\x00")
|
||||
|
||||
def test_random_data_is_not_bson(self):
|
||||
qcheck.check_unittest(self, qcheck.isnt(is_valid),
|
||||
qcheck.gen_string(qcheck.gen_range(0, 40)))
|
||||
|
||||
def test_basic_decode(self):
|
||||
self.assertEqual({"test": u"hello world"},
|
||||
BSON(b("\x1B\x00\x00\x00\x0E\x74\x65\x73\x74\x00\x0C"
|
||||
"\x00\x00\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F"
|
||||
"\x72\x6C\x64\x00\x00")).decode())
|
||||
self.assertEqual([{"test": u"hello world"}, {}],
|
||||
decode_all(b("\x1B\x00\x00\x00\x0E\x74\x65\x73\x74"
|
||||
"\x00\x0C\x00\x00\x00\x68\x65\x6C\x6C"
|
||||
"\x6f\x20\x77\x6F\x72\x6C\x64\x00\x00"
|
||||
"\x05\x00\x00\x00\x00")))
|
||||
self.assertEqual({"test": u("hello world")},
|
||||
BSON(b"\x1B\x00\x00\x00\x0E\x74\x65\x73\x74\x00\x0C"
|
||||
b"\x00\x00\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F"
|
||||
b"\x72\x6C\x64\x00\x00").decode())
|
||||
self.assertEqual([{"test": u("hello world")}, {}],
|
||||
decode_all(b"\x1B\x00\x00\x00\x0E\x74\x65\x73\x74"
|
||||
b"\x00\x0C\x00\x00\x00\x68\x65\x6C\x6C"
|
||||
b"\x6f\x20\x77\x6F\x72\x6C\x64\x00\x00"
|
||||
b"\x05\x00\x00\x00\x00"))
|
||||
|
||||
def test_data_timestamp(self):
|
||||
self.assertEqual({"test": Timestamp(4, 20)},
|
||||
BSON(b("\x13\x00\x00\x00\x11\x74\x65\x73\x74\x00\x14"
|
||||
"\x00\x00\x00\x04\x00\x00\x00\x00")).decode())
|
||||
BSON(b"\x13\x00\x00\x00\x11\x74\x65\x73\x74\x00\x14"
|
||||
b"\x00\x00\x00\x04\x00\x00\x00\x00").decode())
|
||||
|
||||
def test_basic_encode(self):
|
||||
self.assertRaises(TypeError, BSON.encode, 100)
|
||||
@ -168,93 +169,94 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertRaises(TypeError, BSON.encode, None)
|
||||
self.assertRaises(TypeError, BSON.encode, [])
|
||||
|
||||
self.assertEqual(BSON.encode({}), BSON(b("\x05\x00\x00\x00\x00")))
|
||||
self.assertEqual(BSON.encode({"test": u"hello world"}),
|
||||
b("\x1B\x00\x00\x00\x02\x74\x65\x73\x74\x00\x0C\x00"
|
||||
"\x00\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C"
|
||||
"\x64\x00\x00"))
|
||||
self.assertEqual(BSON.encode({u"mike": 100}),
|
||||
b("\x0F\x00\x00\x00\x10\x6D\x69\x6B\x65\x00\x64\x00"
|
||||
"\x00\x00\x00"))
|
||||
self.assertEqual(BSON.encode({}), BSON(b"\x05\x00\x00\x00\x00"))
|
||||
self.assertEqual(BSON.encode({"test": u("hello world")}),
|
||||
b"\x1B\x00\x00\x00\x02\x74\x65\x73\x74\x00\x0C\x00"
|
||||
b"\x00\x00\x68\x65\x6C\x6C\x6F\x20\x77\x6F\x72\x6C"
|
||||
b"\x64\x00\x00")
|
||||
self.assertEqual(BSON.encode({u("mike"): 100}),
|
||||
b"\x0F\x00\x00\x00\x10\x6D\x69\x6B\x65\x00\x64\x00"
|
||||
b"\x00\x00\x00")
|
||||
self.assertEqual(BSON.encode({"hello": 1.5}),
|
||||
b("\x14\x00\x00\x00\x01\x68\x65\x6C\x6C\x6F\x00\x00"
|
||||
"\x00\x00\x00\x00\x00\xF8\x3F\x00"))
|
||||
b"\x14\x00\x00\x00\x01\x68\x65\x6C\x6C\x6F\x00\x00"
|
||||
b"\x00\x00\x00\x00\x00\xF8\x3F\x00")
|
||||
self.assertEqual(BSON.encode({"true": True}),
|
||||
b("\x0C\x00\x00\x00\x08\x74\x72\x75\x65\x00\x01\x00"))
|
||||
b"\x0C\x00\x00\x00\x08\x74\x72\x75\x65\x00\x01\x00")
|
||||
self.assertEqual(BSON.encode({"false": False}),
|
||||
b("\x0D\x00\x00\x00\x08\x66\x61\x6C\x73\x65\x00\x00"
|
||||
"\x00"))
|
||||
b"\x0D\x00\x00\x00\x08\x66\x61\x6C\x73\x65\x00\x00"
|
||||
b"\x00")
|
||||
self.assertEqual(BSON.encode({"empty": []}),
|
||||
b("\x11\x00\x00\x00\x04\x65\x6D\x70\x74\x79\x00\x05"
|
||||
"\x00\x00\x00\x00\x00"))
|
||||
b"\x11\x00\x00\x00\x04\x65\x6D\x70\x74\x79\x00\x05"
|
||||
b"\x00\x00\x00\x00\x00")
|
||||
self.assertEqual(BSON.encode({"none": {}}),
|
||||
b("\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00"
|
||||
"\x00\x00\x00\x00"))
|
||||
self.assertEqual(BSON.encode({"test": Binary(b("test"), 0)}),
|
||||
b("\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00"
|
||||
"\x00\x00\x00\x74\x65\x73\x74\x00"))
|
||||
self.assertEqual(BSON.encode({"test": Binary(b("test"), 2)}),
|
||||
b("\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00"
|
||||
"\x00\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00"))
|
||||
self.assertEqual(BSON.encode({"test": Binary(b("test"), 128)}),
|
||||
b("\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00"
|
||||
"\x00\x00\x80\x74\x65\x73\x74\x00"))
|
||||
b"\x10\x00\x00\x00\x03\x6E\x6F\x6E\x65\x00\x05\x00"
|
||||
b"\x00\x00\x00\x00")
|
||||
self.assertEqual(BSON.encode({"test": Binary(b"test", 0)}),
|
||||
b"\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00"
|
||||
b"\x00\x00\x00\x74\x65\x73\x74\x00")
|
||||
self.assertEqual(BSON.encode({"test": Binary(b"test", 2)}),
|
||||
b"\x18\x00\x00\x00\x05\x74\x65\x73\x74\x00\x08\x00"
|
||||
b"\x00\x00\x02\x04\x00\x00\x00\x74\x65\x73\x74\x00")
|
||||
self.assertEqual(BSON.encode({"test": Binary(b"test", 128)}),
|
||||
b"\x14\x00\x00\x00\x05\x74\x65\x73\x74\x00\x04\x00"
|
||||
b"\x00\x00\x80\x74\x65\x73\x74\x00")
|
||||
self.assertEqual(BSON.encode({"test": None}),
|
||||
b("\x0B\x00\x00\x00\x0A\x74\x65\x73\x74\x00\x00"))
|
||||
b"\x0B\x00\x00\x00\x0A\x74\x65\x73\x74\x00\x00")
|
||||
self.assertEqual(BSON.encode({"date": datetime.datetime(2007, 1, 8,
|
||||
0, 30, 11)}),
|
||||
b("\x13\x00\x00\x00\x09\x64\x61\x74\x65\x00\x38\xBE"
|
||||
"\x1C\xFF\x0F\x01\x00\x00\x00"))
|
||||
self.assertEqual(BSON.encode({"regex": re.compile(b("a*b"),
|
||||
b"\x13\x00\x00\x00\x09\x64\x61\x74\x65\x00\x38\xBE"
|
||||
b"\x1C\xFF\x0F\x01\x00\x00\x00")
|
||||
self.assertEqual(BSON.encode({"regex": re.compile(b"a*b",
|
||||
re.IGNORECASE)}),
|
||||
b("\x12\x00\x00\x00\x0B\x72\x65\x67\x65\x78\x00\x61"
|
||||
"\x2A\x62\x00\x69\x00\x00"))
|
||||
b"\x12\x00\x00\x00\x0B\x72\x65\x67\x65\x78\x00\x61"
|
||||
b"\x2A\x62\x00\x69\x00\x00")
|
||||
self.assertEqual(BSON.encode({"$where": Code("test")}),
|
||||
b("\x16\x00\x00\x00\r$where\x00\x05\x00\x00\x00test"
|
||||
"\x00\x00"))
|
||||
b"\x16\x00\x00\x00\r$where\x00\x05\x00\x00\x00test"
|
||||
b"\x00\x00")
|
||||
self.assertEqual(BSON.encode({"$field":
|
||||
Code("function(){ return true;}", scope=None)}),
|
||||
b("+\x00\x00\x00\r$field\x00\x1a\x00\x00\x00"
|
||||
"function(){ return true;}\x00\x00"))
|
||||
b"+\x00\x00\x00\r$field\x00\x1a\x00\x00\x00"
|
||||
b"function(){ return true;}\x00\x00")
|
||||
self.assertEqual(BSON.encode({"$field":
|
||||
Code("return function(){ return x; }",
|
||||
scope={'x': False})}),
|
||||
b("=\x00\x00\x00\x0f$field\x000\x00\x00\x00\x1f\x00"
|
||||
"\x00\x00return function(){ return x; }\x00\t\x00"
|
||||
"\x00\x00\x08x\x00\x00\x00\x00"))
|
||||
a = ObjectId(b("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B"))
|
||||
b"=\x00\x00\x00\x0f$field\x000\x00\x00\x00\x1f\x00"
|
||||
b"\x00\x00return function(){ return x; }\x00\t\x00"
|
||||
b"\x00\x00\x08x\x00\x00\x00\x00")
|
||||
a = ObjectId(b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B")
|
||||
self.assertEqual(BSON.encode({"oid": a}),
|
||||
b("\x16\x00\x00\x00\x07\x6F\x69\x64\x00\x00\x01\x02"
|
||||
"\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00"))
|
||||
b"\x16\x00\x00\x00\x07\x6F\x69\x64\x00\x00\x01\x02"
|
||||
b"\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00")
|
||||
self.assertEqual(BSON.encode({"ref": DBRef("coll", a)}),
|
||||
b("\x2F\x00\x00\x00\x03ref\x00\x25\x00\x00\x00\x02"
|
||||
"$ref\x00\x05\x00\x00\x00coll\x00\x07$id\x00\x00"
|
||||
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00"
|
||||
"\x00"))
|
||||
b"\x2F\x00\x00\x00\x03ref\x00\x25\x00\x00\x00\x02"
|
||||
b"$ref\x00\x05\x00\x00\x00coll\x00\x07$id\x00\x00"
|
||||
b"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x00"
|
||||
b"\x00")
|
||||
|
||||
def test_encode_then_decode(self):
|
||||
|
||||
def helper(dict):
|
||||
self.assertEqual(dict, (BSON.encode(dict)).decode())
|
||||
helper({})
|
||||
helper({"test": u"hello"})
|
||||
helper({"test": u("hello")})
|
||||
self.assertTrue(isinstance(BSON.encode({"hello": "world"})
|
||||
.decode()["hello"],
|
||||
unicode))
|
||||
text_type))
|
||||
helper({"mike": -10120})
|
||||
helper({"long": long(10)})
|
||||
helper({"really big long": 2147483648})
|
||||
helper({u"hello": 0.0013109})
|
||||
helper({u("hello"): 0.0013109})
|
||||
helper({"something": True})
|
||||
helper({"false": False})
|
||||
helper({"an array": [1, True, 3.8, u"world"]})
|
||||
helper({"an object": {"test": u"something"}})
|
||||
helper({"a binary": Binary(b("test"), 100)})
|
||||
helper({"a binary": Binary(b("test"), 128)})
|
||||
helper({"a binary": Binary(b("test"), 254)})
|
||||
helper({"another binary": Binary(b("test"), 2)})
|
||||
helper(SON([(u'test dst', datetime.datetime(1993, 4, 4, 2))]))
|
||||
helper(SON([(u'test negative dst', datetime.datetime(1, 1, 1, 1, 1, 1))]))
|
||||
helper({"an array": [1, True, 3.8, u("world")]})
|
||||
helper({"an object": {"test": u("something")}})
|
||||
helper({"a binary": Binary(b"test", 100)})
|
||||
helper({"a binary": Binary(b"test", 128)})
|
||||
helper({"a binary": Binary(b"test", 254)})
|
||||
helper({"another binary": Binary(b"test", 2)})
|
||||
helper(SON([(u('test dst'), datetime.datetime(1993, 4, 4, 2))]))
|
||||
helper(SON([(u('test negative dst'),
|
||||
datetime.datetime(1, 1, 1, 1, 1, 1))]))
|
||||
helper({"big float": float(10000000000)})
|
||||
helper({"ref": DBRef("coll", 5)})
|
||||
helper({"ref": DBRef("coll", 5, foo="bar", bar=4)})
|
||||
@ -285,8 +287,8 @@ class TestBSON(unittest.TestCase):
|
||||
# not support creation of the DBPointer type, but will decode
|
||||
# DBPointer to DBRef.
|
||||
|
||||
bs = b("\x18\x00\x00\x00\x0c\x00\x01\x00\x00"
|
||||
"\x00\x00RY\xb5j\xfa[\xd8A\xd6X]\x99\x00")
|
||||
bs = (b"\x18\x00\x00\x00\x0c\x00\x01\x00\x00"
|
||||
b"\x00\x00RY\xb5j\xfa[\xd8A\xd6X]\x99\x00")
|
||||
|
||||
self.assertEqual({'': DBRef('', ObjectId('5259b56afa5bd841d6585d99'))},
|
||||
bson.BSON(bs).decode())
|
||||
@ -300,7 +302,7 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertEqual(id_only, BSON.encode(id_only).decode())
|
||||
|
||||
def test_bytes_as_keys(self):
|
||||
doc = {b("foo"): 'bar'}
|
||||
doc = {b"foo": 'bar'}
|
||||
# Since `bytes` are stored as Binary you can't use them
|
||||
# as keys in python 3.x. Using binary data as a key makes
|
||||
# no sense in BSON anyway and little sense in python.
|
||||
@ -367,13 +369,13 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertRaises(RuntimeError, BSON.encode, evil_data)
|
||||
|
||||
def test_overflow(self):
|
||||
self.assertTrue(BSON.encode({"x": 9223372036854775807L}))
|
||||
self.assertTrue(BSON.encode({"x": long(9223372036854775807)}))
|
||||
self.assertRaises(OverflowError, BSON.encode,
|
||||
{"x": 9223372036854775808L})
|
||||
{"x": long(9223372036854775808)})
|
||||
|
||||
self.assertTrue(BSON.encode({"x": -9223372036854775808L}))
|
||||
self.assertTrue(BSON.encode({"x": long(-9223372036854775808)}))
|
||||
self.assertRaises(OverflowError, BSON.encode,
|
||||
{"x": -9223372036854775809L})
|
||||
{"x": long(-9223372036854775809)})
|
||||
|
||||
def test_small_long_encode_decode(self):
|
||||
if PY3:
|
||||
@ -384,10 +386,10 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertEqual(256, decoded1)
|
||||
self.assertEqual(type(256), type(decoded1))
|
||||
|
||||
encoded2 = BSON.encode({'x': 256L})
|
||||
encoded2 = BSON.encode({'x': long(256)})
|
||||
decoded2 = BSON.decode(encoded2)['x']
|
||||
self.assertEqual(256L, decoded2)
|
||||
self.assertEqual(type(256L), type(decoded2))
|
||||
self.assertEqual(long(256), decoded2)
|
||||
self.assertEqual(type(long(256)), type(decoded2))
|
||||
|
||||
self.assertNotEqual(type(decoded1), type(decoded2))
|
||||
|
||||
@ -421,17 +423,18 @@ class TestBSON(unittest.TestCase):
|
||||
# The C extension was segfaulting on unicode RegExs, so we have this test
|
||||
# that doesn't really test anything but the lack of a segfault.
|
||||
def test_unicode_regex(self):
|
||||
regex = re.compile(u'revisi\xf3n')
|
||||
regex = re.compile(u('revisi\xf3n'))
|
||||
BSON.encode({"regex": regex}).decode()
|
||||
|
||||
def test_non_string_keys(self):
|
||||
self.assertRaises(InvalidDocument, BSON.encode, {8.9: "test"})
|
||||
|
||||
def test_utf8(self):
|
||||
w = {u"aéあ": u"aéあ"}
|
||||
w = {u("aéあ"): u("aéあ")}
|
||||
self.assertEqual(w, BSON.encode(w).decode())
|
||||
|
||||
iso8859_bytes = u"aé".encode("iso-8859-1")
|
||||
# b'a\xe9' == u"aé".encode("iso-8859-1")
|
||||
iso8859_bytes = b'a\xe9'
|
||||
y = {"hello": iso8859_bytes}
|
||||
if PY3:
|
||||
# Stored as BSON binary subtype 0.
|
||||
@ -442,12 +445,12 @@ class TestBSON(unittest.TestCase):
|
||||
# Python 2.
|
||||
try:
|
||||
BSON.encode(y)
|
||||
except InvalidStringData, e:
|
||||
except InvalidStringData as e:
|
||||
self.assertTrue(repr(iso8859_bytes) in str(e))
|
||||
|
||||
# The next two tests only make sense in python 2.x since
|
||||
# you can't use `bytes` type as document keys in python 3.x.
|
||||
x = {u"aéあ".encode("utf-8"): u"aéあ".encode("utf-8")}
|
||||
x = {u("aéあ").encode("utf-8"): u("aéあ").encode("utf-8")}
|
||||
self.assertEqual(w, BSON.encode(x).decode())
|
||||
|
||||
z = {iso8859_bytes: "hello"}
|
||||
@ -460,27 +463,27 @@ class TestBSON(unittest.TestCase):
|
||||
# This test doesn't make much sense in Python2
|
||||
# since {'a': '\x00'} == {'a': u'\x00'}.
|
||||
# Decoding here actually returns {'a': '\x00'}
|
||||
doc = {"a": u"\x00"}
|
||||
doc = {"a": u("\x00")}
|
||||
self.assertEqual(doc, BSON.encode(doc).decode())
|
||||
|
||||
self.assertRaises(InvalidDocument, BSON.encode, {b("\x00"): "a"})
|
||||
self.assertRaises(InvalidDocument, BSON.encode, {u"\x00": "a"})
|
||||
self.assertRaises(InvalidDocument, BSON.encode, {b"\x00": "a"})
|
||||
self.assertRaises(InvalidDocument, BSON.encode, {u("\x00"): "a"})
|
||||
|
||||
self.assertRaises(InvalidDocument, BSON.encode,
|
||||
{"a": re.compile(b("ab\x00c"))})
|
||||
{"a": re.compile(b"ab\x00c")})
|
||||
self.assertRaises(InvalidDocument, BSON.encode,
|
||||
{"a": re.compile(u"ab\x00c")})
|
||||
{"a": re.compile(u("ab\x00c"))})
|
||||
|
||||
def test_move_id(self):
|
||||
self.assertEqual(b("\x19\x00\x00\x00\x02_id\x00\x02\x00\x00\x00a\x00"
|
||||
"\x02a\x00\x02\x00\x00\x00a\x00\x00"),
|
||||
self.assertEqual(b"\x19\x00\x00\x00\x02_id\x00\x02\x00\x00\x00a\x00"
|
||||
b"\x02a\x00\x02\x00\x00\x00a\x00\x00",
|
||||
BSON.encode(SON([("a", "a"), ("_id", "a")])))
|
||||
|
||||
self.assertEqual(b("\x2c\x00\x00\x00"
|
||||
"\x02_id\x00\x02\x00\x00\x00b\x00"
|
||||
"\x03b\x00"
|
||||
"\x19\x00\x00\x00\x02a\x00\x02\x00\x00\x00a\x00"
|
||||
"\x02_id\x00\x02\x00\x00\x00a\x00\x00\x00"),
|
||||
self.assertEqual(b"\x2c\x00\x00\x00"
|
||||
b"\x02_id\x00\x02\x00\x00\x00b\x00"
|
||||
b"\x03b\x00"
|
||||
b"\x19\x00\x00\x00\x02a\x00\x02\x00\x00\x00a\x00"
|
||||
b"\x02_id\x00\x02\x00\x00\x00a\x00\x00\x00",
|
||||
BSON.encode(SON([("b",
|
||||
SON([("a", "a"), ("_id", "a")])),
|
||||
("_id", "b")])))
|
||||
@ -515,14 +518,14 @@ class TestBSON(unittest.TestCase):
|
||||
class _myfloat(float):
|
||||
pass
|
||||
|
||||
class _myunicode(unicode):
|
||||
class _myunicode(text_type):
|
||||
pass
|
||||
|
||||
d = {'a': _myint(42), 'b': _myfloat(63.9),
|
||||
'c': _myunicode('hello world')
|
||||
}
|
||||
d2 = BSON.encode(d).decode()
|
||||
for key, value in d2.iteritems():
|
||||
for key, value in iteritems(d2):
|
||||
orig_value = d[key]
|
||||
orig_type = orig_value.__class__.__bases__[0]
|
||||
self.assertEqual(type(value), orig_type)
|
||||
@ -543,10 +546,10 @@ class TestBSON(unittest.TestCase):
|
||||
self.assertEqual(0, bson_re1.flags)
|
||||
|
||||
doc1 = {'r': bson_re1}
|
||||
doc1_bson = b(
|
||||
'\x11\x00\x00\x00' # document length
|
||||
'\x0br\x00[\\w-\\.]\x00\x00' # r: regex
|
||||
'\x00') # document terminator
|
||||
doc1_bson = (
|
||||
b'\x11\x00\x00\x00' # document length
|
||||
b'\x0br\x00[\\w-\\.]\x00\x00' # r: regex
|
||||
b'\x00') # document terminator
|
||||
|
||||
self.assertEqual(doc1_bson, BSON.encode(doc1))
|
||||
self.assertEqual(doc1, BSON(doc1_bson).decode(compile_re=False))
|
||||
@ -557,10 +560,10 @@ class TestBSON(unittest.TestCase):
|
||||
|
||||
doc2_with_re = {'r': re2}
|
||||
doc2_with_bson_re = {'r': bson_re2}
|
||||
doc2_bson = b(
|
||||
"\x12\x00\x00\x00" # document length
|
||||
"\x0br\x00.*\x00ilmsux\x00" # r: regex
|
||||
"\x00") # document terminator
|
||||
doc2_bson = (
|
||||
b"\x12\x00\x00\x00" # document length
|
||||
b"\x0br\x00.*\x00ilmsux\x00" # r: regex
|
||||
b"\x00") # document terminator
|
||||
|
||||
self.assertEqual(doc2_bson, BSON.encode(doc2_with_re))
|
||||
self.assertEqual(doc2_bson, BSON.encode(doc2_with_bson_re))
|
||||
@ -574,9 +577,9 @@ class TestBSON(unittest.TestCase):
|
||||
|
||||
def test_regex_from_native(self):
|
||||
self.assertEqual('.*', Regex.from_native(re.compile('.*')).pattern)
|
||||
self.assertEqual(0, Regex.from_native(re.compile(b(''))).flags)
|
||||
self.assertEqual(0, Regex.from_native(re.compile(b'')).flags)
|
||||
|
||||
regex = re.compile(b(''), re.I | re.L | re.M | re.S | re.X)
|
||||
regex = re.compile(b'', re.I | re.L | re.M | re.S | re.X)
|
||||
self.assertEqual(
|
||||
re.I | re.L | re.M | re.S | re.X,
|
||||
Regex.from_native(regex).flags)
|
||||
|
||||
@ -22,6 +22,7 @@ from nose.plugins.skip import SkipTest
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from bson import InvalidDocument, SON
|
||||
from bson.py3compat import string_type
|
||||
from pymongo.errors import BulkWriteError, InvalidOperation, OperationFailure
|
||||
from test import version
|
||||
from test.test_client import get_client
|
||||
@ -627,7 +628,7 @@ class TestBulk(BulkTestBase):
|
||||
|
||||
try:
|
||||
batch.execute()
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -666,7 +667,7 @@ class TestBulk(BulkTestBase):
|
||||
|
||||
try:
|
||||
batch.execute()
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -721,7 +722,7 @@ class TestBulk(BulkTestBase):
|
||||
|
||||
try:
|
||||
batch.execute()
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -760,7 +761,7 @@ class TestBulk(BulkTestBase):
|
||||
|
||||
try:
|
||||
batch.execute()
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -802,7 +803,7 @@ class TestBulk(BulkTestBase):
|
||||
|
||||
try:
|
||||
batch.execute()
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -834,7 +835,7 @@ class TestBulk(BulkTestBase):
|
||||
|
||||
try:
|
||||
batch.execute()
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -929,7 +930,7 @@ class TestBulkWriteConcern(BulkTestBase):
|
||||
else:
|
||||
try:
|
||||
batch.execute({'w': self.w + 1, 'wtimeout': 1})
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -951,7 +952,7 @@ class TestBulkWriteConcern(BulkTestBase):
|
||||
|
||||
failed = result['writeConcernErrors'][0]
|
||||
self.assertEqual(64, failed['code'])
|
||||
self.assertTrue(isinstance(failed['errmsg'], basestring))
|
||||
self.assertTrue(isinstance(failed['errmsg'], string_type))
|
||||
|
||||
self.coll.remove()
|
||||
self.coll.ensure_index('a', unique=True)
|
||||
@ -966,7 +967,7 @@ class TestBulkWriteConcern(BulkTestBase):
|
||||
batch.insert({'a': 2})
|
||||
try:
|
||||
batch.execute({'w': self.w + 1, 'wtimeout': 1})
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -1008,7 +1009,7 @@ class TestBulkWriteConcern(BulkTestBase):
|
||||
else:
|
||||
try:
|
||||
batch.execute({'w': self.w + 1, 'wtimeout': 1})
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -1035,7 +1036,7 @@ class TestBulkWriteConcern(BulkTestBase):
|
||||
batch.insert({'a': 2})
|
||||
try:
|
||||
batch.execute({'w': self.w + 1, 'wtimeout': 1})
|
||||
except BulkWriteError, exc:
|
||||
except BulkWriteError as exc:
|
||||
result = exc.details
|
||||
self.assertEqual(exc.code, 65)
|
||||
else:
|
||||
@ -1051,12 +1052,12 @@ class TestBulkWriteConcern(BulkTestBase):
|
||||
failed = result['writeErrors'][0]
|
||||
self.assertEqual(2, failed['index'])
|
||||
self.assertEqual(11000, failed['code'])
|
||||
self.assertTrue(isinstance(failed['errmsg'], basestring))
|
||||
self.assertTrue(isinstance(failed['errmsg'], string_type))
|
||||
self.assertEqual(1, failed['op']['a'])
|
||||
|
||||
failed = result['writeConcernErrors'][0]
|
||||
self.assertEqual(64, failed['code'])
|
||||
self.assertTrue(isinstance(failed['errmsg'], basestring))
|
||||
self.assertTrue(isinstance(failed['errmsg'], string_type))
|
||||
|
||||
upserts = result['upserted']
|
||||
self.assertEqual(1, len(upserts))
|
||||
|
||||
@ -20,14 +20,13 @@ import threading
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import thread
|
||||
import unittest
|
||||
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.py3compat import thread, u
|
||||
from bson.son import SON
|
||||
from bson.tz_util import utc
|
||||
from pymongo.mongo_client import MongoClient
|
||||
@ -189,8 +188,8 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
def test_database_names(self):
|
||||
client = MongoClient(host, port)
|
||||
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
client.pymongo_test_mike.test.save({"dummy": u("object")})
|
||||
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
@ -204,14 +203,14 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
|
||||
raise SkipTest("This test often fails due to SERVER-2329")
|
||||
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
client.drop_database("pymongo_test")
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
client.drop_database(client.pymongo_test)
|
||||
@ -545,7 +544,7 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
where_func = delay(timeout_sec + 1)
|
||||
|
||||
def get_x(db):
|
||||
doc = db.test.find().where(where_func).next()
|
||||
doc = next(db.test.find().where(where_func))
|
||||
return doc["x"]
|
||||
self.assertEqual(1, get_x(no_timeout.pymongo_test))
|
||||
self.assertRaises(ConnectionFailure, get_x, timeout.pymongo_test)
|
||||
@ -590,8 +589,8 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
MongoClient("[::1]:%d,localhost:%d" % (port, port))
|
||||
|
||||
client = MongoClient("localhost:%d,[::1]:%d" % (port, port))
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_bernie.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
client.pymongo_test_bernie.test.save({"dummy": u("object")})
|
||||
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
@ -617,7 +616,7 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
self.assertTrue(c.is_locked)
|
||||
locked = True
|
||||
c.unlock()
|
||||
for _ in xrange(5):
|
||||
for _ in range(5):
|
||||
locked = c.is_locked
|
||||
if not locked:
|
||||
break
|
||||
@ -625,9 +624,6 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
self.assertFalse(locked)
|
||||
|
||||
def test_contextlib(self):
|
||||
if sys.version_info < (2, 6):
|
||||
raise SkipTest("With statement requires Python >= 2.6")
|
||||
|
||||
import contextlib
|
||||
|
||||
client = get_client(auto_start_request=False)
|
||||
@ -640,17 +636,13 @@ class TestClient(unittest.TestCase, TestRequestMixin):
|
||||
|
||||
# We need exec here because if the Python version is less than 2.6
|
||||
# these with-statements won't even compile.
|
||||
exec """
|
||||
with contextlib.closing(client):
|
||||
self.assertEqual("bar", client.pymongo_test.test.find_one()["foo"])
|
||||
self.assertEqual(None, client._MongoClient__member)
|
||||
"""
|
||||
with contextlib.closing(client):
|
||||
self.assertEqual("bar", client.pymongo_test.test.find_one()["foo"])
|
||||
self.assertEqual(None, client._MongoClient__member)
|
||||
|
||||
exec """
|
||||
with get_client() as client:
|
||||
self.assertEqual("bar", client.pymongo_test.test.find_one()["foo"])
|
||||
self.assertEqual(None, client._MongoClient__member)
|
||||
"""
|
||||
with get_client() as client:
|
||||
self.assertEqual("bar", client.pymongo_test.test.find_one()["foo"])
|
||||
self.assertEqual(None, client._MongoClient__member)
|
||||
|
||||
def test_with_start_request(self):
|
||||
client = get_client()
|
||||
@ -676,20 +668,15 @@ self.assertEqual(None, client._MongoClient__member)
|
||||
self.assertDifferentSock(pool)
|
||||
|
||||
# Test the 'with' statement
|
||||
if sys.version_info >= (2, 6):
|
||||
# We need exec here because if the Python version is less than 2.6
|
||||
# these with-statements won't even compile.
|
||||
exec """
|
||||
with client.start_request() as request:
|
||||
self.assertEqual(client, request.connection)
|
||||
self.assertNoSocketYet(pool)
|
||||
self.assertSameSock(pool)
|
||||
self.assertRequestSocket(pool)
|
||||
"""
|
||||
with client.start_request() as request:
|
||||
self.assertEqual(client, request.connection)
|
||||
self.assertNoSocketYet(pool)
|
||||
self.assertSameSock(pool)
|
||||
self.assertRequestSocket(pool)
|
||||
|
||||
# Request has ended
|
||||
self.assertNoRequest(pool)
|
||||
self.assertDifferentSock(pool)
|
||||
# Request has ended
|
||||
self.assertNoRequest(pool)
|
||||
self.assertDifferentSock(pool)
|
||||
|
||||
def test_auto_start_request(self):
|
||||
for bad_horrible_value in (None, 5, 'hi!'):
|
||||
@ -821,7 +808,7 @@ with client.start_request() as request:
|
||||
raised = False
|
||||
try:
|
||||
# Will be interrupted by a KeyboardInterrupt.
|
||||
db.foo.find({'$where': where}).next()
|
||||
next(db.foo.find({'$where': where}))
|
||||
except KeyboardInterrupt:
|
||||
raised = True
|
||||
|
||||
@ -834,7 +821,7 @@ with client.start_request() as request:
|
||||
# request id's don't match.
|
||||
self.assertEqual(
|
||||
{'_id': 1},
|
||||
db.foo.find().next()
|
||||
next(db.foo.find())
|
||||
)
|
||||
|
||||
def test_operation_failure_without_request(self):
|
||||
@ -843,7 +830,7 @@ with client.start_request() as request:
|
||||
c = get_client()
|
||||
pool = get_pool(c)
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
old_sock_info = iter(pool.sockets).next()
|
||||
old_sock_info = next(iter(pool.sockets))
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert({'_id': 'foo'})
|
||||
self.assertRaises(
|
||||
@ -851,7 +838,7 @@ with client.start_request() as request:
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'})
|
||||
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
new_sock_info = iter(pool.sockets).next()
|
||||
new_sock_info = next(iter(pool.sockets))
|
||||
self.assertEqual(old_sock_info, new_sock_info)
|
||||
|
||||
def test_operation_failure_with_request(self):
|
||||
|
||||
@ -19,6 +19,7 @@ import sys
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from bson.code import Code
|
||||
from bson.py3compat import u
|
||||
|
||||
|
||||
class TestCode(unittest.TestCase):
|
||||
@ -26,11 +27,11 @@ class TestCode(unittest.TestCase):
|
||||
self.assertRaises(TypeError, Code, 5)
|
||||
self.assertRaises(TypeError, Code, None)
|
||||
self.assertRaises(TypeError, Code, "aoeu", 5)
|
||||
self.assertRaises(TypeError, Code, u"aoeu", 5)
|
||||
self.assertRaises(TypeError, Code, u("aoeu"), 5)
|
||||
self.assertTrue(Code("aoeu"))
|
||||
self.assertTrue(Code(u"aoeu"))
|
||||
self.assertTrue(Code(u("aoeu")))
|
||||
self.assertTrue(Code("aoeu", {}))
|
||||
self.assertTrue(Code(u"aoeu", {}))
|
||||
self.assertTrue(Code(u("aoeu"), {}))
|
||||
|
||||
def test_read_only(self):
|
||||
c = Code("blah")
|
||||
|
||||
@ -33,7 +33,7 @@ from bson.regex import Regex
|
||||
from bson.code import Code
|
||||
from bson.dbref import DBRef
|
||||
from bson.objectid import ObjectId
|
||||
from bson.py3compat import b
|
||||
from bson.py3compat import u, itervalues
|
||||
from bson.son import SON, RE_TYPE
|
||||
from pymongo import (ASCENDING, DESCENDING, GEO2D,
|
||||
GEOHAYSTACK, GEOSPHERE, HASHED)
|
||||
@ -56,6 +56,7 @@ from test.utils import (is_mongos, joinall, enable_text_search, get_pool,
|
||||
from test import (qcheck,
|
||||
version)
|
||||
|
||||
|
||||
have_uuid = True
|
||||
try:
|
||||
import uuid
|
||||
@ -120,7 +121,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
db.test.drop_indexes()
|
||||
db.test.insert({})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 1)
|
||||
|
||||
db.test.create_index("hello")
|
||||
@ -130,7 +131,7 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.create_index((("world", ASCENDING),))
|
||||
|
||||
count = 0
|
||||
for _ in db.system.indexes.find({"ns": u"pymongo_test.test"}):
|
||||
for _ in db.system.indexes.find({"ns": u("pymongo_test.test")}):
|
||||
count += 1
|
||||
self.assertEqual(count, 4)
|
||||
|
||||
@ -140,20 +141,20 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertEqual(ix, "hello_world")
|
||||
|
||||
db.test.drop_indexes()
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 1)
|
||||
db.test.create_index("hello")
|
||||
self.assertTrue(u"hello_1" in
|
||||
self.assertTrue(u("hello_1") in
|
||||
[a["name"] for a in db.system.indexes
|
||||
.find({"ns": u"pymongo_test.test"})])
|
||||
.find({"ns": u("pymongo_test.test")})])
|
||||
|
||||
db.test.drop_indexes()
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 1)
|
||||
db.test.create_index([("hello", DESCENDING), ("world", ASCENDING)])
|
||||
self.assertTrue(u"hello_-1_world_1" in
|
||||
self.assertTrue(u("hello_-1_world_1") in
|
||||
[a["name"] for a in db.system.indexes
|
||||
.find({"ns": u"pymongo_test.test"})])
|
||||
.find({"ns": u("pymongo_test.test")})])
|
||||
|
||||
db.test.drop()
|
||||
db.test.insert({'a': 1})
|
||||
@ -244,7 +245,7 @@ class TestCollection(unittest.TestCase):
|
||||
def test_ensure_unique_index_threaded(self):
|
||||
coll = self.db.test_unique_threaded
|
||||
coll.drop()
|
||||
coll.insert(({'foo': i} for i in xrange(10000)))
|
||||
coll.insert(({'foo': i} for i in range(10000)))
|
||||
|
||||
class Indexer(threading.Thread):
|
||||
def run(self):
|
||||
@ -256,12 +257,12 @@ class TestCollection(unittest.TestCase):
|
||||
pass
|
||||
|
||||
threads = []
|
||||
for _ in xrange(10):
|
||||
for _ in range(10):
|
||||
t = Indexer()
|
||||
t.setDaemon(True)
|
||||
threads.append(t)
|
||||
|
||||
for i in xrange(10):
|
||||
for i in range(10):
|
||||
threads[i].start()
|
||||
|
||||
joinall(threads)
|
||||
@ -272,15 +273,15 @@ class TestCollection(unittest.TestCase):
|
||||
def test_index_on_binary(self):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
db.test.save({"bin": Binary(b("def"))})
|
||||
db.test.save({"bin": Binary(b("abc"))})
|
||||
db.test.save({"bin": Binary(b("ghi"))})
|
||||
db.test.save({"bin": Binary(b"def")})
|
||||
db.test.save({"bin": Binary(b"abc")})
|
||||
db.test.save({"bin": Binary(b"ghi")})
|
||||
|
||||
self.assertEqual(db.test.find({"bin": Binary(b("abc"))})
|
||||
self.assertEqual(db.test.find({"bin": Binary(b"abc")})
|
||||
.explain()["nscanned"], 3)
|
||||
|
||||
db.test.create_index("bin")
|
||||
self.assertEqual(db.test.find({"bin": Binary(b("abc"))})
|
||||
self.assertEqual(db.test.find({"bin": Binary(b"abc")})
|
||||
.explain()["nscanned"], 1)
|
||||
|
||||
def test_drop_index(self):
|
||||
@ -289,29 +290,29 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.create_index("hello")
|
||||
name = db.test.create_index("goodbye")
|
||||
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 3)
|
||||
self.assertEqual(name, "goodbye_1")
|
||||
db.test.drop_index(name)
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 2)
|
||||
self.assertTrue(u"hello_1" in
|
||||
self.assertTrue(u("hello_1") in
|
||||
[a["name"] for a in db.system.indexes
|
||||
.find({"ns": u"pymongo_test.test"})])
|
||||
.find({"ns": u("pymongo_test.test")})])
|
||||
|
||||
db.test.drop_indexes()
|
||||
db.test.create_index("hello")
|
||||
name = db.test.create_index("goodbye")
|
||||
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 3)
|
||||
self.assertEqual(name, "goodbye_1")
|
||||
db.test.drop_index([("goodbye", ASCENDING)])
|
||||
self.assertEqual(db.system.indexes.find({"ns": u"pymongo_test.test"})
|
||||
self.assertEqual(db.system.indexes.find({"ns": u("pymongo_test.test")})
|
||||
.count(), 2)
|
||||
self.assertTrue(u"hello_1" in
|
||||
self.assertTrue(u("hello_1") in
|
||||
[a["name"] for a in db.system.indexes
|
||||
.find({"ns": u"pymongo_test.test"})])
|
||||
.find({"ns": u("pymongo_test.test")})])
|
||||
|
||||
def test_reindex(self):
|
||||
db = self.db
|
||||
@ -334,7 +335,7 @@ class TestCollection(unittest.TestCase):
|
||||
reindexed = db.test.reindex()
|
||||
if 'raw' in reindexed:
|
||||
# mongos
|
||||
for result in reindexed['raw'].itervalues():
|
||||
for result in itervalues(reindexed['raw']):
|
||||
check_result(result)
|
||||
else:
|
||||
check_result(reindexed)
|
||||
@ -551,50 +552,50 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert(doc)
|
||||
|
||||
# Test field inclusion
|
||||
doc = db.test.find({}, ["_id"]).next()
|
||||
self.assertEqual(doc.keys(), ["_id"])
|
||||
doc = db.test.find({}, ["a"]).next()
|
||||
l = doc.keys()
|
||||
doc = next(db.test.find({}, ["_id"]))
|
||||
self.assertEqual(list(doc), ["_id"])
|
||||
doc = next(db.test.find({}, ["a"]))
|
||||
l = list(doc)
|
||||
l.sort()
|
||||
self.assertEqual(l, ["_id", "a"])
|
||||
doc = db.test.find({}, ["b"]).next()
|
||||
l = doc.keys()
|
||||
doc = next(db.test.find({}, ["b"]))
|
||||
l = list(doc)
|
||||
l.sort()
|
||||
self.assertEqual(l, ["_id", "b"])
|
||||
doc = db.test.find({}, ["c"]).next()
|
||||
l = doc.keys()
|
||||
doc = next(db.test.find({}, ["c"]))
|
||||
l = list(doc)
|
||||
l.sort()
|
||||
self.assertEqual(l, ["_id", "c"])
|
||||
doc = db.test.find({}, ["a"]).next()
|
||||
doc = next(db.test.find({}, ["a"]))
|
||||
self.assertEqual(doc["a"], 1)
|
||||
doc = db.test.find({}, ["b"]).next()
|
||||
doc = next(db.test.find({}, ["b"]))
|
||||
self.assertEqual(doc["b"], 5)
|
||||
doc = db.test.find({}, ["c"]).next()
|
||||
doc = next(db.test.find({}, ["c"]))
|
||||
self.assertEqual(doc["c"], {"d": 5, "e": 10})
|
||||
|
||||
# Test inclusion of fields with dots
|
||||
doc = db.test.find({}, ["c.d"]).next()
|
||||
doc = next(db.test.find({}, ["c.d"]))
|
||||
self.assertEqual(doc["c"], {"d": 5})
|
||||
doc = db.test.find({}, ["c.e"]).next()
|
||||
doc = next(db.test.find({}, ["c.e"]))
|
||||
self.assertEqual(doc["c"], {"e": 10})
|
||||
doc = db.test.find({}, ["b", "c.e"]).next()
|
||||
doc = next(db.test.find({}, ["b", "c.e"]))
|
||||
self.assertEqual(doc["c"], {"e": 10})
|
||||
|
||||
doc = db.test.find({}, ["b", "c.e"]).next()
|
||||
l = doc.keys()
|
||||
doc = next(db.test.find({}, ["b", "c.e"]))
|
||||
l = list(doc)
|
||||
l.sort()
|
||||
self.assertEqual(l, ["_id", "b", "c"])
|
||||
doc = db.test.find({}, ["b", "c.e"]).next()
|
||||
doc = next(db.test.find({}, ["b", "c.e"]))
|
||||
self.assertEqual(doc["b"], 5)
|
||||
|
||||
# Test field exclusion
|
||||
doc = db.test.find({}, {"a": False, "b": 0}).next()
|
||||
l = doc.keys()
|
||||
doc = next(db.test.find({}, {"a": False, "b": 0}))
|
||||
l = list(doc)
|
||||
l.sort()
|
||||
self.assertEqual(l, ["_id", "c"])
|
||||
|
||||
doc = db.test.find({}, {"_id": False}).next()
|
||||
l = doc.keys()
|
||||
doc = next(db.test.find({}, {"_id": False}))
|
||||
l = list(doc)
|
||||
self.assertFalse("_id" in l)
|
||||
|
||||
def test_options(self):
|
||||
@ -617,7 +618,7 @@ class TestCollection(unittest.TestCase):
|
||||
db = self.db
|
||||
db.test.remove({})
|
||||
self.assertEqual(0, len(list(db.test.find())))
|
||||
doc = {"hello": u"world"}
|
||||
doc = {"hello": u("world")}
|
||||
id = db.test.insert(doc)
|
||||
self.assertEqual(1, len(list(db.test.find())))
|
||||
self.assertEqual(doc, db.test.find_one())
|
||||
@ -643,11 +644,11 @@ class TestCollection(unittest.TestCase):
|
||||
db = self.db
|
||||
db.test.remove({})
|
||||
self.assertEqual(db.test.find().count(), 0)
|
||||
db.test.insert(({'a': i} for i in xrange(5)), manipulate=False)
|
||||
db.test.insert(({'a': i} for i in range(5)), manipulate=False)
|
||||
self.assertEqual(5, db.test.count())
|
||||
db.test.remove({})
|
||||
|
||||
db.test.insert(({'a': i} for i in xrange(5)), manipulate=True)
|
||||
db.test.insert(({'a': i} for i in range(5)), manipulate=True)
|
||||
self.assertEqual(5, db.test.count())
|
||||
db.test.remove({})
|
||||
|
||||
@ -683,23 +684,23 @@ class TestCollection(unittest.TestCase):
|
||||
db.test.insert({"x": 1, "mike": "awesome",
|
||||
"extra thing": "abcdefghijklmnopqrstuvwxyz"})
|
||||
self.assertEqual(1, db.test.count())
|
||||
doc = db.test.find({}).next()
|
||||
doc = next(db.test.find({}))
|
||||
self.assertTrue("x" in doc)
|
||||
doc = db.test.find({}).next()
|
||||
doc = next(db.test.find({}))
|
||||
self.assertTrue("mike" in doc)
|
||||
doc = db.test.find({}).next()
|
||||
doc = next(db.test.find({}))
|
||||
self.assertTrue("extra thing" in doc)
|
||||
doc = db.test.find({}, ["x", "mike"]).next()
|
||||
doc = next(db.test.find({}, ["x", "mike"]))
|
||||
self.assertTrue("x" in doc)
|
||||
doc = db.test.find({}, ["x", "mike"]).next()
|
||||
doc = next(db.test.find({}, ["x", "mike"]))
|
||||
self.assertTrue("mike" in doc)
|
||||
doc = db.test.find({}, ["x", "mike"]).next()
|
||||
doc = next(db.test.find({}, ["x", "mike"]))
|
||||
self.assertFalse("extra thing" in doc)
|
||||
doc = db.test.find({}, ["mike"]).next()
|
||||
doc = next(db.test.find({}, ["mike"]))
|
||||
self.assertFalse("x" in doc)
|
||||
doc = db.test.find({}, ["mike"]).next()
|
||||
doc = next(db.test.find({}, ["mike"]))
|
||||
self.assertTrue("mike" in doc)
|
||||
doc = db.test.find({}, ["mike"]).next()
|
||||
doc = next(db.test.find({}, ["mike"]))
|
||||
self.assertFalse("extra thing" in doc)
|
||||
|
||||
def test_fields_specifier_as_dict(self):
|
||||
@ -752,7 +753,7 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertEqual(object["_id"], numeric)
|
||||
|
||||
for x in db.test.find():
|
||||
self.assertEqual(x["hello"], u"world")
|
||||
self.assertEqual(x["hello"], u("world"))
|
||||
self.assertTrue("_id" in x)
|
||||
|
||||
def test_iteration(self):
|
||||
@ -793,13 +794,13 @@ class TestCollection(unittest.TestCase):
|
||||
def test_insert_multiple(self):
|
||||
db = self.db
|
||||
db.drop_collection("test")
|
||||
doc1 = {"hello": u"world"}
|
||||
doc2 = {"hello": u"mike"}
|
||||
doc1 = {"hello": u("world")}
|
||||
doc2 = {"hello": u("mike")}
|
||||
self.assertEqual(db.test.find().count(), 0)
|
||||
ids = db.test.insert([doc1, doc2])
|
||||
self.assertEqual(db.test.find().count(), 2)
|
||||
self.assertEqual(doc1, db.test.find_one({"hello": u"world"}))
|
||||
self.assertEqual(doc2, db.test.find_one({"hello": u"mike"}))
|
||||
self.assertEqual(doc1, db.test.find_one({"hello": u("world")}))
|
||||
self.assertEqual(doc2, db.test.find_one({"hello": u("mike")}))
|
||||
|
||||
self.assertEqual(2, len(ids))
|
||||
self.assertEqual(doc1["_id"], ids[0])
|
||||
@ -861,13 +862,13 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
db.drop_collection("test")
|
||||
self.assertEqual(db.test.find().count(), 0)
|
||||
ids = db.test.insert(({"hello": u"world"}, {"hello": u"world"}))
|
||||
ids = db.test.insert(({"hello": u("world")}, {"hello": u("world")}))
|
||||
self.assertEqual(db.test.find().count(), 2)
|
||||
|
||||
db.drop_collection("test")
|
||||
self.assertEqual(db.test.find().count(), 0)
|
||||
ids = db.test.insert(itertools.imap(lambda x: {"hello": "world"},
|
||||
itertools.repeat(None, 10)))
|
||||
ids = db.test.insert(map(lambda x: {"hello": "world"},
|
||||
itertools.repeat(None, 10)))
|
||||
self.assertEqual(db.test.find().count(), 10)
|
||||
|
||||
def test_insert_manipulate_false(self):
|
||||
@ -998,7 +999,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
try:
|
||||
db.test.insert({"_id": 1})
|
||||
except expected_error, exc:
|
||||
except expected_error as exc:
|
||||
# Just check that we set the error document. Fields
|
||||
# vary by MongoDB version.
|
||||
self.assertTrue(exc.details is not None)
|
||||
@ -1058,7 +1059,7 @@ class TestCollection(unittest.TestCase):
|
||||
def test_error_code(self):
|
||||
try:
|
||||
self.db.test.update({}, {"$thismodifierdoesntexist": 1})
|
||||
except OperationFailure, exc:
|
||||
except OperationFailure as exc:
|
||||
if version.at_least(self.db.connection, (1, 3)):
|
||||
self.assertTrue(exc.code in (9, 10147, 16840, 17009))
|
||||
# Just check that we set the error document. Fields
|
||||
@ -1312,7 +1313,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
try:
|
||||
self.db.test.save({"x": 1}, w=w, wtimeout=1)
|
||||
except WTimeoutError, exc:
|
||||
except WTimeoutError as exc:
|
||||
# Just check that we set the error document. Fields
|
||||
# vary by MongoDB version.
|
||||
self.assertTrue(exc.details is not None)
|
||||
@ -1421,7 +1422,7 @@ class TestCollection(unittest.TestCase):
|
||||
# Test that getMore messages are sent to the right server.
|
||||
db.read_preference = ReadPreference.SECONDARY
|
||||
coll = db.test
|
||||
coll.insert(({'_id': i} for i in xrange(8000)), w=self.w)
|
||||
coll.insert(({'_id': i} for i in range(8000)), w=self.w)
|
||||
docs = []
|
||||
threads = [threading.Thread(target=docs.extend, args=(cursor,))
|
||||
for cursor in coll.parallel_scan(3)]
|
||||
@ -1629,7 +1630,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
self.assertTrue("hello" in db.test.find_one(fields=["hello"]))
|
||||
self.assertTrue("hello" not in db.test.find_one(fields=["foo"]))
|
||||
self.assertEqual(["_id"], db.test.find_one(fields=[]).keys())
|
||||
self.assertEqual(["_id"], list(db.test.find_one(fields=[])))
|
||||
|
||||
self.assertEqual(None, db.test.find_one({"hello": "foo"}))
|
||||
self.assertEqual(None, db.test.find_one(ObjectId()))
|
||||
@ -1727,7 +1728,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
self.db.drop_collection("test")
|
||||
# Insert enough documents to require more than one batch
|
||||
self.db.test.insert([{'i': i} for i in xrange(150)])
|
||||
self.db.test.insert([{'i': i} for i in range(150)])
|
||||
|
||||
client = get_client(max_pool_size=1)
|
||||
socks = get_pool(client).sockets
|
||||
@ -1735,7 +1736,7 @@ class TestCollection(unittest.TestCase):
|
||||
|
||||
# Make sure the socket is returned after exhaustion.
|
||||
cur = client[self.db.name].test.find(exhaust=True)
|
||||
cur.next()
|
||||
next(cur)
|
||||
self.assertEqual(0, len(socks))
|
||||
for doc in cur:
|
||||
pass
|
||||
@ -1750,7 +1751,7 @@ class TestCollection(unittest.TestCase):
|
||||
# completely iterated we have to close and
|
||||
# discard the socket.
|
||||
cur = client[self.db.name].test.find(exhaust=True)
|
||||
cur.next()
|
||||
next(cur)
|
||||
self.assertEqual(0, len(socks))
|
||||
if sys.platform.startswith('java') or 'PyPy' in sys.version:
|
||||
# Don't wait for GC or use gc.collect(), it's unreliable.
|
||||
@ -1879,7 +1880,7 @@ class TestCollection(unittest.TestCase):
|
||||
batch[3]['_id'] = batch[2]['_id']
|
||||
try:
|
||||
self.db.test.insert(batch, continue_on_error=True, w=1)
|
||||
except OperationFailure, e:
|
||||
except OperationFailure as e:
|
||||
# Make sure we report the last error, not the first.
|
||||
self.assertTrue(str(batch[2]['_id']) in str(e))
|
||||
else:
|
||||
@ -2040,11 +2041,11 @@ class TestCollection(unittest.TestCase):
|
||||
def test_messages_with_unicode_collection_names(self):
|
||||
db = self.db
|
||||
|
||||
db[u"Employés"].insert({"x": 1})
|
||||
db[u"Employés"].update({"x": 1}, {"x": 2})
|
||||
db[u"Employés"].remove({})
|
||||
db[u"Employés"].find_one()
|
||||
list(db[u"Employés"].find())
|
||||
db[u("Employés")].insert({"x": 1})
|
||||
db[u("Employés")].update({"x": 1}, {"x": 2})
|
||||
db[u("Employés")].remove({})
|
||||
db[u("Employés")].find_one()
|
||||
list(db[u("Employés")].find())
|
||||
|
||||
def test_drop_indexes_non_existant(self):
|
||||
self.db.drop_collection("test")
|
||||
@ -2091,11 +2092,11 @@ class TestCollection(unittest.TestCase):
|
||||
c.drop()
|
||||
c.insert({"x": 1})
|
||||
|
||||
doc = c.find().next()
|
||||
doc = next(c.find())
|
||||
self.assertTrue(isinstance(doc, dict))
|
||||
doc = c.find().next()
|
||||
doc = next(c.find())
|
||||
self.assertFalse(isinstance(doc, SON))
|
||||
doc = c.find(as_class=SON).next()
|
||||
doc = next(c.find(as_class=SON))
|
||||
self.assertTrue(isinstance(doc, SON))
|
||||
|
||||
self.assertTrue(isinstance(c.find_one(), dict))
|
||||
@ -2103,7 +2104,7 @@ class TestCollection(unittest.TestCase):
|
||||
self.assertTrue(isinstance(c.find_one(as_class=SON), SON))
|
||||
|
||||
self.assertEqual(1, c.find_one(as_class=SON)["x"])
|
||||
doc = c.find(as_class=SON).next()
|
||||
doc = next(c.find(as_class=SON))
|
||||
self.assertEqual(1, doc["x"])
|
||||
|
||||
def test_find_and_modify(self):
|
||||
@ -2187,7 +2188,7 @@ class TestCollection(unittest.TestCase):
|
||||
def test_find_and_modify_with_sort(self):
|
||||
c = self.db.test
|
||||
c.drop()
|
||||
for j in xrange(5):
|
||||
for j in range(5):
|
||||
c.insert({'j': j, 'i': 0})
|
||||
|
||||
sort={'j': DESCENDING}
|
||||
|
||||
@ -24,6 +24,7 @@ sys.path[0:0] = [""]
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.code import Code
|
||||
from bson.py3compat import u, PY3
|
||||
from bson.son import SON
|
||||
from pymongo import (ASCENDING,
|
||||
DESCENDING,
|
||||
@ -39,6 +40,9 @@ from test import version
|
||||
from test.test_client import get_client
|
||||
from test.utils import is_mongos, get_command_line, server_started_with_auth
|
||||
|
||||
if PY3:
|
||||
long = int
|
||||
|
||||
|
||||
class TestCursor(unittest.TestCase):
|
||||
|
||||
@ -61,7 +65,7 @@ class TestCursor(unittest.TestCase):
|
||||
coll.insert({"amalia": 2})
|
||||
|
||||
coll.find().max_time_ms(None)
|
||||
coll.find().max_time_ms(1L)
|
||||
coll.find().max_time_ms(long(1))
|
||||
|
||||
cursor = coll.find().max_time_ms(999)
|
||||
self.assertEqual(999, cursor._Cursor__max_time_ms)
|
||||
@ -84,7 +88,7 @@ class TestCursor(unittest.TestCase):
|
||||
try:
|
||||
cursor = coll.find().max_time_ms(1)
|
||||
try:
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
except ExecutionTimeout:
|
||||
pass
|
||||
else:
|
||||
@ -109,7 +113,7 @@ class TestCursor(unittest.TestCase):
|
||||
cursor = coll.find().max_time_ms(100)
|
||||
|
||||
# Send initial query before turning on failpoint.
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
self.client.admin.command("configureFailPoint",
|
||||
"maxTimeAlwaysTimeOut",
|
||||
mode="alwaysOn")
|
||||
@ -181,7 +185,7 @@ class TestCursor(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.test.find().limit, None)
|
||||
self.assertRaises(TypeError, db.test.find().limit, "hello")
|
||||
self.assertRaises(TypeError, db.test.find().limit, 5.5)
|
||||
self.assertTrue(db.test.find().limit(5L))
|
||||
self.assertTrue(db.test.find().limit(long(5)))
|
||||
|
||||
db.test.drop()
|
||||
for i in range(100):
|
||||
@ -295,7 +299,7 @@ class TestCursor(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.test.find().batch_size, "hello")
|
||||
self.assertRaises(TypeError, db.test.find().batch_size, 5.5)
|
||||
self.assertRaises(ValueError, db.test.find().batch_size, -1)
|
||||
self.assertTrue(db.test.find().batch_size(5L))
|
||||
self.assertTrue(db.test.find().batch_size(long(5)))
|
||||
a = db.test.find()
|
||||
for _ in a:
|
||||
break
|
||||
@ -335,27 +339,27 @@ class TestCursor(unittest.TestCase):
|
||||
db.test.save({"x": x})
|
||||
|
||||
curs = db.test.find().limit(0).batch_size(10)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(10, curs._Cursor__retrieved)
|
||||
|
||||
curs = db.test.find().limit(-2).batch_size(0)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(2, curs._Cursor__retrieved)
|
||||
|
||||
curs = db.test.find().limit(-4).batch_size(5)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(4, curs._Cursor__retrieved)
|
||||
|
||||
curs = db.test.find().limit(50).batch_size(500)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(50, curs._Cursor__retrieved)
|
||||
|
||||
curs = db.test.find().batch_size(500)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(500, curs._Cursor__retrieved)
|
||||
|
||||
curs = db.test.find().limit(50)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(50, curs._Cursor__retrieved)
|
||||
|
||||
# these two might be shaky, as the default
|
||||
@ -363,11 +367,11 @@ class TestCursor(unittest.TestCase):
|
||||
# or 1MB (whichever is smaller) is default
|
||||
# for queries without ntoreturn
|
||||
curs = db.test.find()
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(101, curs._Cursor__retrieved)
|
||||
|
||||
curs = db.test.find().limit(0).batch_size(0)
|
||||
curs.next()
|
||||
next(curs)
|
||||
self.assertEqual(101, curs._Cursor__retrieved)
|
||||
|
||||
def test_skip(self):
|
||||
@ -377,7 +381,7 @@ class TestCursor(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.test.find().skip, "hello")
|
||||
self.assertRaises(TypeError, db.test.find().skip, 5.5)
|
||||
self.assertRaises(ValueError, db.test.find().skip, -5)
|
||||
self.assertTrue(db.test.find().skip(5L))
|
||||
self.assertTrue(db.test.find().skip(long(5)))
|
||||
|
||||
db.drop_collection("test")
|
||||
|
||||
@ -428,21 +432,20 @@ class TestCursor(unittest.TestCase):
|
||||
|
||||
db.test.drop()
|
||||
|
||||
unsort = range(10)
|
||||
unsort = list(range(10))
|
||||
random.shuffle(unsort)
|
||||
|
||||
for i in unsort:
|
||||
db.test.save({"x": i})
|
||||
|
||||
asc = [i["x"] for i in db.test.find().sort("x", ASCENDING)]
|
||||
self.assertEqual(asc, range(10))
|
||||
self.assertEqual(asc, list(range(10)))
|
||||
asc = [i["x"] for i in db.test.find().sort("x")]
|
||||
self.assertEqual(asc, range(10))
|
||||
self.assertEqual(asc, list(range(10)))
|
||||
asc = [i["x"] for i in db.test.find().sort([("x", ASCENDING)])]
|
||||
self.assertEqual(asc, range(10))
|
||||
self.assertEqual(asc, list(range(10)))
|
||||
|
||||
expect = range(10)
|
||||
expect.reverse()
|
||||
expect = list(reversed(range(10)))
|
||||
desc = [i["x"] for i in db.test.find().sort("x", DESCENDING)]
|
||||
self.assertEqual(desc, expect)
|
||||
desc = [i["x"] for i in db.test.find().sort([("x", DESCENDING)])]
|
||||
@ -516,7 +519,7 @@ class TestCursor(unittest.TestCase):
|
||||
|
||||
self.assertEqual(3, db.test.find().where('this.x < 3').count())
|
||||
self.assertEqual(10, db.test.find().count())
|
||||
self.assertEqual(3, db.test.find().where(u'this.x < 3').count())
|
||||
self.assertEqual(3, db.test.find().where(u('this.x < 3')).count())
|
||||
self.assertEqual([0, 1, 2],
|
||||
[a["x"] for a in
|
||||
db.test.find().where('this.x < 3')])
|
||||
@ -818,7 +821,8 @@ class TestCursor(unittest.TestCase):
|
||||
self.fail()
|
||||
|
||||
self.assertEqual(5, len(list(self.db.test.find()[20:25])))
|
||||
self.assertEqual(5, len(list(self.db.test.find()[20L:25L])))
|
||||
self.assertEqual(5, len(list(
|
||||
self.db.test.find()[long(20):long(25)])))
|
||||
for a, b in zip(count(20), self.db.test.find()[20:25]):
|
||||
self.assertEqual(a, b['i'])
|
||||
|
||||
@ -864,7 +868,7 @@ class TestCursor(unittest.TestCase):
|
||||
self.assertEqual(50, self.db.test.find()[50]['i'])
|
||||
self.assertEqual(50, self.db.test.find().skip(50)[0]['i'])
|
||||
self.assertEqual(50, self.db.test.find().skip(49)[1]['i'])
|
||||
self.assertEqual(50, self.db.test.find()[50L]['i'])
|
||||
self.assertEqual(50, self.db.test.find()[long(50)]['i'])
|
||||
self.assertEqual(99, self.db.test.find()[99]['i'])
|
||||
|
||||
self.assertRaises(IndexError, lambda x: self.db.test.find()[x], -1)
|
||||
@ -948,7 +952,7 @@ class TestCursor(unittest.TestCase):
|
||||
# Capped rollover - the collection can never
|
||||
# have more than 3 documents. Just make sure
|
||||
# this doesn't raise...
|
||||
db.test.insert(({"x": i} for i in xrange(4, 7)))
|
||||
db.test.insert(({"x": i} for i in range(4, 7)))
|
||||
self.assertEqual(0, len(list(cursor)))
|
||||
|
||||
# and that the cursor doesn't think it's still alive.
|
||||
@ -1001,23 +1005,18 @@ class TestCursor(unittest.TestCase):
|
||||
.max_scan(90).max_scan(50))))
|
||||
|
||||
def test_with_statement(self):
|
||||
if sys.version_info < (2, 6):
|
||||
raise SkipTest("With statement requires Python >= 2.6")
|
||||
|
||||
self.db.drop_collection("test")
|
||||
for _ in range(100):
|
||||
self.db.test.insert({})
|
||||
|
||||
c1 = self.db.test.find()
|
||||
exec """
|
||||
with self.db.test.find() as c2:
|
||||
self.assertTrue(c2.alive)
|
||||
self.assertFalse(c2.alive)
|
||||
with self.db.test.find() as c2:
|
||||
self.assertTrue(c2.alive)
|
||||
self.assertFalse(c2.alive)
|
||||
|
||||
with self.db.test.find() as c2:
|
||||
self.assertEqual(100, len(list(c2)))
|
||||
self.assertFalse(c2.alive)
|
||||
"""
|
||||
with self.db.test.find() as c2:
|
||||
self.assertEqual(100, len(list(c2)))
|
||||
self.assertFalse(c2.alive)
|
||||
self.assertTrue(c1.alive)
|
||||
|
||||
def test_comment(self):
|
||||
@ -1066,7 +1065,7 @@ self.assertFalse(c2.alive)
|
||||
|
||||
self.db.test.insert([{}, {}])
|
||||
cursor = self.db.test.find()
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
self.assertRaises(InvalidOperation, cursor.comment, 'hello')
|
||||
|
||||
self.db.system.profile.drop()
|
||||
@ -1075,7 +1074,7 @@ self.assertFalse(c2.alive)
|
||||
|
||||
# This is just a test, don't try this at home...
|
||||
self.db.test.remove({})
|
||||
self.db.test.insert({'_id': i} for i in xrange(200))
|
||||
self.db.test.insert({'_id': i} for i in range(200))
|
||||
|
||||
class CManager(CursorManager):
|
||||
def __init__(self, connection):
|
||||
@ -1090,7 +1089,7 @@ self.assertFalse(c2.alive)
|
||||
client.set_cursor_manager(CManager)
|
||||
docs = []
|
||||
cursor = self.db.test.find().batch_size(10)
|
||||
docs.append(cursor.next())
|
||||
docs.append(next(cursor))
|
||||
cursor.close()
|
||||
docs.extend(cursor)
|
||||
self.assertEqual(len(docs), 10)
|
||||
|
||||
@ -29,6 +29,7 @@ from bson.code import Code
|
||||
from bson.regex import Regex
|
||||
from bson.dbref import DBRef
|
||||
from bson.objectid import ObjectId
|
||||
from bson.py3compat import u, string_type, text_type, PY3
|
||||
from bson.son import SON, RE_TYPE
|
||||
from pymongo import (ALL,
|
||||
auth,
|
||||
@ -51,6 +52,9 @@ from test.utils import (get_command_line, is_mongos,
|
||||
remove_all_users, server_started_with_auth)
|
||||
from test.test_client import get_client
|
||||
|
||||
if PY3:
|
||||
long = int
|
||||
|
||||
|
||||
class TestDatabase(unittest.TestCase):
|
||||
|
||||
@ -65,7 +69,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(InvalidName, Database, self.client, "my db")
|
||||
self.assertRaises(InvalidName, Database, self.client, "my\x00db")
|
||||
self.assertRaises(InvalidName, Database,
|
||||
self.client, u"my\u0000db")
|
||||
self.client, u("my\u0000db"))
|
||||
self.assertEqual("name", Database(self.client, "name").name)
|
||||
|
||||
def test_equality(self):
|
||||
@ -81,7 +85,7 @@ class TestDatabase(unittest.TestCase):
|
||||
def test_repr(self):
|
||||
self.assertEqual(repr(Database(self.client, "pymongo_test")),
|
||||
"Database(%r, %s)" % (self.client,
|
||||
repr(u"pymongo_test")))
|
||||
repr(u("pymongo_test"))))
|
||||
|
||||
def test_get_coll(self):
|
||||
db = Database(self.client, "pymongo_test")
|
||||
@ -103,20 +107,20 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(InvalidName, db.create_collection, "coll..ection")
|
||||
|
||||
test = db.create_collection("test")
|
||||
test.save({"hello": u"world"})
|
||||
test.save({"hello": u("world")})
|
||||
self.assertEqual(db.test.find_one()["hello"], "world")
|
||||
self.assertTrue(u"test" in db.collection_names())
|
||||
self.assertTrue(u("test") in db.collection_names())
|
||||
|
||||
db.drop_collection("test.foo")
|
||||
db.create_collection("test.foo")
|
||||
self.assertTrue(u"test.foo" in db.collection_names())
|
||||
self.assertTrue(u("test.foo") in db.collection_names())
|
||||
self.assertEqual(db.test.foo.options(), {})
|
||||
self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
|
||||
|
||||
def test_collection_names(self):
|
||||
db = Database(self.client, "pymongo_test")
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.mike.save({"dummy": u"object"})
|
||||
db.test.save({"dummy": u("object")})
|
||||
db.test.mike.save({"dummy": u("object")})
|
||||
|
||||
colls = db.collection_names()
|
||||
self.assertTrue("test" in colls)
|
||||
@ -134,22 +138,22 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.drop_collection, 5)
|
||||
self.assertRaises(TypeError, db.drop_collection, None)
|
||||
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.save({"dummy": u("object")})
|
||||
self.assertTrue("test" in db.collection_names())
|
||||
db.drop_collection("test")
|
||||
self.assertFalse("test" in db.collection_names())
|
||||
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.save({"dummy": u("object")})
|
||||
self.assertTrue("test" in db.collection_names())
|
||||
db.drop_collection(u"test")
|
||||
db.drop_collection(u("test"))
|
||||
self.assertFalse("test" in db.collection_names())
|
||||
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.save({"dummy": u("object")})
|
||||
self.assertTrue("test" in db.collection_names())
|
||||
db.drop_collection(db.test)
|
||||
self.assertFalse("test" in db.collection_names())
|
||||
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.save({"dummy": u("object")})
|
||||
self.assertTrue("test" in db.collection_names())
|
||||
db.test.drop()
|
||||
self.assertFalse("test" in db.collection_names())
|
||||
@ -163,7 +167,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.validate_collection, 5)
|
||||
self.assertRaises(TypeError, db.validate_collection, None)
|
||||
|
||||
db.test.save({"dummy": u"object"})
|
||||
db.test.save({"dummy": u("object")})
|
||||
|
||||
self.assertRaises(OperationFailure, db.validate_collection,
|
||||
"test.doesnotexist")
|
||||
@ -231,12 +235,12 @@ class TestDatabase(unittest.TestCase):
|
||||
if version.at_least(db.connection, (1, 9, 1, -1)):
|
||||
self.assertTrue(isinstance(info[0]['responseLength'], int))
|
||||
self.assertTrue(isinstance(info[0]['millis'], int))
|
||||
self.assertTrue(isinstance(info[0]['client'], basestring))
|
||||
self.assertTrue(isinstance(info[0]['user'], basestring))
|
||||
self.assertTrue(isinstance(info[0]['ns'], basestring))
|
||||
self.assertTrue(isinstance(info[0]['op'], basestring))
|
||||
self.assertTrue(isinstance(info[0]['client'], string_type))
|
||||
self.assertTrue(isinstance(info[0]['user'], string_type))
|
||||
self.assertTrue(isinstance(info[0]['ns'], string_type))
|
||||
self.assertTrue(isinstance(info[0]['op'], string_type))
|
||||
else:
|
||||
self.assertTrue(isinstance(info[0]["info"], basestring))
|
||||
self.assertTrue(isinstance(info[0]["info"], string_type))
|
||||
self.assertTrue(isinstance(info[0]["millis"], float))
|
||||
self.assertTrue(isinstance(info[0]["ts"], datetime.datetime))
|
||||
|
||||
@ -324,13 +328,13 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, auth._password_digest, None)
|
||||
|
||||
self.assertTrue(isinstance(auth._password_digest("mike", "password"),
|
||||
unicode))
|
||||
text_type))
|
||||
self.assertEqual(auth._password_digest("mike", "password"),
|
||||
u"cd7e45b3b2767dc2fa9b6b548457ed00")
|
||||
u("cd7e45b3b2767dc2fa9b6b548457ed00"))
|
||||
self.assertEqual(auth._password_digest("mike", "password"),
|
||||
auth._password_digest(u"mike", u"password"))
|
||||
self.assertEqual(auth._password_digest("Gustave", u"Dor\xe9"),
|
||||
u"81e0e2364499209f466e75926a162d73")
|
||||
auth._password_digest(u("mike"), u("password")))
|
||||
self.assertEqual(auth._password_digest("Gustave", u("Dor\xe9")),
|
||||
u("81e0e2364499209f466e75926a162d73"))
|
||||
|
||||
def test_authenticate_add_remove_user(self):
|
||||
if (is_mongos(self.client) and not
|
||||
@ -377,7 +381,7 @@ class TestDatabase(unittest.TestCase):
|
||||
db.authenticate, "faker", "password")
|
||||
self.assertTrue(db.authenticate("mike", "password"))
|
||||
db.logout()
|
||||
self.assertTrue(db.authenticate(u"mike", u"password"))
|
||||
self.assertTrue(db.authenticate(u("mike"), u("password")))
|
||||
db.remove_user("mike")
|
||||
db.logout()
|
||||
|
||||
@ -386,20 +390,20 @@ class TestDatabase(unittest.TestCase):
|
||||
|
||||
# Add / authenticate / change password
|
||||
self.assertRaises(OperationFailure,
|
||||
db.authenticate, "Gustave", u"Dor\xe9")
|
||||
db.add_user("Gustave", u"Dor\xe9")
|
||||
self.assertTrue(db.authenticate("Gustave", u"Dor\xe9"))
|
||||
db.authenticate, "Gustave", u("Dor\xe9"))
|
||||
db.add_user("Gustave", u("Dor\xe9"))
|
||||
self.assertTrue(db.authenticate("Gustave", u("Dor\xe9")))
|
||||
db.add_user("Gustave", "password")
|
||||
db.logout()
|
||||
self.assertRaises(OperationFailure,
|
||||
db.authenticate, "Gustave", u"Dor\xe9")
|
||||
self.assertTrue(db.authenticate("Gustave", u"password"))
|
||||
db.authenticate, "Gustave", u("Dor\xe9"))
|
||||
self.assertTrue(db.authenticate("Gustave", u("password")))
|
||||
|
||||
if not version.at_least(self.client, (2, 5, 3, -1)):
|
||||
# Add a readOnly user
|
||||
db.add_user("Ross", "password", read_only=True)
|
||||
db.logout()
|
||||
self.assertTrue(db.authenticate("Ross", u"password"))
|
||||
self.assertTrue(db.authenticate("Ross", u("password")))
|
||||
self.assertTrue(db.system.users.find({"readOnly": True}).count())
|
||||
db.logout()
|
||||
|
||||
@ -694,7 +698,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertRaises(TypeError, db.eval, [])
|
||||
|
||||
self.assertEqual(3, db.eval("function (x) {return x;}", 3))
|
||||
self.assertEqual(3, db.eval(u"function (x) {return x;}", 3))
|
||||
self.assertEqual(3, db.eval(u("function (x) {return x;}"), 3))
|
||||
|
||||
self.assertEqual(None,
|
||||
db.eval("function (x) {db.test.save({y:x});}", 5))
|
||||
@ -716,18 +720,18 @@ class TestDatabase(unittest.TestCase):
|
||||
db = Database(self.client, "pymongo_test")
|
||||
db.test.remove({})
|
||||
|
||||
a_doc = SON({"hello": u"world"})
|
||||
a_doc = SON({"hello": u("world")})
|
||||
a_key = db.test.save(a_doc)
|
||||
self.assertTrue(isinstance(a_doc["_id"], ObjectId))
|
||||
self.assertEqual(a_doc["_id"], a_key)
|
||||
self.assertEqual(a_doc, db.test.find_one({"_id": a_doc["_id"]}))
|
||||
self.assertEqual(a_doc, db.test.find_one(a_key))
|
||||
self.assertEqual(None, db.test.find_one(ObjectId()))
|
||||
self.assertEqual(a_doc, db.test.find_one({"hello": u"world"}))
|
||||
self.assertEqual(None, db.test.find_one({"hello": u"test"}))
|
||||
self.assertEqual(a_doc, db.test.find_one({"hello": u("world")}))
|
||||
self.assertEqual(None, db.test.find_one({"hello": u("test")}))
|
||||
|
||||
b = db.test.find_one()
|
||||
b["hello"] = u"mike"
|
||||
b["hello"] = u("mike")
|
||||
db.test.save(b)
|
||||
|
||||
self.assertNotEqual(a_doc, db.test.find_one(a_key))
|
||||
@ -742,8 +746,8 @@ class TestDatabase(unittest.TestCase):
|
||||
def test_long(self):
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
db.test.save({"x": 9223372036854775807L})
|
||||
self.assertEqual(9223372036854775807L, db.test.find_one()["x"])
|
||||
db.test.save({"x": long(9223372036854775807)})
|
||||
self.assertEqual(long(9223372036854775807), db.test.find_one()["x"])
|
||||
|
||||
def test_remove(self):
|
||||
db = self.client.pymongo_test
|
||||
@ -783,7 +787,7 @@ class TestDatabase(unittest.TestCase):
|
||||
db = self.client.pymongo_test
|
||||
db.test.remove({})
|
||||
|
||||
for i in xrange(1000):
|
||||
for i in range(1000):
|
||||
db.test.save({"x": i})
|
||||
|
||||
count = 0
|
||||
@ -793,7 +797,7 @@ class TestDatabase(unittest.TestCase):
|
||||
self.assertEqual(1000, count)
|
||||
|
||||
# test that kill cursors doesn't assert or anything
|
||||
for _ in xrange(62):
|
||||
for _ in range(62):
|
||||
for _ in db.test.find():
|
||||
break
|
||||
|
||||
@ -806,7 +810,7 @@ class TestDatabase(unittest.TestCase):
|
||||
db.test.b.remove({})
|
||||
db.test.c.remove({})
|
||||
|
||||
a = {"hello": u"world"}
|
||||
a = {"hello": u("world")}
|
||||
db.test.a.save(a)
|
||||
|
||||
b = {"test": a}
|
||||
@ -923,7 +927,7 @@ class TestDatabase(unittest.TestCase):
|
||||
|
||||
try:
|
||||
helpers._check_command_response({'$err': 'foo'}, reset=None)
|
||||
except OperationFailure, e:
|
||||
except OperationFailure as e:
|
||||
self.assertEqual(e.args[0], 'foo')
|
||||
else:
|
||||
self.fail("_check_command_response didn't raise OperationFailure")
|
||||
|
||||
@ -21,7 +21,7 @@ sys.path[0:0] = [""]
|
||||
|
||||
from bson.dbref import DBRef
|
||||
from bson.objectid import ObjectId
|
||||
from bson.py3compat import b
|
||||
from bson.py3compat import u
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
@ -37,9 +37,9 @@ class TestDBRef(unittest.TestCase):
|
||||
self.assertRaises(TypeError, DBRef, None, a)
|
||||
self.assertRaises(TypeError, DBRef, "coll", a, 5)
|
||||
self.assertTrue(DBRef("coll", a))
|
||||
self.assertTrue(DBRef(u"coll", a))
|
||||
self.assertTrue(DBRef(u"coll", 5))
|
||||
self.assertTrue(DBRef(u"coll", 5, "database"))
|
||||
self.assertTrue(DBRef(u("coll"), a))
|
||||
self.assertTrue(DBRef(u("coll"), 5))
|
||||
self.assertTrue(DBRef(u("coll"), 5, "database"))
|
||||
|
||||
def test_read_only(self):
|
||||
a = DBRef("coll", ObjectId())
|
||||
@ -60,10 +60,10 @@ class TestDBRef(unittest.TestCase):
|
||||
self.assertEqual(repr(DBRef("coll",
|
||||
ObjectId("1234567890abcdef12345678"))),
|
||||
"DBRef('coll', ObjectId('1234567890abcdef12345678'))")
|
||||
self.assertEqual(repr(DBRef(u"coll",
|
||||
self.assertEqual(repr(DBRef(u("coll"),
|
||||
ObjectId("1234567890abcdef12345678"))),
|
||||
"DBRef(%s, ObjectId('1234567890abcdef12345678'))"
|
||||
% (repr(u'coll'),)
|
||||
% (repr(u('coll')),)
|
||||
)
|
||||
self.assertEqual(repr(DBRef("coll", 5, foo="bar")),
|
||||
"DBRef('coll', 5, foo='bar')")
|
||||
@ -76,23 +76,23 @@ class TestDBRef(unittest.TestCase):
|
||||
obj_id = ObjectId("1234567890abcdef12345678")
|
||||
|
||||
self.assertEqual(DBRef('foo', 5), DBRef('foo', 5))
|
||||
self.assertEqual(DBRef("coll", obj_id), DBRef(u"coll", obj_id))
|
||||
self.assertEqual(DBRef("coll", obj_id), DBRef(u("coll"), obj_id))
|
||||
self.assertNotEqual(DBRef("coll", obj_id),
|
||||
DBRef(u"coll", obj_id, "foo"))
|
||||
DBRef(u("coll"), obj_id, "foo"))
|
||||
self.assertNotEqual(DBRef("coll", obj_id), DBRef("col", obj_id))
|
||||
self.assertNotEqual(DBRef("coll", obj_id),
|
||||
DBRef("coll", ObjectId(b("123456789011"))))
|
||||
DBRef("coll", ObjectId(b"123456789011")))
|
||||
self.assertNotEqual(DBRef("coll", obj_id), 4)
|
||||
self.assertEqual(DBRef("coll", obj_id, "foo"),
|
||||
DBRef(u"coll", obj_id, "foo"))
|
||||
DBRef(u("coll"), obj_id, "foo"))
|
||||
self.assertNotEqual(DBRef("coll", obj_id, "foo"),
|
||||
DBRef(u"coll", obj_id, "bar"))
|
||||
DBRef(u("coll"), obj_id, "bar"))
|
||||
|
||||
# Explicitly test inequality
|
||||
self.assertFalse(DBRef('foo', 5) != DBRef('foo', 5))
|
||||
self.assertFalse(DBRef("coll", obj_id) != DBRef(u"coll", obj_id))
|
||||
self.assertFalse(DBRef("coll", obj_id) != DBRef(u("coll"), obj_id))
|
||||
self.assertFalse(DBRef("coll", obj_id, "foo") !=
|
||||
DBRef(u"coll", obj_id, "foo"))
|
||||
DBRef(u("coll"), obj_id, "foo"))
|
||||
|
||||
def test_kwargs(self):
|
||||
self.assertEqual(DBRef("coll", 5, foo="bar"),
|
||||
|
||||
@ -25,7 +25,7 @@ sys.path[0:0] = [""]
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.objectid import ObjectId
|
||||
from bson.py3compat import b, StringIO
|
||||
from bson.py3compat import u, StringIO
|
||||
from gridfs import GridFS
|
||||
from gridfs.grid_file import (DEFAULT_CHUNK_SIZE,
|
||||
_SEEK_CUR,
|
||||
@ -54,17 +54,17 @@ class TestGridFile(unittest.TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
f = GridIn(self.db.fs, filename="test")
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
self.assertEqual(1, self.db.fs.files.find().count())
|
||||
self.assertEqual(1, self.db.fs.chunks.find().count())
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("hello world"), g.read())
|
||||
self.assertEqual(b"hello world", g.read())
|
||||
|
||||
# make sure it's still there...
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("hello world"), g.read())
|
||||
self.assertEqual(b"hello world", g.read())
|
||||
|
||||
f = GridIn(self.db.fs, filename="test")
|
||||
f.close()
|
||||
@ -72,14 +72,14 @@ class TestGridFile(unittest.TestCase):
|
||||
self.assertEqual(1, self.db.fs.chunks.find().count())
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b(""), g.read())
|
||||
self.assertEqual(b"", g.read())
|
||||
|
||||
# test that reading 0 returns proper type
|
||||
self.assertEqual(b(""), g.read(0))
|
||||
self.assertEqual(b"", g.read(0))
|
||||
|
||||
def test_md5(self):
|
||||
f = GridIn(self.db.fs)
|
||||
f.write(b("hello world\n"))
|
||||
f.write(b"hello world\n")
|
||||
f.close()
|
||||
self.assertEqual("6f5902ac237024bdd0c176cb93063dc4", f.md5)
|
||||
|
||||
@ -88,14 +88,14 @@ class TestGridFile(unittest.TestCase):
|
||||
self.db.alt.chunks.remove({})
|
||||
|
||||
f = GridIn(self.db.alt)
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
|
||||
self.assertEqual(1, self.db.alt.files.find().count())
|
||||
self.assertEqual(1, self.db.alt.chunks.find().count())
|
||||
|
||||
g = GridOut(self.db.alt, f._id)
|
||||
self.assertEqual(b("hello world"), g.read())
|
||||
self.assertEqual(b"hello world", g.read())
|
||||
|
||||
# test that md5 still works...
|
||||
self.assertEqual("5eb63bbbe01eeed093cb22bb8f5acdc3", g.md5)
|
||||
@ -230,7 +230,7 @@ class TestGridFile(unittest.TestCase):
|
||||
one = GridIn(self.db.fs, _id=5, filename="my_file",
|
||||
contentType="text/html", chunkSize=1000, aliases=["foo"],
|
||||
metadata={"foo": 1, "bar": 2}, bar=3, baz="hello")
|
||||
one.write(b("hello world"))
|
||||
one.write(b"hello world")
|
||||
one.close()
|
||||
|
||||
two = GridOut(self.db.fs, 5)
|
||||
@ -253,20 +253,20 @@ class TestGridFile(unittest.TestCase):
|
||||
|
||||
def test_grid_out_file_document(self):
|
||||
one = GridIn(self.db.fs)
|
||||
one.write(b("foo bar"))
|
||||
one.write(b"foo bar")
|
||||
one.close()
|
||||
|
||||
two = GridOut(self.db.fs, file_document=self.db.fs.files.find_one())
|
||||
self.assertEqual(b("foo bar"), two.read())
|
||||
self.assertEqual(b"foo bar", two.read())
|
||||
|
||||
three = GridOut(self.db.fs, 5, file_document=self.db.fs.files.find_one())
|
||||
self.assertEqual(b("foo bar"), three.read())
|
||||
self.assertEqual(b"foo bar", three.read())
|
||||
|
||||
self.assertRaises(NoFile, GridOut, self.db.fs, file_document={})
|
||||
|
||||
def test_write_file_like(self):
|
||||
one = GridIn(self.db.fs)
|
||||
one.write(b("hello world"))
|
||||
one.write(b"hello world")
|
||||
one.close()
|
||||
|
||||
two = GridOut(self.db.fs, one._id)
|
||||
@ -276,23 +276,23 @@ class TestGridFile(unittest.TestCase):
|
||||
three.close()
|
||||
|
||||
four = GridOut(self.db.fs, three._id)
|
||||
self.assertEqual(b("hello world"), four.read())
|
||||
self.assertEqual(b"hello world", four.read())
|
||||
|
||||
five = GridIn(self.db.fs, chunk_size=2)
|
||||
five.write(b("hello"))
|
||||
buffer = StringIO(b(" world"))
|
||||
five.write(b"hello")
|
||||
buffer = StringIO(b" world")
|
||||
five.write(buffer)
|
||||
five.write(b(" and mongodb"))
|
||||
five.write(b" and mongodb")
|
||||
five.close()
|
||||
self.assertEqual(b("hello world and mongodb"),
|
||||
self.assertEqual(b"hello world and mongodb",
|
||||
GridOut(self.db.fs, five._id).read())
|
||||
|
||||
def test_write_lines(self):
|
||||
a = GridIn(self.db.fs)
|
||||
a.writelines([b("hello "), b("world")])
|
||||
a.writelines([b"hello ", b"world"])
|
||||
a.close()
|
||||
|
||||
self.assertEqual(b("hello world"), GridOut(self.db.fs, a._id).read())
|
||||
self.assertEqual(b"hello world", GridOut(self.db.fs, a._id).read())
|
||||
|
||||
def test_close(self):
|
||||
f = GridIn(self.db.fs)
|
||||
@ -301,7 +301,7 @@ class TestGridFile(unittest.TestCase):
|
||||
f.close()
|
||||
|
||||
def test_multi_chunk_file(self):
|
||||
random_string = b('a') * (DEFAULT_CHUNK_SIZE + 1000)
|
||||
random_string = b'a' * (DEFAULT_CHUNK_SIZE + 1000)
|
||||
|
||||
f = GridIn(self.db.fs)
|
||||
f.write(random_string)
|
||||
@ -340,31 +340,31 @@ class TestGridFile(unittest.TestCase):
|
||||
|
||||
def test_seek(self):
|
||||
f = GridIn(self.db.fs, chunkSize=3)
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("hello world"), g.read())
|
||||
self.assertEqual(b"hello world", g.read())
|
||||
g.seek(0)
|
||||
self.assertEqual(b("hello world"), g.read())
|
||||
self.assertEqual(b"hello world", g.read())
|
||||
g.seek(1)
|
||||
self.assertEqual(b("ello world"), g.read())
|
||||
self.assertEqual(b"ello world", g.read())
|
||||
self.assertRaises(IOError, g.seek, -1)
|
||||
|
||||
g.seek(-3, _SEEK_END)
|
||||
self.assertEqual(b("rld"), g.read())
|
||||
self.assertEqual(b"rld", g.read())
|
||||
g.seek(0, _SEEK_END)
|
||||
self.assertEqual(b(""), g.read())
|
||||
self.assertEqual(b"", g.read())
|
||||
self.assertRaises(IOError, g.seek, -100, _SEEK_END)
|
||||
|
||||
g.seek(3)
|
||||
g.seek(3, _SEEK_CUR)
|
||||
self.assertEqual(b("world"), g.read())
|
||||
self.assertEqual(b"world", g.read())
|
||||
self.assertRaises(IOError, g.seek, -100, _SEEK_CUR)
|
||||
|
||||
def test_tell(self):
|
||||
f = GridIn(self.db.fs, chunkSize=3)
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
@ -380,21 +380,21 @@ class TestGridFile(unittest.TestCase):
|
||||
|
||||
def test_multiple_reads(self):
|
||||
f = GridIn(self.db.fs, chunkSize=3)
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("he"), g.read(2))
|
||||
self.assertEqual(b("ll"), g.read(2))
|
||||
self.assertEqual(b("o "), g.read(2))
|
||||
self.assertEqual(b("wo"), g.read(2))
|
||||
self.assertEqual(b("rl"), g.read(2))
|
||||
self.assertEqual(b("d"), g.read(2))
|
||||
self.assertEqual(b(""), g.read(2))
|
||||
self.assertEqual(b"he", g.read(2))
|
||||
self.assertEqual(b"ll", g.read(2))
|
||||
self.assertEqual(b"o ", g.read(2))
|
||||
self.assertEqual(b"wo", g.read(2))
|
||||
self.assertEqual(b"rl", g.read(2))
|
||||
self.assertEqual(b"d", g.read(2))
|
||||
self.assertEqual(b"", g.read(2))
|
||||
|
||||
def test_readline(self):
|
||||
f = GridIn(self.db.fs, chunkSize=5)
|
||||
f.write(b("""Hello world,
|
||||
f.write((b"""Hello world,
|
||||
How are you?
|
||||
Hope all is well.
|
||||
Bye"""))
|
||||
@ -402,27 +402,27 @@ Bye"""))
|
||||
|
||||
# Try read(), then readline().
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("H"), g.read(1))
|
||||
self.assertEqual(b("ello world,\n"), g.readline())
|
||||
self.assertEqual(b("How a"), g.readline(5))
|
||||
self.assertEqual(b(""), g.readline(0))
|
||||
self.assertEqual(b("re you?\n"), g.readline())
|
||||
self.assertEqual(b("Hope all is well.\n"), g.readline(1000))
|
||||
self.assertEqual(b("Bye"), g.readline())
|
||||
self.assertEqual(b(""), g.readline())
|
||||
self.assertEqual(b"H", g.read(1))
|
||||
self.assertEqual(b"ello world,\n", g.readline())
|
||||
self.assertEqual(b"How a", g.readline(5))
|
||||
self.assertEqual(b"", g.readline(0))
|
||||
self.assertEqual(b"re you?\n", g.readline())
|
||||
self.assertEqual(b"Hope all is well.\n", g.readline(1000))
|
||||
self.assertEqual(b"Bye", g.readline())
|
||||
self.assertEqual(b"", g.readline())
|
||||
|
||||
# Try readline() first, then read().
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("He"), g.readline(2))
|
||||
self.assertEqual(b("l"), g.read(1))
|
||||
self.assertEqual(b("lo"), g.readline(2))
|
||||
self.assertEqual(b(" world,\n"), g.readline())
|
||||
self.assertEqual(b"He", g.readline(2))
|
||||
self.assertEqual(b"l", g.read(1))
|
||||
self.assertEqual(b"lo", g.readline(2))
|
||||
self.assertEqual(b" world,\n", g.readline())
|
||||
|
||||
# Only readline().
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("H"), g.readline(1))
|
||||
self.assertEqual(b("e"), g.readline(1))
|
||||
self.assertEqual(b("llo world,\n"), g.readline())
|
||||
self.assertEqual(b"H", g.readline(1))
|
||||
self.assertEqual(b"e", g.readline(1))
|
||||
self.assertEqual(b"llo world,\n", g.readline())
|
||||
|
||||
def test_iterator(self):
|
||||
f = GridIn(self.db.fs)
|
||||
@ -431,30 +431,30 @@ Bye"""))
|
||||
self.assertEqual([], list(g))
|
||||
|
||||
f = GridIn(self.db.fs)
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual([b("hello world")], list(g))
|
||||
self.assertEqual(b("hello"), g.read(5))
|
||||
self.assertEqual([b("hello world")], list(g))
|
||||
self.assertEqual(b(" worl"), g.read(5))
|
||||
self.assertEqual([b"hello world"], list(g))
|
||||
self.assertEqual(b"hello", g.read(5))
|
||||
self.assertEqual([b"hello world"], list(g))
|
||||
self.assertEqual(b" worl", g.read(5))
|
||||
|
||||
f = GridIn(self.db.fs, chunk_size=2)
|
||||
f.write(b("hello world"))
|
||||
f.write(b"hello world")
|
||||
f.close()
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual([b("he"), b("ll"), b("o "),
|
||||
b("wo"), b("rl"), b("d")], list(g))
|
||||
self.assertEqual([b"he", b"ll", b"o ",
|
||||
b"wo", b"rl", b"d"], list(g))
|
||||
|
||||
def test_read_unaligned_buffer_size(self):
|
||||
in_data = b("This is a text that doesn't "
|
||||
"quite fit in a single 16-byte chunk.")
|
||||
in_data = (b"This is a text that doesn't "
|
||||
b"quite fit in a single 16-byte chunk.")
|
||||
f = GridIn(self.db.fs, chunkSize=16)
|
||||
f.write(in_data)
|
||||
f.close()
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
out_data = b('')
|
||||
out_data = b''
|
||||
while 1:
|
||||
s = g.read(13)
|
||||
if not s:
|
||||
@ -464,7 +464,7 @@ Bye"""))
|
||||
self.assertEqual(in_data, out_data)
|
||||
|
||||
def test_readchunk(self):
|
||||
in_data = b('a') * 10
|
||||
in_data = b'a' * 10
|
||||
f = GridIn(self.db.fs, chunkSize=3)
|
||||
f.write(in_data)
|
||||
f.close()
|
||||
@ -483,21 +483,21 @@ Bye"""))
|
||||
|
||||
def test_write_unicode(self):
|
||||
f = GridIn(self.db.fs)
|
||||
self.assertRaises(TypeError, f.write, u"foo")
|
||||
self.assertRaises(TypeError, f.write, u("foo"))
|
||||
|
||||
f = GridIn(self.db.fs, encoding="utf-8")
|
||||
f.write(u"foo")
|
||||
f.write(u("foo"))
|
||||
f.close()
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(b("foo"), g.read())
|
||||
self.assertEqual(b"foo", g.read())
|
||||
|
||||
f = GridIn(self.db.fs, encoding="iso-8859-1")
|
||||
f.write(u"aé")
|
||||
f.write(u("aé"))
|
||||
f.close()
|
||||
|
||||
g = GridOut(self.db.fs, f._id)
|
||||
self.assertEqual(u"aé".encode("iso-8859-1"), g.read())
|
||||
self.assertEqual(u("aé").encode("iso-8859-1"), g.read())
|
||||
|
||||
def test_set_after_close(self):
|
||||
f = GridIn(self.db.fs, _id="foo", bar="baz")
|
||||
@ -535,18 +535,13 @@ Bye"""))
|
||||
self.assertRaises(AttributeError, getattr, g, "_closed")
|
||||
|
||||
def test_context_manager(self):
|
||||
if sys.version_info < (2, 6):
|
||||
raise SkipTest("With statement requires Python >= 2.6")
|
||||
contents = b"Imagine this is some important data..."
|
||||
|
||||
contents = b("Imagine this is some important data...")
|
||||
# Hack around python2.4 an 2.5 not supporting 'with' syntax
|
||||
exec """
|
||||
with GridIn(self.db.fs, filename="important") as infile:
|
||||
infile.write(contents)
|
||||
with GridIn(self.db.fs, filename="important") as infile:
|
||||
infile.write(contents)
|
||||
|
||||
with GridOut(self.db.fs, infile._id) as outfile:
|
||||
self.assertEqual(contents, outfile.read())
|
||||
"""
|
||||
with GridOut(self.db.fs, infile._id) as outfile:
|
||||
self.assertEqual(contents, outfile.read())
|
||||
|
||||
def test_prechunked_string(self):
|
||||
|
||||
@ -555,7 +550,7 @@ with GridOut(self.db.fs, infile._id) as outfile:
|
||||
infile = GridIn(self.db.fs)
|
||||
while True:
|
||||
to_write = buf.read(chunk_size)
|
||||
if to_write == b(''):
|
||||
if to_write == b'':
|
||||
break
|
||||
infile.write(to_write)
|
||||
infile.close()
|
||||
@ -565,7 +560,7 @@ with GridOut(self.db.fs, infile._id) as outfile:
|
||||
data = outfile.read()
|
||||
self.assertEqual(s, data)
|
||||
|
||||
s = b('x' * DEFAULT_CHUNK_SIZE * 4)
|
||||
s = b'x' * DEFAULT_CHUNK_SIZE * 4
|
||||
# Test with default chunk size
|
||||
write_me(s, DEFAULT_CHUNK_SIZE)
|
||||
# Multiple
|
||||
@ -590,7 +585,7 @@ with GridOut(self.db.fs, infile._id) as outfile:
|
||||
client = MongoClient('badhost', _connect=False)
|
||||
fs = client.db.fs
|
||||
infile = GridIn(fs, file_id=-1, chunk_size=1)
|
||||
self.assertRaises(ConnectionFailure, infile.write, b('data goes here'))
|
||||
self.assertRaises(ConnectionFailure, infile.write, b'data goes here')
|
||||
self.assertRaises(ConnectionFailure, infile.close)
|
||||
|
||||
def test_grid_out_cursor_options(self):
|
||||
|
||||
@ -30,7 +30,7 @@ import threading
|
||||
import time
|
||||
import gridfs
|
||||
|
||||
from bson.py3compat import b, StringIO
|
||||
from bson.py3compat import u, StringIO, string_type
|
||||
from gridfs.errors import (FileExists,
|
||||
NoFile)
|
||||
from test.test_client import get_client
|
||||
@ -48,7 +48,7 @@ class JustWrite(threading.Thread):
|
||||
def run(self):
|
||||
for _ in range(self.n):
|
||||
file = self.fs.new_file(filename="test")
|
||||
file.write(b("hello"))
|
||||
file.write(b"hello")
|
||||
file.close()
|
||||
|
||||
|
||||
@ -66,7 +66,7 @@ class JustRead(threading.Thread):
|
||||
file = self.fs.get("test")
|
||||
data = file.read()
|
||||
self.results.append(data)
|
||||
assert data == b("hello")
|
||||
assert data == b"hello"
|
||||
|
||||
|
||||
class TestGridfs(unittest.TestCase):
|
||||
@ -88,8 +88,8 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertRaises(TypeError, gridfs.GridFS, self.db, 5)
|
||||
|
||||
def test_basic(self):
|
||||
oid = self.fs.put(b("hello world"))
|
||||
self.assertEqual(b("hello world"), self.fs.get(oid).read())
|
||||
oid = self.fs.put(b"hello world")
|
||||
self.assertEqual(b"hello world", self.fs.get(oid).read())
|
||||
self.assertEqual(1, self.db.fs.files.count())
|
||||
self.assertEqual(1, self.db.fs.chunks.count())
|
||||
|
||||
@ -99,13 +99,13 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertEqual(0, self.db.fs.chunks.count())
|
||||
|
||||
self.assertRaises(NoFile, self.fs.get, "foo")
|
||||
oid = self.fs.put(b("hello world"), _id="foo")
|
||||
oid = self.fs.put(b"hello world", _id="foo")
|
||||
self.assertEqual("foo", oid)
|
||||
self.assertEqual(b("hello world"), self.fs.get("foo").read())
|
||||
self.assertEqual(b"hello world", self.fs.get("foo").read())
|
||||
|
||||
def test_list(self):
|
||||
self.assertEqual([], self.fs.list())
|
||||
self.fs.put(b("hello world"))
|
||||
self.fs.put(b"hello world")
|
||||
self.assertEqual([], self.fs.list())
|
||||
|
||||
# PYTHON-598: in server versions before 2.5.x, creating an index on
|
||||
@ -113,16 +113,16 @@ class TestGridfs(unittest.TestCase):
|
||||
self.fs.get_last_version()
|
||||
self.assertEqual([], self.fs.list())
|
||||
|
||||
self.fs.put(b(""), filename="mike")
|
||||
self.fs.put(b("foo"), filename="test")
|
||||
self.fs.put(b(""), filename="hello world")
|
||||
self.fs.put(b"", filename="mike")
|
||||
self.fs.put(b"foo", filename="test")
|
||||
self.fs.put(b"", filename="hello world")
|
||||
|
||||
self.assertEqual(set(["mike", "test", "hello world"]),
|
||||
set(self.fs.list()))
|
||||
|
||||
def test_empty_file(self):
|
||||
oid = self.fs.put(b(""))
|
||||
self.assertEqual(b(""), self.fs.get(oid).read())
|
||||
oid = self.fs.put(b"")
|
||||
self.assertEqual(b"", self.fs.get(oid).read())
|
||||
self.assertEqual(1, self.db.fs.files.count())
|
||||
self.assertEqual(0, self.db.fs.chunks.count())
|
||||
|
||||
@ -131,11 +131,11 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertEqual(oid, raw["_id"])
|
||||
self.assertTrue(isinstance(raw["uploadDate"], datetime.datetime))
|
||||
self.assertEqual(255 * 1024, raw["chunkSize"])
|
||||
self.assertTrue(isinstance(raw["md5"], basestring))
|
||||
self.assertTrue(isinstance(raw["md5"], string_type))
|
||||
|
||||
def test_alt_collection(self):
|
||||
oid = self.alt.put(b("hello world"))
|
||||
self.assertEqual(b("hello world"), self.alt.get(oid).read())
|
||||
oid = self.alt.put(b"hello world")
|
||||
self.assertEqual(b"hello world", self.alt.get(oid).read())
|
||||
self.assertEqual(1, self.db.alt.files.count())
|
||||
self.assertEqual(1, self.db.alt.chunks.count())
|
||||
|
||||
@ -145,19 +145,19 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertEqual(0, self.db.alt.chunks.count())
|
||||
|
||||
self.assertRaises(NoFile, self.alt.get, "foo")
|
||||
oid = self.alt.put(b("hello world"), _id="foo")
|
||||
oid = self.alt.put(b"hello world", _id="foo")
|
||||
self.assertEqual("foo", oid)
|
||||
self.assertEqual(b("hello world"), self.alt.get("foo").read())
|
||||
self.assertEqual(b"hello world", self.alt.get("foo").read())
|
||||
|
||||
self.alt.put(b(""), filename="mike")
|
||||
self.alt.put(b("foo"), filename="test")
|
||||
self.alt.put(b(""), filename="hello world")
|
||||
self.alt.put(b"", filename="mike")
|
||||
self.alt.put(b"foo", filename="test")
|
||||
self.alt.put(b"", filename="hello world")
|
||||
|
||||
self.assertEqual(set(["mike", "test", "hello world"]),
|
||||
set(self.alt.list()))
|
||||
|
||||
def test_threaded_reads(self):
|
||||
self.fs.put(b("hello"), _id="test")
|
||||
self.fs.put(b"hello", _id="test")
|
||||
|
||||
threads = []
|
||||
results = []
|
||||
@ -168,7 +168,7 @@ class TestGridfs(unittest.TestCase):
|
||||
joinall(threads)
|
||||
|
||||
self.assertEqual(
|
||||
100 * [b('hello')],
|
||||
100 * [b'hello'],
|
||||
results
|
||||
)
|
||||
|
||||
@ -181,7 +181,7 @@ class TestGridfs(unittest.TestCase):
|
||||
joinall(threads)
|
||||
|
||||
f = self.fs.get_last_version("test")
|
||||
self.assertEqual(f.read(), b("hello"))
|
||||
self.assertEqual(f.read(), b"hello")
|
||||
|
||||
# Should have created 100 versions of 'test' file
|
||||
self.assertEqual(
|
||||
@ -190,40 +190,40 @@ class TestGridfs(unittest.TestCase):
|
||||
)
|
||||
|
||||
def test_get_last_version(self):
|
||||
one = self.fs.put(b("foo"), filename="test")
|
||||
one = self.fs.put(b"foo", filename="test")
|
||||
time.sleep(0.01)
|
||||
two = self.fs.new_file(filename="test")
|
||||
two.write(b("bar"))
|
||||
two.write(b"bar")
|
||||
two.close()
|
||||
time.sleep(0.01)
|
||||
two = two._id
|
||||
three = self.fs.put(b("baz"), filename="test")
|
||||
three = self.fs.put(b"baz", filename="test")
|
||||
|
||||
self.assertEqual(b("baz"), self.fs.get_last_version("test").read())
|
||||
self.assertEqual(b"baz", self.fs.get_last_version("test").read())
|
||||
self.fs.delete(three)
|
||||
self.assertEqual(b("bar"), self.fs.get_last_version("test").read())
|
||||
self.assertEqual(b"bar", self.fs.get_last_version("test").read())
|
||||
self.fs.delete(two)
|
||||
self.assertEqual(b("foo"), self.fs.get_last_version("test").read())
|
||||
self.assertEqual(b"foo", self.fs.get_last_version("test").read())
|
||||
self.fs.delete(one)
|
||||
self.assertRaises(NoFile, self.fs.get_last_version, "test")
|
||||
|
||||
def test_get_last_version_with_metadata(self):
|
||||
one = self.fs.put(b("foo"), filename="test", author="author")
|
||||
one = self.fs.put(b"foo", filename="test", author="author")
|
||||
time.sleep(0.01)
|
||||
two = self.fs.put(b("bar"), filename="test", author="author")
|
||||
two = self.fs.put(b"bar", filename="test", author="author")
|
||||
|
||||
self.assertEqual(b("bar"), self.fs.get_last_version(author="author").read())
|
||||
self.assertEqual(b"bar", self.fs.get_last_version(author="author").read())
|
||||
self.fs.delete(two)
|
||||
self.assertEqual(b("foo"), self.fs.get_last_version(author="author").read())
|
||||
self.assertEqual(b"foo", self.fs.get_last_version(author="author").read())
|
||||
self.fs.delete(one)
|
||||
|
||||
one = self.fs.put(b("foo"), filename="test", author="author1")
|
||||
one = self.fs.put(b"foo", filename="test", author="author1")
|
||||
time.sleep(0.01)
|
||||
two = self.fs.put(b("bar"), filename="test", author="author2")
|
||||
two = self.fs.put(b"bar", filename="test", author="author2")
|
||||
|
||||
self.assertEqual(b("foo"), self.fs.get_last_version(author="author1").read())
|
||||
self.assertEqual(b("bar"), self.fs.get_last_version(author="author2").read())
|
||||
self.assertEqual(b("bar"), self.fs.get_last_version(filename="test").read())
|
||||
self.assertEqual(b"foo", self.fs.get_last_version(author="author1").read())
|
||||
self.assertEqual(b"bar", self.fs.get_last_version(author="author2").read())
|
||||
self.assertEqual(b"bar", self.fs.get_last_version(filename="test").read())
|
||||
|
||||
self.assertRaises(NoFile, self.fs.get_last_version, author="author3")
|
||||
self.assertRaises(NoFile, self.fs.get_last_version, filename="nottest", author="author1")
|
||||
@ -232,38 +232,38 @@ class TestGridfs(unittest.TestCase):
|
||||
self.fs.delete(two)
|
||||
|
||||
def test_get_version(self):
|
||||
self.fs.put(b("foo"), filename="test")
|
||||
self.fs.put(b"foo", filename="test")
|
||||
time.sleep(0.01)
|
||||
self.fs.put(b("bar"), filename="test")
|
||||
self.fs.put(b"bar", filename="test")
|
||||
time.sleep(0.01)
|
||||
self.fs.put(b("baz"), filename="test")
|
||||
self.fs.put(b"baz", filename="test")
|
||||
time.sleep(0.01)
|
||||
|
||||
self.assertEqual(b("foo"), self.fs.get_version("test", 0).read())
|
||||
self.assertEqual(b("bar"), self.fs.get_version("test", 1).read())
|
||||
self.assertEqual(b("baz"), self.fs.get_version("test", 2).read())
|
||||
self.assertEqual(b"foo", self.fs.get_version("test", 0).read())
|
||||
self.assertEqual(b"bar", self.fs.get_version("test", 1).read())
|
||||
self.assertEqual(b"baz", self.fs.get_version("test", 2).read())
|
||||
|
||||
self.assertEqual(b("baz"), self.fs.get_version("test", -1).read())
|
||||
self.assertEqual(b("bar"), self.fs.get_version("test", -2).read())
|
||||
self.assertEqual(b("foo"), self.fs.get_version("test", -3).read())
|
||||
self.assertEqual(b"baz", self.fs.get_version("test", -1).read())
|
||||
self.assertEqual(b"bar", self.fs.get_version("test", -2).read())
|
||||
self.assertEqual(b"foo", self.fs.get_version("test", -3).read())
|
||||
|
||||
self.assertRaises(NoFile, self.fs.get_version, "test", 3)
|
||||
self.assertRaises(NoFile, self.fs.get_version, "test", -4)
|
||||
|
||||
def test_get_version_with_metadata(self):
|
||||
one = self.fs.put(b("foo"), filename="test", author="author1")
|
||||
one = self.fs.put(b"foo", filename="test", author="author1")
|
||||
time.sleep(0.01)
|
||||
two = self.fs.put(b("bar"), filename="test", author="author1")
|
||||
two = self.fs.put(b"bar", filename="test", author="author1")
|
||||
time.sleep(0.01)
|
||||
three = self.fs.put(b("baz"), filename="test", author="author2")
|
||||
three = self.fs.put(b"baz", filename="test", author="author2")
|
||||
|
||||
self.assertEqual(b("foo"), self.fs.get_version(filename="test", author="author1", version=-2).read())
|
||||
self.assertEqual(b("bar"), self.fs.get_version(filename="test", author="author1", version=-1).read())
|
||||
self.assertEqual(b("foo"), self.fs.get_version(filename="test", author="author1", version=0).read())
|
||||
self.assertEqual(b("bar"), self.fs.get_version(filename="test", author="author1", version=1).read())
|
||||
self.assertEqual(b("baz"), self.fs.get_version(filename="test", author="author2", version=0).read())
|
||||
self.assertEqual(b("baz"), self.fs.get_version(filename="test", version=-1).read())
|
||||
self.assertEqual(b("baz"), self.fs.get_version(filename="test", version=2).read())
|
||||
self.assertEqual(b"foo", self.fs.get_version(filename="test", author="author1", version=-2).read())
|
||||
self.assertEqual(b"bar", self.fs.get_version(filename="test", author="author1", version=-1).read())
|
||||
self.assertEqual(b"foo", self.fs.get_version(filename="test", author="author1", version=0).read())
|
||||
self.assertEqual(b"bar", self.fs.get_version(filename="test", author="author1", version=1).read())
|
||||
self.assertEqual(b"baz", self.fs.get_version(filename="test", author="author2", version=0).read())
|
||||
self.assertEqual(b"baz", self.fs.get_version(filename="test", version=-1).read())
|
||||
self.assertEqual(b"baz", self.fs.get_version(filename="test", version=2).read())
|
||||
|
||||
self.assertRaises(NoFile, self.fs.get_version, filename="test", author="author3")
|
||||
self.assertRaises(NoFile, self.fs.get_version, filename="test", author="author1", version=2)
|
||||
@ -273,26 +273,26 @@ class TestGridfs(unittest.TestCase):
|
||||
self.fs.delete(three)
|
||||
|
||||
def test_put_filelike(self):
|
||||
oid = self.fs.put(StringIO(b("hello world")), chunk_size=1)
|
||||
oid = self.fs.put(StringIO(b"hello world"), chunk_size=1)
|
||||
self.assertEqual(11, self.db.fs.chunks.count())
|
||||
self.assertEqual(b("hello world"), self.fs.get(oid).read())
|
||||
self.assertEqual(b"hello world", self.fs.get(oid).read())
|
||||
|
||||
def test_file_exists(self):
|
||||
db = get_client(w=1).pymongo_test
|
||||
fs = gridfs.GridFS(db)
|
||||
|
||||
oid = fs.put(b("hello"))
|
||||
self.assertRaises(FileExists, fs.put, b("world"), _id=oid)
|
||||
oid = fs.put(b"hello")
|
||||
self.assertRaises(FileExists, fs.put, b"world", _id=oid)
|
||||
|
||||
one = fs.new_file(_id=123)
|
||||
one.write(b("some content"))
|
||||
one.write(b"some content")
|
||||
one.close()
|
||||
|
||||
two = fs.new_file(_id=123)
|
||||
self.assertRaises(FileExists, two.write, b('x' * 262146))
|
||||
self.assertRaises(FileExists, two.write, b'x' * 262146)
|
||||
|
||||
def test_exists(self):
|
||||
oid = self.fs.put(b("hello"))
|
||||
oid = self.fs.put(b"hello")
|
||||
self.assertTrue(self.fs.exists(oid))
|
||||
self.assertTrue(self.fs.exists({"_id": oid}))
|
||||
self.assertTrue(self.fs.exists(_id=oid))
|
||||
@ -300,7 +300,7 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertFalse(self.fs.exists(filename="mike"))
|
||||
self.assertFalse(self.fs.exists("mike"))
|
||||
|
||||
oid = self.fs.put(b("hello"), filename="mike", foo=12)
|
||||
oid = self.fs.put(b"hello", filename="mike", foo=12)
|
||||
self.assertTrue(self.fs.exists(oid))
|
||||
self.assertTrue(self.fs.exists({"_id": oid}))
|
||||
self.assertTrue(self.fs.exists(_id=oid))
|
||||
@ -317,19 +317,19 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertFalse(self.fs.exists({"foo": {"$gt": 12}}))
|
||||
|
||||
def test_put_unicode(self):
|
||||
self.assertRaises(TypeError, self.fs.put, u"hello")
|
||||
self.assertRaises(TypeError, self.fs.put, u("hello"))
|
||||
|
||||
oid = self.fs.put(u"hello", encoding="utf-8")
|
||||
self.assertEqual(b("hello"), self.fs.get(oid).read())
|
||||
oid = self.fs.put(u("hello"), encoding="utf-8")
|
||||
self.assertEqual(b"hello", self.fs.get(oid).read())
|
||||
self.assertEqual("utf-8", self.fs.get(oid).encoding)
|
||||
|
||||
oid = self.fs.put(u"aé", encoding="iso-8859-1")
|
||||
self.assertEqual(u"aé".encode("iso-8859-1"), self.fs.get(oid).read())
|
||||
oid = self.fs.put(u("aé"), encoding="iso-8859-1")
|
||||
self.assertEqual(u("aé").encode("iso-8859-1"), self.fs.get(oid).read())
|
||||
self.assertEqual("iso-8859-1", self.fs.get(oid).encoding)
|
||||
|
||||
def test_missing_length_iter(self):
|
||||
# Test fix that guards against PHP-237
|
||||
self.fs.put(b(""), filename="empty")
|
||||
self.fs.put(b"", filename="empty")
|
||||
doc = self.db.fs.files.find_one({"filename": "empty"})
|
||||
doc.pop("length")
|
||||
self.db.fs.files.save(doc)
|
||||
@ -348,7 +348,7 @@ class TestGridfs(unittest.TestCase):
|
||||
n = 5
|
||||
for i in range(n):
|
||||
file = self.fs.new_file(filename="test")
|
||||
file.write(b("hello"))
|
||||
file.write(b"hello")
|
||||
file.close()
|
||||
|
||||
c.end_request()
|
||||
@ -360,7 +360,7 @@ class TestGridfs(unittest.TestCase):
|
||||
|
||||
def test_gridfs_request(self):
|
||||
self.assertFalse(self.db.connection.in_request())
|
||||
self.fs.put(b("hello world"))
|
||||
self.fs.put(b"hello world")
|
||||
# Request started and ended by put(), we're back to original state
|
||||
self.assertFalse(self.db.connection.in_request())
|
||||
|
||||
@ -374,25 +374,25 @@ class TestGridfs(unittest.TestCase):
|
||||
self.assertRaises(ConnectionFailure, f.close)
|
||||
|
||||
def test_gridfs_find(self):
|
||||
self.fs.put(b("test2"), filename="two")
|
||||
self.fs.put(b"test2", filename="two")
|
||||
time.sleep(0.01)
|
||||
self.fs.put(b("test2+"), filename="two")
|
||||
self.fs.put(b"test2+", filename="two")
|
||||
time.sleep(0.01)
|
||||
self.fs.put(b("test1"), filename="one")
|
||||
self.fs.put(b"test1", filename="one")
|
||||
time.sleep(0.01)
|
||||
self.fs.put(b("test2++"), filename="two")
|
||||
self.fs.put(b"test2++", filename="two")
|
||||
self.assertEqual(3, self.fs.find({"filename":"two"}).count())
|
||||
self.assertEqual(4, self.fs.find().count())
|
||||
cursor = self.fs.find(timeout=False).sort("uploadDate", -1).skip(1).limit(2)
|
||||
# 2to3 hint...
|
||||
gout = cursor.next()
|
||||
self.assertEqual(b("test1"), gout.read())
|
||||
gout = next(cursor)
|
||||
self.assertEqual(b"test1", gout.read())
|
||||
cursor.rewind()
|
||||
gout = cursor.next()
|
||||
self.assertEqual(b("test1"), gout.read())
|
||||
gout = cursor.next()
|
||||
self.assertEqual(b("test2+"), gout.read())
|
||||
self.assertRaises(StopIteration, cursor.next)
|
||||
gout = next(cursor)
|
||||
self.assertEqual(b"test1", gout.read())
|
||||
gout = next(cursor)
|
||||
self.assertEqual(b"test2+", gout.read())
|
||||
self.assertRaises(StopIteration, cursor.__next__)
|
||||
cursor.close()
|
||||
self.assertRaises(TypeError, self.fs.find, {}, {"_id": True})
|
||||
|
||||
@ -405,9 +405,9 @@ class TestGridfsReplicaSet(TestReplicaSetClientBase):
|
||||
|
||||
try:
|
||||
fs = gridfs.GridFS(rsc.pymongo_test)
|
||||
oid = fs.put(b('foo'))
|
||||
oid = fs.put(b'foo')
|
||||
content = fs.get(oid).read()
|
||||
self.assertEqual(b('foo'), content)
|
||||
self.assertEqual(b'foo', content)
|
||||
finally:
|
||||
rsc.close()
|
||||
|
||||
@ -428,7 +428,7 @@ class TestGridfsReplicaSet(TestReplicaSetClientBase):
|
||||
fs = gridfs.GridFS(secondary_connection.pymongo_test)
|
||||
|
||||
# This won't detect secondary, raises error
|
||||
self.assertRaises(ConnectionFailure, fs.put, b('foo'))
|
||||
self.assertRaises(ConnectionFailure, fs.put, b'foo')
|
||||
|
||||
def test_gridfs_secondary_lazy(self):
|
||||
# Should detect it's connected to secondary and not attempt to
|
||||
|
||||
@ -24,7 +24,6 @@ from nose.plugins.skip import SkipTest
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
import bson
|
||||
from bson.py3compat import b
|
||||
from bson import json_util
|
||||
from bson.binary import Binary, MD5_SUBTYPE, USER_DEFINED_SUBTYPE
|
||||
from bson.code import Code
|
||||
@ -40,7 +39,6 @@ from bson.tz_util import utc
|
||||
from test.test_client import get_client
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
PY24 = sys.version_info[:2] == (2, 4)
|
||||
|
||||
|
||||
class TestJsonUtil(unittest.TestCase):
|
||||
@ -71,11 +69,10 @@ class TestJsonUtil(unittest.TestCase):
|
||||
self.round_trip({"ref": DBRef("foo", 5, "db")})
|
||||
self.round_trip({"ref": DBRef("foo", ObjectId())})
|
||||
|
||||
if not PY24:
|
||||
# Check order.
|
||||
self.assertEqual(
|
||||
'{"$ref": "collection", "$id": 1, "$db": "db"}',
|
||||
json_util.dumps(DBRef('collection', 1, 'db')))
|
||||
# Check order.
|
||||
self.assertEqual(
|
||||
'{"$ref": "collection", "$id": 1, "$db": "db"}',
|
||||
json_util.dumps(DBRef('collection', 1, 'db')))
|
||||
|
||||
def test_datetime(self):
|
||||
# only millis, not micros
|
||||
@ -132,15 +129,14 @@ class TestJsonUtil(unittest.TestCase):
|
||||
'{"r": {"$regex": ".*", "$options": "ilm"}}',
|
||||
compile_re=False)['r'])
|
||||
|
||||
if not PY24:
|
||||
# Check order.
|
||||
self.assertEqual(
|
||||
'{"$regex": ".*", "$options": "mx"}',
|
||||
json_util.dumps(Regex('.*', re.M | re.X)))
|
||||
# Check order.
|
||||
self.assertEqual(
|
||||
'{"$regex": ".*", "$options": "mx"}',
|
||||
json_util.dumps(Regex('.*', re.M | re.X)))
|
||||
|
||||
self.assertEqual(
|
||||
'{"$regex": ".*", "$options": "mx"}',
|
||||
json_util.dumps(re.compile(b('.*'), re.M | re.X)))
|
||||
self.assertEqual(
|
||||
'{"$regex": ".*", "$options": "mx"}',
|
||||
json_util.dumps(re.compile(b'.*', re.M | re.X)))
|
||||
|
||||
def test_minkey(self):
|
||||
self.round_trip({"m": MinKey()})
|
||||
@ -150,9 +146,8 @@ class TestJsonUtil(unittest.TestCase):
|
||||
|
||||
def test_timestamp(self):
|
||||
res = json_util.dumps({"ts": Timestamp(4, 13)}, default=json_util.default)
|
||||
if not PY24:
|
||||
# Check order.
|
||||
self.assertEqual('{"ts": {"t": 4, "i": 13}}', res)
|
||||
# Check order.
|
||||
self.assertEqual('{"ts": {"t": 4, "i": 13}}', res)
|
||||
|
||||
dct = json_util.loads(res)
|
||||
self.assertEqual(dct['ts']['t'], 4)
|
||||
@ -166,11 +161,11 @@ class TestJsonUtil(unittest.TestCase):
|
||||
'f47ac10b-58cc-4372-a567-0e02b2c3d479')})
|
||||
|
||||
def test_binary(self):
|
||||
bin_type_dict = {"bin": Binary(b("\x00\x01\x02\x03\x04"))}
|
||||
bin_type_dict = {"bin": Binary(b"\x00\x01\x02\x03\x04")}
|
||||
md5_type_dict = {
|
||||
"md5": Binary(b(' n7\x18\xaf\t/\xd1\xd1/\x80\xca\xe7q\xcc\xac'),
|
||||
"md5": Binary(b' n7\x18\xaf\t/\xd1\xd1/\x80\xca\xe7q\xcc\xac',
|
||||
MD5_SUBTYPE)}
|
||||
custom_type_dict = {"custom": Binary(b("hello"), USER_DEFINED_SUBTYPE)}
|
||||
custom_type_dict = {"custom": Binary(b"hello", USER_DEFINED_SUBTYPE)}
|
||||
|
||||
self.round_trip(bin_type_dict)
|
||||
self.round_trip(md5_type_dict)
|
||||
@ -183,12 +178,11 @@ class TestJsonUtil(unittest.TestCase):
|
||||
json_util.loads('{"bin": {"$type": 0, "$binary": "AAECAwQ="}}'))
|
||||
|
||||
json_bin_dump = json_util.dumps(md5_type_dict)
|
||||
if not PY24:
|
||||
# Check order.
|
||||
self.assertEqual(
|
||||
'{"md5": {"$binary": "IG43GK8JL9HRL4DK53HMrA==",'
|
||||
+ ' "$type": "05"}}',
|
||||
json_bin_dump)
|
||||
# Check order.
|
||||
self.assertEqual(
|
||||
'{"md5": {"$binary": "IG43GK8JL9HRL4DK53HMrA==",'
|
||||
+ ' "$type": "05"}}',
|
||||
json_bin_dump)
|
||||
|
||||
self.assertEqual(md5_type_dict,
|
||||
json_util.loads('{"md5": {"$type": 5, "$binary":'
|
||||
@ -216,9 +210,8 @@ class TestJsonUtil(unittest.TestCase):
|
||||
res = json_util.dumps(code)
|
||||
self.assertEqual(code, json_util.loads(res))
|
||||
|
||||
if not PY24:
|
||||
# Check order.
|
||||
self.assertEqual('{"$code": "return z", "$scope": {"z": 2}}', res)
|
||||
# Check order.
|
||||
self.assertEqual('{"$code": "return z", "$scope": {"z": 2}}', res)
|
||||
|
||||
def test_cursor(self):
|
||||
db = self.db
|
||||
@ -228,7 +221,7 @@ class TestJsonUtil(unittest.TestCase):
|
||||
{'foo': [1, 2]},
|
||||
{'bar': {'hello': 'world'}},
|
||||
{'code': Code("function x() { return 1; }")},
|
||||
{'bin': Binary(b("\x00\x01\x02\x03\x04"))},
|
||||
{'bin': Binary(b"\x00\x01\x02\x03\x04")},
|
||||
{'dbref': {'_ref': DBRef('simple',
|
||||
ObjectId('509b8db456c02c5ab7e63c34'))}}
|
||||
]
|
||||
|
||||
@ -25,12 +25,10 @@ from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.errors import InvalidId
|
||||
from bson.objectid import ObjectId
|
||||
from bson.py3compat import b, binary_type
|
||||
from bson.py3compat import PY3, u, _unicode
|
||||
from bson.tz_util import (FixedOffset,
|
||||
utc)
|
||||
|
||||
PY3 = sys.version_info[0] == 3
|
||||
|
||||
|
||||
def oid(x):
|
||||
return ObjectId()
|
||||
@ -46,46 +44,46 @@ class TestObjectId(unittest.TestCase):
|
||||
self.assertRaises(InvalidId, ObjectId, "12345678901")
|
||||
self.assertRaises(InvalidId, ObjectId, "1234567890123")
|
||||
self.assertTrue(ObjectId())
|
||||
self.assertTrue(ObjectId(b("123456789012")))
|
||||
self.assertTrue(ObjectId(b"123456789012"))
|
||||
a = ObjectId()
|
||||
self.assertTrue(ObjectId(a))
|
||||
|
||||
def test_unicode(self):
|
||||
a = ObjectId()
|
||||
self.assertEqual(a, ObjectId(unicode(a)))
|
||||
self.assertEqual(a, ObjectId(_unicode(a)))
|
||||
self.assertEqual(ObjectId("123456789012123456789012"),
|
||||
ObjectId(u"123456789012123456789012"))
|
||||
self.assertRaises(InvalidId, ObjectId, u"hello")
|
||||
ObjectId(u("123456789012123456789012")))
|
||||
self.assertRaises(InvalidId, ObjectId, u("hello"))
|
||||
|
||||
def test_from_hex(self):
|
||||
ObjectId("123456789012123456789012")
|
||||
self.assertRaises(InvalidId, ObjectId, "123456789012123456789G12")
|
||||
self.assertRaises(InvalidId, ObjectId, u"123456789012123456789G12")
|
||||
self.assertRaises(InvalidId, ObjectId, u("123456789012123456789G12"))
|
||||
|
||||
def test_repr_str(self):
|
||||
self.assertEqual(repr(ObjectId("1234567890abcdef12345678")),
|
||||
"ObjectId('1234567890abcdef12345678')")
|
||||
self.assertEqual(str(ObjectId("1234567890abcdef12345678")),
|
||||
"1234567890abcdef12345678")
|
||||
self.assertEqual(str(ObjectId(b("123456789012"))),
|
||||
self.assertEqual(str(ObjectId(b"123456789012")),
|
||||
"313233343536373839303132")
|
||||
self.assertEqual(ObjectId("1234567890abcdef12345678").binary,
|
||||
b('\x124Vx\x90\xab\xcd\xef\x124Vx'))
|
||||
self.assertEqual(str(ObjectId(b('\x124Vx\x90\xab\xcd\xef\x124Vx'))),
|
||||
b'\x124Vx\x90\xab\xcd\xef\x124Vx')
|
||||
self.assertEqual(str(ObjectId(b'\x124Vx\x90\xab\xcd\xef\x124Vx')),
|
||||
"1234567890abcdef12345678")
|
||||
|
||||
def test_equality(self):
|
||||
a = ObjectId()
|
||||
self.assertEqual(a, ObjectId(a))
|
||||
self.assertEqual(ObjectId(b("123456789012")),
|
||||
ObjectId(b("123456789012")))
|
||||
self.assertEqual(ObjectId(b"123456789012"),
|
||||
ObjectId(b"123456789012"))
|
||||
self.assertNotEqual(ObjectId(), ObjectId())
|
||||
self.assertNotEqual(ObjectId(b("123456789012")), b("123456789012"))
|
||||
self.assertNotEqual(ObjectId(b"123456789012"), b"123456789012")
|
||||
|
||||
# Explicitly test inequality
|
||||
self.assertFalse(a != ObjectId(a))
|
||||
self.assertFalse(ObjectId(b("123456789012")) !=
|
||||
ObjectId(b("123456789012")))
|
||||
self.assertFalse(ObjectId(b"123456789012") !=
|
||||
ObjectId(b"123456789012"))
|
||||
|
||||
def test_binary_str_equivalence(self):
|
||||
a = ObjectId()
|
||||
@ -152,22 +150,21 @@ class TestObjectId(unittest.TestCase):
|
||||
|
||||
# This string was generated by pickling an ObjectId in pymongo
|
||||
# version 1.9
|
||||
pickled_with_1_9 = b(
|
||||
"ccopy_reg\n_reconstructor\np0\n"
|
||||
"(cbson.objectid\nObjectId\np1\nc__builtin__\n"
|
||||
"object\np2\nNtp3\nRp4\n"
|
||||
"(dp5\nS'_ObjectId__id'\np6\n"
|
||||
"S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np7\nsb.")
|
||||
pickled_with_1_9 = (
|
||||
b"ccopy_reg\n_reconstructor\np0\n"
|
||||
b"(cbson.objectid\nObjectId\np1\nc__builtin__\n"
|
||||
b"object\np2\nNtp3\nRp4\n"
|
||||
b"(dp5\nS'_ObjectId__id'\np6\n"
|
||||
b"S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np7\nsb.")
|
||||
|
||||
# We also test against a hardcoded "New" pickle format so that we
|
||||
# make sure we're backward compatible with the current version in
|
||||
# the future as well.
|
||||
pickled_with_1_10 = b(
|
||||
"ccopy_reg\n_reconstructor\np0\n"
|
||||
"(cbson.objectid\nObjectId\np1\nc__builtin__\n"
|
||||
"object\np2\nNtp3\nRp4\n"
|
||||
"S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np5\nb."
|
||||
)
|
||||
pickled_with_1_10 = (
|
||||
b"ccopy_reg\n_reconstructor\np0\n"
|
||||
b"(cbson.objectid\nObjectId\np1\nc__builtin__\n"
|
||||
b"object\np2\nNtp3\nRp4\n"
|
||||
b"S'M\\x9afV\\x13v\\xc0\\x0b\\x88\\x00\\x00\\x00'\np5\nb.")
|
||||
|
||||
if PY3:
|
||||
# Have to load using 'latin-1' since these were pickled in python2.x.
|
||||
@ -189,7 +186,7 @@ class TestObjectId(unittest.TestCase):
|
||||
self.assertFalse(ObjectId.is_valid("12345678901"))
|
||||
self.assertFalse(ObjectId.is_valid("1234567890123"))
|
||||
|
||||
self.assertTrue(ObjectId.is_valid(b("123456789012")))
|
||||
self.assertTrue(ObjectId.is_valid(b"123456789012"))
|
||||
self.assertTrue(ObjectId.is_valid("123456789012123456789012"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@ -15,12 +15,12 @@
|
||||
"""Test built in connection-pooling with threads."""
|
||||
|
||||
import sys
|
||||
import thread
|
||||
import time
|
||||
import unittest
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from bson.py3compat import thread
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from test import host, port
|
||||
|
||||
@ -19,7 +19,6 @@ import gc
|
||||
import random
|
||||
import socket
|
||||
import sys
|
||||
import thread
|
||||
import threading
|
||||
import time
|
||||
|
||||
@ -28,6 +27,7 @@ sys.path[0:0] = [""]
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
import pymongo.pool
|
||||
from bson.py3compat import thread
|
||||
from pymongo.mongo_client import MongoClient
|
||||
from pymongo.pool import Pool, NO_REQUEST, NO_SOCKET_YET, SocketInfo
|
||||
from pymongo.errors import ConfigurationError, ConnectionFailure
|
||||
@ -113,7 +113,7 @@ class MongoThread(object):
|
||||
class SaveAndFind(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
for _ in range(N):
|
||||
rand = random.randint(0, N)
|
||||
_id = self.db.sf.save({"x": rand})
|
||||
self.ut.assertEqual(rand, self.db.sf.find_one(_id)["x"])
|
||||
@ -122,7 +122,7 @@ class SaveAndFind(MongoThread):
|
||||
class Unique(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
for _ in range(N):
|
||||
self.client.start_request()
|
||||
self.db.unique.insert({}) # no error
|
||||
self.client.end_request()
|
||||
@ -131,7 +131,7 @@ class Unique(MongoThread):
|
||||
class NonUnique(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
for _ in range(N):
|
||||
self.client.start_request()
|
||||
self.db.unique.insert({"_id": "jesse"}, w=0)
|
||||
self.ut.assertNotEqual(None, self.db.error())
|
||||
@ -141,7 +141,7 @@ class NonUnique(MongoThread):
|
||||
class Disconnect(MongoThread):
|
||||
|
||||
def run_mongo_thread(self):
|
||||
for _ in xrange(N):
|
||||
for _ in range(N):
|
||||
self.client.disconnect()
|
||||
|
||||
|
||||
@ -150,7 +150,7 @@ class NoRequest(MongoThread):
|
||||
def run_mongo_thread(self):
|
||||
self.client.start_request()
|
||||
errors = 0
|
||||
for _ in xrange(N):
|
||||
for _ in range(N):
|
||||
self.db.unique.insert({"_id": "jesse"}, w=0)
|
||||
if not self.db.error():
|
||||
errors += 1
|
||||
@ -1111,7 +1111,7 @@ class _TestWaitQueueMultiple(_TestPoolingBase):
|
||||
|
||||
# Reach max_size * wait_queue_multiple waiters.
|
||||
threads = []
|
||||
for _ in xrange(6):
|
||||
for _ in range(6):
|
||||
t = SocketGetter(self, pool)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
@ -1127,11 +1127,11 @@ class _TestWaitQueueMultiple(_TestPoolingBase):
|
||||
def test_wait_queue_multiple_unset(self):
|
||||
pool = self.get_pool_with_wait_queue_multiple(None)
|
||||
socks = []
|
||||
for _ in xrange(2):
|
||||
for _ in range(2):
|
||||
sock = pool.get_socket()
|
||||
socks.append(sock)
|
||||
threads = []
|
||||
for _ in xrange(30):
|
||||
for _ in range(30):
|
||||
t = SocketGetter(self, pool)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
|
||||
@ -22,6 +22,7 @@ from nose.plugins.skip import SkipTest
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from bson.py3compat import MAXSIZE
|
||||
from bson.son import SON
|
||||
from pymongo.cursor import _QUERY_OPTIONS
|
||||
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
|
||||
@ -54,7 +55,7 @@ class TestReadPreferencesBase(TestReplicaSetClientBase):
|
||||
"""Do a find() on the client and return which host was used
|
||||
"""
|
||||
cursor = client.pymongo_test.test.find()
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
return cursor._Cursor__connection_id
|
||||
|
||||
def read_from_which_kind(self, client):
|
||||
@ -417,10 +418,10 @@ class TestCommandAndReadPreference(TestReplicaSetClientBase):
|
||||
def test_create_collection(self):
|
||||
# Collections should be created on primary, obviously
|
||||
self._test_fn(False, lambda: self.c.pymongo_test.command(
|
||||
'create', 'some_collection%s' % random.randint(0, sys.maxint)))
|
||||
'create', 'some_collection%s' % random.randint(0, MAXSIZE)))
|
||||
|
||||
self._test_fn(False, lambda: self.c.pymongo_test.create_collection(
|
||||
'some_collection%s' % random.randint(0, sys.maxint)))
|
||||
'some_collection%s' % random.randint(0, MAXSIZE)))
|
||||
|
||||
def test_drop_collection(self):
|
||||
self._test_fn(False, lambda: self.c.pymongo_test.drop_collection(
|
||||
|
||||
@ -21,7 +21,6 @@ import signal
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import thread
|
||||
import threading
|
||||
import traceback
|
||||
import unittest
|
||||
@ -30,6 +29,7 @@ sys.path[0:0] = [""]
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
from bson.py3compat import thread, u, _unicode
|
||||
from bson.son import SON
|
||||
from bson.tz_util import utc
|
||||
from pymongo.mongo_client import MongoClient
|
||||
@ -220,7 +220,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
host_dict = dict([(host, 1) for host in self.hosts])
|
||||
hosts_set = frozenset(host_dict)
|
||||
hosts_repr = ', '.join([
|
||||
repr(unicode('%s:%s' % host)) for host in hosts_set])
|
||||
repr(_unicode('%s:%s' % host)) for host in hosts_set])
|
||||
|
||||
self.assertEqual(repr(client),
|
||||
"MongoReplicaSetClient([%s])" % hosts_repr)
|
||||
@ -356,13 +356,13 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
self.assertEqual(1, db.test.count())
|
||||
|
||||
cursor = db.test.find()
|
||||
doc = cursor.next()
|
||||
doc = next(cursor)
|
||||
self.assertEqual('x', doc['foo'])
|
||||
# Ensure we read from the primary
|
||||
self.assertEqual(c.primary, cursor._Cursor__connection_id)
|
||||
|
||||
cursor = db.test.find(read_preference=ReadPreference.SECONDARY)
|
||||
doc = cursor.next()
|
||||
doc = next(cursor)
|
||||
self.assertEqual('x', doc['foo'])
|
||||
# Ensure we didn't read from the primary
|
||||
self.assertTrue(cursor._Cursor__connection_id in c.secondaries)
|
||||
@ -376,8 +376,8 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
def test_database_names(self):
|
||||
client = self._get_client()
|
||||
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_mike.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
client.pymongo_test_mike.test.save({"dummy": u("object")})
|
||||
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
@ -390,14 +390,14 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
self.assertRaises(TypeError, client.drop_database, 5)
|
||||
self.assertRaises(TypeError, client.drop_database, None)
|
||||
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
client.drop_database("pymongo_test")
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" not in dbs)
|
||||
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
client.drop_database(client.pymongo_test)
|
||||
@ -650,7 +650,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
|
||||
try:
|
||||
timeout.pymongo_test.test.find_one(query)
|
||||
except AutoReconnect, e:
|
||||
except AutoReconnect as e:
|
||||
self.assertTrue('%d: timed out' % (port,) in e.args[0])
|
||||
else:
|
||||
self.fail('RS client should have raised timeout error')
|
||||
@ -659,7 +659,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
timeout.pymongo_test.test.find_one(
|
||||
query,
|
||||
read_preference=ReadPreference.SECONDARY)
|
||||
except AutoReconnect, e:
|
||||
except AutoReconnect as e:
|
||||
# Like 'No replica set secondary available for query with
|
||||
# ReadPreference SECONDARY. host:27018: timed out,
|
||||
# host:27019: timed out'.
|
||||
@ -752,8 +752,8 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
client = MongoReplicaSetClient("localhost:%d,[::1]:"
|
||||
"%d" % (port, port),
|
||||
replicaSet=self.name)
|
||||
client.pymongo_test.test.save({"dummy": u"object"})
|
||||
client.pymongo_test_bernie.test.save({"dummy": u"object"})
|
||||
client.pymongo_test.test.save({"dummy": u("object")})
|
||||
client.pymongo_test_bernie.test.save({"dummy": u("object")})
|
||||
|
||||
dbs = client.database_names()
|
||||
self.assertTrue("pymongo_test" in dbs)
|
||||
@ -770,7 +770,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
|
||||
# Partially evaluate cursor so it's left alive, then kill it
|
||||
cursor = test.find().batch_size(10)
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
self.assertNotEqual(0, cursor.cursor_id)
|
||||
|
||||
connection_id = cursor._Cursor__connection_id
|
||||
@ -837,7 +837,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
raised = False
|
||||
try:
|
||||
# Will be interrupted by a KeyboardInterrupt.
|
||||
db.foo.find({'$where': where}).next()
|
||||
next(db.foo.find({'$where': where}))
|
||||
except KeyboardInterrupt:
|
||||
raised = True
|
||||
|
||||
@ -850,7 +850,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
# request id's don't match.
|
||||
self.assertEqual(
|
||||
{'_id': 1},
|
||||
db.foo.find().next()
|
||||
next(db.foo.find())
|
||||
)
|
||||
finally:
|
||||
if old_signal_handler:
|
||||
@ -862,7 +862,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
c = self._get_client(auto_start_request=False)
|
||||
pool = get_pool(c)
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
old_sock_info = iter(pool.sockets).next()
|
||||
old_sock_info = next(iter(pool.sockets))
|
||||
c.pymongo_test.test.drop()
|
||||
c.pymongo_test.test.insert({'_id': 'foo'})
|
||||
self.assertRaises(
|
||||
@ -870,7 +870,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
c.pymongo_test.test.insert, {'_id': 'foo'})
|
||||
|
||||
self.assertEqual(1, len(pool.sockets))
|
||||
new_sock_info = iter(pool.sockets).next()
|
||||
new_sock_info = next(iter(pool.sockets))
|
||||
|
||||
self.assertEqual(old_sock_info, new_sock_info)
|
||||
c.close()
|
||||
@ -918,7 +918,7 @@ class TestReplicaSetClient(TestReplicaSetClientBase, TestRequestMixin):
|
||||
cursor = client.pymongo_test.test.find(
|
||||
read_preference=ReadPreference.SECONDARY)
|
||||
try:
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
except StopIteration:
|
||||
# No results, no problem
|
||||
pass
|
||||
|
||||
@ -53,7 +53,7 @@ class TestSecondaryBecomesStandalone(unittest.TestCase):
|
||||
|
||||
try:
|
||||
c.db.collection.find_one()
|
||||
except ConfigurationError, e:
|
||||
except ConfigurationError as e:
|
||||
self.assertTrue('not a member of replica set' in str(e))
|
||||
else:
|
||||
self.fail("MongoClient didn't raise AutoReconnect")
|
||||
|
||||
@ -33,9 +33,9 @@ class TestSON(unittest.TestCase):
|
||||
a1["hello"] = "world"
|
||||
a1["mike"] = "awesome"
|
||||
a1["hello_"] = "mike"
|
||||
self.assertEqual(a1.items(), [("hello", "world"),
|
||||
("mike", "awesome"),
|
||||
("hello_", "mike")])
|
||||
self.assertEqual(list(a1.items()), [("hello", "world"),
|
||||
("mike", "awesome"),
|
||||
("hello_", "mike")])
|
||||
|
||||
b2 = SON({"hello": "world"})
|
||||
self.assertEqual(b2["hello"], "world")
|
||||
@ -87,7 +87,7 @@ class TestSON(unittest.TestCase):
|
||||
complex_son = SON([('son', simple_son),
|
||||
('list', [simple_son, simple_son])])
|
||||
|
||||
for protocol in xrange(pickle.HIGHEST_PROTOCOL + 1):
|
||||
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||
pickled = pickle.loads(pickle.dumps(complex_son,
|
||||
protocol=protocol))
|
||||
self.assertEqual(pickled['son'], pickled['list'][0])
|
||||
@ -140,7 +140,7 @@ class TestSON(unittest.TestCase):
|
||||
self.assertEqual(complex_son, complex_son1)
|
||||
|
||||
reflexive_son1 = copy.deepcopy(reflexive_son)
|
||||
self.assertEqual(reflexive_son.keys(), reflexive_son1.keys())
|
||||
self.assertEqual(list(reflexive_son), list(reflexive_son1))
|
||||
self.assertEqual(id(reflexive_son1), id(reflexive_son1["reflexive"]))
|
||||
|
||||
|
||||
|
||||
@ -27,7 +27,11 @@ except ImportError:
|
||||
|
||||
sys.path[0:0] = [""]
|
||||
|
||||
from urllib import quote_plus
|
||||
try:
|
||||
from urllib.parse import quote_plus
|
||||
except ImportError:
|
||||
# Python 2
|
||||
from urllib import quote_plus
|
||||
|
||||
from nose.plugins.skip import SkipTest
|
||||
|
||||
|
||||
@ -207,11 +207,11 @@ class TestCounter(unittest.TestCase):
|
||||
done = set()
|
||||
|
||||
def f(n):
|
||||
for i in xrange(n):
|
||||
for i in range(n):
|
||||
self.assertEqual(i, counter.get())
|
||||
self.assertEqual(i + 1, counter.inc())
|
||||
|
||||
for i in xrange(n, 0, -1):
|
||||
for i in range(n, 0, -1):
|
||||
self.assertEqual(i, counter.get())
|
||||
self.assertEqual(i - 1, counter.dec())
|
||||
|
||||
@ -227,11 +227,11 @@ class TestCounter(unittest.TestCase):
|
||||
|
||||
if use_greenlets:
|
||||
greenlets = [
|
||||
greenlet.greenlet(my_partial(f, i)) for i in xrange(10)]
|
||||
greenlet.greenlet(my_partial(f, i)) for i in range(10)]
|
||||
looplet(greenlets)
|
||||
else:
|
||||
threads = [
|
||||
threading.Thread(target=my_partial(f, i)) for i in xrange(10)]
|
||||
threading.Thread(target=my_partial(f, i)) for i in range(10)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
|
||||
@ -39,7 +39,7 @@ class AutoAuthenticateThreads(threading.Thread):
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
for i in xrange(self.num):
|
||||
for i in range(self.num):
|
||||
self.coll.insert({'num':i})
|
||||
self.coll.find_one({'num':i})
|
||||
except Exception:
|
||||
@ -71,7 +71,7 @@ class Insert(threading.Thread):
|
||||
self.setDaemon(True)
|
||||
|
||||
def run(self):
|
||||
for _ in xrange(self.n):
|
||||
for _ in range(self.n):
|
||||
error = True
|
||||
|
||||
try:
|
||||
@ -95,7 +95,7 @@ class Update(threading.Thread):
|
||||
self.setDaemon(True)
|
||||
|
||||
def run(self):
|
||||
for _ in xrange(self.n):
|
||||
for _ in range(self.n):
|
||||
error = True
|
||||
|
||||
try:
|
||||
@ -185,7 +185,7 @@ class BaseTestThreads(object):
|
||||
|
||||
def test_threading(self):
|
||||
self.db.drop_collection("test")
|
||||
for i in xrange(1000):
|
||||
for i in range(1000):
|
||||
self.db.test.save({"x": i})
|
||||
|
||||
threads = []
|
||||
@ -349,7 +349,7 @@ class BaseTestThreadsAuth(object):
|
||||
|
||||
nthreads = 10
|
||||
threads = []
|
||||
for _ in xrange(nthreads):
|
||||
for _ in range(nthreads):
|
||||
t = AutoAuthenticateThreads(client.auth_test.test, 100)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
@ -364,7 +364,7 @@ class BaseTestThreadsAuth(object):
|
||||
client.auth_test.authenticate("test-user", "password")
|
||||
|
||||
threads = []
|
||||
for _ in xrange(nthreads):
|
||||
for _ in range(nthreads):
|
||||
t = AutoAuthenticateThreads(client.auth_test.test, 100)
|
||||
t.start()
|
||||
threads.append(t)
|
||||
|
||||
@ -29,6 +29,7 @@ from pymongo.uri_parser import (_partition,
|
||||
from pymongo.errors import ConfigurationError, InvalidURI
|
||||
from pymongo import ReadPreference
|
||||
from bson.binary import JAVA_LEGACY
|
||||
from bson.py3compat import string_type, _unicode
|
||||
|
||||
|
||||
class TestURI(unittest.TestCase):
|
||||
@ -117,7 +118,7 @@ class TestURI(unittest.TestCase):
|
||||
self.assertEqual({'connecttimeoutms': 0.0001}, split_options('connectTimeoutMS=0.1'))
|
||||
self.assertTrue(split_options('connectTimeoutMS=300'))
|
||||
self.assertTrue(isinstance(split_options('w=5')['w'], int))
|
||||
self.assertTrue(isinstance(split_options('w=5.5')['w'], basestring))
|
||||
self.assertTrue(isinstance(split_options('w=5.5')['w'], string_type))
|
||||
self.assertTrue(split_options('w=foo'))
|
||||
self.assertTrue(split_options('w=majority'))
|
||||
self.assertRaises(ConfigurationError, split_options, 'wtimeoutms=foo')
|
||||
@ -371,7 +372,7 @@ class TestURI(unittest.TestCase):
|
||||
# Ensure parsing a unicode returns option names that can be passed
|
||||
# as kwargs. In Python 2.4, keyword argument names must be ASCII.
|
||||
# In all Pythons, str is the type of valid keyword arg names.
|
||||
res = parse_uri(unicode("mongodb://localhost/?fsync=true"))
|
||||
res = parse_uri(_unicode("mongodb://localhost/?fsync=true"))
|
||||
for key in res['options']:
|
||||
self.assertTrue(isinstance(key, str))
|
||||
|
||||
|
||||
@ -45,7 +45,7 @@ def my_partial(f, *args, **kwargs):
|
||||
|
||||
def one(s):
|
||||
"""Get one element of a set"""
|
||||
return iter(s).next()
|
||||
return next(iter(s))
|
||||
|
||||
def oid_generated_on_client(doc):
|
||||
"""Is this process's PID in the document's _id?"""
|
||||
@ -158,7 +158,7 @@ def assertRaisesExactly(cls, fn, *args, **kwargs):
|
||||
"""
|
||||
try:
|
||||
fn(*args, **kwargs)
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
assert e.__class__ == cls, "got %s, expected %s" % (
|
||||
e.__class__.__name__, cls.__name__)
|
||||
else:
|
||||
@ -296,7 +296,7 @@ def read_from_which_host(
|
||||
cursor = db.test.find()
|
||||
try:
|
||||
try:
|
||||
cursor.next()
|
||||
next(cursor)
|
||||
except StopIteration:
|
||||
# No documents in collection, that's fine
|
||||
pass
|
||||
|
||||
Loading…
Reference in New Issue
Block a user