本文整理汇总了Python中test.assert_raises函数的典型用法代码示例。如果您正苦于以下问题:Python assert_raises函数的具体用法?Python assert_raises怎么用?Python assert_raises使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了assert_raises函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_alt_collection
def test_alt_collection(self):
db = self.cx.pymongo_test
alt = yield motor.MotorGridFS(db, 'alt').open()
oid = yield alt.put(b("hello world"))
gridout = yield alt.get(oid)
self.assertEqual(b("hello world"), (yield gridout.read()))
self.assertEqual(1, (yield db.alt.files.count()))
self.assertEqual(1, (yield db.alt.chunks.count()))
yield alt.delete(oid)
with assert_raises(NoFile):
yield alt.get(oid)
self.assertEqual(0, (yield db.alt.files.count()))
self.assertEqual(0, (yield db.alt.chunks.count()))
with assert_raises(NoFile):
yield alt.get("foo")
oid = yield alt.put(b("hello world"), _id="foo")
self.assertEqual("foo", oid)
gridout = yield alt.get("foo")
self.assertEqual(b("hello world"), (yield gridout.read()))
yield alt.put(b(""), filename="mike")
yield alt.put(b("foo"), filename="test")
yield alt.put(b(""), filename="hello world")
self.assertEqual(set(["mike", "test", "hello world"]),
set((yield alt.list())))
示例2: test_max_time_ms_getmore
def test_max_time_ms_getmore(self):
# Cursor handles server timeout during getmore, also.
yield self.collection.insert({} for _ in range(200))
try:
# Send initial query.
cursor = self.collection.find().max_time_ms(1)
yield cursor.fetch_next
cursor.next_object()
# Test getmore timeout.
yield self.enable_timeout()
with assert_raises(ExecutionTimeout):
while (yield cursor.fetch_next):
cursor.next_object()
# Send another initial query.
yield self.disable_timeout()
cursor = self.collection.find().max_time_ms(1)
yield cursor.fetch_next
cursor.next_object()
# Test getmore timeout.
yield self.enable_timeout()
with assert_raises(ExecutionTimeout):
yield cursor.to_list(None)
# Avoid 'IOLoop is closing' warning.
yield cursor.close()
finally:
# Cleanup.
yield self.disable_timeout()
yield self.collection.remove()
示例3: test_def_operations
def test_def_operations(self):
"""test get/list/has def"""
template = Template("""
this is the body
<%def name="a()">
this is a
</%def>
<%def name="b(x, y)">
this is b, ${x} ${y}
</%def>
""")
assert template.get_def("a")
assert template.get_def("b")
assert_raises(AttributeError,
template.get_def,
("c")
)
assert template.has_def("a")
assert template.has_def("b")
assert not template.has_def("c")
defs = template.list_defs()
assert "a" in defs
assert "b" in defs
assert "body" in defs
assert "c" not in defs
示例4: test_copy_db_auth
def test_copy_db_auth(self):
# See SERVER-6427.
cx = self.get_client()
if cx.is_mongos:
raise SkipTest("Can't copy database with auth via mongos.")
target_db_name = "motor_test_2"
collection = cx.motor_test.test_collection
yield collection.remove()
yield collection.insert({"_id": 1})
yield cx.admin.add_user("admin", "password")
yield cx.admin.authenticate("admin", "password")
try:
yield cx.motor_test.add_user("mike", "password")
with assert_raises(pymongo.errors.OperationFailure):
yield cx.copy_database("motor_test", target_db_name, username="foo", password="bar")
with assert_raises(pymongo.errors.OperationFailure):
yield cx.copy_database("motor_test", target_db_name, username="mike", password="bar")
# Copy a database using name and password.
yield cx.copy_database("motor_test", target_db_name, username="mike", password="password")
self.assertEqual({"_id": 1}, (yield cx[target_db_name].test_collection.find_one()))
yield cx.drop_database(target_db_name)
finally:
yield remove_all_users(cx.motor_test)
yield cx.admin.remove_user("admin")
示例5: test_strict
def test_strict(self):
t = Template("""
% if x is UNDEFINED:
undefined
% else:
x: ${x}
% endif
""", strict_undefined=True)
assert result_lines(t.render(x=12)) == ['x: 12']
assert_raises(
NameError,
t.render, y=12
)
l = TemplateLookup(strict_undefined=True)
l.put_string("a", "some template")
l.put_string("b", """
<%namespace name='a' file='a' import='*'/>
% if x is UNDEFINED:
undefined
% else:
x: ${x}
% endif
""")
assert result_lines(t.render(x=12)) == ['x: 12']
assert_raises(
NameError,
t.render, y=12
)
示例6: test_copy_db_auth
def test_copy_db_auth(self):
# SERVER-6427, can't copy database via mongos with auth.
yield skip_if_mongos(self.cx)
yield self.collection.insert({'_id': 1})
try:
# self.cx is logged in as root.
yield self.cx.motor_test.add_user('mike', 'password')
client = self.get_client()
target_db_name = 'motor_test_2'
with assert_raises(pymongo.errors.OperationFailure):
yield client.copy_database(
'motor_test', target_db_name,
username='foo', password='bar')
with assert_raises(pymongo.errors.OperationFailure):
yield client.copy_database(
'motor_test', target_db_name,
username='mike', password='bar')
# Copy a database using name and password.
yield client.copy_database(
'motor_test', target_db_name,
username='mike', password='password')
self.assertEqual(
{'_id': 1},
(yield client[target_db_name].test_collection.find_one()))
yield client.drop_database(target_db_name)
finally:
yield remove_all_users(self.cx.motor_test)
示例7: test_recovering_member_triggers_refresh
def test_recovering_member_triggers_refresh(self):
# To test that find_one() and count() trigger immediate refreshes,
# we'll create a separate client for each
self.c_find_one, self.c_count = yield [
motor.MotorReplicaSetClient(
self.seed, replicaSet=self.name, read_preference=SECONDARY
).open() for _ in xrange(2)]
# We've started the primary and one secondary
primary = ha_tools.get_primary()
secondary = ha_tools.get_secondaries()[0]
# Pre-condition: just make sure they all connected OK
for c in self.c_find_one, self.c_count:
self.assertEqual(one(c.secondaries), _partition_node(secondary))
ha_tools.set_maintenance(secondary, True)
# Trigger a refresh in various ways
with assert_raises(AutoReconnect):
yield self.c_find_one.test.test.find_one()
with assert_raises(AutoReconnect):
yield self.c_count.test.test.count()
# Wait for the immediate refresh to complete - we're not waiting for
# the periodic refresh, which has been disabled
yield self.pause(1)
for c in self.c_find_one, self.c_count:
self.assertFalse(c.secondaries)
self.assertEqual(_partition_node(primary), c.primary)
示例8: test_copy_db
def test_copy_db(self):
# 1. Drop old test DBs
# 2. Copy a test DB N times at once (we need to do it many times at
# once to make sure the pool's start_request() is properly isolating
# operations from each other)
# 3. Create a username and password
# 4. Copy a database using name and password
ncopies = 10
test_db_names = ['pymongo_test%s' % i for i in range(ncopies)]
def check_copydb_results():
db_names = self.sync_cx.database_names()
for test_db_name in test_db_names:
self.assertTrue(test_db_name in db_names)
result = self.sync_cx[test_db_name].test_collection.find_one()
self.assertTrue(result, "No results in %s" % test_db_name)
self.assertEqual(
"bar", result.get("foo"),
"Wrong result from %s: %s" % (test_db_name, result))
# 1. Drop old test DBs
yield self.cx.drop_database('pymongo_test')
self.drop_databases(test_db_names)
# 2. Copy a test DB N times at once
yield self.cx.pymongo_test.test_collection.insert({"foo": "bar"})
yield [
self.cx.copy_database("pymongo_test", test_db_name)
for test_db_name in test_db_names]
check_copydb_results()
self.drop_databases(test_db_names)
# 3. Create a username and password
yield self.cx.pymongo_test.add_user("mike", "password")
with assert_raises(pymongo.errors.OperationFailure):
yield self.cx.copy_database(
"pymongo_test", "pymongo_test0",
username="foo", password="bar")
with assert_raises(pymongo.errors.OperationFailure):
yield self.cx.copy_database(
"pymongo_test", "pymongo_test0",
username="mike", password="bar")
# 4. Copy a database using name and password
if not self.cx.is_mongos:
# See SERVER-6427
yield [
self.cx.copy_database(
"pymongo_test", test_db_name,
username="mike", password="password")
for test_db_name in test_db_names]
check_copydb_results()
self.drop_databases(test_db_names)
示例9: test_copy_db_argument_checking
def test_copy_db_argument_checking(self):
with assert_raises(TypeError):
yield self.cx.copy_database(4, "foo")
with assert_raises(TypeError):
yield self.cx.copy_database("foo", 4)
with assert_raises(pymongo.errors.InvalidName):
yield self.cx.copy_database("foo", "$foo")
示例10: test_max_pool_size_validation
def test_max_pool_size_validation(self):
with assert_raises(ConfigurationError):
motor.MotorClient(max_pool_size=-1)
with assert_raises(ConfigurationError):
motor.MotorClient(max_pool_size="foo")
cx = self.motor_client(max_pool_size=100)
self.assertEqual(cx.max_pool_size, 100)
cx.close()
示例11: test_copy_db_argument_checking
def test_copy_db_argument_checking(self):
cx = self.get_client()
with assert_raises(TypeError):
yield cx.copy_database(4, 'foo')
with assert_raises(TypeError):
yield cx.copy_database('foo', 4)
with assert_raises(pymongo.errors.InvalidName):
yield cx.copy_database('foo', '$foo')
示例12: test_basic
def test_basic(self):
template = Template("""
<%page args="x, y, z=7"/>
this is page, ${x}, ${y}, ${z}
""")
assert flatten_result(template.render(x=5, y=10)) == "this is page, 5, 10, 7"
assert flatten_result(template.render(x=5, y=10, z=32)) == "this is page, 5, 10, 32"
assert_raises(TypeError, template.render, y=10)
示例13: test_raise
def test_raise(self):
template = Template("""
<%
raise Exception("this is a test")
%>
""", format_errors=False)
assert_raises(
Exception,
template.render
)
示例14: test_max_pool_size_validation
def test_max_pool_size_validation(self):
with assert_raises(ConfigurationError):
motor.MotorClient(host=host, port=port, max_pool_size=-1)
with assert_raises(ConfigurationError):
motor.MotorClient(host=host, port=port, max_pool_size="foo")
cx = motor.MotorClient(host=host, port=port, max_pool_size=100, io_loop=self.io_loop)
self.assertEqual(cx.max_pool_size, 100)
cx.close()
示例15: test_traditional_assignment_plus_undeclared
def test_traditional_assignment_plus_undeclared(self):
t = Template(
"""
t is: ${t}
<%
t = 12
%>
"""
)
assert_raises(UnboundLocalError, t.render, t="T")