Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么从Python的unittest运行时对MySQL的写入无法持久化?_Mysql_Python 3.x_Amazon Rds_Python Unittest_Mysql Connector Python - Fatal编程技术网

为什么从Python的unittest运行时对MySQL的写入无法持久化?

为什么从Python的unittest运行时对MySQL的写入无法持久化?,mysql,python-3.x,amazon-rds,python-unittest,mysql-connector-python,Mysql,Python 3.x,Amazon Rds,Python Unittest,Mysql Connector Python,目前,我正在使用Python 3.8中的unittest为新创建的MySQL数据库编写一个测试用例。该数据库是一个运行Aurora MySQL 5.6的AWS RDS实例,它有一个表users,其中有一个主键字段uuid VARCHAR36。测试用例如下所示: import unittest import mysql.connector from config import MYSQL_CONNECTION_INFO class SQLSchemaTests(unittest.TestCase

目前,我正在使用Python 3.8中的unittest为新创建的MySQL数据库编写一个测试用例。该数据库是一个运行Aurora MySQL 5.6的AWS RDS实例,它有一个表users,其中有一个主键字段uuid VARCHAR36。测试用例如下所示:

import unittest
import mysql.connector
from config import MYSQL_CONNECTION_INFO

class SQLSchemaTests(unittest.TestCase):
    """Verifies the correct behavior of the schema itself. (i.e. that the tables were set up correctly)"""
    
    def setUp(self):
        self.cnxn = mysql.connector.connect(**MYSQL_CONNECTION_INFO)
        self.cursor = self.cnxn.cursor()
        
    def tearDown(self):
        self.cnxn.close()
    
    def test_create_users(self):
        """Verify that a client can create user entries in the data store with appropriate parameters."""
        self.cursor.execute("SELECT COUNT(*) from users")
        user_entries_count = self.cursor.fetchone()[0]
        self.assertEqual(user_entries_count, 0)
        
        self.cursor.execute("INSERT INTO users (uuid) VALUES ('aaa-bbb-ccc-ddd-eee')")
        
        self.cursor.execute("SELECT COUNT(*) from users")
        user_entries_count = self.cursor.fetchone()[0]
        self.assertEqual(user_entries_count, 1)
让我困惑的是,这个测试用例在每次运行时都会通过——换句话说,在我没有执行清理操作的情况下,它不会因为重复条目而失败。我使用PyCharm的调试器在INSERT语句后放置一个断点,然后在暂停测试执行时从单独的数据库控制台中的用户运行SELECT COUNT*:结果返回为零。更重要的是,当我使用数据库控制台向users表写入相同的条目时,测试才因为重复条目而失败

我想知道以下几点:

为什么单元测试中的INSERT语句不保留到表中?它是由MySQL连接器、unittest还是其他原因引起的? 有哪些规则规定了这种情况是如何发生的?在什么情况下保证这种行为? 是否有任何官方文件可以澄清这些问题?
为了在测试之间看到插入持久化,我需要在调用INSERT语句execute后添加self.cnxn.commit:指定默认情况下禁用自动提交

此外,我可以从测试中而不是从单独的数据库控制台中获取更新的计数,这是因为在本例中,数据库级别的事务隔离设置为REPEATABLE-READ。有关更多信息,请参阅和中的关于数据库中的隔离