|
1 | | -# # Standard library imports |
2 | | -# import time |
3 | | -# import shutil |
4 | | - |
5 | | -# # Third party imports |
6 | | -# import os |
7 | | -# import pytest |
8 | | - |
9 | | -# # Local application imports |
10 | | -# from app import app |
11 | | -# from src.opengeodeweb_microservice.database import initialize_database |
12 | | - |
13 | | - |
14 | | -# @pytest.fixture(scope="session", autouse=True) |
15 | | -# def copy_data(): |
16 | | -# app.config["DATA_FOLDER_PATH"] = "./data/" |
17 | | -# BASE_DIR = os.path.abspath(os.path.dirname(__file__)) |
18 | | -# db_path = os.path.join(BASE_DIR, "data", "project.db") |
19 | | -# app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_path}" |
20 | | -# initialize_database(app) |
21 | | - |
22 | | - |
23 | | -# @pytest.fixture |
24 | | -# def client(): |
25 | | -# app.config["REQUEST_COUNTER"] = 0 |
26 | | -# app.config["LAST_REQUEST_TIME"] = time.time() |
27 | | -# client = app.test_client() |
28 | | -# client.headers = {"Content-type": "application/json", "Accept": "application/json"} |
29 | | -# yield client |
30 | | - |
31 | | - |
32 | | -# @pytest.fixture |
33 | | -# def app_context(): |
34 | | -# with app.app_context(): |
35 | | -# yield |
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from flask import Flask |
| 4 | +from src.opengeodeweb_microservice.database.session import init_database, get_session |
| 5 | +from src.opengeodeweb_microservice.microservice.data import Data |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope="session") |
| 9 | +def app(): |
| 10 | + app = Flask(__name__) |
| 11 | + app.config["TESTING"] = True |
| 12 | + |
| 13 | + BASE_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 14 | + db_path = os.path.join(BASE_DIR, "test_project.db") |
| 15 | + app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{db_path}" |
| 16 | + |
| 17 | + with app.app_context(): |
| 18 | + db = init_database(app, "test_project.db") |
| 19 | + yield app |
| 20 | + try: |
| 21 | + session = get_session() |
| 22 | + if session: |
| 23 | + session.session.close() |
| 24 | + if hasattr(session, 'engine'): |
| 25 | + session.engine.dispose() |
| 26 | + except Exception: |
| 27 | + pass |
| 28 | + import time |
| 29 | + time.sleep(0.1) |
| 30 | + if os.path.exists(db_path): |
| 31 | + try: |
| 32 | + os.remove(db_path) |
| 33 | + except PermissionError: |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def app_context(app): |
| 39 | + with app.app_context(): |
| 40 | + yield |
| 41 | + |
| 42 | + |
| 43 | +@pytest.fixture |
| 44 | +def clean_database(app_context): |
| 45 | + session = get_session() |
| 46 | + if session: |
| 47 | + session.session.query(Data).delete() |
| 48 | + session.session.commit() |
| 49 | + yield |
0 commit comments