Files
cwshed-clone/tests/test_pending_reports.py
2026-11-17 13:47:04 +03:00

70 lines
3.9 KiB
Python

import http.client
import json
import os
import tempfile
import threading
import unittest
from pathlib import Path
from unittest.mock import patch
from app.belki.history import CalculationStore
from app.main import BelkiHTTPServer, Handler
class PendingReportsTests(unittest.TestCase):
def test_external_poll_and_acknowledgement(self):
with tempfile.TemporaryDirectory() as directory:
store = CalculationStore(sqlite_path=Path(directory) / 'history.sqlite3')
ids = [store.save(actor_id=1, source='web', payload={'mode': 'training'},
response={'message': str(i), 'result': {}}) for i in range(503)]
server = BelkiHTTPServer(('127.0.0.1', 0), Handler)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
def request(method, path, body=None, authorized=True):
connection = http.client.HTTPConnection('127.0.0.1', server.server_port)
headers = {'Connection': 'close'}
if authorized:
headers['X-Admin-Password'] = 'test-password'
if body is not None:
headers['Content-Type'] = 'application/json'
body = json.dumps(body)
connection.request(method, path, body=body, headers=headers)
response = connection.getresponse()
result = response.status, json.loads(response.read())
connection.close()
return result
try:
with patch.dict(os.environ, {'BELKI_ADMIN_PASSWORD': 'test-password'}), patch('app.main.history_store', return_value=store):
self.assertEqual(request('GET', '/api/admin/calculations/pending', authorized=False)[0], 401)
self.assertEqual(request('PATCH', f'/api/admin/calculations/{ids[0]}/entered', {'entered': True}, False)[0], 401)
status, page = request('GET', '/api/admin/calculations/pending?limit=2')
self.assertEqual(status, 200)
self.assertEqual([item['id'] for item in page['items']], ids[:2])
self.assertEqual(page['next_after_id'], ids[1])
status, updated = request('PATCH', f'/api/admin/calculations/{ids[0]}/entered', {'entered': True})
self.assertEqual(status, 200)
self.assertTrue(updated['item']['entered'])
self.assertEqual(request('PATCH', f'/api/admin/calculations/{ids[0]}/entered', {'entered': True})[0], 200)
self.assertEqual(request('PATCH', f'/api/admin/calculations/{ids[1]}/entered', {'entered': 'false'})[0], 400)
self.assertEqual(request('PATCH', '/api/admin/calculations/99999/entered', {'entered': True})[0], 404)
self.assertEqual(request('GET', '/api/admin/calculations/pending?limit=0')[0], 400)
self.assertEqual(request('GET', '/api/admin/calculations/pending?after_id=-1')[0], 400)
self.assertEqual(request('GET', '/api/admin/calculations/pending?after_id=9223372036854775808')[0], 400)
_, page = request('GET', f'/api/admin/calculations/pending?after_id={ids[-2]}')
self.assertEqual([item['id'] for item in page['items']], ids[-1:])
self.assertIsNone(page['next_after_id'])
_, page = request('GET', '/api/admin/calculations/pending?limit=1')
self.assertEqual(page['items'][0]['id'], ids[1])
request('PATCH', f'/api/admin/calculations/{ids[0]}/entered', {'entered': False})
self.assertFalse(store.list_calculations(limit=1, sort_by='created_at', descending=False)[0]['entered'])
finally:
server.shutdown()
server.server_close()
worker.join(timeout=2)
if __name__ == '__main__':
unittest.main()