1 """Shared test utilities."""
2 from unittest import TestCase
3 from threading import Thread
4 from datetime import datetime
5 from os import remove as remove_file
6 from plomtask.db import DatabaseFile, DatabaseConnection
7 from plomtask.http import TaskHandler, TaskServer
10 class TestCaseWithDB(TestCase):
11 """Module tests not requiring DB setup."""
14 timestamp = datetime.now().timestamp()
15 self.db_file = DatabaseFile(f'test_db:{timestamp}')
17 self.db_conn = DatabaseConnection(self.db_file)
21 remove_file(self.db_file.path)
24 class TestCaseWithServer(TestCaseWithDB):
25 """Module tests against our HTTP server/handler (and database)."""
29 self.httpd = TaskServer(self.db_file, ('localhost', 0), TaskHandler)
30 self.server_thread = Thread(target=self.httpd.serve_forever)
31 self.server_thread.daemon = True
32 self.server_thread.start()
36 self.httpd.server_close()
37 self.server_thread.join()