import k_ctds as ctds

from .base import TestExternalDatabase

class TestConnection(TestExternalDatabase):

    def test___doc__(self):
        self.assertEqual(
            ctds.Connection.__doc__,
            '''\
A connection to the database server.

:pep:`0249#connection-objects`
'''
        )

    def test_typeerror(self):
        self.assertRaises(TypeError, ctds.Connection)

    def test_timeout(self):
        with self.connect(timeout=1) as connection:
            with connection.cursor() as cursor:
                try:
                    cursor.execute("WAITFOR DELAY '00:00:02';SELECT @@VERSION")
                except ctds.DatabaseError as ex:
                    self.assertIn('connection timed out', str(ex))
                    self.assertEqual(ex.severity, 6)
                    self.assertEqual(ex.db_error['number'], 20003)
                    self.assertIn('connection timed out', ex.db_error['description'])
                    self.assertEqual(ex.os_error, None)
                    self.assertEqual(ex.last_message, None)
                else:
                    self.fail('.execute() did not fail as expected') # pragma: nocover

                # Subsequent operations should fail.
                try:
                    cursor.execute("SELECT 1")
                except ctds.DatabaseError:
                    pass
                else:
                    self.fail('.execute() did not fail as expected') # pragma: nocover

    def test___repr___open(self):
        with self.connect() as connection:
            r = repr(connection)
            self.assertTrue(r.startswith('<k_ctds.Connection '))
            self.assertIn("database='", r)
            self.assertIn('spid=', r)
            self.assertTrue(r.endswith('>'))
            # Verify the database matches the actual property
            self.assertIn(connection.database, r)

    def test___repr___closed(self):
        connection = self.connect()
        connection.close()
        self.assertEqual(repr(connection), '<k_ctds.Connection (closed)>')
