def test_upgrade_from_sha_with_unicode_password(self):
user = factories.User()
password = u'testpassword\xc2\xa0'
user_obj = model.User.by_name(user['name'])
# setup our user with an old password hash
old_hash = self._set_password(password)
user_obj._password = old_hash
user_obj.save()
nt.assert_true(user_obj.validate_password(password))
nt.assert_not_equals(old_hash, user_obj.password)
nt.assert_true(pbkdf2_sha512.identify(user_obj.password))
nt.assert_true(pbkdf2_sha512.verify(password, user_obj.password))
# check that we now allow unicode characters
nt.assert_false(pbkdf2_sha512.verify('testpassword',
user_obj.password))
python类assert_false()的实例源码
def test_histmanager_disabled():
"""Ensure that disabling the history manager doesn't create a database."""
cfg = Config()
cfg.HistoryAccessor.enabled = False
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
hist_manager_ori = ip.history_manager
hist_file = os.path.join(tmpdir, 'history.sqlite')
cfg.HistoryManager.hist_file = hist_file
try:
ip.history_manager = HistoryManager(shell=ip, config=cfg)
hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
ip.history_manager.reset()
ip.history_manager.end_session()
finally:
ip.history_manager = hist_manager_ori
# hist_file should not be created
nt.assert_false(os.path.exists(hist_file))
def test_command_chain_dispatcher_fofo():
"""Test a mixture of failing and succeeding hooks."""
fail1 = Fail(u'fail1')
fail2 = Fail(u'fail2')
okay1 = Okay(u'okay1')
okay2 = Okay(u'okay2')
dp = CommandChainDispatcher([(0, fail1),
# (5, okay1), # add this later
(10, fail2),
(15, okay2)])
dp.add(okay1, 5)
nt.assert_equal(dp(), u'okay1')
nt.assert_true(fail1.called)
nt.assert_true(okay1.called)
nt.assert_false(fail2.called)
nt.assert_false(okay2.called)
def test_last_two_blanks():
nt.assert_false(isp.last_two_blanks(''))
nt.assert_false(isp.last_two_blanks('abc'))
nt.assert_false(isp.last_two_blanks('abc\n'))
nt.assert_false(isp.last_two_blanks('abc\n\na'))
nt.assert_false(isp.last_two_blanks('abc\n \n'))
nt.assert_false(isp.last_two_blanks('abc\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n '))
nt.assert_true(isp.last_two_blanks('\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n '))
nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
def test_deepreload():
"Test that dreload does deep reloads and skips excluded modules."
with TemporaryDirectory() as tmpdir:
with prepended_to_syspath(tmpdir):
with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
f.write("class Object(object):\n pass\n")
with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
f.write("import A\n")
import A
import B
# Test that A is not reloaded.
obj = A.Object()
dreload(B, exclude=['A'])
nt.assert_true(isinstance(obj, A.Object))
# Test that A is reloaded.
obj = A.Object()
dreload(B)
nt.assert_false(isinstance(obj, A.Object))
def test_greedy_completions():
ip = get_ipython()
ip.ex('a=list(range(5))')
_,c = ip.complete('.',line='a[0].')
nt.assert_false('.real' in c,
"Shouldn't have completed on a[0]: %s"%c)
with greedy_completion():
def _(line, cursor_pos, expect, message):
_,c = ip.complete('.', line=line, cursor_pos=cursor_pos)
nt.assert_in(expect, c, message%c)
yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s"
yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s"
if sys.version_info > (3,4):
yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s"
def test_histmanager_disabled():
"""Ensure that disabling the history manager doesn't create a database."""
cfg = Config()
cfg.HistoryAccessor.enabled = False
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
hist_manager_ori = ip.history_manager
hist_file = os.path.join(tmpdir, 'history.sqlite')
cfg.HistoryManager.hist_file = hist_file
try:
ip.history_manager = HistoryManager(shell=ip, config=cfg)
hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
ip.history_manager.reset()
ip.history_manager.end_session()
finally:
ip.history_manager = hist_manager_ori
# hist_file should not be created
nt.assert_false(os.path.exists(hist_file))
def test_command_chain_dispatcher_fofo():
"""Test a mixture of failing and succeeding hooks."""
fail1 = Fail(u'fail1')
fail2 = Fail(u'fail2')
okay1 = Okay(u'okay1')
okay2 = Okay(u'okay2')
dp = CommandChainDispatcher([(0, fail1),
# (5, okay1), # add this later
(10, fail2),
(15, okay2)])
dp.add(okay1, 5)
nt.assert_equal(dp(), u'okay1')
nt.assert_true(fail1.called)
nt.assert_true(okay1.called)
nt.assert_false(fail2.called)
nt.assert_false(okay2.called)
def test_last_two_blanks():
nt.assert_false(isp.last_two_blanks(''))
nt.assert_false(isp.last_two_blanks('abc'))
nt.assert_false(isp.last_two_blanks('abc\n'))
nt.assert_false(isp.last_two_blanks('abc\n\na'))
nt.assert_false(isp.last_two_blanks('abc\n \n'))
nt.assert_false(isp.last_two_blanks('abc\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n '))
nt.assert_true(isp.last_two_blanks('\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n '))
nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
def test_deepreload():
"Test that dreload does deep reloads and skips excluded modules."
with TemporaryDirectory() as tmpdir:
with prepended_to_syspath(tmpdir):
with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
f.write("class Object(object):\n pass\n")
with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
f.write("import A\n")
import A
import B
# Test that A is not reloaded.
obj = A.Object()
dreload(B, exclude=['A'])
nt.assert_true(isinstance(obj, A.Object))
# Test that A is reloaded.
obj = A.Object()
dreload(B)
nt.assert_false(isinstance(obj, A.Object))
def test_get_phi_topic_from_memory():
sampler2 = GibbsSampler(corpus, nTopics=3, nIter=5)
sampler2._initialize()
sampler2.run()
f = sampler2.get_phi_topic_file_name(0)
yield assert_false, os.path.isfile(f)
phi = sampler2.get_phi_topic()
yield assert_equal, phi.shape, (sampler2.nTopics, sampler2.VT)
phi = sampler2.get_phi_topic(index=2)
yield assert_equal, phi.shape, (sampler2.nTopics, sampler2.VT)
phi = sampler2.get_phi_topic(start=0, end=5)
yield assert_equal, phi.shape, (sampler2.nTopics, sampler2.VT)
def test_get_theta_from_memory():
sampler2 = GibbsSampler(corpus, nTopics=3, nIter=5)
sampler2._initialize()
sampler2.run()
f = sampler2.get_theta_file_name(0)
yield assert_false, os.path.isfile(f)
r = sampler2.get_theta()
yield assert_equal, r.shape, (sampler2.DT, sampler2.nTopics)
r = sampler2.get_theta(index=2)
yield assert_equal, r.shape, (sampler2.DT, sampler2.nTopics)
r = sampler2.get_theta(start=0, end=5)
yield assert_equal, r.shape, (sampler2.DT, sampler2.nTopics)
def test_get_phi_opinion_from_memory():
sampler2 = GibbsSampler(corpus, nTopics=3, nIter=5)
sampler2._initialize()
sampler2.run()
f = sampler2.get_phi_opinion_file_name(0, 0)
yield assert_false, os.path.isfile(f)
phi = sampler2.get_phi_opinion()
yield assert_equal, len(phi), sampler2.nPerspectives
yield assert_equal, phi[0].shape, (sampler2.nTopics, sampler2.VO)
phi = sampler2.get_phi_opinion(index=2)
yield assert_equal, len(phi), sampler2.nPerspectives
yield assert_equal, phi[0].shape, (sampler2.nTopics, sampler2.VO)
phi = sampler2.get_phi_opinion(start=0, end=5)
yield assert_equal, len(phi), sampler2.nPerspectives
yield assert_equal, phi[0].shape, (sampler2.nTopics, sampler2.VO)
def remove_bonding(api):
engine = api.system_service()
def _remove_bonding(host):
host_service = engine.hosts_service().host_service(id=host.id)
network_utils_v4.detach_network_from_host(
engine, host_service, MIGRATION_NETWORK, BOND_NAME)
network_utils_v4.set_network_required_in_cluster(engine, MIGRATION_NETWORK,
CLUSTER_NAME, False)
utils.invoke_in_parallel(
_remove_bonding, test_utils.hosts_in_cluster_v4(engine, CLUSTER_NAME))
for host in test_utils.hosts_in_cluster_v4(engine, CLUSTER_NAME):
host_service = engine.hosts_service().host_service(id=host.id)
nt.assert_false(_host_is_attached_to_network(engine, host_service,
MIGRATION_NETWORK))
def remove_bonding(api):
engine = api.system_service()
def _remove_bonding(host):
host_service = engine.hosts_service().host_service(id=host.id)
network_utils_v4.detach_network_from_host(
engine, host_service, MIGRATION_NETWORK, BOND_NAME)
network_utils_v4.set_network_required_in_cluster(engine, MIGRATION_NETWORK,
CLUSTER_NAME, False)
utils.invoke_in_parallel(
_remove_bonding, test_utils.hosts_in_cluster_v4(engine, CLUSTER_NAME))
for host in test_utils.hosts_in_cluster_v4(engine, CLUSTER_NAME):
host_service = engine.hosts_service().host_service(id=host.id)
nt.assert_false(_host_is_attached_to_network(engine, host_service,
MIGRATION_NETWORK))
def test_greedy_completions():
ip = get_ipython()
ip.ex('a=list(range(5))')
_,c = ip.complete('.',line='a[0].')
nt.assert_false('.real' in c,
"Shouldn't have completed on a[0]: %s"%c)
with greedy_completion():
def _(line, cursor_pos, expect, message):
_,c = ip.complete('.', line=line, cursor_pos=cursor_pos)
nt.assert_in(expect, c, message%c)
yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s"
yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s"
if sys.version_info > (3,4):
yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s"
def test_histmanager_disabled():
"""Ensure that disabling the history manager doesn't create a database."""
cfg = Config()
cfg.HistoryAccessor.enabled = False
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
hist_manager_ori = ip.history_manager
hist_file = os.path.join(tmpdir, 'history.sqlite')
cfg.HistoryManager.hist_file = hist_file
try:
ip.history_manager = HistoryManager(shell=ip, config=cfg)
hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
ip.history_manager.reset()
ip.history_manager.end_session()
finally:
ip.history_manager = hist_manager_ori
# hist_file should not be created
nt.assert_false(os.path.exists(hist_file))
def test_last_two_blanks():
nt.assert_false(isp.last_two_blanks(''))
nt.assert_false(isp.last_two_blanks('abc'))
nt.assert_false(isp.last_two_blanks('abc\n'))
nt.assert_false(isp.last_two_blanks('abc\n\na'))
nt.assert_false(isp.last_two_blanks('abc\n \n'))
nt.assert_false(isp.last_two_blanks('abc\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n '))
nt.assert_true(isp.last_two_blanks('\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n '))
nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
def test_deepreload():
"Test that dreload does deep reloads and skips excluded modules."
with TemporaryDirectory() as tmpdir:
with prepended_to_syspath(tmpdir):
with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
f.write("class Object(object):\n pass\n")
with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
f.write("import A\n")
import A
import B
# Test that A is not reloaded.
obj = A.Object()
dreload(B, exclude=['A'])
nt.assert_true(isinstance(obj, A.Object))
# Test that A is reloaded.
obj = A.Object()
dreload(B)
nt.assert_false(isinstance(obj, A.Object))
def test_histmanager_disabled():
"""Ensure that disabling the history manager doesn't create a database."""
cfg = Config()
cfg.HistoryAccessor.enabled = False
ip = get_ipython()
with TemporaryDirectory() as tmpdir:
hist_manager_ori = ip.history_manager
hist_file = os.path.join(tmpdir, 'history.sqlite')
cfg.HistoryManager.hist_file = hist_file
try:
ip.history_manager = HistoryManager(shell=ip, config=cfg)
hist = [u'a=1', u'def f():\n test = 1\n return test', u"b='€Æ¾÷ß'"]
for i, h in enumerate(hist, start=1):
ip.history_manager.store_inputs(i, h)
nt.assert_equal(ip.history_manager.input_hist_raw, [''] + hist)
ip.history_manager.reset()
ip.history_manager.end_session()
finally:
ip.history_manager = hist_manager_ori
# hist_file should not be created
nt.assert_false(os.path.exists(hist_file))
def test_command_chain_dispatcher_fofo():
"""Test a mixture of failing and succeeding hooks."""
fail1 = Fail(u'fail1')
fail2 = Fail(u'fail2')
okay1 = Okay(u'okay1')
okay2 = Okay(u'okay2')
dp = CommandChainDispatcher([(0, fail1),
# (5, okay1), # add this later
(10, fail2),
(15, okay2)])
dp.add(okay1, 5)
nt.assert_equal(dp(), u'okay1')
nt.assert_true(fail1.called)
nt.assert_true(okay1.called)
nt.assert_false(fail2.called)
nt.assert_false(okay2.called)
def test_last_two_blanks():
nt.assert_false(isp.last_two_blanks(''))
nt.assert_false(isp.last_two_blanks('abc'))
nt.assert_false(isp.last_two_blanks('abc\n'))
nt.assert_false(isp.last_two_blanks('abc\n\na'))
nt.assert_false(isp.last_two_blanks('abc\n \n'))
nt.assert_false(isp.last_two_blanks('abc\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n'))
nt.assert_true(isp.last_two_blanks('\n\n '))
nt.assert_true(isp.last_two_blanks('\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n '))
nt.assert_true(isp.last_two_blanks('abc\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n'))
nt.assert_true(isp.last_two_blanks('abc\n\n \n '))
nt.assert_true(isp.last_two_blanks('abc\n\n \n \n'))
nt.assert_true(isp.last_two_blanks('abc\nd\n\n\n'))
nt.assert_true(isp.last_two_blanks('abc\nd\ne\nf\n\n\n'))
def test_deepreload():
"Test that dreload does deep reloads and skips excluded modules."
with TemporaryDirectory() as tmpdir:
with prepended_to_syspath(tmpdir):
with open(os.path.join(tmpdir, 'A.py'), 'w') as f:
f.write("class Object(object):\n pass\n")
with open(os.path.join(tmpdir, 'B.py'), 'w') as f:
f.write("import A\n")
import A
import B
# Test that A is not reloaded.
obj = A.Object()
dreload(B, exclude=['A'])
nt.assert_true(isinstance(obj, A.Object))
# Test that A is reloaded.
obj = A.Object()
dreload(B)
nt.assert_false(isinstance(obj, A.Object))
def assert_is_not_none(obj, msg=None):
"""Same as assert_false(obj is None), with a nicer default message."""
if not msg:
msg = '{orig!r} is None'.format(orig=obj)
assert_false(obj is None, msg=msg)
def test_powerview_create_private_defaults_to_false(self):
'''
PowerViews are public by default, if private option isn't specified.
'''
sysadmin = Sysadmin()
create_dict = self._make_create_data_dict()
del create_dict['private']
powerview_result = toolkit.get_action('powerview_create')(
context={'user': sysadmin['name']},
data_dict=create_dict
)
nosetools.assert_false(powerview_result['private'])
def test_upgrade_from_sha_with_wrong_password_fails_to_upgrade(self):
user = factories.User()
password = u'testpassword'
user_obj = model.User.by_name(user['name'])
old_hash = self._set_password(password)
user_obj._password = old_hash
user_obj.save()
nt.assert_false(user_obj.validate_password('wrongpass'))
nt.assert_equals(old_hash, user_obj.password)
nt.assert_false(pbkdf2_sha512.identify(user_obj.password))
def test_upgrade_from_pbkdf2_fails_with_wrong_password(self):
user = factories.User()
password = u'testpassword'
user_obj = model.User.by_name(user['name'])
# setup hash with salt/rounds less than the default
old_hash = pbkdf2_sha512.encrypt(password, salt_size=2, rounds=10)
user_obj._password = old_hash
user_obj.save()
nt.assert_false(user_obj.validate_password('wrong_pass'))
# check that the hash has _not_ been updated
nt.assert_equals(old_hash, user_obj.password)
def test_has_open_quotes3():
for s in ["''", "''' '''", "'hi' 'ipython'"]:
nt.assert_false(completer.has_open_quotes(s))
def test_has_open_quotes4():
for s in ['""', '""" """', '"hi" "ipython"']:
nt.assert_false(completer.has_open_quotes(s))
def test_greedy_completions():
"""
Test the capability of the Greedy completer.
Most of the test here do not really show off the greedy completer, for proof
each of the text bellow now pass with Jedi. The greedy completer is capable of more.
See the :any:`test_dict_key_completion_contexts`
"""
ip = get_ipython()
ip.ex('a=list(range(5))')
_,c = ip.complete('.',line='a[0].')
nt.assert_false('.real' in c,
"Shouldn't have completed on a[0]: %s"%c)
with greedy_completion(), provisionalcompleter():
def _(line, cursor_pos, expect, message, completion):
_,c = ip.complete('.', line=line, cursor_pos=cursor_pos)
with provisionalcompleter():
completions = ip.Completer.completions(line, cursor_pos)
nt.assert_in(expect, c, message%c)
nt.assert_in(completion, completions)
yield _, 'a[0].', 5, 'a[0].real', "Should have completed on a[0].: %s", Completion(5,5, 'real')
yield _, 'a[0].r', 6, 'a[0].real', "Should have completed on a[0].r: %s", Completion(5,6, 'real')
if sys.version_info > (3, 4):
yield _, 'a[0].from_', 10, 'a[0].from_bytes', "Should have completed on a[0].from_: %s", Completion(5, 10, 'from_bytes')