def test__unflatten_dict_raises_error_column_index(self): """Test unflatten dict raises error column_index""" # Setup sampler = Mock(spec=Sampler) flat = {'foo__1__0': 'some value'} # Run with pytest.raises(ValueError): Sampler._unflatten_dict(sampler, flat)
def test__unflatten_dict_raise_value_error_column_index(self): """Raises ValueError by column_index""" # Setup setdefault_mock = Mock() setdefault_mock.return_value = [[1, 2, 3, 4]] # Run and assert sampler = Mock() sampler._setdefault = setdefault_mock flat = {'foo__1__1': 'foo'} with self.assertRaises(ValueError): Sampler._unflatten_dict(sampler, flat)
def test__unflatten_dict(self): """Test unflatten_dict""" # Setup sampler = Mock(spec=Sampler) sampler._key_order = None # Run flat = { 'foo__0__foo': 'foo value', 'bar__0__0': 'bar value', 'tar': 'tar value' } result = Sampler._unflatten_dict(sampler, flat) # Asserts expected = { 'foo': { 0: { 'foo': 'foo value' } }, 'bar': [['bar value']], 'tar': 'tar value', } assert result == expected
def test__unflatten_dict_respect_covariance_matrix(self): """unflatten_dict restructures the covariance matrix into an square matrix.""" # Setup data_navigator = MagicMock() modeler = MagicMock() sampler = Sampler(data_navigator, modeler) def fake_values(i, j): return '{}, {}'.format(i, j) expected_result = { 'covariance': np.array([[fake_values(i, j) for j in range(40)] for i in range(40)]).tolist() } flat = { 'covariance__{}__{}'.format(i, j): fake_values(i, j) for i in range(40) for j in range(40) } # Run result = sampler._unflatten_dict(flat) # Check assert result == expected_result
def test__unflatten_dict_child_name(self): """unflatten_dict will respect the name of child tables.""" # Setup data_navigator = MagicMock() modeler = MagicMock() sampler = Sampler(data_navigator, modeler) flat = { 'first_key__a__b': 1, 'first_key____CHILD_TABLE__model_param': 0, 'distribs____CHILD_TABLE__distribs__UNIT_PRICE__std__mean': 0 } expected_result = { 'first_key': { 'a': { 'b': 1 }, '__CHILD_TABLE': { 'model_param': 0 } }, 'distribs': { '__CHILD_TABLE__distribs__UNIT_PRICE__std': { 'mean': 0 } } } # Run result = sampler._unflatten_dict(flat) # Check assert result == expected_result modeler.assert_not_called() data_navigator.assert_not_called()
def test__unflatten_dict(self): """unflatten_dict restructure flatten dicts.""" # Setup data_navigator = MagicMock() modeler = MagicMock() sampler = Sampler(data_navigator, modeler) flat = { 'a__first_key__a': 1, 'a__first_key__b': 2, 'b__second_key__x': 0 } expected_result = { 'a': { 'first_key': { 'a': 1, 'b': 2 }, }, 'b': { 'second_key': { 'x': 0 }, } } # Run result = sampler._unflatten_dict(flat) # Check assert result == expected_result data_navigator.assert_not_called() modeler.assert_not_called()
def test__unflatten_dict_child_name(self): """unflatten_dict will respect the name of child tables.""" # Setup data_navigator = MagicMock() data_navigator.get_children.return_value = ['CHILD_TABLE'] modeler = MagicMock() sampler = Sampler(data_navigator, modeler) flat = { 'first_key__a': 1, 'first_key____CHILD_TABLE__model_param': 0, 'distribs____CHILD_TABLE__distribs__UNIT_PRICE__std__mean': 0 } table_name = 'TABLE_NAME' expected_result = { 'first_key': { 'a': 1, '__CHILD_TABLE': { 'model_param': 0 } }, 'distribs': { '__CHILD_TABLE__distribs__UNIT_PRICE__std': { 'mean': 0 } } } # Run result = sampler._unflatten_dict(flat, table_name) # Check assert result == expected_result modeler.assert_not_called() data_navigator.get_children.assert_called_once_with('TABLE_NAME')
def test__unflatten_dict_alrady_unflatted(self): """Already unflatted dict.""" # Setup # Run sampler = Mock() flat = {'foo': 'bar'} result = Sampler._unflatten_dict(sampler, flat) # Asserts exp_dict = {'foo': 'bar'} assert result == exp_dict
def test__unflatten_dict_mixed_array(self): """unflatten_dict restructure arrays.""" # Setup data_navigator = MagicMock() modeler = MagicMock() sampler = Sampler(data_navigator, modeler) flat = { 'first_key__0__0': 1, 'first_key__0__1': 0, 'first_key__1__0': 0, 'first_key__1__1': 1, 'second_key__0__std': 0.5, 'second_key__0__mean': 0.5, 'second_key__1__std': 0.25, 'second_key__1__mean': 0.25 } expected_result = { 'first_key': [ [1, 0], [0, 1] ], 'second_key': [ { 'std': 0.5, 'mean': 0.5 }, { 'std': 0.25, 'mean': 0.25 } ] } # Run result = sampler._unflatten_dict(flat) # Check assert result == expected_result data_navigator.assert_not_called() modeler.assert_not_called()