File: test_cursor_mariadb.py

package info (click to toggle)
mariadb-connector-python 1.1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 812 kB
  • sloc: python: 6,246; ansic: 4,971; sh: 23; makefile: 14
file content (96 lines) | stat: -rw-r--r-- 3,764 bytes parent folder | download | duplicates (2)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python -O
# -*- coding: utf-8 -*-

import datetime
import unittest

from test.base_test import create_connection


class CursorMariaDBTest(unittest.TestCase):

    def setUp(self):
        self.connection = create_connection()

    def tearDown(self):
        del self.connection

        def test_insert_parameter(self):
            cursor = self.connection.cursor()
            cursor.execute("CREATE TEMPORARY TABLE test_insert_parameter("
                           "a int not null auto_increment primary key,"
                           "b int, c int, d varchar(20),e date)")
            list_in = []
            for i in range(1, 300001):
                row = (i, i, i, "bar", datetime.date(2019, 1, 1))
                list_in.append(row)
            cursor.executemany("INSERT INTO test_insert_parameter VALUES "
                               "(?,?,?,?,?)", list_in)
            self.assertEqual(len(list_in), cursor.rowcount)
            self.connection.commit()
            cursor.execute("SELECT * FROM test_insert_parameter order by a")
            list_out = cursor.fetchall()
            self.assertEqual(len(list_in), cursor.rowcount)
            self.assertEqual(list_in, list_out)
            cursor.close()

        def test_update_parameter(self):
            cursor = self.connection.cursor()
            cursor.execute("CREATE TEMPORARY TABLE test_update_parameter("
                           "a int not null auto_increment primary key,"
                           "b int, c int, d varchar(20),e date)")
            cursor.execute("set @@autocommit=0")
            list_in = []
            for i in range(1, 300001):
                row = (i, i, i, "bar", datetime.date(2019, 1, 1))
                list_in.append(row)
            cursor.executemany("INSERT INTO test_update_parameter VALUES "
                               "(?,?,?,?,?)", list_in)
            self.assertEqual(len(list_in), cursor.rowcount)
            self.connection.commit()
            cursor.close()
            list_update = []

            cursor = self.connection.cursor()
            cursor.execute("set @@autocommit=0")
            for i in range(1, 300001):
                row = (i + 1, i)
                list_update.append(row)

            cursor.executemany("UPDATE test_update_parameter SET b=? "
                               "WHERE a=?", list_update)
            self.assertEqual(cursor.rowcount, 300000)
            self.connection.commit()
            cursor.close()

        def test_delete_parameter(self):
            cursor = self.connection.cursor()
            cursor.execute("CREATE TEMPORARY TABLE test_delete_parameter("
                           "a int not null auto_increment primary key,"
                           "b int, c int, d varchar(20),e date)")
            cursor.execute("set @@autocommit=0")
            list_in = []
            for i in range(1, 300001):
                row = (i, i, i, "bar", datetime.date(2019, 1, 1))
                list_in.append(row)
            cursor.executemany("INSERT INTO test_delete_parameter VALUES "
                               "(?,?,?,?,?)", list_in)
            self.assertEqual(len(list_in), cursor.rowcount)
            self.connection.commit()
            cursor.close()
            list_delete = []

            cursor = self.connection.cursor()
            cursor.execute("set @@autocommit=0")
            for i in range(1, 300001):
                list_delete.append((i,))

            cursor.executemany("DELETE FROM test_delete_parameter WHERE "
                               "a=?", list_delete)
            self.assertEqual(cursor.rowcount, 300000)
            self.connection.commit()
            cursor.close()


if __name__ == '__main__':
    unittest.main()