0

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 using unit tests the dictionary initialization in the setUpClass function is not working. It seems that the dictionary validator_storage at /data_managment and the imported dictionary (using from data_managment import validator_storage) in /test_datamanagement is not the same.

I am grateful for every idea.

EDIT


I think my question is why does validator_storage["value0"].type return int (as it should) in TestDataStorageTypeValidation and bool in data_storage_type_validation


My Code

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")
Ph.lpp
  • 484
  • 1
  • 4
  • 17
  • I didn't fully understand the code (it could be more minimal), but I suspect that this explains the behaviour you see: https://stackoverflow.com/questions/47644879/why-does-python-think-a-boolean-is-an-integer – mkrieger1 Jul 20 '20 at 08:54
  • I am aware of that. This also happens with strings, lists, ... I suspect an error in the initialization at `setUpClass`. Or an error caused by instances. I imported `validator_storage` in a file where it is initialized and try to use it in a different file. – Ph.lpp Jul 20 '20 at 09:10

1 Answers1

0

In the end I answered the question myself. The cause was wrong class instancing:

I typed:

something = some_class
something.__init__()

instead of:

something = some_class()

So every instance referenced the same value.

I'm sorry for wasting your time this was a really dumb mistake but this happens when you are constantly switching between languages.

Ph.lpp
  • 484
  • 1
  • 4
  • 17