-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from alice-biometrics/feature/sql-executor-an…
…d-multithreading-sql-session-scope Feature/sql executor and multithreading sql session scope
- Loading branch information
Showing
10 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Callable, List | ||
|
||
from sqlalchemy import text | ||
|
||
|
||
class SqlExecutor: | ||
def __init__(self, session_scope: Callable): | ||
self.session_scope = session_scope | ||
|
||
def _get_command(self, statement: str): | ||
command = text(statement.rstrip("\n")) | ||
return command | ||
|
||
def execute_from_filename(self, filename: str): | ||
with open(filename) as file: | ||
statements = file.read().split(";") | ||
self.execute_statements(statements) | ||
|
||
def execute_statement(self, statement: str): | ||
with self.session_scope() as session: | ||
command = self._get_command(statement) | ||
session.execute(command) | ||
|
||
def execute_statements(self, statements: List[str]): | ||
with self.session_scope() as session: | ||
for statement in statements: | ||
command = self._get_command(statement) | ||
session.execute(command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
tests/modules/persistence/integration/test_sql_executor.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import pytest | ||
|
||
from petisco import SqliteDatabase, SqliteConnection, Persistence, SqlExecutor | ||
from tests.modules.persistence.mother.model_filename_mother import ModelFilenameMother | ||
|
||
|
||
@pytest.mark.integration | ||
def test_should_sql_executor_insert_statement(): | ||
filename = ModelFilenameMother.get("sql/persistence.sql.models.yml") | ||
connection = SqliteConnection.create( | ||
server_name="sqlite", database_name="petisco.db" | ||
) | ||
database = SqliteDatabase( | ||
name="sqlite_test", connection=connection, model_filename=filename | ||
) | ||
|
||
persistence = Persistence() | ||
persistence.add(database) | ||
persistence.create() | ||
|
||
session_scope = Persistence.get_session_scope("sqlite_test") | ||
sql_executor = SqlExecutor(session_scope) | ||
|
||
sql_executor.execute_statement( | ||
'INSERT INTO Client (client_id,name) VALUES ("65dd83ef-d315-417d-bfa8-1ab398e16f02","myclient")' | ||
) | ||
sql_executor.execute_statement( | ||
'DELETE FROM Client WHERE client_id=="65dd83ef-d315-417d-bfa8-1ab398e16f02";' | ||
) | ||
|
||
persistence.clear_data() | ||
persistence.delete() | ||
Persistence.clear() | ||
|
||
|
||
@pytest.mark.integration | ||
def test_should_sql_executor_from_filename_with_statement(): | ||
filename = ModelFilenameMother.get("sql/persistence.sql.models.yml") | ||
connection = SqliteConnection.create( | ||
server_name="sqlite", database_name="petisco.db" | ||
) | ||
database = SqliteDatabase( | ||
name="sqlite_test", connection=connection, model_filename=filename | ||
) | ||
|
||
persistence = Persistence() | ||
persistence.add(database) | ||
persistence.create() | ||
|
||
session_scope = Persistence.get_session_scope("sqlite_test") | ||
sql_executor = SqlExecutor(session_scope) | ||
|
||
sql_executor.execute_from_filename( | ||
"tests/modules/persistence/sql/client_create.sql" | ||
) | ||
sql_executor.execute_from_filename( | ||
"tests/modules/persistence/sql/client_delete.sql" | ||
) | ||
|
||
persistence.clear_data() | ||
persistence.delete() | ||
Persistence.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
INSERT INTO Client (client_id,name) VALUES ("65dd83ef-d315-417d-bfa8-1ab398e16f02","myclient-bad"); | ||
UPDATE Client SET name="myclient" WHERE client_id="65dd83ef-d315-417d-bfa8-1ab398e16f02" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DELETE FROM Client WHERE name=="myclient" |