class TestObject(unittest.TestCase): """Unit tests for Object""" @classmethod def setUpClass(self): self.ob = Object(id='idtester', description='nametester') def test_attr(self): self.assertTrue(hasattr(self.ob, 'id')) self.assertTrue(hasattr(self.ob, 'description')) def test_update(self): new_stuff = { 'newkey': 'newvalue', 'dontadd': 'dontadd', 'description': 'newdescription' } self.ob.update(newdata=new_stuff, overwrite=False, only_keys=['newkey', 'description']) self.assertTrue(hasattr(self.ob, 'newkey')) self.assertEqual('nametester', self.ob.description) self.ob.update(newdata=new_stuff, overwrite=True, only_keys=['description']) self.assertEqual('newdescription', self.ob.description) self.ob.update(newdata=new_stuff, overwrite=True) self.assertEqual('newdescription', self.ob.description) self.assertEqual('newvalue', self.ob.newkey) self.assertEqual('dontadd', self.ob.dontadd) def test_get_dict(self): gotdict = self.ob.get_dict(only_attributes='id') self.assertEqual(gotdict, {'id': 'idtester'})
def get_dict_with_chain(self, chain, only_keys=None, chain_keys=None, exclude_attributes=None, df_format=False): """get_dict method which incorporates attributes found in a specific chain. Does not overwrite any attributes in the original StructProp. Args: chain: only_keys: chain_keys: exclude_attributes: df_format: Returns: dict: attributes of StructProp + the chain specified """ # Choose attributes to return, return everything in the object if a list is not specified if not only_keys: keys = list(self.__dict__.keys()) else: keys = ssbio.utils.force_list(only_keys) # Remove keys you don't want returned if exclude_attributes: exclude_attributes = ssbio.utils.force_list(exclude_attributes) for x in exclude_attributes: if x in keys: keys.remove(x) else: exclude_attributes = [] exclude_attributes.extend(['mapped_chains', 'chains']) final_dict = { k: v for k, v in Object.get_dict(self, only_attributes=keys, exclude_attributes=exclude_attributes, df_format=df_format).items() } chain_prop = self.chains.get_by_id(chain) # Filter out keys that show up in StructProp if not chain_keys: chain_keys = [ x for x in chain_prop.get_dict().keys() if x not in final_dict ] chain_dict = chain_prop.get_dict(only_attributes=chain_keys, df_format=df_format) final_dict.update(chain_dict) return final_dict