-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adbc ingestion #62
Open
dejandbt
wants to merge
5
commits into
main
Choose a base branch
from
adbc_ingestion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Adbc ingestion #62
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Empty file.
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,130 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock | ||
import pyarrow as pa | ||
import pandas as pd | ||
from sqlalchemy.engine import Engine, Connection | ||
|
||
|
||
from matchbox.server.postgresql.utils.db import adbc_ingest_data, _adbc_insert_data, _save_to_postgresql, _run_query, _run_queries | ||
|
||
|
||
class TestAdbcIngestData(unittest.TestCase): | ||
|
||
@patch('my_module._create_adbc_table_constraints') | ||
@patch('my_module._adbc_insert_data') | ||
@patch('my_module.datetime') | ||
def test_adbc_ingest_data(self, mock_datetime, mock_adbc_insert_data, mock_create_adbc_table_constraints): | ||
# Mock datetime | ||
mock_datetime.now.return_value.strftime.return_value = "20250101123045" | ||
|
||
# Create mock arguments | ||
clusters = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
contains = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
probabilities = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
engine = MagicMock(spec=Engine) | ||
resolution_id = 1 | ||
|
||
# Mock the engine connection context manager | ||
mock_connection = engine.connect.return_value.__enter__.return_value | ||
|
||
# Test when _adbc_insert_data returns True | ||
mock_adbc_insert_data.return_value = True | ||
mock_create_adbc_table_constraints.return_value = True | ||
result = adbc_ingest_data(clusters, contains, probabilities, engine, resolution_id) | ||
self.assertTrue(result) | ||
|
||
# Test when _adbc_insert_data returns False | ||
mock_adbc_insert_data.return_value = False | ||
result = adbc_ingest_data(clusters, contains, probabilities, engine, resolution_id) | ||
self.assertFalse(result) | ||
|
||
|
||
@patch('my_module._save_to_postgresql') | ||
@patch('my_module._run_query') | ||
@patch('my_module.adbc_driver_postgresql.dbapi.connect') | ||
def test_adbc_insert_data(self, mock_connect, mock_run_query, mock_save_to_postgresql): | ||
# Mock the connect method | ||
mock_conn = mock_connect.return_value.__enter__.return_value | ||
|
||
# Create mock arguments | ||
clusters = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
contains = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
probabilities = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
suffix = "20250101123045" | ||
alchemy_conn = MagicMock() | ||
resolution_id = 1 | ||
|
||
# Test when all queries and saves succeed | ||
mock_run_query.side_effect = [None, None, None] | ||
mock_save_to_postgresql.side_effect = [None, None, None] | ||
result = _adbc_insert_data(clusters, contains, probabilities, suffix, alchemy_conn, resolution_id) | ||
self.assertTrue(result) | ||
|
||
# Test when a query fails | ||
mock_run_query.side_effect = Exception("Query failed") | ||
result = _adbc_insert_data(clusters, contains, probabilities, suffix, alchemy_conn, resolution_id) | ||
self.assertFalse(result) | ||
|
||
# Test when save_to_postgresql fails | ||
mock_run_query.side_effect = [None, None, None] | ||
mock_save_to_postgresql.side_effect = [None, Exception("Save failed"), None] | ||
result = _adbc_insert_data(clusters, contains, probabilities, suffix, alchemy_conn, resolution_id) | ||
self.assertFalse(result) | ||
|
||
@patch('my_module.pa.RecordBatchReader.from_batches') | ||
def test_save_to_postgresql(self, mock_from_batches): | ||
# Mock the from_batches method | ||
mock_batch_reader = MagicMock() | ||
mock_from_batches.return_value = mock_batch_reader | ||
|
||
# Create mock arguments | ||
table = pa.Table.from_pandas(pd.DataFrame({"column1": [1, 2], "column2": [3, 4]})) | ||
conn = MagicMock() | ||
schema = "test_schema" | ||
table_name = "test_table" | ||
|
||
# Mock the cursor context manager | ||
mock_cursor = conn.cursor.return_value.__enter__.return_value | ||
|
||
# Call the function | ||
_save_to_postgresql(table, conn, schema, table_name) | ||
|
||
# Verify the cursor method was called correctly | ||
mock_cursor.adbc_ingest.assert_called_once_with( | ||
table_name=table_name, | ||
data=mock_batch_reader, | ||
mode="append", | ||
db_schema_name=schema, | ||
) | ||
|
||
@patch('my_module.text') | ||
def test_run_query(self, mock_text): | ||
# Create mock arguments | ||
query = "SELECT * FROM test_table" | ||
conn = MagicMock(spec=Connection) | ||
|
||
# Call the function | ||
_run_query(query, conn) | ||
|
||
# Verify the execute method was called correctly | ||
conn.execute.assert_called_once_with(mock_text(query)) | ||
conn.commit.assert_called_once() | ||
|
||
@patch('my_module.text') | ||
def test_run_queries(self, mock_text): | ||
# Create mock arguments | ||
queries = ["SELECT * FROM test_table", "DELETE FROM test_table"] | ||
conn = MagicMock(spec=Connection) | ||
|
||
# Call the function | ||
_run_queries(queries, conn) | ||
|
||
# Verify the execute method was called correctly for each query | ||
conn.begin.assert_called_once() | ||
self.assertEqual(conn.execute.call_count, len(queries)) | ||
for query in queries: | ||
conn.execute.assert_any_call(mock_text(query)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We test using pytest, though we do use unittest for mocks.