Skip to content

Commit

Permalink
Updated tests to be compatible with new framework versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorell committed Aug 30, 2024
1 parent 0d3b111 commit 1e8d4e8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
2 changes: 2 additions & 0 deletions rollbar/lib/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def transform(obj, transforms, key=None, batch_transforms=False):
transforms = [BatchedTransform(transforms)]

for transform in transforms:
if not isinstance(transform, Transform):
continue
obj = _transform(obj, transform, key=key)

return obj
Expand Down
5 changes: 3 additions & 2 deletions rollbar/test/fastapi_tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_should_add_framework_version_to_payload(self, mock_send_payload, *mocks

app = FastAPI()
app.add_middleware(LoggerMiddleware)
app.build_middleware_stack()

rollbar.report_exc_info()

Expand Down Expand Up @@ -70,10 +71,10 @@ def test_should_store_current_request(self, store_current_request):
'client': ['testclient', 50000],
'headers': [
(b'host', b'testserver'),
(b'user-agent', b'testclient'),
(b'accept-encoding', b'gzip, deflate'),
(b'accept', b'*/*'),
(b'accept-encoding', b'gzip, deflate'),
(b'connection', b'keep-alive'),
(b'user-agent', b'testclient'),
],
'http_version': '1.1',
'method': 'GET',
Expand Down
8 changes: 5 additions & 3 deletions rollbar/test/fastapi_tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import rollbar
from rollbar.lib._async import AsyncMock
from rollbar.test import BaseTest
from rollbar.test.utils import get_public_attrs

ALLOWED_PYTHON_VERSION = sys.version_info >= (3, 6)

Expand Down Expand Up @@ -152,6 +153,7 @@ def test_should_add_framework_version_to_payload(self, mock_send_payload, *mocks

app = FastAPI()
app.add_middleware(ReporterMiddleware)
app.build_middleware_stack()

rollbar.report_exc_info()

Expand Down Expand Up @@ -272,10 +274,10 @@ def test_should_store_current_request(self, store_current_request):
'client': ['testclient', 50000],
'headers': [
(b'host', b'testserver'),
(b'user-agent', b'testclient'),
(b'accept-encoding', b'gzip, deflate'),
(b'accept', b'*/*'),
(b'accept-encoding', b'gzip, deflate'),
(b'connection', b'keep-alive'),
(b'user-agent', b'testclient'),
],
'http_version': '1.1',
'method': 'GET',
Expand Down Expand Up @@ -324,7 +326,7 @@ def test_should_return_current_request(self):
async def read_root(original_request: Request):
request = get_current_request()

self.assertEqual(request, original_request)
self.assertEqual(get_public_attrs(request), get_public_attrs(original_request))

client = TestClient(app)
client.get('/')
Expand Down
5 changes: 3 additions & 2 deletions rollbar/test/starlette_tests/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_should_add_framework_version_to_payload(self, mock_send_payload, *mocks

app = Starlette()
app.add_middleware(LoggerMiddleware)
app.build_middleware_stack()

rollbar.report_exc_info()

Expand Down Expand Up @@ -67,10 +68,10 @@ def test_should_store_current_request(self, store_current_request):
'client': ['testclient', 50000],
'headers': [
(b'host', b'testserver'),
(b'user-agent', b'testclient'),
(b'accept-encoding', b'gzip, deflate'),
(b'accept', b'*/*'),
(b'accept-encoding', b'gzip, deflate'),
(b'connection', b'keep-alive'),
(b'user-agent', b'testclient'),
],
'http_version': '1.1',
'method': 'GET',
Expand Down
8 changes: 5 additions & 3 deletions rollbar/test/starlette_tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import rollbar
from rollbar.lib._async import AsyncMock
from rollbar.test import BaseTest
from rollbar.test.utils import get_public_attrs

ALLOWED_PYTHON_VERSION = sys.version_info >= (3, 6)

Expand Down Expand Up @@ -138,6 +139,7 @@ def test_should_add_framework_version_to_payload(self, mock_send_payload, *mocks

app = Starlette()
app.add_middleware(ReporterMiddleware)
app.build_middleware_stack()

rollbar.report_exc_info()

Expand Down Expand Up @@ -243,10 +245,10 @@ def test_should_store_current_request(self, store_current_request):
'client': ['testclient', 50000],
'headers': [
(b'host', b'testserver'),
(b'user-agent', b'testclient'),
(b'accept-encoding', b'gzip, deflate'),
(b'accept', b'*/*'),
(b'accept-encoding', b'gzip, deflate'),
(b'connection', b'keep-alive'),
(b'user-agent', b'testclient'),
],
'http_version': '1.1',
'method': 'GET',
Expand Down Expand Up @@ -290,7 +292,7 @@ def test_should_return_current_request(self):
async def root(original_request):
request = get_current_request()

self.assertEqual(request, original_request)
self.assertEqual(get_public_attrs(request), get_public_attrs(original_request))

return PlainTextResponse('OK')

Expand Down
5 changes: 3 additions & 2 deletions rollbar/test/starlette_tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import unittest

from rollbar.test import BaseTest
from rollbar.test.utils import get_public_attrs

ALLOWED_PYTHON_VERSION = sys.version_info >= (3, 6)

Expand Down Expand Up @@ -49,7 +50,7 @@ def test_should_accept_request_param(self):

stored_request = store_current_request(request)

self.assertEqual(request, stored_request)
self.assertEqual(get_public_attrs(request), get_public_attrs(stored_request))

def test_should_accept_scope_param_if_http_type(self):
from starlette.requests import Request
Expand Down Expand Up @@ -81,7 +82,7 @@ def test_should_accept_scope_param_if_http_type(self):

request = store_current_request(scope, receive)

self.assertEqual(request, expected_request)
self.assertEqual(get_public_attrs(request), get_public_attrs(expected_request))

def test_should_not_accept_scope_param_if_not_http_type(self):
from rollbar.contrib.starlette.requests import store_current_request
Expand Down
12 changes: 7 additions & 5 deletions rollbar/test/test_rollbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from rollbar.lib import string_types

from rollbar.test import BaseTest
from rollbar.test.utils import get_public_attrs

try:
eval("""
Expand Down Expand Up @@ -173,6 +174,7 @@ def test_starlette_request_data_with_consumed_body(self):
body = b'body body body'
scope = {
'type': 'http',
'client': ('127.0.0.1', 1453),
'headers': [
(b'content-type', b'text/html'),
(b'content-length', str(len(body)).encode('latin-1')),
Expand Down Expand Up @@ -410,7 +412,7 @@ def test_get_request_starlette_middleware(self):
def root(starlette_request):
current_request = rollbar.get_request()

self.assertEqual(current_request, starlette_request)
self.assertEqual(get_public_attrs(current_request), get_public_attrs(starlette_request))

return PlainTextResponse("bye bye")

Expand All @@ -437,7 +439,7 @@ def test_get_request_starlette_logger(self):
def root(starlette_request):
current_request = rollbar.get_request()

self.assertEqual(current_request, starlette_request)
self.assertEqual(get_public_attrs(current_request), get_public_attrs(starlette_request))

return PlainTextResponse("bye bye")

Expand Down Expand Up @@ -465,7 +467,7 @@ def test_get_request_fastapi_middleware(self):
def root(param, fastapi_request: Request):
current_request = rollbar.get_request()

self.assertEqual(current_request, fastapi_request)
self.assertEqual(get_public_attrs(current_request), get_public_attrs(fastapi_request))

root = fastapi_add_route_with_request_param(
app, root, '/{param}', 'fastapi_request'
Expand All @@ -492,7 +494,7 @@ def test_get_request_fastapi_logger(self):
def root(fastapi_request: Request):
current_request = rollbar.get_request()

self.assertEqual(current_request, fastapi_request)
self.assertEqual(get_public_attrs(current_request), get_public_attrs(fastapi_request))

root = fastapi_add_route_with_request_param(
app, root, '/{param}', 'fastapi_request'
Expand Down Expand Up @@ -523,7 +525,7 @@ def test_get_request_fastapi_router(self):
def root(fastapi_request: Request):
current_request = rollbar.get_request()

self.assertEqual(current_request, fastapi_request)
self.assertEqual(get_public_attrs(current_request), get_public_attrs(fastapi_request))

root = fastapi_add_route_with_request_param(
app, root, '/{param}', 'fastapi_request'
Expand Down
4 changes: 4 additions & 0 deletions rollbar/test/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from collections.abc import Mapping

def get_public_attrs(obj: Mapping) -> dict:
return {k: obj[k] for k in obj if not k.startswith('_')}

0 comments on commit 1e8d4e8

Please sign in to comment.