py-scopes/tests/test_api.py

58 lines
1.8 KiB
Python

# py-scopes: tests.test_api
"""Tests for the fastapi- (and asyncio-) based stuff"""
from scopes import config
import pytest
from fastapi.testclient import TestClient
from httpx2 import ASGITransport, AsyncClient
from scopes.api import main
from scopes.api.main import app
from scopes.storage import tracking
@pytest.fixture(scope='module')
def storage():
storage = config.StorageFactory(config.settings)(config.settings.dbschema)
storage.dropTable('tracks')
storage.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):
tracks = storage.create(tracking.Container)
tr01 = tracking.Track('t01', 'john')
tr01.update(dict(activity='testing'))
trid01 = tracks.save(tr01)
storage.commit()
with TestClient(app) as clt:
caplog.set_level(config.settings.log_level)
tracks = clt.get('/tracks').json()
assert len(tracks) == 1
tr = tracks[0]
assert tr['timeStamp'].startswith('202')
tr['timeStamp'] = '...'
assert tr == {'uid': 'rec-1', 'head': {'taskId': 't01', 'userName': 'john'},
'data': {'activity': 'testing'}, 'timeStamp': '...'}
resp = clt.post('/tracks', json={'head': {'taskId': 't02', 'userName': 'jim'},
'data': {'activity': 'implement post track'}})
assert resp.status_code == 200
tr02 = resp.json()
assert tr02['uid'] == 'rec-2'
#print('\n*** Logs: ', caplog.text)
#print('*** tr02', tr02)