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

问题描述 投票:0回答:1

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

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

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

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

有人可以帮我吗?

我的DatabaseConfig.yml:

database: 

dbopt:
   host: None
   port: 6313
   dbname: spam_eggs2

query:
   select * from manufacturing_product

我的database_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_db_type.py for database_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


python python-unittest pyyaml
1个回答
0
投票

这里:

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

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.