def test_post(events, client): client.post('/test', data=json.dumps({ 'my_param': 'example' }), content_type='application/json; charset=UTF-8', headers={ 'Authorization': 'token "test-token"', 'Accept': 'application/json', 'Accept-Language': 'pl' } ) assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.str', 'value': "'example'" }) ] assert events[0].http_server_request == DictIncluding({ 'request_method': 'POST', 'path_info': '/test', 'protocol': 'HTTP/1.1', 'authorization': 'token "test-token"', 'mime_type': 'application/json; charset=UTF-8', }) assert events[0].http_server_request['headers'] == DictIncluding({ 'Accept': 'application/json', 'Accept-Language': 'pl' })
def test_exception(client, events, monkeypatch): def raise_on_call(*args): raise RuntimeError('An error') monkeypatch.setattr(django.core.handlers.exception, 'response_for_exception', raise_on_call) with pytest.raises(RuntimeError): client.get('/exception') assert events[0].http_server_request == DictIncluding({ 'request_method': 'GET', 'path_info': '/exception', 'protocol': 'HTTP/1.1' }) assert events[1].parent_id == events[0].id assert events[1].exceptions == [ DictIncluding({ 'class': 'builtins.RuntimeError', 'message': 'An error' }) ]
def test_http_client_capture(mock_requests, events): requests.get('https://example.test/foo/bar?q=one&q=two&q2=%F0%9F%A6%A0') request = events[0] assert request.http_client_request == { 'request_method': 'GET', 'url': 'https://example.test/foo/bar', 'headers': { 'Connection': 'keep-alive' }, } message = request.message assert message[0] == DictIncluding({ 'name': 'q', 'value': "['one', 'two']" }) assert ((message[1] == DictIncluding({ 'name': 'q2', 'value': "'Ъда'" })) or (message[1] == DictIncluding({ 'name': 'q2', 'value': "'\\U0001f9a0'" }))) assert events[1].http_client_response == DictIncluding({ 'status_code': 200, 'mime_type': 'text/plain; charset=utf-8' })
def test_message_path_segments(events, client): client.get('/post/alice/42/summary') assert events[0].message == [ DictIncluding({ 'name': 'username', 'class': 'builtins.str', 'value': "'alice'" }), DictIncluding({ 'name': 'post_id', 'class': 'builtins.int', 'value': "42" }) ]
def test_http_capture(client, events): """Test GET request and response capture.""" client.get('/test') assert events[0].http_server_request == DictIncluding({ 'request_method': 'GET', 'path_info': '/test', 'protocol': 'HTTP/1.1' }) response = events[1].http_server_response assert response == DictIncluding({ 'status_code': 200, 'mime_type': 'text/html; charset=utf-8' }) assert 'ETag' in response['headers']
def test_template(events): render_to_string('hello_world.html') assert events[0].to_dict() == DictIncluding({ 'path': 'appmap/test/data/django/app/hello_world.html', 'event': 'call', 'defined_class': '<templates>.AppmapTestDataDjangoAppHello_WorldHtml', 'method_id': 'render', 'static': False })
def test_get_arr(events, client): client.get('/test?my_param=example&my_param=example2') assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.list', 'value': "['example', 'example2']" }), ]
def test_post_multipart(events, client): client.post('/test', data={ 'my_param': 'example' }, content_type='multipart/form-data') assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.str', 'value': "'example'" }) ]
def test_template(app, events): with app.app_context(): flask.render_template('test.html') assert events[0].to_dict() == DictIncluding({ 'path': 'appmap/test/data/flask/templates/test.html', 'event': 'call', 'defined_class': '<templates>.AppmapTestDataFlaskTemplatesTestHtml', 'method_id': 'render', 'static': False })
def test_get(events, client): client.get('/test?my_param=example') assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.str', 'value': "'example'" }) ]
def test_post_with_query(events, client): client.post('/test?my_param=get', data={ 'my_param': 'example' }) assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.list', 'value': "['get', 'example']" }) ]
def test_included_view(client, events): client.get('/post/included/test_user') assert len(events) == 2 assert events[0].http_server_request == DictIncluding({ 'path_info': '/post/included/test_user', 'normalized_path_info': '/post/included/{username}' })
def test_post_bad_json(events, client): client.post('/test?my_param=example', data="bad json", content_type='application/json') assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.str', 'value': "'example'" }) ]
def test_put(events, client): client.put('/test', json={ 'my_param': 'example' }) assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.str', 'value': "'example'" }) ]
def test_sql_capture(connection, events): connection.execute('SELECT 1') assert events[0].sql_query == DictIncluding({ 'sql': 'SELECT 1', 'database_type': 'sqlite' }) assert events[0].sql_query['server_version'].startswith('3.') assert Metadata()['frameworks'] == [{ 'name': 'SQLAlchemy', 'version': sqlalchemy.__version__ }]
def verify_expected_metadata(testdir): """Verifies if the test outputs contain the expected metadata. The expected metadata are JSON documents with common suffix to the tests: test_foo.appmap.json -> foo.metadata.json """ pattern = re.compile(r'test_(status_.+)\.appmap\.json') for file in testdir.output().glob('*test_status_*.appmap.json'): name = pattern.search(file.name).group(1) metadata = json.loads(file.read_text())['metadata'] expected = testdir.expected / f'{name}.metadata.json' assert metadata == DictIncluding(json.loads(expected.read_text()))
def test_sql_capture(events): conn = django.db.connections['default'] conn.cursor().execute('SELECT 1').fetchall() assert events[0].sql_query == DictIncluding({ 'sql': 'SELECT 1', 'database_type': 'sqlite' }) assert events[0].sql_query['server_version'].startswith('3.') assert Metadata()['frameworks'] == [{ 'name': 'Django', 'version': django.get_version() }]
def test_http_capture_post(client, events): """Test POST request with JSON body capture.""" client.post( '/test', json={'my_param': 'example'}, headers={ 'Authorization': 'token "test-token"', 'Accept': 'application/json', 'Accept-Language': 'pl' } ) assert events[0].http_server_request == DictIncluding({ 'request_method': 'POST', 'path_info': '/test', 'protocol': 'HTTP/1.1', 'authorization': 'token "test-token"', 'mime_type': 'application/json', }) assert events[0].http_server_request['headers'] == DictIncluding({ 'Accept': 'application/json', 'Accept-Language': 'pl' })
def test_post_form_urlencoded(events, client): client.post( '/test', data='my_param=example', content_type='application/x-www-form-urlencoded' ) assert events[0].message == [ DictIncluding({ 'name': 'my_param', 'class': 'builtins.str', 'value': "'example'" }) ]
def test_git_metadata(git): metadata = Metadata(root_dir=git.cwd) assert 'git' in metadata git_md = metadata['git'] assert git_md == DictIncluding({ 'repository': 'https://www.example.test/repo.git', 'branch': 'master', 'status': [ '?? new_file' ] }) for key in ( 'tag', 'annotated_tag', 'commits_since_tag', 'commits_since_annotated_tag' ): assert key not in git_md
def test_tags(git): atag = 'new_annotated_tag' git(f'tag -a "{atag}" -m "add annotated tag"') git('add new_file') git('commit -m "added new file"') tag = 'new_tag' git(f'tag {tag}') git('rm README.metadata') git('commit -m "Removed readme"') metadata = Metadata(root_dir=git.cwd) git_md = metadata['git'] assert git_md == DictIncluding({ 'repository': 'https://www.example.test/repo.git', 'branch': 'master', 'tag': tag, 'annotated_tag': atag, 'commits_since_tag': 1, 'commits_since_annotated_tag': 2 })
def test_deeply_nested_routes(client, events): client.get('/admincp/permissions/edit/1') assert len(events) == 2 assert events[0].http_server_request == DictIncluding( {'normalized_path_info': '/admincp/permissions/edit/{pk}'})