|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch, MagicMock |
| 3 | +import pyodbc |
| 4 | +import pandas as pd |
| 5 | +from mindsql.databases.sqlserver import SQLServer, ERROR_WHILE_RUNNING_QUERY, ERROR_CONNECTING_TO_DB_CONSTANT, \ |
| 6 | + INVALID_DB_CONNECTION_OBJECT, CONNECTION_ESTABLISH_ERROR_CONSTANT |
| 7 | +from mindsql.databases.sqlserver import log as logger |
| 8 | + |
| 9 | + |
| 10 | +class TestSQLServer(unittest.TestCase): |
| 11 | + |
| 12 | + @patch('mindsql.databases.sqlserver.pyodbc.connect') |
| 13 | + def test_create_connection_success(self, mock_connect): |
| 14 | + mock_connect.return_value = MagicMock(spec=pyodbc.Connection) |
| 15 | + connection = SQLServer.create_connection( |
| 16 | + 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=server_name;DATABASE=database_name;UID=user;PWD=password') |
| 17 | + self.assertIsInstance(connection, pyodbc.Connection) |
| 18 | + |
| 19 | + @patch('mindsql.databases.sqlserver.pyodbc.connect') |
| 20 | + def test_create_connection_failure(self, mock_connect): |
| 21 | + mock_connect.side_effect = pyodbc.Error('Connection failed') |
| 22 | + with self.assertLogs(logger, level='ERROR') as cm: |
| 23 | + connection = SQLServer.create_connection( |
| 24 | + 'DRIVER={ODBC Driver 17 for SQL Server};SERVER=server_name;DATABASE=database_name;UID=user;PWD=password') |
| 25 | + self.assertIsNone(connection) |
| 26 | + self.assertTrue(any( |
| 27 | + ERROR_CONNECTING_TO_DB_CONSTANT.format("SQL Server", 'Connection failed') in message for message in |
| 28 | + cm.output)) |
| 29 | + |
| 30 | + @patch('mindsql.databases.sqlserver.pyodbc.connect') |
| 31 | + def test_execute_sql_success(self, mock_connect): |
| 32 | + # Mock the connection and cursor |
| 33 | + mock_connection = MagicMock(spec=pyodbc.Connection) |
| 34 | + mock_cursor = MagicMock() |
| 35 | + |
| 36 | + mock_connect.return_value = mock_connection |
| 37 | + mock_connection.cursor.return_value = mock_cursor |
| 38 | + |
| 39 | + # Mock cursor behavior |
| 40 | + mock_cursor.execute.return_value = None |
| 41 | + mock_cursor.description = [('column1',), ('column2',)] |
| 42 | + mock_cursor.fetchall.return_value = [(1, 'a'), (2, 'b')] |
| 43 | + |
| 44 | + connection = SQLServer.create_connection('fake_connection_string') |
| 45 | + sql = "SELECT * FROM table" |
| 46 | + sql_server = SQLServer() |
| 47 | + result = sql_server.execute_sql(connection, sql) |
| 48 | + expected_df = pd.DataFrame(data=[(1, 'a'), (2, 'b')], columns=['column1', 'column2']) |
| 49 | + pd.testing.assert_frame_equal(result, expected_df) |
| 50 | + |
| 51 | + @patch('mindsql.databases.sqlserver.pyodbc.connect') |
| 52 | + def test_execute_sql_failure(self, mock_connect): |
| 53 | + # Mock the connection and cursor |
| 54 | + mock_connection = MagicMock(spec=pyodbc.Connection) |
| 55 | + mock_cursor = MagicMock() |
| 56 | + |
| 57 | + mock_connect.return_value = mock_connection |
| 58 | + mock_connection.cursor.return_value = mock_cursor |
| 59 | + mock_cursor.execute.side_effect = pyodbc.Error('Query failed') |
| 60 | + |
| 61 | + connection = SQLServer.create_connection('fake_connection_string') |
| 62 | + sql = "SELECT * FROM table" |
| 63 | + sql_server = SQLServer() |
| 64 | + |
| 65 | + with self.assertLogs(logger, level='ERROR') as cm: |
| 66 | + result = sql_server.execute_sql(connection, sql) |
| 67 | + self.assertIsNone(result) |
| 68 | + self.assertTrue(any(ERROR_WHILE_RUNNING_QUERY.format('Query failed') in message for message in cm.output)) |
| 69 | + |
| 70 | + @patch('mindsql.databases.sqlserver.pyodbc.connect') |
| 71 | + def test_get_databases_success(self, mock_connect): |
| 72 | + # Mock the connection and cursor |
| 73 | + mock_connection = MagicMock(spec=pyodbc.Connection) |
| 74 | + mock_cursor = MagicMock() |
| 75 | + |
| 76 | + mock_connect.return_value = mock_connection |
| 77 | + mock_connection.cursor.return_value = mock_cursor |
| 78 | + |
| 79 | + # Mock cursor behavior |
| 80 | + mock_cursor.execute.return_value = None |
| 81 | + mock_cursor.fetchall.return_value = [('database1',), ('database2',)] |
| 82 | + |
| 83 | + connection = SQLServer.create_connection('fake_connection_string') |
| 84 | + sql_server = SQLServer() |
| 85 | + result = sql_server.get_databases(connection) |
| 86 | + self.assertEqual(result, ['database1', 'database2']) |
| 87 | + |
| 88 | + @patch('mindsql.databases.sqlserver.pyodbc.connect') |
| 89 | + def test_get_databases_failure(self, mock_connect): |
| 90 | + # Mock the connection and cursor |
| 91 | + mock_connection = MagicMock(spec=pyodbc.Connection) |
| 92 | + mock_cursor = MagicMock() |
| 93 | + |
| 94 | + mock_connect.return_value = mock_connection |
| 95 | + mock_connection.cursor.return_value = mock_cursor |
| 96 | + mock_cursor.execute.side_effect = pyodbc.Error('Query failed') |
| 97 | + |
| 98 | + connection = SQLServer.create_connection('fake_connection_string') |
| 99 | + sql_server = SQLServer() |
| 100 | + |
| 101 | + with self.assertLogs(logger, level='ERROR') as cm: |
| 102 | + result = sql_server.get_databases(connection) |
| 103 | + self.assertEqual(result, []) |
| 104 | + self.assertTrue(any(ERROR_WHILE_RUNNING_QUERY.format('Query failed') in message for message in cm.output)) |
| 105 | + |
| 106 | + @patch('mindsql.databases.sqlserver.SQLServer.execute_sql') |
| 107 | + def test_get_table_names_success(self, mock_execute_sql): |
| 108 | + mock_execute_sql.return_value = pd.DataFrame(data=[('schema1.table1',), ('schema2.table2',)], |
| 109 | + columns=['table_name']) |
| 110 | + |
| 111 | + connection = MagicMock(spec=pyodbc.Connection) |
| 112 | + sql_server = SQLServer() |
| 113 | + result = sql_server.get_table_names(connection, 'database_name') |
| 114 | + expected_df = pd.DataFrame(data=[('schema1.table1',), ('schema2.table2',)], columns=['table_name']) |
| 115 | + pd.testing.assert_frame_equal(result, expected_df) |
| 116 | + |
| 117 | + @patch('mindsql.databases.sqlserver.SQLServer.execute_sql') |
| 118 | + def test_get_all_ddls_success(self, mock_execute_sql): |
| 119 | + mock_execute_sql.side_effect = [ |
| 120 | + pd.DataFrame(data=[('schema1.table1',)], columns=['table_name']), |
| 121 | + pd.DataFrame(data=['CREATE TABLE schema1.table1 (...);'], columns=['SQLQuery']) |
| 122 | + ] |
| 123 | + |
| 124 | + connection = MagicMock(spec=pyodbc.Connection) |
| 125 | + sql_server = SQLServer() |
| 126 | + result = sql_server.get_all_ddls(connection, 'database_name') |
| 127 | + |
| 128 | + expected_df = pd.DataFrame(data=[{'Table': 'schema1.table1', 'DDL': 'CREATE TABLE schema1.table1 (...);'}]) |
| 129 | + pd.testing.assert_frame_equal(result, expected_df) |
| 130 | + |
| 131 | + def test_validate_connection_success(self): |
| 132 | + connection = MagicMock(spec=pyodbc.Connection) |
| 133 | + sql_server = SQLServer() |
| 134 | + # Should not raise any exception |
| 135 | + sql_server.validate_connection(connection) |
| 136 | + |
| 137 | + def test_validate_connection_failure(self): |
| 138 | + sql_server = SQLServer() |
| 139 | + |
| 140 | + with self.assertRaises(ValueError) as cm: |
| 141 | + sql_server.validate_connection(None) |
| 142 | + self.assertEqual(str(cm.exception), CONNECTION_ESTABLISH_ERROR_CONSTANT) |
| 143 | + |
| 144 | + with self.assertRaises(ValueError) as cm: |
| 145 | + sql_server.validate_connection("InvalidConnectionObject") |
| 146 | + self.assertEqual(str(cm.exception), INVALID_DB_CONNECTION_OBJECT.format("SQL Server")) |
| 147 | + |
| 148 | + @patch('mindsql.databases.sqlserver.SQLServer.execute_sql') |
| 149 | + def test_get_ddl_success(self, mock_execute_sql): |
| 150 | + mock_execute_sql.return_value = pd.DataFrame(data=['CREATE TABLE schema1.table1 (...);'], columns=['SQLQuery']) |
| 151 | + |
| 152 | + connection = MagicMock(spec=pyodbc.Connection) |
| 153 | + sql_server = SQLServer() |
| 154 | + result = sql_server.get_ddl(connection, 'schema1.table1') |
| 155 | + self.assertEqual(result, 'CREATE TABLE schema1.table1 (...);') |
| 156 | + |
| 157 | + def test_get_dialect(self): |
| 158 | + sql_server = SQLServer() |
| 159 | + self.assertEqual(sql_server.get_dialect(), 'tsql') |
| 160 | + |
| 161 | + |
| 162 | +if __name__ == '__main__': |
| 163 | + unittest.main() |
0 commit comments