def setup_bond_self_test(test_instance, spy_groups=None): """ Setup Bond for self-tests :param spy_groups: :return: """ # Since we use Bond both as a system-under-test and as a testing library, we # have to be careful with mocking. We want to mock the reconcile prompts in the # system under test, but not in the testing Bond. # We keep a variable that is set whether we are running the test. In that case, # the mocking works. Once the test ends, then Bond becomes the testing one. test_instance.during_test = True bond_observations_dir = os.environ.get('BOND_OBSERVATION_DIR', os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'tests', 'test_observations')) spy_groups = 'bond_self_test' if spy_groups is None else spy_groups bond.start_test(test_instance, spy_groups=spy_groups, observation_directory=bond_observations_dir, reconcile=os.environ.get('BOND_RECONCILE', 'abort')) # By default we abort the test if it fails. No user-interface # Still even the abort reconcile wants to run a quick diff. We let it do it bond.deploy_agent('bond_reconcile._invoke_command', cmd__startswith='diff', result=bond.AGENT_RESULT_CONTINUE) # if we ever get to the read_console, it means that there is a problem with the test. # continue bond.deploy_agent('bond_reconcile._read_console', result=bond.AGENT_RESULT_CONTINUE)
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_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_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_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 setup_bond_self_tests(test_instance, spy_groups=None): """ Setup Bond for self-tests :param spy_groups: :return: """ bond_observations_dir = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'tests', 'test_observations') spy_groups = 'bond_self_test' if spy_groups is None else spy_groups bond.start_test(test_instance, spy_groups=spy_groups, observation_directory=bond_observations_dir, reconcile=os.environ.get('BOND_RECONCILE', 'abort')) # By default we abort the test if it fails. No user-interface # Still even the abort reconcile wants to run a quick diff bond.deploy_agent('bond_reconcile._invoke_command', cmd__startswith='diff', result=0)
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_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_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_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_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_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_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 setUp(self): bond.start_test(self)
def setUp(self): # We will run the test with Bond if BOND_RECONCILE is defined self.with_bond = os.environ.get('BOND_RECONCILE') if self.with_bond: bond.start_test(self)
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_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)