
import os
import shutil
import unittest

from pycs import app
from pycs import db
from pycs import settings
from pycs.frontend.WebServer import WebServer
from pycs.database.Model import Model
from pycs.database.LabelProvider import LabelProvider

server = None

class BaseTestCase(unittest.TestCase):
    def setUp(self, discovery: bool = True):
        global server
        app.config["TESTING"] = True
        self.projects_dir = app.config["TEST_PROJECTS_DIR"] = "test_projects"
        app.config["WTF_CSRF_ENABLED"] = False
        app.config["DEBUG"] = False
        app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///"

        db.create_all()

        self.client = app.test_client()

        # init the server once
        if server is None:
            server = WebServer(app, settings, discovery)

        elif discovery:
            # run discovery modules manually
            Model.discover("models/")
            LabelProvider.discover("labels/")

    def tearDown(self):

        if os.path.exists(self.projects_dir):
            shutil.rmtree(self.projects_dir)

        db.drop_all()

