def testDelete(self): tree = self.create_tree_1() tree.delete(4) # Add self.assertEquals here to verify the position in the tree # of all the data points, in the order in which they were inserted if not self.with_bond: self.assertEquals(8, tree.data) self.assertEquals(12, tree.right.data) self.assertEquals(3, tree.left.data) self.assertEquals(6, tree.left.right.data) else: # Add here a call to bond.spy to spy the tree bond.spy('testDelete1', tree=NodeTest.dumpTree(tree)) tree.delete(8) if not self.with_bond: # Add self.assertEquals here to verify the position in the tree # of all the data points, in the order in which they were inserted self.assertEquals(12, tree.data) self.assertEquals(3, tree.left.data) self.assertEquals(6, tree.left.right.data) else: # Add here a call to bond.spy to spy the tree bond.spy('testDelete2', tree=NodeTest.dumpTree(tree))
def test_spy_basic(self): "A basic spy test" bond.spy(int_arg=1, string_arg="a string", bool_arg=False, array_arg=[1, 2, 3, 'a string at the end'], dict_arg=dict(foo=1, bar=2)) bond.spy('there', val=10)
def test_ambiguous_match(self): url = 'https://giant.balloon.com/rides' self.mocker.register_response(url=url, status_code=500, request_verbs=['get']) self.mocker.responses.append(self.mocker.responses[0]) bond.start_test(self) try: self.mocker.get(url) except AmbiguousURLMatch as am: bond.spy(ambiguous_match=am.message)
def test_reset_spy_groups(self): self.annotated_method_group_enabled() self.annotated_method_no_group() bond.settings(spy_groups='group2') bond.spy(msg='spy groups set') self.annotated_method_group_enabled() self.annotated_method_no_group() bond.settings(spy_groups=()) bond.spy(msg='spy groups reset', obs_dir=os.path.basename(os.path.normpath(bond.Bond.instance()._observation_directory()))) self.annotated_method_group_enabled() self.annotated_method_no_group()
def test_spy_not_testing(self): "Trying to spy when not in testing" # Pretend the test ended bond_instance = bond.Bond.instance() bond_instance._finish_test() # We reach into the internal API bond.spy('first_observation', val=1) bond.spy('second_observation', val=2) # Just before the test ends, we restore current_python_test bond_instance.test_framework_bridge = bond.TestFrameworkBridge.make_bridge(self) # Has to allow the test to continue
def test_formatter(self): "Apply the formatter" def my_formatter(obs): obs['new_key'] = 'new_value' bond.deploy_agent('fun1', cmd__contains="fun", formatter=my_formatter) bond.deploy_agent('fun1', cmd__startswith="myfun", result=lambda obs: obs) # Now the spying. The result should be the modified observation bond.spy('spy result', res=bond.spy('fun1', cmd="myfun3"))
def test_spy_not_testing(self): "Trying to spy when not in testing mode" # Pretend the test ended bond_instance = bond.Bond.instance() old_test_framework_bridge = bond_instance.test_framework_bridge bond_instance.test_framework_bridge = None # We reach into the internal API self.assertFalse(bond.active()) bond.spy('first_observation', val=1) bond.spy('second_observation', val=2) # Just before the test ends, we restore current_python_test bond_instance.test_framework_bridge = old_test_framework_bridge # Has to allow the test to continue
def test_same_url_diff_params_not_ambiguous(self): url = 'https://giant.balloon.com/rides' self.mocker.register_response(url=url, status_code=200, request_verbs=['get'], decoded_json={'page': '1'}) self.mocker.register_response(url=url, status_code=200, request_verbs=['get'], url_params={'page': '2'}, decoded_json={'page': '2'}) self.mocker.register_response(url=url, status_code=200, request_verbs=['get'], url_params={'page': '3'}, decoded_json={'page': '3'}) bond.start_test(self) results = list() results.append(self.mocker.get(url).json()) results.append(self.mocker.get(url, params={'page': '2'}).json()) results.append(self.mocker.get(url, params={'page': '3'}).json()) results.append(self.mocker.get(url + '?page=2').json()) results.append(self.mocker.get(url + '?page=3').json()) bond.spy(url_params=results)
def test_result(self): "Test the result aspect of the bond mocking" def my_formatter(obs): obs['cmd'] += ' : formatted' # The formatter is processed after the result is computed bond.deploy_agent('fun1', cmd__contains="fun", formatter=my_formatter, result=lambda obs: obs['cmd'] + ' here') bond.deploy_agent('fun1', cmd__startswith="myfun", result=123) # Now the spying self.assertEquals('a ton of fun here', bond.spy(spy_point_name='fun1', cmd="a ton of fun")) self.assertEquals(bond.AGENT_RESULT_NONE, bond.spy(spy_point_name='nofun', cmd="myfun2")) self.assertEquals(123, bond.spy('fun1', cmd="myfun3"))
def invoke_top_reconcile(self, current_lines, reconcile=None, no_save=None): """ Helper function to invoke the top-level reconcile :return: """ result = bond_reconcile.reconcile_observations(dict(reconcile=reconcile), 'test 1', self.reference_file, current_lines, no_save=no_save) # Observe the directory bond.spy('invoke_top_reconcile_results', result=result, observation_dir=collect_directory_contents(self.testing_observation_dir, collect_file_contents=True))
def test_exception(self): "A test that throws exceptions" def my_formatter(obs): obs['cmd'] += ' : formatted' bond.deploy_agent('fun1', cmd__startswith="myfun", result=1, formatter=my_formatter, exception=Exception("some exception")) self.assertRaises(Exception, lambda : bond.spy('fun1', cmd="myfun1")) bond.deploy_agent('fun1', cmd="myfun2", result=1, formatter=my_formatter, exception=lambda obs: Exception("some exception: "+obs['cmd'])) self.assertRaises(Exception, lambda : bond.spy(spy_point_name='fun1', cmd="myfun2"))
def testAdd1(self): """ Test adding nodes to the BST. No Bond. :return: """ tree = self.create_tree_1() # Add self.assertEquals here to verify the position in the tree # of all the data points, in the order in which they were inserted if not self.with_bond: self.assertEquals(8, tree.data) self.assertEquals(12, tree.right.data) self.assertEquals(None, tree.right.left) self.assertEquals(None, tree.right.right) self.assertEquals(3, tree.left.data) self.assertEquals(4, tree.left.right.data) self.assertEquals(6, tree.left.right.right.data) else: # Add here a call to bond.spy to spy the tree bond.spy('testAdd1', tree=NodeTest.dumpTree(tree))
def test_mock_only(self): bond.spy('unmocked_return', val=self.mock_only_method()) bond.deploy_agent('AnnotationTests.mock_only_method', result='mocked value') bond.spy('mocked_return', val=self.mock_only_method()) bond.deploy_agent('AnnotationTests.mock_only_method', skip_save_observation=False, result='mocked value') bond.spy('mocked_return', val=self.mock_only_method())
def test_cannot_alter_responses_via_reference(self): bond.start_test(self) expected = { 'things': 'stuff', 'words': 'series of letters', 'login_date': 'today', 'login_token': 'abcd' } self.mocker.register_response(url='https://oranges.org/squeeze_orange', status_code=201, request_verbs=['post'], decoded_json=expected) response = self.mocker.post('https://oranges.org/squeeze_orange', headers={'orange_type': 'California'}, payload={'squeeze_level': 5}) original = response.json() first = deepcopy(original) del first['things'] del first['login_token'] del first['login_date'] second = response.json() self.assertNotEqual(first, second) self.assertEqual(original, second) bond.spy(result=second)
def test_get(self): bond.start_test(self) self.mocker.register_response( url='https://giant.balloon.com/rides?time=now', status_code=200, request_verbs=['get'], decoded_json='Sorry, tickets are sold out.') response = self.mocker.get('https://giant.balloon.com/rides', params={'time': 'now'}) bond.spy(get_json=response.json()) bond.spy(get_url=response.url) bond.spy(get_status=response.status_code)
def test_post(self): bond.start_test(self) self.mocker.register_response(url='https://oranges.org/squeeze_orange', status_code=201, request_verbs=['post'], decoded_json='Success.') response = self.mocker.post('https://oranges.org/squeeze_orange', headers={'orange_type': 'California'}, payload={'squeeze_level': 5}) bond.spy(get_json=response.json()) bond.spy(get_url=response.url) bond.spy(get_status=response.status_code)
def test_doers(self): "Test the doer aspect of the bond mocking" results = [] def my_formatter(obs): obs['cmd'] += ' : formatted' bond.deploy_agent('fun1', cmd__startswith="myfun", formatter=my_formatter, do=(lambda args: results.append("1: " + args['cmd']))) bond.deploy_agent('fun1', cmd__contains="fun1", do=(lambda args: results.append("2: " + args['cmd']))) # Now the spying bond.spy(spy_point_name='fun1', cmd="myfun1") # agent 2: only bond.spy(spy_point_name='nofun', cmd="myfun2") # no agent bond.spy(spy_point_name='fun1', cmd="myfun3") # agent 1: only self.assertSequenceEqual(['2: myfun1', '1: myfun3'], results)
def test_spy_create_dir(self): "Test the creation of the observation directory" test_dir = '/tmp/bondTestOther' bond.settings(observation_directory=test_dir, reconcile='accept') if os.path.isdir(test_dir): shutil.rmtree(test_dir) bond.spy(spy_point_name='first_observation', val=1) bond.spy(spy_point_name='second_observation', val=2) # Now spy the contents of the observation directory itself bond_instance = bond.Bond.instance() bond_instance._finish_test() # We reach into the internal API bond_instance.test_framework_bridge = bond.TestFrameworkBridge.make_bridge(self) # Has to allow the test to continue bond.spy('collect_spy_observation_dirs', directory=bond_helpers.collect_directory_contents(test_dir, collect_file_contents=True)) # Now we delete the observation directory, which we already collected above. # Otherwise, the test will try to reconcile with it shutil.rmtree(test_dir)
def test_skip_save_observation(self): "Test the ability to specify skipping saving observations and overriding it" bond.spy('skipped_spy_point', skip_save_observation=True, key='val') bond.deploy_agent('skipped_spy_point', result='Mock Value') ret = bond.spy('skipped_spy_point', skip_save_observation=True, key='val') bond.spy('skipped_return_value', val=ret) bond.deploy_agent('normal_spy_point', skip_save_observation=False, result='Mock Value') ret = bond.spy('normal_spy_point', skip_save_observation=True, key='val') bond.spy('not_skipped_return_value', val=ret) bond.deploy_agent('skipped_spy_point', skip_save_observation=True, result='Mock Value') ret = bond.spy('skipped_spy_point', key='val') bond.spy('skipped_return_value', val=ret)
def test_register_with_url_args(self): bond.start_test(self) self.mocker.register_response(url='https://real.com', status_code=200, request_verbs=['get'], url_params={'this': 'thing'}) bond.spy(urlArgs=self.mocker.responses)
def test_unregistered_response(self): bond.start_test(self) try: self.mocker.get('https://giant.balloon.com/rides', params={'time': 'now'}) except UnregisteredURL as uu: bond.spy(unregistered=uu.message)
def test_register_same_url_different_verb(self): bond.start_test(self) self.mocker.register_response(url='hi.bob', status_code='999', request_verbs=['bang']) self.mocker.register_response(url='hi.bob', status_code='999', request_verbs=['boom']) bond.spy(sameUrlDiffVerb=self.mocker.responses)
def test_register_same_verb_diff_url(self): bond.start_test(self) self.mocker.register_response(url='hi.bob', status_code='999', request_verbs=['bang']) self.mocker.register_response(url='hi.bill', status_code='999', request_verbs=['bang']) bond.spy(sameVerbDiffUrl=self.mocker.responses)
def test_register_multi_verb(self): bond.start_test(self) self.mocker.register_response(url='1', status_code='999', request_verbs=['bang', 'boom']) bond.spy(multi=self.mocker.responses)
def test_register_multi(self): bond.start_test(self) self.mocker.register_response(url='1', status_code='999', request_verbs=['bang']) self.mocker.register_response(url='2', status_code='666', request_verbs=['flip']) bond.spy(multiURL=self.mocker.responses)
def test_register_url(self): bond.start_test(self) self.mocker.register_response(url='1', status_code='999', request_verbs=['bang']) bond.spy(register=self.mocker.responses)
def test_with_return_none(self): arr = [0] bond.deploy_agent('AnnotationTests.annotated_with_side_effects', result=None) ret = self.annotated_with_side_effects(arr) bond.spy('return value', ret=ret, arr_value=arr[0])
def test_custom_serializer(self): bond.spy(obj=CustomClass(12, 87), func=lambda x: True)
def test_clear_responses(self): bond.start_test(self) self.mocker.register_response(url='hi.bob', status_code='999', request_verbs=['bang']) self.mocker.register_response(url='hi.bill', status_code='999', request_verbs=['bang']) self.mocker.clear_responses() bond.spy(cleared=self.mocker.responses)
def test_url_array_parameterization(self): bond.start_test(self) data = {'a': '1', 'b': '2', 'c': ['4', '5', '6', '7', '8']} x = self.mocker._format_params(data) bond.spy(array_params=x)