-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
331 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
import logging | ||
import logging.config | ||
import os | ||
import uuid | ||
from typing import Optional | ||
|
||
import httpx | ||
import pytest | ||
import structlog | ||
|
||
from origami.clients.api import APIClient | ||
from origami.models.api.files import File | ||
from origami.models.api.projects import Project | ||
from origami.models.notebook import Notebook | ||
|
||
logger = structlog.get_logger() | ||
|
||
|
||
@pytest.fixture(autouse=True, scope='session') | ||
def setup_logging(): | ||
"""Configure structlog in tests the same way we do in production apps""" | ||
structlog.configure( | ||
processors=[ | ||
structlog.stdlib.PositionalArgumentsFormatter(), | ||
structlog.processors.StackInfoRenderer(), | ||
structlog.processors.format_exc_info, | ||
structlog.stdlib.ProcessorFormatter.wrap_for_formatter, | ||
], | ||
logger_factory=structlog.stdlib.LoggerFactory(), | ||
wrapper_class=structlog.stdlib.BoundLogger, | ||
cache_logger_on_first_use=True, | ||
) | ||
|
||
# shared processors to be applied to both vanilla and structlog messages | ||
# after each is appropriately pre-processed | ||
processors = [ | ||
# log level / logger name, effects coloring in ConsoleRenderer(colors=True) | ||
structlog.stdlib.add_log_level, | ||
structlog.stdlib.add_logger_name, | ||
# timestamp format | ||
structlog.processors.TimeStamper(fmt="iso"), | ||
# To see all CallsiteParameterAdder options: | ||
# https://www.structlog.org/en/stable/api.html#structlog.processors.CallsiteParameterAdder | ||
# more options include module, pathname, process, process_name, thread, thread_name | ||
structlog.processors.CallsiteParameterAdder( | ||
{ | ||
structlog.processors.CallsiteParameter.FILENAME, | ||
structlog.processors.CallsiteParameter.FUNC_NAME, | ||
structlog.processors.CallsiteParameter.LINENO, | ||
} | ||
), | ||
# Any structlog.contextvars.bind_contextvars included in middleware/functions | ||
structlog.contextvars.merge_contextvars, | ||
# strip _record and _from_structlog keys from event dictionary | ||
structlog.stdlib.ProcessorFormatter.remove_processors_meta, | ||
structlog.dev.ConsoleRenderer(colors=True), | ||
# ^^ In prod with any kind of logging service (datadog, grafana, etc), ConsoleRenderer | ||
# would probably be replaced with structlog.processors.JSONRenderer() or similar | ||
] | ||
|
||
# Configs applied to logs generated by structlog or vanilla logging | ||
logging.config.dictConfig( | ||
{ | ||
"version": 1, | ||
"disable_existing_loggers": False, | ||
"formatters": { | ||
"default": { | ||
"()": structlog.stdlib.ProcessorFormatter, | ||
"processors": processors, | ||
"foreign_pre_chain": [structlog.stdlib.ExtraAdder()], | ||
}, | ||
}, | ||
"handlers": { | ||
"default": { | ||
"class": "logging.StreamHandler", | ||
"formatter": "default", | ||
"stream": "ext://sys.stdout", | ||
}, | ||
}, | ||
"loggers": { | ||
# "" for applying handler to "root" (all libraries) | ||
# you could set this to "kernel_sidecar" to only see logs from this library | ||
"": { | ||
"handlers": ["default"], | ||
"level": 'INFO', | ||
"propagate": True, | ||
}, | ||
}, | ||
} | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def jwt(): | ||
token = os.environ.get('NOTEABLE_TOKEN') | ||
if not token: | ||
raise RuntimeError('NOTEABLE_TOKEN environment variable not set') | ||
return token | ||
|
||
|
||
@pytest.fixture | ||
def api_base_url(): | ||
# TODO: use env var or otherwise make configurable for CI | ||
return 'http://localhost:8001/api' | ||
|
||
|
||
@pytest.fixture | ||
def test_space_id() -> uuid.UUID: | ||
# TODO: use env var or otherwise make configurable for CI | ||
return uuid.UUID('1f8300cd-454e-4b14-8adf-57d953d87a07') | ||
|
||
|
||
@pytest.fixture | ||
def test_project_id() -> uuid.UUID: | ||
# TODO: use env var or otherwise make configurable for CI | ||
return uuid.UUID('57da9e36-bd84-4f26-be5f-d4e92d1f4b95') | ||
|
||
|
||
@pytest.fixture | ||
def test_user_id() -> uuid.UUID: | ||
# TODO: use env var or otherwise make configurable for CI | ||
return uuid.UUID('5bd43c56-9fce-4e7e-b1d7-c92f567aac68') | ||
|
||
|
||
class LogWarningTransport(httpx.AsyncHTTPTransport): | ||
""" | ||
Automatically log information about any non-2xx response. | ||
""" | ||
|
||
async def handle_async_request(self, request: httpx.Request) -> httpx.Response: | ||
resp = await super().handle_async_request(request) | ||
if resp.is_error: | ||
response_content = await resp.aread() | ||
logger.warning(f'{request.method} {request.url} {resp.status_code} {response_content}') | ||
return resp | ||
|
||
|
||
@pytest.fixture | ||
def api_client(api_base_url, jwt) -> APIClient: | ||
return APIClient( | ||
authorization_token=jwt, | ||
api_base_url=api_base_url, | ||
transport=LogWarningTransport(), | ||
) | ||
|
||
|
||
@pytest.fixture | ||
async def new_project(api_client: APIClient, test_space_id: uuid.UUID) -> Project: | ||
"""Create and cleanup a new Project""" | ||
name = 'test-project-' + str(uuid.uuid4()) | ||
new_project = await api_client.create_project(name=name, space_id=test_space_id) | ||
yield new_project | ||
await api_client.delete_project(new_project.id) | ||
|
||
|
||
@pytest.fixture | ||
async def file_maker(api_client: APIClient, test_project_id: uuid.UUID): | ||
"""Create and cleanup non-Notebook files""" | ||
file_ids = [] | ||
|
||
async def make_file( | ||
project_id: Optional[uuid.UUID] = None, path: Optional[str] = None, content: bytes = b"" | ||
) -> File: | ||
if not project_id: | ||
project_id = test_project_id | ||
if not path: | ||
salt = str(uuid.uuid4()) | ||
path = f'test-file-{salt}.txt' | ||
file = await api_client.create_file(project_id, path, content) | ||
file_ids.append(file.id) | ||
return file | ||
|
||
yield make_file | ||
for file_id in file_ids: | ||
await api_client.delete_file(file_id) | ||
|
||
|
||
@pytest.fixture | ||
async def notebook_maker(api_client: APIClient, test_project_id: uuid.UUID): | ||
"""Create and cleanup Notebook files""" | ||
notebook_ids = [] | ||
|
||
async def make_notebook( | ||
project_id: Optional[uuid.UUID] = None, | ||
path: Optional[str] = None, | ||
notebook: Optional[Notebook] = None, | ||
) -> File: | ||
if not project_id: | ||
project_id = test_project_id | ||
if not path: | ||
salt = str(uuid.uuid4()) | ||
path = f'test-notebook-{salt}.ipynb' | ||
file = await api_client.create_notebook(project_id, path, notebook) | ||
notebook_ids.append(file.id) | ||
return file | ||
|
||
yield make_notebook | ||
for notebook_id in notebook_ids: | ||
await api_client.delete_file(notebook_id) |
Oops, something went wrong.