def test_with_missing_team(self): mock_responses.add( mock_responses.POST, github.GRAPHQL_URL, body='''{ "data": { "viewer": { "login": "******", "organization": { "teams": { "edges": [ {"cursor": "cursor1"} ], "nodes": [ {"slug": "team1"} ] } } } } }''', status=200, ) user = self._auth(access_token='token') self.assertIsNotNone(user) self.assertEqual(user['sub']['name'], 'user') self.assertFalse(validate_scope(['team2'], user['scope']))
def test_parse_func_count(self): mock.add(mock.POST, self.url, status=200, body='count_1\nUInt64\n42\n') table = Table('t1', self.metadata(), Column('x', types.Int32, primary_key=True)) rv = session.query(func.count()).select_from(table).scalar() self.assertEqual(rv, 42)
def test_makes_request_when_key_is_set(self): mock_responses.add(mock_responses.DELETE, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain') action = DeleteSendgridMailbox('my-key') action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 1)
def test_call_raises_custom_error(self): client = Client() responses.add( responses.POST, client.server, json={"error": {"code": 1, "message": "Custom message"}}, status=200 ) with pytest.raises(ClientException, match=r"Custom message") as e: client.call("aria2.method") assert e.code == 1
def givenTestImage(cls, content_type='image/png', status=200): with open(join(TEST_DATA_DIRECTORY, 'test_image.png'), 'rb') as image: image_bytes = image.read() mock.add(mock.GET, 'http://test-url.png', headers={'Content-Type': content_type}, body=image_bytes, status=status)
def __enter__(self): for request_mock in self.request_mocks: response_mock.add( method=request_mock.request_method.value, url=request_mock.request_url, json=request_mock.response_json, status=request_mock.response_status_code, ) response_mock.__enter__()
def test_skips_request_when_domain_already_exists(self): mock_responses.add(mock_responses.GET, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=200) action = SetupSendgridMailbox('my-key', max_retries=1, retry_interval_seconds=1) action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 1)
def test_handles_missing_mailbox(self): mock_responses.add(mock_responses.DELETE, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=404) action = DeleteSendgridMailbox('my-key') action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 1)
def test_parse_date_types(self): mock.add(mock.POST, 'http://localhost:8123', status=200, body=('a\n' + 'Date\n' + '2012-10-25\n')) table = Table('t1', self.metadata(), Column('a', types.Date)) rv = session.query(*table.c).first() self.assertEqual(rv, (date(2012, 10, 25), ))
def test_parse_nullable_nothing(self): mock.add(mock.POST, self.url, status=200, body=('a\n' + 'Nullable(Nothing)\n' + '\\N\n')) table = Table('t1', self.metadata(), Column('a', types.Float)) rv = self.session.query(*table.c).all() self.assertEqual(rv, [(None, )])
def test_parse_date_types(self, patched_server_info): mock.add(mock.POST, self.url, status=200, body=('a\n' + 'Date\n' + '2012-10-25\n')) table = Table('t1', self.metadata(), Column('a', types.Date)) rv = session.query(*table.c).first() self.assertEqual(rv, (date(2012, 10, 25), ))
def test_parse_nullable_type(self): mock.add(mock.POST, self.url, status=200, body=('a\n' + 'String\n' + '\\N\n' + '\\\\N\n' + '\n')) table = Table('t1', self.metadata(), Column('a', types.String)) rv = session.query(*table.c).all() self.assertEqual(rv, [(None, ), ('\\N', ), ('', )])
def test_parse_decimal(self): mock.add(mock.POST, self.url, status=200, body=('a\n' + 'Decimal(8,8)\n' + '1.1\n')) table = Table('t1', self.metadata(), Column('a', types.Decimal)) rv = self.session.query(*table.c).all() self.assertEqual(rv, [(Decimal('1.1'), )])
def givenTestImage(cls, content_type='image/png', status=200, url='http://test-url.png'): with open(join(TEST_DATA_DIRECTORY, 'test_image.png'), 'rb') as image: image_bytes = image.read() mock_responses.add( mock_responses.GET, url, content_type=content_type, body=image_bytes, status=status, )
def test_with_bad_password(self): mock_responses.add( mock_responses.POST, github.GRAPHQL_URL, json={'message': 'Bad credentials'}, status=401, ) user = self._auth(access_token='incorrect') self.assertIsNone(user)
def test_throws_on_errors(self): mock_responses.add(mock_responses.DELETE, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=500) action = DeleteSendgridMailbox('my-key') with self.assertRaises(Exception): action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 1)
def test_call_raises_known_error(self): client = Client() responses.add( responses.POST, client.server, json={"error": {"code": JSONRPC_PARSER_ERROR, "message": "Custom message"}}, status=200, ) with pytest.raises(ClientException, match=rf"{JSONRPC_CODES[JSONRPC_PARSER_ERROR]}\nCustom message") as e: client.call("aria2.method") assert e.code == JSONRPC_PARSER_ERROR
def test_makes_request_when_key_is_set(self): mock_responses.add( mock_responses.POST, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings') action = SetupSendgridMailbox('my-key') action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 1) self.assertIn(b'"hostname": "my-domain"', mock_responses.calls[0].request.body)
def test_makes_request_when_key_is_set(self): mock_responses.add(mock_responses.GET, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=404) mock_responses.add(mock_responses.POST, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings', status=200) action = SetupSendgridMailbox('my-key', max_retries=1, retry_interval_seconds=1) action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 2) self.assertIn(b'"hostname": "my-domain"', mock_responses.calls[1].request.body)
def test_fails_request_when_retry_limit_is_exceeded(self): mock_responses.add(mock_responses.GET, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=404) mock_responses.add_callback(mock_responses.POST, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings', callback=MockResponses([(500, '', '') for _ in range(5)])) action = SetupSendgridMailbox('my-key', max_retries=3, retry_interval_seconds=0.001) with self.assertRaises(Exception): action('my-client-id', 'my-domain')
def test_retries_request_when_creation_failed(self): mock_responses.add(mock_responses.GET, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings/my-domain', status=404) mock_responses.add_callback(mock_responses.POST, 'https://api.sendgrid.com/v3/user/webhooks/parse/settings', callback=MockResponses([(500, '', ''), (200, '', '')])) action = SetupSendgridMailbox('my-key', max_retries=3, retry_interval_seconds=0.001) action('my-client-id', 'my-domain') self.assertEqual(len(mock_responses.calls), 4)
def test_parse_float_types(self): types_ = ['Float32', 'Float64'] columns = ['a', 'b'] mock.add(mock.POST, self.url, status=200, body=('\t'.join(columns) + '\n' + '\t'.join(types_) + '\n' + '\t'.join(['42'] * len(types_)) + '\n')) table = Table('t1', self.metadata(), *[Column(col, types.Float) for col in columns]) rv = session.query(*table.c).first() self.assertEqual(rv, tuple([42.0] * len(columns)))
def test_parse_int_types(self): types_ = [ 'Int8', 'UInt8', 'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64' ] columns = [chr(i + ord('a')) for i in range(len(types_))] mock.add(mock.POST, self.url, status=200, body=('\t'.join(columns) + '\n' + '\t'.join(types_) + '\n' + '\t'.join(['42'] * len(types_)) + '\n')) table = Table('t1', self.metadata(), *[Column(col, types.Int) for col in columns]) rv = session.query(*table.c).first() self.assertEqual(rv, tuple([42] * len(columns)))
def test_returned_article(self): email = { 'to': ['*****@*****.**'], 'from': '*****@*****.**', 'subject': 'en', 'body': 'Linear Regression', 'sent_at': '2020-02-01 21:17' } self.page_fetch.return_value = Mock( title='Linear regression', url='https://en.wikipedia.org/wiki/Linear_regression') mock_responses.add( mock_responses.GET, 'https://en.wikipedia.org/api/rest_v1/page/pdf/Linear_regression', body=b'some bytes', status=200, content_type='application/json') result_email = self._execute_format(email) self.assertEqual('Linear regression', result_email['subject']) self.assertEqual('Linear regression.pdf', result_email['attachments'][0]['filename'])