Skip to content

Commit

Permalink
Unit Tests - loki_db (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreRico committed Oct 9, 2024
1 parent 72ad713 commit a7127c9
Show file tree
Hide file tree
Showing 13 changed files with 887 additions and 202 deletions.
20 changes: 10 additions & 10 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
; [flake8]
; max-line-length = 88 # Black standard
; exclude = .git,__pycache__,build,dist # Ignore folders and files
; max-complexity = 10 # Set the maximum complexity allowed
; ignore =
; ; E203, # space before ':'
; ; W503, # line break before binary operator
; ; W191, # indentation contains tabs
; ; E101, # indentation contains mixed spaces and tabs
[flake8]
max-line-length = 88 # Black standard
exclude = .git,__pycache__,build,dist # Ignore folders and files
max-complexity = 10 # Set the maximum complexity allowed
ignore =
; E203, # space before ':'
; W503, # line break before binary operator
; W191, # indentation contains tabs
; E101, # indentation contains mixed spaces and tabs

; ; be more restrictive after the refactor code
; be more restrictive after the refactor code
41 changes: 32 additions & 9 deletions loki/loki_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ def getVersionTuple(cls):
"""
# tuple = (major,minor,revision,dev,build,date)
# dev must be in ('a','b','rc','release') for lexicographic comparison
return (2, 2, 5, "release", "", "2019-03-15") # NOTE: (ANDRE) WHAT IS THIS VERSION?
return (
2,
2,
5,
"release",
"",
"2019-03-15",
) # NOTE: (ANDRE) WHAT IS THIS VERSION?

# getVersionTuple()

Expand Down Expand Up @@ -1027,10 +1034,14 @@ def createDatabaseObjects(
idxList = (idxList,)
for tblName in tblList or schema.keys():
if doTables:
cursor.execute(
"CREATE %sTABLE IF NOT EXISTS `%s`.`%s` %s"
% (dbType, dbName, tblName, schema[tblName]["table"])
create_table_sql = "CREATE %sTABLE IF NOT EXISTS `%s`.`%s` %s" % (
dbType,
dbName,
tblName,
schema[tblName]["table"],
)
self.log(f"Executing SQL: {create_table_sql}")
cursor.execute(create_table_sql)
if "data" in schema[tblName] and schema[tblName]["data"]:
sql = "INSERT OR IGNORE INTO `%s`.`%s` VALUES (%s)" % (
dbName,
Expand All @@ -1049,12 +1060,16 @@ def createDatabaseObjects(
"ERROR: no definition for index '%s' on table '%s'"
% (idxName, tblName)
)
cursor.execute(
create_index_sql = (
"CREATE INDEX IF NOT EXISTS `%s`.`%s` ON `%s` %s"
% (dbName, idxName, tblName, schema[tblName]["index"][idxName])
)
self.log(f"Executing SQL: {create_index_sql}")
cursor.execute(create_index_sql)
# foreach idxName in idxList
cursor.execute("ANALYZE `%s`.`%s`" % (dbName, tblName))
# foreach tblName in tblList
cursor.execute("ANALYZE `%s`.`%s`" % (dbName, tblName))
# foreach tblName in tblList

# this shouldn't be necessary since we don't manually modify the sqlite_stat* tables
Expand All @@ -1075,7 +1090,7 @@ def createDatabaseTables(self, schema, dbName, tblList, doIndecies=False):
The function creates the specified tables and optionally creates indices for them.
"""
return self.createDatabsaseObjects(
return self.createDatabaseObjects(
schema, dbName, tblList, True, None, doIndecies
)

Expand Down Expand Up @@ -1178,6 +1193,7 @@ def updateDatabaseSchema(self):
"""
cursor = self._db.cursor()

# FIXME: treat when db is not defined yet (no schema)
if self.getDatabaseSetting("schema", int) < 2:
self.logPush("updating database schema to version 2 ...\n")
updateMap = {
Expand Down Expand Up @@ -1460,13 +1476,20 @@ def getDatabaseSetting(self, setting, type=None):
The setting value, cast to the specified type if provided.
"""
value = None
if self._dbFile:
if self._dbFile == "temp":
for row in self._db.cursor().execute(
"SELECT value FROM `temp`.`setting` WHERE setting = ?",
(setting,), # noqa E501
):
value = row[0]
elif self._dbFile:
for row in self._db.cursor().execute(
"SELECT value FROM `db`.`setting` WHERE setting = ?", (setting,)
"SELECT value FROM `db`.`setting` WHERE setting = ?",
(setting,), # noqa E501
):
value = row[0]
if type:
value = type(value) if (value != None) else type()
value = type(value) if (value is not None) else type()
return value

# getDatabaseSetting()
Expand Down
62 changes: 62 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import pytest
from loki.loki_db import Database

"""
Using a Memory Database for Testing
=======================================
1. Realism: You are testing with an actual database, ensuring that SQL
operations are executed just as they would in production, potentially
catching errors that mocks might miss.
2. Safety: Since the database is temporary and stored in memory, there's no
risk of corrupting real data or accidentally persisting test data.
3. Performance: In-memory databases typically have very fast performance,
making them ideal for unit tests.
4. Isolation: Each test can create its own temporary database, which
disappears automatically after the test finishes, ensuring that tests are
independent of one another.
How It Works:
-------------
- `temp_db` Fixture:
- The `pytest.fixture` creates a temporary instance of the `Database` class
connected to an in-memory database.
- The temporary database is automatically cleaned up after the test.
- Database Creation and Operation:
- During the test, you can create tables, insert, and retrieve data using
a real connection to the temporary database.
- This database only exists for the duration of the test and is
automatically deleted afterward, ensuring that the test remains
isolated and has no impact on the real environment.
"""


# Fixture for a clean in-memory database (no schema applied)
@pytest.fixture
def clean_memory_db():
temp_db = Database(tempMem=True)
yield temp_db
temp_db._db.close() # Close the connection after the test


# Fixture for an in-memory database with the schema loaded
@pytest.fixture
def schema_memory_db():
temp_db = Database(tempMem=True)
temp_db.createDatabaseObjects(
temp_db._schema["db"], "temp", list(temp_db._schema["db"].keys())
)
# setting test parameters
temp_db._dbFile = "temp"
temp_db._is_test = True
yield temp_db
temp_db._db.close()


# Fixture for a file-based database
@pytest.fixture
def file_based_db():
temp_db = Database(dbFile="tests/loki_test.db")
yield temp_db
temp_db._db.close()
6 changes: 3 additions & 3 deletions tests/loaders/test_biogrid.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

# import zipfile
# from unittest.mock import MagicMock, patch
from loki.loaders.loki_source_biogrid import Source_biogrid
Expand All @@ -25,9 +26,8 @@ def database():
# options = {}
# path = "/some/path"
# expected_files = [path + "/BIOGRID-ORGANISM-LATEST.tab2.zip"]

# result = source_biogrid.download(options, path)

# # Aqui, em vez de usar mocks, você verifica os resultados com base no banco de dados real
# assert result == expected_files

Empty file added tests/loki_db/__init__.py
Empty file.
Loading

0 comments on commit a7127c9

Please sign in to comment.