home · contact · privacy
Add Processes module tests, re-organize/re-factor unit tests.
[plomtask] / tests / utils.py
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
8
9
10 class TestCaseWithDB(TestCase):
11     """Module tests not requiring DB setup."""
12
13     def setUp(self):
14         timestamp = datetime.now().timestamp()
15         self.db_file = DatabaseFile(f'test_db:{timestamp}')
16         self.db_file.remake()
17         self.db_conn = DatabaseConnection(self.db_file)
18
19     def tearDown(self):
20         self.db_conn.close()
21         remove_file(self.db_file.path)
22
23
24 class TestCaseWithServer(TestCaseWithDB):
25     """Module tests against our HTTP server/handler (and database)."""
26
27     def setUp(self):
28         super().setUp()
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()
33
34     def tearDown(self):
35         self.httpd.shutdown()
36         self.httpd.server_close()
37         self.server_thread.join()
38         super().tearDown()