def test_set_or_create(self): """Test the internal set or create method.""" key = {'_id': 4096, 'key': 'bigger'} obj = Keys() # pylint: disable=protected-access obj._set_or_create(key) keys = [{'_id': 4097, 'key': 'blah'}] obj._set_or_create(keys) obj._set_or_create(key) chk_obj = Keys.get_by_id(4096) self.assertEqual(chk_obj.id, 4096) chk_obj = Keys.get_by_id(4097) self.assertEqual(chk_obj.id, 4097) obj._meta.database.close()
def from_hash(self, obj): """Convert the hash into the object.""" super(FileKeyValue, self).from_hash(obj) self._set_only_if('file_id', obj, 'file', lambda: Files.get(Files.id == obj['file_id'])) self._set_only_if('key_id', obj, 'key', lambda: Keys.get(Keys.id == obj['key_id'])) self._set_only_if('value_id', obj, 'value', lambda: Values.get(Values.id == obj['value_id']))
def test_hash_list_with_keys(self): """Test method to check the results of available hash list.""" sample_key1 = {'_id': 127, 'key': 'proposal', 'encoding': 'UTF8'} sample_key2 = {'_id': 128, 'key': 'proposal', 'encoding': 'UTF8'} sample_key3 = {'_id': 130, 'key': 'proposal', 'encoding': 'UTF8'} self.base_create_obj(Keys, sample_key1) self.base_create_obj(Keys, sample_key2) self.base_create_obj(Keys, sample_key3) third_obj = Keys() hash_list, hash_dict = third_obj.available_hash_list() self.assertTrue(len(hash_list) == 3) # some sanity checking for the layout of the two objects for hashed_key in hash_list: self.assertTrue(hashed_key in hash_dict) obj_key_meta = hash_dict[hashed_key] self.assertTrue('index_hash' in obj_key_meta) self.assertTrue('key_list' in obj_key_meta) self.assertTrue('id' in obj_key_meta['key_list']) self.assertTrue(hashed_key == obj_key_meta['index_hash'])
def generate_tkvs(json): """Extract TransactionKeyValues as a hash from the json hash.""" keys = [] values = [] tkvs = [] for key, value in pull_kv_by_attr(json): keys.append({'key': key}) values.append({'value': value}) # pylint: disable=protected-access Keys()._set_or_create(keys) Values()._set_or_create(values) # pylint: enable=protected-access for key, value in pull_kv_by_attr(json): # key_obj = Keys.get(key=key) # value_obj = Values.get(value=value) tkvs.append({ 'key': Keys.get(key=key).id, 'transaction': transaction_hash['_id'], 'value': Values.get(value=value).id }) return tkvs
def from_hash(self, obj): """Convert the hash into the object.""" super(TransactionKeyValue, self).from_hash(obj) self._set_only_if( 'transaction_id', obj, 'transaction', lambda: Transactions.get(Transactions.id == obj['transaction_id']) ) self._set_only_if( 'value_id', obj, 'value', lambda: Values.get( Values.id == obj['value_id']) ) self._set_only_if( 'key_id', obj, 'key', lambda: Keys.get(Keys.id == obj['key_id']) )
def generate_fkvs(json): """Extract FileKeyValues as a hash from the json hash.""" file_keys = [] file_values = [] fkvs = [] for key, value, file_id in pull_fkv_by_attr(json): file_keys.append({'key': key}) file_values.append({'value': value}) # pylint: disable=protected-access Keys()._set_or_create(file_keys) Values()._set_or_create(file_values) # pylint: enable=protected-access for key, value, file_id in pull_fkv_by_attr(json): # key_obj = # value_obj = Values.get(value=value) fkvs.append({ 'key': Keys.get(key=key).id, 'value': Values.get(value=value).id, 'file': file_id }) return fkvs
def base_create_dep_objs(cls): """Create all objects that TransactionKeyValue need.""" trans = Transactions() keys = Keys() values = Values() TestTransactions.base_create_dep_objs() trans.from_hash(SAMPLE_TRANSACTION_HASH) trans.save(force_insert=True) TestKeys.base_create_dep_objs() keys.from_hash(SAMPLE_KEY_HASH) keys.save(force_insert=True) TestValues.base_create_dep_objs() values.from_hash(SAMPLE_VALUE_HASH) values.save(force_insert=True)
def base_create_dep_objs(cls): """Create all objects that FileKeyValue need.""" keys = Keys() TestKeys.base_create_dep_objs() keys.from_hash(SAMPLE_KEY_HASH) keys.save(force_insert=True) values = Values() TestValues.base_create_dep_objs() values.from_hash(SAMPLE_VALUE_HASH) values.save(force_insert=True) files = Files() TestFiles.base_create_dep_objs() files.from_hash(SAMPLE_FILE_HASH) files.save(force_insert=True)
def base_create_dep_objs(cls): """Create all objects that InstrumentKeyValue need.""" rel = Relationships() inst = Instruments() keys = Keys() values = Values() TestRelationships.base_create_dep_objs() rel.from_hash(SAMPLE_RELATIONSHIP_HASH) rel.save(force_insert=True) TestInstruments.base_create_dep_objs() inst.from_hash(SAMPLE_INSTRUMENT_HASH) inst.save(force_insert=True) TestKeys.base_create_dep_objs() keys.from_hash(SAMPLE_KEY_HASH) keys.save(force_insert=True) TestValues.base_create_dep_objs() values.from_hash(SAMPLE_VALUE_HASH) values.save(force_insert=True)
def test_primary_keys_with_keys(self): """Test the method to check primary keys with Keys.""" check_list = Keys.get_primary_keys() self.assertTrue(isinstance(check_list, list)) self.assertTrue(len(check_list) == 1) self.assertTrue('id' in check_list)