我正在为我的一个python项目构建一个数据存储系统。为此,我正在构建和测试一个类型验证。
数据在存储之前应该用data_storage_type_validation
进行类型检查,所以我制作了一个字典validator_storage
,它包含一个类StorageValidator
,它包含关于给定数据的所有信息。
这一切都很好,但当我使用单元测试测试data_storage_type_validation
函数时,setUpClass
函数中的字典初始化不起作用。/data_management中的字典validator_storage
和/test_datamanagement中导入的字典(使用from data_managment import validator_storage
(似乎不一样。
我很感激每一个想法。
编辑
我想我的问题是为什么validator_storage["value0"].type
在TestDataStorageTypeValidation
中返回int(应该如此(而在data_storage_type_validation
中返回bool
我的代码
StorageValidator
类(/StorageValidator(:
class StorageValidator:
def __init__(self, t: type):
self.type = t
data_storage_type_validation
函数(/data_management(:
def data_storage_type_validation(data_label: str, data) -> bool:
if type(data_label) in [str]:
if data_label in validator_storage:
print("Data ",data)
print("Data type", type(data))
print("datalable ", data_label)
print("expected", validator_storage[data_label].type)
print()
if type(data) in [validator_storage[data_label].type]:
return True
else:
return False
else:
raise LabelNotFound
else:
raise TypeError
测试TestDataStorageTypeValidation
(/test_data_management(:
class TestDataStorageTypeValidation(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
value_int_validator = StorageValidator
value_int_validator.__init__(value_int_validator, t=int)
print("Int:", value_int_validator.type)
validator_storage["value0"] = value_int_validator
print("Int:", validator_storage["value0"].type)
value_float_validator = StorageValidator
value_float_validator.__init__(value_float_validator ,t=float)
validator_storage["value3.14"] = value_float_validator
value_complex_validator = StorageValidator
value_complex_validator.__init__(value_complex_validator, t=complex)
validator_storage["value5j"] = value_complex_validator
value_string_validator = StorageValidator
value_string_validator.__init__(value_string_validator, t=str)
validator_storage["valuestring"] = value_string_validator
value_list_validator = StorageValidator
value_list_validator.__init__(value_list_validator, t=list)
validator_storage["value[]"] = value_list_validator
value_dict_validator = StorageValidator
value_dict_validator.__init__(value_dict_validator, t=dict)
validator_storage["value{}"] = value_dict_validator
value_bool_validator = StorageValidator
value_bool_validator.__init__(value_bool_validator, t=bool)
validator_storage["valueTrue"] = value_bool_validator
def test_data_storage_type_validation_success_data_valid(self):
#return_int = data_storage_type_validation("value0", 1)
#self.assertEqual(return_int, True)
return_float = data_storage_type_validation("value3.14", 3.14)
print("Return:",return_float)
self.assertEqual(return_float, True)
return_complex = data_storage_type_validation("value5j", 5j)
self.assertEqual(return_complex, True)
return_string = data_storage_type_validation("valuestring", "string")
self.assertEqual(return_string, True)
return_list = data_storage_type_validation("value[]", [])
self.assertEqual(return_list, True)
return_dict = data_storage_type_validation("value{}", {})
self.assertEqual(return_dict, True)
return_bool = data_storage_type_validation("valueTrue", True)
self.assertEqual(return_bool, True)
def test_data_storage_type_validation_success_data_not_valid(self):
return_int = data_storage_type_validation("value0", "false_type")
self.assertEqual(return_int, False)
return_float = data_storage_type_validation("value3.14", "false_type")
self.assertEqual(return_float, False)
return_complex = data_storage_type_validation("value5j", "false_type")
self.assertEqual(return_complex, False)
return_string = data_storage_type_validation("valuestring", [])
self.assertEqual(return_string, False)
return_list = data_storage_type_validation("value[]", "false_type")
self.assertEqual(return_list, False)
return_dict = data_storage_type_validation("value{}", "false_type")
self.assertEqual(return_dict, False)
return_bool = data_storage_type_validation("valueTrue", "false_type")
self.assertEqual(return_bool, False)
def test_data_storage_type_validation_label(self):
self.assertRaises(LabelNotFound, data_storage_type_validation, "test_label", 1)
def test_data_storage_type_validation_type(self):
self.assertRaises(TypeError, data_storage_type_validation, 5, "test")
self.assertRaises(TypeError, data_storage_type_validation, 3.1415, "test")
self.assertRaises(TypeError, data_storage_type_validation, 5j, "test")
self.assertRaises(TypeError, data_storage_type_validation, [], "test")
self.assertRaises(TypeError, data_storage_type_validation, {}, "test")
self.assertRaises(TypeError, data_storage_type_validation, True, "test")
最后我自己回答了这个问题。原因是类实例化错误:
我键入:
something = some_class
something.__init__()
而不是:
something = some_class()
所以每个实例都引用了相同的值。
很抱歉浪费了你的时间,这是一个非常愚蠢的错误,但当你不断在语言之间切换时,就会发生这种情况。