File: test_nextset.py

package info (click to toggle)
python-pymysql 1.0.2-2%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 860 kB
  • sloc: python: 6,123; makefile: 153; sh: 44
file content (83 lines) | stat: -rw-r--r-- 2,547 bytes parent folder | download
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pytest

import pymysql
from pymysql.tests import base
from pymysql.constants import CLIENT


class TestNextset(base.PyMySQLTestCase):
    def test_nextset(self):
        con = self.connect(
            init_command='SELECT "bar"; SELECT "baz"',
            client_flag=CLIENT.MULTI_STATEMENTS,
        )
        cur = con.cursor()
        cur.execute("SELECT 1; SELECT 2;")
        self.assertEqual([(1,)], list(cur))

        r = cur.nextset()
        self.assertTrue(r)

        self.assertEqual([(2,)], list(cur))
        self.assertIsNone(cur.nextset())

    def test_skip_nextset(self):
        cur = self.connect(client_flag=CLIENT.MULTI_STATEMENTS).cursor()
        cur.execute("SELECT 1; SELECT 2;")
        self.assertEqual([(1,)], list(cur))

        cur.execute("SELECT 42")
        self.assertEqual([(42,)], list(cur))

    def test_nextset_error(self):
        con = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
        cur = con.cursor()

        for i in range(3):
            cur.execute("SELECT %s; xyzzy;", (i,))
            self.assertEqual([(i,)], list(cur))
            with self.assertRaises(pymysql.ProgrammingError):
                cur.nextset()
            self.assertEqual((), cur.fetchall())

    def test_ok_and_next(self):
        cur = self.connect(client_flag=CLIENT.MULTI_STATEMENTS).cursor()
        cur.execute("SELECT 1; commit; SELECT 2;")
        self.assertEqual([(1,)], list(cur))
        self.assertTrue(cur.nextset())
        self.assertTrue(cur.nextset())
        self.assertEqual([(2,)], list(cur))
        self.assertFalse(bool(cur.nextset()))

    @pytest.mark.xfail
    def test_multi_cursor(self):
        con = self.connect(client_flag=CLIENT.MULTI_STATEMENTS)
        cur1 = con.cursor()
        cur2 = con.cursor()

        cur1.execute("SELECT 1; SELECT 2;")
        cur2.execute("SELECT 42")

        self.assertEqual([(1,)], list(cur1))
        self.assertEqual([(42,)], list(cur2))

        r = cur1.nextset()
        self.assertTrue(r)

        self.assertEqual([(2,)], list(cur1))
        self.assertIsNone(cur1.nextset())

    def test_multi_statement_warnings(self):
        con = self.connect(
            init_command='SELECT "bar"; SELECT "baz"',
            client_flag=CLIENT.MULTI_STATEMENTS,
        )
        cursor = con.cursor()

        try:
            cursor.execute("DROP TABLE IF EXISTS a; " "DROP TABLE IF EXISTS b;")
        except TypeError:
            self.fail()

    # TODO: How about SSCursor and nextset?
    # It's very hard to implement correctly...