def test0060_update_only_if_parent_is_modified(self):
'The left and right fields must only be updated if parent is modified'
with Transaction().start(DB_NAME, USER, context=CONTEXT):
records = self.mptt.search([
('parent', '=', None),
])
with patch.object(self.mptt, '_update_tree') as mock:
self.mptt.write(records, {'name': 'Parent Records'})
self.assertFalse(mock.called)
first_parent, second_parent = records[:2]
self.mptt.write(list(first_parent.childs), {
'parent': second_parent.id,
})
self.assertTrue(mock.called)
python类object()的实例源码
def runTest(self):
with patch.object(sys, 'argv', self.build_args):
LOG.info("Running with args %s", self.build_args)
bad_results, good_results, unmatched_results = build.main()
failures = 0
for image, result in six.iteritems(bad_results):
if image in self.excluded_images:
if result is 'error':
continue
failures = failures + 1
LOG.warning(">>> Expected image '%s' to fail, please update"
" the excluded_images in source file above if the"
" image build has been fixed.", image)
else:
if result is not 'error':
continue
failures = failures + 1
LOG.critical(">>> Expected image '%s' to succeed!", image)
for image in unmatched_results.keys():
LOG.warning(">>> Image '%s' was not matched", image)
self.assertEqual(failures, 0, "%d failure(s) occurred" % failures)
def test_enforcing_wait_time_sleeps(mocker):
f = concurrent.Future()
f.set_result(None)
mock_gen_sleep = mocker.patch.object(retry.gen, 'sleep')
mock_gen_sleep.return_value = f
def in_the_future(_):
return 60
request = Mock()
policy = retry.RetryPolicy(try_limit=3, sleep_func=in_the_future)
yield policy.enforce(request)
assert mock_gen_sleep.called is False
yield policy.enforce(request)
mock_gen_sleep.assert_called_once_with(60)
tests_logging_and_monitoring.py 文件源码
项目:Brightside
作者: BrighterCommand
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def test_logging_a_handler(self):
"""s
Given that I have a handler decorated for logging
When I call that handler
Then I should receive logs indicating the call and return of the handler
* N.b. This is an example of using decorators to extend the Brightside pipeline
"""
handler = MyCommandHandler()
request = MyCommand()
self._subscriber_registry.register(MyCommand, lambda: handler)
logger = logging.getLogger("tests.handlers_testdoubles")
with patch.object(logger, 'log') as mock_log:
self._commandProcessor.send(request)
mock_log.assert_has_calls([call(logging.DEBUG, "Entering handle " + str(request)),
call(logging.DEBUG, "Exiting handle " + str(request))])
def test_data_files(self, lsdibag):
with patch.object(lsdibag, 'image_files') as mock_imgfiles:
with patch.object(lsdibag, 'page_text_files') as mock_txtfiles:
mock_imgfiles.return_value = [
'001.tif', '002.tif', '003.tif'
]
mock_txtfiles.return_value = [
'001.txt', '002.txt', '003.txt'
'001.pos', '002.pos', '003.pos'
]
datafiles = lsdibag.data_files()
# should include pdf and ocr file
assert lsdibag.item.pdf in datafiles
assert lsdibag.item.ocr_file in datafiles
# should all image and text files
for imgfile in mock_imgfiles.return_value:
assert imgfile in datafiles
for txtfile in mock_txtfiles.return_value:
assert txtfile in datafiles
def setUp(self):
# Cleanup
self.addCleanup(patch.stopall)
# Request patch
self.request = patch.object(module, 'request').start()
# Various patches
self.services = patch.object(module, 'services').start()
self.original_config = dict(module.config)
module.config['STORAGE_BUCKET_NAME'] = self.bucket = 'buckbuck'
module.config['STORAGE_ACCESS_KEY_ID'] = ''
module.config['STORAGE_SECRET_ACCESS_KEY'] = ''
module.config['ACCESS_KEY_EXPIRES_IN'] = ''
module.config['STORAGE_PATH_PATTERN'] = '{owner}/{dataset}/{path}'
self.s3 = boto3.client('s3')
def patch(self, method):
_m = patch.object(self.obj, method)
mock = _m.start()
self.addCleanup(_m.stop)
return mock
def test_has_access_is_in_group(self):
"""
Test has access if passed user is in group
"""
user, usrmgr_mock = self.__get_test_instance(
"@foouser", 1337, group="foogroup")
usrmgr_mock.return_value.user_is_in_group.return_value = True
with patch.object(user, "save"):
user.has_access("foogroup")
def test_has_access_is_not_in_group(self):
"""
Test has access if passed user is not in group
"""
user, usrmgr_mock = self.__get_test_instance(
"@foouser", 1337, group="bargroup")
usrmgr_mock.return_value.user_is_in_group.return_value = False
usrmgr_mock.return_value.verify_user.return_value = False
with patch.object(user, "save"):
user.has_access("foogroup")
def __get_dummy_object():
"""Returns a dummy usermanager object."""
with patch("os.mkdir"):
return UserManager()
def __set_config(usrmgr, config):
"""Sets the config attribute of the passed usermanager instance"""
with patch.object(usrmgr, "_UserManager__save_config"):
usrmgr.config = config
def test_get_set_config(self):
"""
Test getting and setting the config attribute
"""
usrmgr = self.__get_dummy_object()
config = {"hello": {}, "world": {}}
with patch.object(usrmgr, "_UserManager__save_config"):
usrmgr.config = config
with patch.object(usrmgr, "_UserManager__load_config"):
self.assertEqual(usrmgr.config, config)
def test_userid_is_not_verified_grp(self):
"""
Test user id is not verified in group check
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"users": [{"id": 1337, "username": "@foo"}]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.userid_is_verified_in_group("foogroup", 1234)
self.assertFalse(result)
def test_username_is_verified_grp(self):
"""
Test username is verified in group check
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"users": [{"id": 1337, "username": "@foo"}]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.username_is_verified_in_group("foogroup", "@foo")
self.assertTrue(result)
def test_usernm_is_not_verified_grp(self):
"""
Test username is not verified in group check
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"users": [{"id": 1337, "username": "@foo"}]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.username_is_verified_in_group("foogroup", "@bar")
self.assertFalse(result)
def test_user_is_unverified_grp(self):
"""
Test username is unverified in group check
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"unverified": ["@foo"]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.user_is_unverified_in_group("foogroup", "@foo")
self.assertTrue(result)
def test_user_is_not_unverified_grp(self):
"""
Test username is not unverified in group check
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"unverified": ["@foo"]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.user_is_unverified_in_group("foogroup", "@bar")
self.assertFalse(result)
def test_user_is_in_group_username(self):
"""
Test user is in group check if username is passed
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"unverified": ["@foo"]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.user_is_in_group("foogroup", username="@foo")
self.assertTrue(result)
def test_user_is_in_group_userid(self):
"""
Test user is in group check if userid is passed
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"users": [{"id": 1337, "username": "@foo"}]}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.user_is_in_group("foogroup", user_id=1337)
self.assertTrue(result)
def test_verify_user_no_verify(self):
"""
Test verify user if passed user is not unverified in group
"""
usrmgr = self.__get_dummy_object()
config = {"foogroup": {"unverified": []}}
self.__set_config(usrmgr, config)
with patch.object(usrmgr, "_UserManager__load_config"):
result = usrmgr.verify_user(1337, "@foouser", "foogroup")
self.assertFalse(result)