Skip to content

Commit 2883758

Browse files
committed
tests added
gitignore updated
1 parent 8775ad2 commit 2883758

5 files changed

Lines changed: 131 additions & 46 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.pytest_cache
2+
*.egg-info
23
venv
34
__pycache__
45
node_modules
56
.mypy_cache
6-
*.egg-info
7+
*.db

src/opengeodeweb_microservice/database/session.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from src.opengeodeweb_microservice.database.connection import DatabaseConnection
66

7-
# Global database instance
87
_database_connection = DatabaseConnection()
98

109

tests/conftest.py

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,49 @@
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

tests/test_connection.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pytest
2+
from src.opengeodeweb_microservice.database.session import (
3+
get_session,
4+
get_database_connection
5+
)
6+
from src.opengeodeweb_microservice.database.connection import DatabaseConnection
7+
from src.opengeodeweb_microservice.microservice.data import Data
8+
9+
10+
def test_database_connection_basic(app_context):
11+
session = get_session()
12+
assert session is not None
13+
assert session.session is not None
14+
connection = get_database_connection()
15+
assert connection is not None
16+
17+
18+
def test_data_crud_operations(app_context, clean_database):
19+
data = Data.create(
20+
geode_object="test_object",
21+
input_file="test.txt"
22+
)
23+
assert data.id is not None
24+
session = get_session()
25+
session.session.commit()
26+
retrieved = Data.get(data.id)
27+
assert retrieved is not None
28+
assert retrieved.geode_object == "test_object"
29+
non_existent = Data.get("fake_id")
30+
assert non_existent is None
31+
32+
33+
def test_data_with_additional_files(app_context, clean_database):
34+
files = ["file1.txt", "file2.txt"]
35+
data = Data.create(
36+
geode_object="test_files",
37+
additional_files=files
38+
)
39+
session = get_session()
40+
session.session.commit()
41+
retrieved = Data.get(data.id)
42+
assert retrieved.additional_files == files

tests/test_database.py

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1-
# import os
1+
import pytest
2+
from src.opengeodeweb_microservice.database.session import get_session, get_database_connection
3+
from src.opengeodeweb_microservice.microservice.data import Data
24

35

4-
# def test_database_uri_path(client):
5-
# app = client.application
6-
# with app.app_context():
7-
# base_dir = os.path.abspath(os.path.dirname(__file__))
8-
# expected_db_path = os.path.join(base_dir, "data", "project.db")
9-
# expected_uri = f"sqlite:///{expected_db_path}"
6+
def test_database_connection(app_context):
7+
session = get_session()
8+
assert session is not None
9+
assert session.session is not None
10+
connection = get_database_connection()
11+
assert connection is not None
1012

11-
# assert app.config["SQLALCHEMY_DATABASE_URI"] == expected_uri
1213

13-
# assert os.path.exists(expected_db_path)
14+
def test_data_creation_and_retrieval(app_context, clean_database):
15+
data = Data.create(
16+
geode_object="test_object",
17+
input_file="test.txt"
18+
)
19+
assert data.id is not None
20+
assert data.geode_object == "test_object"
21+
session = get_session()
22+
session.session.commit()
23+
retrieved = Data.get(data.id)
24+
assert retrieved is not None
25+
assert retrieved.geode_object == "test_object"
26+
27+
28+
def test_data_with_additional_files(app_context, clean_database):
29+
files = ["file1.txt", "file2.txt"]
30+
data = Data.create(
31+
geode_object="test_with_files",
32+
additional_files=files
33+
)
34+
session = get_session()
35+
session.session.commit()
36+
retrieved = Data.get(data.id)
37+
assert retrieved.additional_files == files
38+
39+
40+
def test_data_get_nonexistent(app_context):
41+
result = Data.get("nonexistent_id")
42+
assert result is None

0 commit comments

Comments
 (0)