def test_serialization_default_types(self):
     for x in self.testdata:
         s = serialize(x)
         xx = unserialize(s)
         self.assertEqual(x, xx)
     for x in self.testdata_arrays:
         s = serialize(x)
         xx = unserialize(s)
         self.assertSequenceEqual(x.tolist(), xx.tolist())
Exemple #2
0
    def test_serialization_container_types(self):
        """ Creates a PythonJsonStucture with all the data-types. Serializes the
            object and directly afterwards unserializes. The unserialized object
            should equal the original created PythonJsonStructure.

            Note:
                Currently a PythonJsonStructure with a tuple is not tested. A tuple
                object can only be serialized to a list.
        """
        settable_containers = {
            'list': [1, 2, 3],
            'dict': {
                'a': 1,
                'b': 2,
                'c': 3
            },
            'ndarray': np.random.rand(3, 2, 4, 5).astype(np.cfloat),
            # 'tuple': (1, 2, 3),
            'json_object': PythonJsonStructure()
        }
        for key, expected in settable_containers.items():
            json_object = PythonJsonStructure({key: expected})
            serialized_object = serialize(json_object)
            unserialized_object = unserialize(serialized_object)
            np.testing.assert_equal(json_object, unserialized_object)
    def test_save_configuration(self):
        file_path = '/dev/null'
        configuration = {'sample_rate': 1000, 'period': 2}

        with patch('builtins.open', new_callable=mock_open) as file_mock:
            save_configuration(file_path, configuration)

        file_mock.return_value.write.assert_called_once_with(serialize(configuration))
        self.assertNotEqual(0, len(file_mock.mock_calls))
def save_configuration(file_path: str,
                       configuration: PythonJsonStructure) -> None:
    """ Saves the instrument configuration to disk storage.

    Args:
        file_path: The store file location on disk.
        configuration: The instrument configuration that needs to be stored to disk.
    """
    with open(file_path, 'wb') as file_pointer:
        serialized_configuration = serialization.serialize(configuration)
        file_pointer.write(serialized_configuration)
Exemple #5
0
 def test_serialization_data_types(self):
     settable_objects = {
         'none': None,
         'bool': False,
         'int': 25,
         'float': 3.141592,
         'str': 'some_string',
         'bytes': b'1010101',
         'np.float32': np.float32(3.1415),
         'np.float64': np.float64(-3.1415),
         'np.int32': np.int32(4),
         'np.in64': np.int64(-4),
         'np.cfloat': np.random.rand(2, 4, 5, 3).astype(np.cfloat),
     }
     for key, expected in settable_objects.items():
         json_object = PythonJsonStructure({key: expected})
         serialized_object = serialize(json_object)
         unserialized_object = unserialize(serialized_object)
         np.testing.assert_equal(json_object, unserialized_object)
 def test_load_configuration(self):
     expected_configuration = {'sample_rate': 1000, 'period': 2}
     with patch('builtins.open', mock_open(read_data=serialize(expected_configuration))):
         actual_configuration = load_configuration('/dev/null')
     self.assertEqual(expected_configuration, actual_configuration)
 def test_non_serializable_objects(self):
     with self.assertRaisesRegex(TypeError, 'is not JSON serializable'):
         serialize(object())
 def test_serialize(self):
     data = {'hello': 'world'}
     serialized = serialize(data)
     self.assertEqual(type(serialized), bytes)
     self.assertEqual(data, unserialize(serialize(data)))
 def test_encode_decode_json_dataclass(self):
     serializer.register_dataclass(CustomDataClass)
     custom_dataclass = CustomDataClass(1.25, 'my_dummy_value')
     new_dataclass = unserialize(serialize(custom_dataclass))
     self.assertEqual(new_dataclass, custom_dataclass)
 def test_serialize_non_string_keys(self):
     data = PythonJsonStructure({'a': 1, 1: 'a'})
     self.assertDictEqual(unserialize(serialize(data)), {'a': 1, '1': 'a'})