class GroupBuilderGetterTests(TestCase): def setUp(self): self.subgroup1 = GroupBuilder('subgroup1') self.dataset1 = DatasetBuilder('dataset1', list(range(10))) self.soft_link1 = LinkBuilder(self.subgroup1, 'soft_link1') self.int_attr = 1 self.str_attr = "my_str" self.group1 = GroupBuilder('group1', {'subgroup1': self.subgroup1}) self.gb = GroupBuilder('gb', {'group1': self.group1}, {'dataset1': self.dataset1}, { 'int_attr': self.int_attr, 'str_attr': self.str_attr }, {'soft_link1': self.soft_link1}) # {'soft_link1': self.soft_link1, # 'external_link1': self.external_link1})) def tearDown(self): pass def test_path(self): self.assertEqual(self.subgroup1.path, 'gb/group1/subgroup1') self.assertEqual(self.dataset1.path, 'gb/dataset1') self.assertEqual(self.soft_link1.path, 'gb/soft_link1') self.assertEqual(self.group1.path, 'gb/group1') self.assertEqual(self.gb.path, 'gb') def test_get_item_group(self): """Test __get_item__ for groups""" self.assertIs(self.gb['group1'], self.group1) def test_get_item_group_subgroup1(self): """Test __get_item__ for groups deeper in hierarchy""" self.assertIs(self.gb['group1/subgroup1'], self.subgroup1) def test_get_item_dataset(self): """Test __get_item__ for datasets""" self.assertIs(self.gb['dataset1'], self.dataset1) def test_get_item_attr1(self): """Test __get_item__ for attributes""" self.assertEqual(self.gb['int_attr'], self.int_attr) def test_get_item_attr2(self): """Test __get_item__ for attributes""" self.assertEqual(self.gb['str_attr'], self.str_attr) def test_get_item_invalid_key(self): """Test __get_item__ for invalid key""" with self.assertRaises(KeyError): self.gb['invalid_key'] def test_get_item_soft_link(self): """Test __get_item__ for soft links""" self.assertIs(self.gb['soft_link1'], self.soft_link1) def test_get_group(self): """Test get() for groups""" self.assertIs(self.gb.get('group1'), self.group1) def test_get_group_subgroup1(self): """Test get() for groups deeper in hierarchy""" self.assertIs(self.gb.get('group1/subgroup1'), self.subgroup1) def test_get_dataset(self): """Test get() for datasets""" self.assertIs(self.gb.get('dataset1'), self.dataset1) def test_get_attr1(self): """Test get() for attributes""" self.assertEqual(self.gb.get('int_attr'), self.int_attr) def test_get_attr2(self): """Test get() for attributes""" self.assertEqual(self.gb.get('str_attr'), self.str_attr) def test_get_item_fcn_soft_link(self): """Test get() for soft links""" self.assertIs(self.gb.get('soft_link1'), self.soft_link1) def test_get_invalid_key(self): """Test get() for invalid key""" self.assertIs(self.gb.get('invalid_key'), None) def test_items(self): """Test items()""" items = ( ('group1', self.group1), ('dataset1', self.dataset1), ('int_attr', self.int_attr), ('str_attr', self.str_attr), ('soft_link1', self.soft_link1), # ('external_link1', self.external_link1) ) # self.assertSetEqual(items, set(self.gb.items())) try: self.assertCountEqual(items, self.gb.items()) except AttributeError: self.assertItemsEqual(items, self.gb.items()) def test_keys(self): """Test keys()""" keys = ( 'group1', 'dataset1', 'int_attr', 'str_attr', 'soft_link1', # 'external_link1', ) try: self.assertCountEqual(keys, self.gb.keys()) except AttributeError: self.assertItemsEqual(keys, self.gb.keys()) def test_values(self): """Test values()""" values = ( self.group1, self.dataset1, self.int_attr, self.str_attr, self.soft_link1, # self.external_link1, ) try: self.assertCountEqual(values, self.gb.values()) except AttributeError: self.assertItemsEqual(values, self.gb.values())
class TestGroupBuilderGetters(TestCase): def setUp(self): self.subgroup1 = GroupBuilder('subgroup1') self.dataset1 = DatasetBuilder('dataset1', list(range(10))) self.link1 = LinkBuilder(self.subgroup1, 'link1') self.int_attr = 1 self.str_attr = "my_str" self.group1 = GroupBuilder('group1', {'subgroup1': self.subgroup1}) self.gb = GroupBuilder(name='gb', groups={'group1': self.group1}, datasets={'dataset1': self.dataset1}, attributes={ 'int_attr': self.int_attr, 'str_attr': self.str_attr }, links={'link1': self.link1}) def test_path(self): self.assertEqual(self.subgroup1.path, 'gb/group1/subgroup1') self.assertEqual(self.dataset1.path, 'gb/dataset1') self.assertEqual(self.link1.path, 'gb/link1') self.assertEqual(self.group1.path, 'gb/group1') self.assertEqual(self.gb.path, 'gb') def test_getitem_group(self): """Test __getitem__ for groups""" self.assertIs(self.gb['group1'], self.group1) def test_getitem_group_deeper(self): """Test __getitem__ for groups deeper in hierarchy""" self.assertIs(self.gb['group1/subgroup1'], self.subgroup1) def test_getitem_dataset(self): """Test __getitem__ for datasets""" self.assertIs(self.gb['dataset1'], self.dataset1) def test_getitem_attr(self): """Test __getitem__ for attributes""" self.assertEqual(self.gb['int_attr'], self.int_attr) self.assertEqual(self.gb['str_attr'], self.str_attr) def test_getitem_invalid_key(self): """Test __getitem__ for invalid key""" with self.assertRaises(KeyError): self.gb['invalid_key'] def test_getitem_invalid_key_deeper(self): """Test __getitem__ for invalid key""" with self.assertRaises(KeyError): self.gb['group/invalid_key'] def test_getitem_link(self): """Test __getitem__ for links""" self.assertIs(self.gb['link1'], self.link1) def test_get_group(self): """Test get() for groups""" self.assertIs(self.gb.get('group1'), self.group1) def test_get_group_deeper(self): """Test get() for groups deeper in hierarchy""" self.assertIs(self.gb.get('group1/subgroup1'), self.subgroup1) def test_get_dataset(self): """Test get() for datasets""" self.assertIs(self.gb.get('dataset1'), self.dataset1) def test_get_attr(self): """Test get() for attributes""" self.assertEqual(self.gb.get('int_attr'), self.int_attr) self.assertEqual(self.gb.get('str_attr'), self.str_attr) def test_get_link(self): """Test get() for links""" self.assertIs(self.gb.get('link1'), self.link1) def test_get_invalid_key(self): """Test get() for invalid key""" self.assertIs(self.gb.get('invalid_key'), None) def test_items(self): """Test items()""" items = ( ('group1', self.group1), ('dataset1', self.dataset1), ('int_attr', self.int_attr), ('str_attr', self.str_attr), ('link1', self.link1), ) # self.assertSetEqual(items, set(self.gb.items())) try: self.assertCountEqual(items, self.gb.items()) except AttributeError: self.assertItemsEqual(items, self.gb.items()) def test_keys(self): """Test keys()""" keys = ( 'group1', 'dataset1', 'int_attr', 'str_attr', 'link1', ) try: self.assertCountEqual(keys, self.gb.keys()) except AttributeError: self.assertItemsEqual(keys, self.gb.keys()) def test_values(self): """Test values()""" values = ( self.group1, self.dataset1, self.int_attr, self.str_attr, self.link1, ) try: self.assertCountEqual(values, self.gb.values()) except AttributeError: self.assertItemsEqual(values, self.gb.values())