import unittest
from ..encoder import JSONEncoder
import logging
import os, shutil, tempfile
import unittest

import connexion
import flask_testing

from jass.models.inittable import create_inittable_file


class JassTestCase(unittest.TestCase):
    @classmethod
    def get_file_path_fn(cls, test_file_name):
        return os.path.join(
            os.path.dirname(os.path.abspath(__file__)), cls.test_folder, test_file_name
        )


class JassFlaskTestCase(JassTestCase, flask_testing.TestCase):
    def create_app(self):
        logging.getLogger("connexion.operation").setLevel("ERROR")
        from jass.config import config

        self.test_dir = tempfile.mkdtemp()
        config["DATA_DIR"] = self.test_dir
        shutil.copy(self.get_file_path_fn("initTable.hdf5"), self.test_dir)
        app = connexion.App(__name__, specification_dir="../swagger/")
        app.app.json_encoder = JSONEncoder
        app.add_api("swagger.yaml")
        return app.app

    def tearDown(self):
        shutil.rmtree(self.test_dir)
