如何模拟pymysqlpool.ConnectionPool构造函数?

huangapple go评论73阅读模式
英文:

How do I mock the pymysqlpool.ConnectionPool constructor?

问题

https://stackoverflow.com/questions/43354242/how-do-i-mock-part-of-a-python-constructor-just-for-testing 类似,但明确尝试使 pymysqlpool.ConnectionPool 正常工作/

class DbTests(TestCase):
    @mock.patch('pymysqlpool.ConnectionPool', autospec=True)
    @mock.patch.dict(
        os.environ,
        {
            "DATASOURCES_0_SERVERID": "server1",
            "DATASOURCES_0_HOST": "non-existent",
            "DATASOURCES_0_PORT": "3307",
            "DATASOURCES_0_DATABASE": "lj_ca1",
            "DATASOURCES_0_USERNAME": "sampleuser",
            "DATASOURCES_0_PASSWORD": "password1",
            "DATASOURCES_0_TIMEZONE": "Americas/Toronto",
        },
    )    
    def test_load(self, connection_pool_mock: mock.Mock):
        ConnectionPool(
            size=2, maxsize=3, pre_create_num=2, host=os.environ["DATASOURCES_0_HOST"]
        )

我期望这段代码能够简单地工作,但我得到了以下错误:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'non-existent' ([Errno 11001] getaddrinfo failed)")

英文:

Similar to https://stackoverflow.com/questions/43354242/how-do-i-mock-part-of-a-python-constructor-just-for-testing but explicitly trying to get pymysqlpool.ConnectionPool to work/

class DbTests(TestCase):
    @mock.patch('pymysqlpool.ConnectionPool', autospec=True)
    @mock.patch.dict(
        os.environ,
        {
            "DATASOURCES_0_SERVERID": "server1",
            "DATASOURCES_0_HOST": "non-existent",
            "DATASOURCES_0_PORT": "3307",
            "DATASOURCES_0_DATABASE": "lj_ca1",
            "DATASOURCES_0_USERNAME": "sampleuser",
            "DATASOURCES_0_PASSWORD": "password1",
            "DATASOURCES_0_TIMEZONE": "Americas/Toronto",
        },
    )    
    def test_load(self, connection_pool_mock: mock.Mock):
        ConnectionPool(
            size=2, maxsize=3, pre_create_num=2, host=os.environ["DATASOURCES_0_HOST"]
        )

I'm expecting the code to simply work, but I am getting

> pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'non-existent' ([Errno 11001] getaddrinfo failed)")

答案1

得分: 1

我假设在你的测试文件中,你有以下的导入语句:

from pymysqlpool import ConnectionPool

这个语句在测试模块中定义了名字 ConnectionPool,但是你的指令 patch('pymysqlpool.ConnectionPool') 将对象替换为了名字 pymysqlpool.ConnectionPool,所以它操作的是另一个名字。

你的测试代码中的导入语句必须改为:

import pymysqlpool

这样你的测试代码就变成了:

import pymysqlpool
from unittest import TestCase, mock

class DbTests(TestCase):
    @mock.patch('pymysqlpool.ConnectionPool', autospec=True)
    @mock.patch.dict(
        os.environ,
        {
            "DATASOURCES_0_SERVERID": "server1",
            "DATASOURCES_0_HOST": "non-existent",
            "DATASOURCES_0_PORT": "3307",
            "DATASOURCES_0_DATABASE": "lj_ca1",
            "DATASOURCES_0_USERNAME": "sampleuser",
            "DATASOURCES_0_PASSWORD": "password1",
            "DATASOURCES_0_TIMEZONE": "Americas/Toronto",
        },
    )    
    def test_load(self, connection_pool_mock: mock.Mock):
        # 请注意,这里我已经更改了对 ConnectionPool 的调用
        pymysqlpool.ConnectionPool(
            size=2, maxsize=3, pre_create_num=2, host=os.environ["DATASOURCES_0_HOST"]
        )

请注意,在 test_load() 代码中,我还更改了对 ConnectionPool 的调用,添加了模块名 pymysqlpool

关于相同主题的有用链接
这个帖子 是关于使用 patch() 的另一个示例。

英文:

I suppose that in your test file you have the following import:

from pymysqlpool import ConnectionPool

This defines the name ConnectionPool in the test module, but your instruction patch('pymysqlpool.ConnectionPool') substitutes the object points by the name pymysqlpool.ConnectionPool so it operates on an other name.

The import in your test code must change to:

import pymysqlpool

So your test code becomes:

import pymysqlpool
from unittest import TestCase, mock

class DbTests(TestCase):
    @mock.patch('pymysqlpool.ConnectionPool', autospec=True)
    @mock.patch.dict(
        os.environ,
        {
            "DATASOURCES_0_SERVERID": "server1",
            "DATASOURCES_0_HOST": "non-existent",
            "DATASOURCES_0_PORT": "3307",
            "DATASOURCES_0_DATABASE": "lj_ca1",
            "DATASOURCES_0_USERNAME": "sampleuser",
            "DATASOURCES_0_PASSWORD": "password1",
            "DATASOURCES_0_TIMEZONE": "Americas/Toronto",
        },
    )    
    def test_load(self, connection_pool_mock: mock.Mock):
        # Note here I have changed the called to ConnectionPool
        pymysqlpool.ConnectionPool(
            size=2, maxsize=3, pre_create_num=2, host=os.environ["DATASOURCES_0_HOST"]
        )

Note that in the test_load() code I have also changed the called to ConnectionPool adding the name of the module pymysqlpool.


Useful link about the same topic
This post is an other example of the use of patch().

huangapple
  • 本文由 发表于 2023年7月11日 08:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658075.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定