--- /dev/null
+#!/usr/bin/python3
+
+import configparser
+import os
+import socket
+import tempfile
+import unittest
+
+from buildservice import Backend
+from buildservice import database
+from buildservice import misc
+
+class TestCase(unittest.IsolatedAsyncioTestCase):
+ """
+ This is the base class for all tests
+ """
+ def source_path(self, path):
+ try:
+ source_path = os.environ["abs_top_srcdir"]
+ except KeyError as e:
+ raise ValueError("source path is not set") from e
+
+ return os.path.join(source_path, path)
+
+ def _setup_database(self):
+ """
+ Creates a new database and imports the default schema
+ """
+ # Path to the schema
+ schema = self.source_path("src/database.sql")
+
+ # Load the schema
+ with open(schema) as f:
+ schema = f.read()
+
+ # Database Server & Name
+ host = os.environ.get("DB_HOST", socket.gethostname())
+ name = os.environ.get("DB_NAME", "pakfiretest")
+
+ # Credentials
+ username = os.environ.get("DB_USER", "pakfiretest")
+ password = os.environ.get("DB_PASS", "pakfiretest")
+
+ # Connect to the database
+ db = database.Connection(host, name, username, password)
+
+ with db.transaction():
+ # Drop anything existing
+ db.execute("DROP SCHEMA public CASCADE")
+
+ # Re-create the schema
+ db.execute("CREATE SCHEMA public")
+ db.execute(schema)
+
+ # Return the credentials
+ return {
+ "name" : name,
+ "hostname" : host,
+ "user" : username,
+ "password" : password,
+ }
+
+ def setUp(self):
+ # Create a configuration file
+ conf = configparser.ConfigParser()
+
+ # Setup the database
+ conf["database"] = self._setup_database()
+
+ with tempfile.NamedTemporaryFile("w") as f:
+ # Write configuration
+ conf.write(f)
+
+ # Initialize the backend
+ self.backend = Backend(f.name)