def test_bad_dict(self): # NOTE(alexcjohnson): # this is a valid dict, but not good JSON because the tuple key cannot # be converted into a string. # It throws an error in compare_dictionaries, which is likely what we # want, but we should be aware of it. a = {(5, 6): (7, 8)} with self.assertRaises(TypeError): compare_dictionaries(a, a)
def test_key_diff(self): a = {'a': 1, 'c': 4} b = {'b': 1, 'c': 4} match, err = compare_dictionaries(a, b) self.assertFalse(match) self.assertIn('Key d1[a] not in d2', err) self.assertIn('Key d2[b] not in d1', err) # try again with dict names for completeness match, err = compare_dictionaries(a, b, 'a', 'b') self.assertFalse(match) self.assertIn('Key a[a] not in b', err) self.assertIn('Key b[b] not in a', err)
def test_key_diff(): a = {'a': 1, 'c': 4} b = {'b': 1, 'c': 4} match, err = compare_dictionaries(a, b) assert not match assert 'Key d1[a] not in d2' in err assert 'Key d2[b] not in d1' in err # try again with dict names for completeness match, err = compare_dictionaries(a, b, 'a', 'b') assert not match assert 'Key a[a] not in b' in err assert 'Key b[b] not in a' in err
def test_nested_key_diff(self): a = {'a': {'b': 'c'}} b = {'a': {'d': 'c'}} match, err = compare_dictionaries(a, b) self.assertFalse(match) self.assertIn('Key d1[a][b] not in d2', err) self.assertIn('Key d2[a][d] not in d1', err)
def test_nested_key_diff(): a = {'a': {'b': 'c'}} b = {'a': {'d': 'c'}} match, err = compare_dictionaries(a, b) assert not match assert 'Key d1[a][b] not in d2' in err assert 'Key d2[a][d] not in d1' in err
def test_val_diff_simple(): a = {'a': 1} b = {'a': 2} match, err = compare_dictionaries(a, b) assert not match assert 'Value of "d1[a]" ("1", type"<class \'int\'>") not same as' in err assert '"d2[a]" ("2", type"<class \'int\'>")' in err
def test_val_diff_simple(self): a = {'a': 1} b = {'a': 2} match, err = compare_dictionaries(a, b) self.assertFalse(match) self.assertIn( 'Value of "d1[a]" ("1", type"<class \'int\'>") not same as', err) self.assertIn('"d2[a]" ("2", type"<class \'int\'>")', err)
def test_same(self): # NOTE(alexcjohnson): the numpy array and list compare equal, # even though a list and tuple would not. See TODO in # compare_dictionaries. a = {'a': 1, 2: [3, 4, {5: 6}], 'b': {'c': 'd'}, 'x': np.array([7, 8])} b = {'a': 1, 2: [3, 4, {5: 6}], 'b': {'c': 'd'}, 'x': [7, 8]} match, err = compare_dictionaries(a, b) self.assertTrue(match) self.assertEqual(err, '')
def test_read_writing_dicts_withlists_to_hdf5(self): some_dict = {} some_dict['list_of_ints'] = list(np.arange(5)) some_dict['list_of_floats'] = list(np.arange(5.1)) fp = self.loc_provider(io=DataSet.default_io, record={'name': 'test_dict_writing'}) + '.hdf5' F = h5py.File(fp, mode='a') self.formatter.write_dict_to_hdf5(some_dict, F) new_dict = {} self.formatter.read_dict_from_hdf5(new_dict, F) dicts_equal, err_msg = compare_dictionaries(some_dict, new_dict, 'written_dict', 'loaded_dict') self.assertTrue(dicts_equal, msg='\n' + err_msg)
def test_val_diff_seq(self): # NOTE(alexcjohnson): # we don't dive recursively into lists at the moment. # Perhaps we want to? Seems like list equality does a deep comparison, # so it's not necessary to get ``match`` right, but the error message # could be more helpful if we did. a = {'a': [1, {2: 3}, 4]} b = {'a': [1, {5: 6}, 4]} match, err = compare_dictionaries(a, b) self.assertFalse(match) self.assertIn('Value of "d1[a]" ("[1, {2: 3}, 4]", ' 'type"<class \'list\'>") not same', err) self.assertIn('"d2[a]" ("[1, {5: 6}, 4]", type"<class \'list\'>")', err)
def test_metadata_write_read(self): """ Test is based on the snapshot of the 1D dataset. Having a more complex snapshot in the metadata would be a better test. """ data = DataSet1D(location=self.loc_provider, name='test_metadata') data.snapshot() # gets the snapshot, not added upon init self.formatter.write(data) # write_metadata is included in write data2 = DataSet(location=data.location, formatter=self.formatter) data2.read() self.formatter.close_file(data) self.formatter.close_file(data2) metadata_equal, err_msg = compare_dictionaries( data.metadata, data2.metadata, 'original_metadata', 'loaded_metadata') self.assertTrue(metadata_equal, msg='\n'+err_msg)
def test_loop_writing_2D(self): # pass station = Station() MockPar = MockParabola(name='Loop_writing_test_2D') station.add_component(MockPar) loop = Loop(MockPar.x[-100:100:20]).loop(MockPar.y[-50:50:10]).each( MockPar.skewed_parabola) data1 = loop.run(name='MockLoop_hdf5_test', formatter=self.formatter) data2 = DataSet(location=data1.location, formatter=self.formatter) data2.read() for key in data2.arrays.keys(): self.checkArraysEqual(data2.arrays[key], data1.arrays[key]) metadata_equal, err_msg = compare_dictionaries(data1.metadata, data2.metadata, 'original_metadata', 'loaded_metadata') self.assertTrue(metadata_equal, msg='\n' + err_msg) self.formatter.close_file(data1) self.formatter.close_file(data2)