错误:单元测试中的“字典更新序列元素#0的长度为1;需要2”

2024-10-05 11:01:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个文件:database_config.py,用于通过DatabaseConfig.yml配置文件检查与数据库的连接。我需要为我的配置文件编写一些测试用例,包括几个场景:

  1. 如果数据库为空,则测试用例失败
  2. 如果数据库不是Sqlite或Postgre,测试用例将失败

我已经创建了一个测试文件,但当我尝试实现它时,我发现了一个错误

ValueError: dictionary update sequence element #0 has length 1; 2 is required

谁能帮我一下吗

My DatabaseConfig.yml:

database: 

dbopt:
   host: None
   port: 6313
   dbname: spam_eggs2

query:
   select * from manufacturing_product

我的数据库_config.py:

import yaml

class InvalidConfigError(Exception):
    pass

class DB():
    def __init__(self, dbconf):
        self._dbconf = dict(dbconf)
        # checking for database type
        dbtype = self.get_db_type()
        if dbtype != 'sqlite' and dbtype != 'postgres':
            raise InvalidConfigError(
                'E01001', 'Invalid database type, should be sqlite or postgres.')
        else:
            self.dbtype = dbtype

    def get_db_type(self):
        return self._dbconf['database']

with open('DatabaseConfig.yml') as f:
    data = yaml.full_load(f)

    for item, doc in data.items():
        print(item, ":", doc)

    database = DB(data)

数据库的测试文件test\u db\u type.py\u config.py:

import yaml
import unittest
import database_config

class TestDBtype(unittest.TestCase):
    #setUpClass gets calls only once where as setUp gets called before every test
    @classmethod
    def setUpClass(cls):
        cls.init_db()

    @classmethod
    def init_db(cls):
        self.db_instance = database_config.DB('DatabaseConfig.yml')

    def test_missing_db(self):
        self.assertEqual(self.db_instance, None, "Connection returned None.")

    def test_db_type(self):
        with self.subTest():
            self.assertEqual(self.db_instance, 'postgres')
        with self.subTest():
            self.assertEqual(self.db_instance, 'sqlite')

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

运行测试时发生错误回溯:

(base) D:\Python>python -u "d:\Python\TestDataSource\test_db_type.py"
database : postgres
dbopt : {'host': 'None', 'port': 6313, 'dbname': 'spam_eggs2'}
query : select * from manufacturing_product
E
======================================================================
ERROR: setUpClass (__main__.TestDBtype)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\Python\TestDataSource\test_db_type.py", line 9, in setUpClass
    cls.init_db()
  File "d:\Python\TestDataSource\test_db_type.py", line 13, in init_db
    self.db_instance = database_config.DB('DatabaseConfig.yml')
  File "d:\Python\TestDataSource\database_config.py", line 8, in __init__
    self._dbconf = dict(dbconf)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

----------------------------------------------------------------------
Ran 0 tests in 0.001s

FAILED (errors=1)

(base) D:\Python>C:/Users/duongnb/AppData/Local/Continuum/anaconda3/Scripts/activate

(base) D:\Python>conda activate base

(base) D:\Python>^A



Tags: instanceinpytestselfconfig数据库db
1条回答
网友
1楼 · 发布于 2024-10-05 11:01:39

在这里:

self.db_instance = database_config.DB('DatabaseConfig.yml')

将单个字符串参数传递给DB构造函数;此参数在此处传递:

class DB():
    def __init__(self, dbconf):
        self._dbconf = dict(dbconf)

但是不能从单个参数创建dict-dict('DatabaseConfig.yml')引发错误

相关问题 更多 >

    热门问题