import unittest

from use_cx_oracle.storage.OracleDb import OracleDb
from use_cx_oracle.ResultBag import ResultBag


# NOTE: This doesn't actually test cursor.execute, just connect and
# makedsn.
class TestOracleDb(unittest.TestCase):
    def setUp(self):
        self.user = "MAKE_IT_FAIL"
        self.password = "BAD_PASS"
        self.host = "BAD_HOST"
        self.port = "1521"
        self.sid = ""
        self.storage = OracleDb()

    def tearDown(self):
        pass

    def test_execute_query_no_query(self):
        foo = self.storage.execute_query(
            self.user,
            self.password,
            self.host,
            self.port,
            self.sid,
            query="",
            args={},
        )
        self.assertTrue(type(foo) == ResultBag)
        self.assertTrue(
            foo.error is not None and "ORA-12545" in str(foo.error),
            f"Error: {foo.error}",
        )

    def test_execute_query_count_user_tables(self):
        foo = self.storage.execute_query(
            self.user,
            self.password,
            self.host,
            self.port,
            self.sid,
            query="select count(table_name) from user_tables",
            args={},
        )
        self.assertTrue(type(foo) == ResultBag)
        self.assertTrue(
            foo.error is not None and "ORA-12545" in str(foo.error),
            f"Error: {foo.error}",
        )

    def test_execute_query_column_names_and_data_types(self):
        foo = self.storage.execute_query(
            self.user,
            self.password,
            self.host,
            self.port,
            self.sid,
            query=(
                "select column_name, data_type "
                "from all_tab_columns "
                "where table_name like :my_table_name"
            ),
            args={"my_table_name": "foo"},
        )
        self.assertTrue(type(foo) == ResultBag)
        self.assertTrue(
            foo.error is not None and "ORA-12545" in str(foo.error),
            f"Error: {foo.error}",
        )
