py-scopes/tests/test_api.py

56 lines
1.6 KiB
Python

# py-scopes: tests.test_api
"""Tests for the fastapi- (and asyncio-) based stuff"""
from scopes import testing
import os, sys
import config
from scopes.storage import tracking
import transaction
import pytest
from fastapi.testclient import TestClient
from httpx2 import ASGITransport, AsyncClient
from scopes.api import main
from scopes.api.main import app
@pytest.fixture(scope='module')
def storage():
#storage = config.StorageFactory(config)(config.dbschema)
storage = main.storage()
storage.dropTable('tracks')
tracks = storage.create(tracking.Container)
tr01 = tracking.Track('t01', 'john')
tr01.update(dict(activity='testing'))
trid01 = tracks.save(tr01)
transaction.commit()
return storage
def async_client():
return AsyncClient(transport=ASGITransport(app=app), base_url='http://test')
#@pytest.mark.anyio
#async def test_root():
#async with async_client() as clt:
#response = await clt.get('/')
def test_root():
with TestClient(app) as clt:
response = clt.get('/')
assert response.status_code == 200
assert response.json() == {'Hello': 'World'}
def test_tracking(storage, caplog):
with TestClient(app) as clt:
caplog.set_level(config.settings.log_level)
tracks = clt.get('/tracks').json()
assert len(tracks) == 1
tr = tracks[0]
tr['timeStamp'] = '...'
assert tr == {'uid': 'rec-1', 'head': {'taskId': 't01', 'userName': 'john'},
'data': {'activity': 'testing'}, 'timeStamp': '...'}
#print('\nLogs: ', caplog.text)