|
@@ -17,6 +17,7 @@ import pandas as pd
|
|
|
import pytest
|
|
|
|
|
|
from taipy.config.common.scope import Scope
|
|
|
+from taipy.core.data.operator import JoinOperator, Operator
|
|
|
from taipy.core.data.sql_table import SQLTableDataNode
|
|
|
|
|
|
|
|
@@ -105,6 +106,103 @@ class TestReadSQLTableDataNode:
|
|
|
assert isinstance(pandas_data, pd.DataFrame)
|
|
|
assert pandas_data.equals(pd.DataFrame(self.mock_read_value()))
|
|
|
|
|
|
+ def test_build_connection_string(self):
|
|
|
+ sql_properties = {
|
|
|
+ "db_username": "sa",
|
|
|
+ "db_password": "Passw0rd",
|
|
|
+ "db_name": "taipy",
|
|
|
+ "db_engine": "mssql",
|
|
|
+ "table_name": "example",
|
|
|
+ "db_driver": "default server",
|
|
|
+ "db_extra_args": {
|
|
|
+ "TrustServerCertificate": "yes",
|
|
|
+ "other": "value",
|
|
|
+ },
|
|
|
+ }
|
|
|
+ custom_properties = sql_properties.copy()
|
|
|
+ mssql_sql_data_node = SQLTableDataNode(
|
|
|
+ "foo",
|
|
|
+ Scope.SCENARIO,
|
|
|
+ properties=custom_properties,
|
|
|
+ )
|
|
|
+ assert (
|
|
|
+ mssql_sql_data_node._conn_string()
|
|
|
+ == "mssql+pyodbc://sa:Passw0rd@localhost:1433/taipy?TrustServerCertificate=yes&other=value&driver=default+server"
|
|
|
+ )
|
|
|
+
|
|
|
+ custom_properties["db_engine"] = "mysql"
|
|
|
+ mysql_sql_data_node = SQLTableDataNode(
|
|
|
+ "foo",
|
|
|
+ Scope.SCENARIO,
|
|
|
+ properties=custom_properties,
|
|
|
+ )
|
|
|
+ assert (
|
|
|
+ mysql_sql_data_node._conn_string()
|
|
|
+ == "mysql+pymysql://sa:Passw0rd@localhost:1433/taipy?TrustServerCertificate=yes&other=value&driver=default+server"
|
|
|
+ )
|
|
|
+
|
|
|
+ custom_properties["db_engine"] = "postgresql"
|
|
|
+ postgresql_sql_data_node = SQLTableDataNode(
|
|
|
+ "foo",
|
|
|
+ Scope.SCENARIO,
|
|
|
+ properties=custom_properties,
|
|
|
+ )
|
|
|
+ assert (
|
|
|
+ postgresql_sql_data_node._conn_string()
|
|
|
+ == "postgresql+psycopg2://sa:Passw0rd@localhost:1433/taipy?TrustServerCertificate=yes&other=value&driver=default+server"
|
|
|
+ )
|
|
|
+
|
|
|
+ custom_properties["db_engine"] = "sqlite"
|
|
|
+ sqlite_sql_data_node = SQLTableDataNode(
|
|
|
+ "foo",
|
|
|
+ Scope.SCENARIO,
|
|
|
+ properties=custom_properties,
|
|
|
+ )
|
|
|
+ assert sqlite_sql_data_node._conn_string() == "sqlite:///taipy.db"
|
|
|
+
|
|
|
+ @pytest.mark.parametrize("sql_properties", __sql_properties)
|
|
|
+ def test_get_read_query(self, sql_properties):
|
|
|
+ custom_properties = sql_properties.copy()
|
|
|
+
|
|
|
+ sql_data_node = SQLTableDataNode(
|
|
|
+ "foo",
|
|
|
+ Scope.SCENARIO,
|
|
|
+ properties=custom_properties,
|
|
|
+ )
|
|
|
+
|
|
|
+ assert sql_data_node._get_read_query(("key", 1, Operator.EQUAL)) == "SELECT * FROM example WHERE key = '1'"
|
|
|
+ assert sql_data_node._get_read_query(("key", 1, Operator.NOT_EQUAL)) == "SELECT * FROM example WHERE key <> '1'"
|
|
|
+ assert (
|
|
|
+ sql_data_node._get_read_query(("key", 1, Operator.GREATER_THAN)) == "SELECT * FROM example WHERE key > '1'"
|
|
|
+ )
|
|
|
+ assert (
|
|
|
+ sql_data_node._get_read_query(("key", 1, Operator.GREATER_OR_EQUAL))
|
|
|
+ == "SELECT * FROM example WHERE key >= '1'"
|
|
|
+ )
|
|
|
+ assert sql_data_node._get_read_query(("key", 1, Operator.LESS_THAN)) == "SELECT * FROM example WHERE key < '1'"
|
|
|
+ assert (
|
|
|
+ sql_data_node._get_read_query(("key", 1, Operator.LESS_OR_EQUAL))
|
|
|
+ == "SELECT * FROM example WHERE key <= '1'"
|
|
|
+ )
|
|
|
+
|
|
|
+ with pytest.raises(NotImplementedError):
|
|
|
+ sql_data_node._get_read_query(
|
|
|
+ [("key", 1, Operator.EQUAL), ("key2", 2, Operator.GREATER_THAN)], "SOME JoinOperator"
|
|
|
+ )
|
|
|
+
|
|
|
+ assert (
|
|
|
+ sql_data_node._get_read_query(
|
|
|
+ [("key", 1, Operator.EQUAL), ("key2", 2, Operator.GREATER_THAN)], JoinOperator.AND
|
|
|
+ )
|
|
|
+ == "SELECT * FROM example WHERE key = '1' AND key2 > '2'"
|
|
|
+ )
|
|
|
+ assert (
|
|
|
+ sql_data_node._get_read_query(
|
|
|
+ [("key", 1, Operator.EQUAL), ("key2", 2, Operator.GREATER_THAN)], JoinOperator.OR
|
|
|
+ )
|
|
|
+ == "SELECT * FROM example WHERE key = '1' OR key2 > '2'"
|
|
|
+ )
|
|
|
+
|
|
|
@pytest.mark.parametrize("sql_properties", __sql_properties)
|
|
|
def test_read_numpy(self, sql_properties):
|
|
|
custom_properties = sql_properties.copy()
|