From 5406fafcf9bba2306107e7c95218806455910c45 Mon Sep 17 00:00:00 2001 From: Julian Maranan Date: Fri, 12 Jan 2024 12:41:05 +1300 Subject: [PATCH] unit test for instructions_records_to_db --- .../test_digitaltwin/data/test_data_to_db.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/test_digitaltwin/data/test_data_to_db.py diff --git a/tests/test_digitaltwin/data/test_data_to_db.py b/tests/test_digitaltwin/data/test_data_to_db.py new file mode 100644 index 000000000..2dc405cc1 --- /dev/null +++ b/tests/test_digitaltwin/data/test_data_to_db.py @@ -0,0 +1,36 @@ +import unittest +from unittest.mock import MagicMock, patch +import pandas as pd + +from src.digitaltwin.setup_environment import get_database +from src.digitaltwin.data_to_db import get_nz_geospatial_layers + +class TestDataToDB(unittest.TestCase): + @classmethod + @patch("src.digitaltwin.setup_environment.get_connection_from_profile", autospec=True) + def setUpClass(cls, mock_get_connection): + # Set up a mock database engine + mock_engine = MagicMock() + + # Mock the SQL query result + mock_query_result = pd.DataFrame({ + 'column1': [1, 2, 3], + 'column2': ['A', 'B', 'C'] + # Add more columns as needed + }) + + # Configure the mock engine to return the query result + mock_engine.execute.return_value.fetchall.return_value = mock_query_result.values + + # Mock the database connection setup function + mock_get_connection.return_value = mock_engine + + # Call the function with the mock engine + cls.dataframe_output = get_nz_geospatial_layers(mock_engine) + + def test_get_nz_geospatial_layers_correct_frame_type(self): + """Test to ensure tabular data is returned in DataFrame format.""" + self.assertIsInstance(self.dataframe_output, pd.DataFrame) + +if __name__ == '__main__': + unittest.main()