def test_owner_database_pairs(self, identifier, expected_schema, expected_owner, use_stmt): dialect = mssql.dialect() schema, owner = base._owner_plus_db(dialect, identifier) eq_(owner, expected_owner) eq_(schema, expected_schema) mock_connection = mock.Mock( dialect=dialect, exec_driver_sql=mock.Mock(return_value=mock.Mock(scalar=mock.Mock( return_value="Some Database"))), ) mock_lambda = mock.Mock() base._switch_db(schema, mock_connection, mock_lambda, "x", y="bar") if schema is None: eq_(mock_connection.mock_calls, []) else: eq_( mock_connection.mock_calls, [ mock.call.exec_driver_sql("select db_name()"), mock.call.exec_driver_sql(use_stmt), mock.call.exec_driver_sql("use [Some Database]"), ], ) eq_( mock_connection.exec_driver_sql.return_value.mock_calls, [mock.call.scalar()], ) eq_(mock_lambda.mock_calls, [mock.call("x", y="bar")])
def test_owner_database_pairs_switch_for_different_db(self): dialect = mssql.dialect() identifier = "my_other_db.some_schema" schema, owner = base._owner_plus_db(dialect, identifier) mock_connection = mock.Mock( dialect=dialect, exec_driver_sql=mock.Mock(return_value=mock.Mock(scalar=mock.Mock( return_value="my_db"))), ) mock_lambda = mock.Mock() base._switch_db(schema, mock_connection, mock_lambda, "x", y="bar") eq_( mock_connection.mock_calls, [ mock.call.exec_driver_sql("select db_name()"), mock.call.exec_driver_sql("use my_other_db"), mock.call.exec_driver_sql("use my_db"), ], eq_( mock_connection.exec_driver_sql.return_value.mock_calls, [mock.call.scalar()], ), ) eq_(mock_lambda.mock_calls, [mock.call("x", y="bar")])
def test_default_schema_name_not_interpreted_as_tokenized(self): dialect = mssql.dialect() dialect.server_version_info = base.MS_2014_VERSION mock_connection = mock.Mock(scalar=lambda sql: "Jonah.The.Whale") schema_name = dialect._get_default_schema_name(mock_connection) eq_(schema_name, "Jonah.The.Whale") eq_( base._owner_plus_db(dialect, schema_name), (None, "Jonah.The.Whale"), )
def test_owner_database_pairs_dont_use_for_same_db(self): dialect = mssql.dialect() identifier = "my_db.some_schema" schema, owner = base._owner_plus_db(dialect, identifier) mock_connection = mock.Mock(dialect=dialect, scalar=mock.Mock(return_value="my_db")) mock_lambda = mock.Mock() base._switch_db(schema, mock_connection, mock_lambda, "x", y="bar") eq_(mock_connection.mock_calls, [mock.call.scalar("select db_name()")]) eq_(mock_lambda.mock_calls, [mock.call("x", y="bar")])
def test_owner_database_pairs(self): dialect = mssql.dialect() for identifier, expected_schema, expected_owner in [ ("foo", None, "foo"), ("foo.bar", "foo", "bar"), ("Foo.Bar", "Foo", "Bar"), ("[Foo.Bar]", None, "Foo.Bar"), ("[Foo.Bar].[bat]", "Foo.Bar", "bat"), ]: schema, owner = base._owner_plus_db(dialect, identifier) eq_(owner, expected_owner) eq_(schema, expected_schema)
def test_owner_database_pairs(self): dialect = mssql.dialect() for identifier, expected_schema, expected_owner, use_stmt in [ ("foo", None, "foo", "use foo"), ("foo.bar", "foo", "bar", "use foo"), ("Foo.Bar", "Foo", "Bar", "use [Foo]"), ("[Foo.Bar]", None, "Foo.Bar", "use [Foo].[Bar]"), ("[Foo.Bar].[bat]", "Foo.Bar", "bat", "use [Foo].[Bar]"), ( "[foo].]do something; select [foo", "foo", "do something; select foo", "use foo", ), ( "something; select [foo].bar", "something; select foo", "bar", "use [something; select foo]", ), ]: schema, owner = base._owner_plus_db(dialect, identifier) eq_(owner, expected_owner) eq_(schema, expected_schema) mock_connection = mock.Mock( dialect=dialect, exec_driver_sql=mock.Mock(return_value=mock.Mock( scalar=mock.Mock(return_value="Some ] Database"))), ) mock_lambda = mock.Mock() base._switch_db(schema, mock_connection, mock_lambda, "x", y="bar") if schema is None: eq_(mock_connection.mock_calls, []) else: eq_( mock_connection.mock_calls, [ mock.call.exec_driver_sql("select db_name()"), mock.call.exec_driver_sql(use_stmt), mock.call.exec_driver_sql("use [Some Database]"), ], ) eq_( mock_connection.exec_driver_sql.return_value.mock_calls, [mock.call.scalar()], ) eq_(mock_lambda.mock_calls, [mock.call("x", y="bar")])