lundi 20 juillet 2020

Python Unit Testing: Dictionary initialization not working

I am buiding a data storage system for one of my python projects. For this I am building and testing a type validation.

The data should be type checked with the data_storage_type_validationbefore storage so I made a dictionary validator_storage which contains a class StorageValidator which contains all information about a given data.

This all works fine but when I test the data_storage_type_validation function the dictionary initialization in the setUpClass function is not working.

(I tryed to debug this with print statments)

The validator-storage in TestDataStorageTypeValidation correctly outputs int as the value of validator_storage["value0"].type

but when I try this at data_storage_type_validation it outputs bool for every data_label I try to define

I am grateful for every idea.

The StorageValidator class(/StorageValidator):

class StorageValidator:

def __init__(self, t: type):
    self.type = t

The data_storage_type_validation function(/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

The test 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")

Aucun commentaire:

Enregistrer un commentaire