forked from pymssql/pymssql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_context_managers.py
44 lines (32 loc) · 1.3 KB
/
test_context_managers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
Test context managers -- i.e.: the `with` statement
"""
try:
import unittest2 as unittest
except ImportError:
import unittest
from pymssql import InterfaceError
from .helpers import pymssqlconn, mssqlconn
class TestContextManagers(unittest.TestCase):
def test_pymssql_Connection_with(self):
with pymssqlconn() as conn:
cursor = conn.cursor()
cursor.execute("SELECT @@version AS version")
self.assertIsNotNone(conn._conn)
with self.assertRaises(InterfaceError) as context:
self.assertIsNotNone(conn._conn)
self.assertEqual(str(context.exception), "Connection is closed.")
def test_pymssql_Cursor_with(self):
conn = pymssqlconn()
with conn.cursor() as cursor:
cursor.execute("SELECT @@version AS version")
self.assertIsNotNone(conn._conn)
self.assertIsNotNone(cursor)
with self.assertRaises(InterfaceError) as context:
cursor.execute("SELECT @@version AS version")
self.assertEqual(str(context.exception), "Cursor is closed.")
def test_mssql_Connection_with(self):
with mssqlconn() as conn:
conn.execute_query("SELECT @@version AS version")
self.assertTrue(conn.connected)
self.assertFalse(conn.connected)