def test_replacement_1(self): n1 = Node().store() n2 = Node().store() n1.set_extra("pippo", [1, 2, u'a']) n1.set_extra("pippobis", [5, 6, u'c']) n2.set_extra("pippo2", [3, 4, u'b']) self.assertEqual(n1.get_extras(), {'pippo': [1, 2, u'a'], 'pippobis': [5, 6, u'c'], '_aiida_hash': n1.get_hash()}) self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()}) new_attrs = {"newval1": "v", "newval2": [1, {"c": "d", "e": 2}]} n1.reset_extras(new_attrs) self.assertEquals(n1.get_extras(), new_attrs) self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()}) n1.del_extra('newval2') del new_attrs['newval2'] self.assertEquals(n1.get_extras(), new_attrs) # Also check that other nodes were not damaged self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()})
def test_replacement_1(self): from aiida.backends.djsite.db.models import DbExtra n1 = Node().store() n2 = Node().store() DbExtra.set_value_for_node(n1._dbnode, "pippo", [1, 2, 'a']) DbExtra.set_value_for_node(n1._dbnode, "pippobis", [5, 6, 'c']) DbExtra.set_value_for_node(n2._dbnode, "pippo2", [3, 4, 'b']) self.assertEquals(n1.get_extras(), {'pippo': [1, 2, 'a'], 'pippobis': [5, 6, 'c'], '_aiida_hash': n1.get_hash() }) self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash() }) new_attrs = {"newval1": "v", "newval2": [1, {"c": "d", "e": 2}]} DbExtra.reset_values_for_node(n1._dbnode, attributes=new_attrs) self.assertEquals(n1.get_extras(), new_attrs) self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()}) DbExtra.del_value_for_node(n1._dbnode, key='newval2') del new_attrs['newval2'] self.assertEquals(n1.get_extras(), new_attrs) # Also check that other nodes were not damaged self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()})
def test_node_access_with_sessions(self): from aiida.utils import timezone from aiida.orm.node import Node import aiida.backends.sqlalchemy as sa from sqlalchemy.orm import sessionmaker from aiida.orm.implementation.sqlalchemy.node import DbNode Session = sessionmaker(bind=sa.engine) custom_session = Session() node = Node().store() master_session = node._dbnode.session self.assertIsNot(master_session, custom_session) # Manually load the DbNode in a different session dbnode_reloaded = custom_session.query(DbNode).get(node.id) # Now, go through one by one changing the possible attributes (of the model) # and check that they're updated when the user reads them from the aiida node def check_attrs_match(name): node_attr = getattr(node, name) dbnode_attr = getattr(dbnode_reloaded, name) self.assertEqual( node_attr, dbnode_attr, "Values of '{}' don't match ({} != {})".format( name, node_attr, dbnode_attr)) def do_value_checks(attr_name, original, changed): try: setattr(node, attr_name, original) except AttributeError: # This may mean that it is immutable, but we should still be able to # change it below directly through the dbnode pass # Refresh the custom session and make sure they match custom_session.refresh(dbnode_reloaded, attribute_names=[str_attr]) check_attrs_match(attr_name) # Change the value in the custom session via the DbNode setattr(dbnode_reloaded, attr_name, changed) custom_session.commit() # Check that the Node 'sees' the change check_attrs_match(str_attr) for str_attr in ['label', 'description']: do_value_checks(str_attr, 'original', 'changed') do_value_checks('nodeversion', 1, 2) do_value_checks('public', True, False) # Attributes self.assertDictEqual(node._attributes(), dbnode_reloaded.attributes) dbnode_reloaded.attributes['test_attrs'] = 'Boo!' custom_session.commit() self.assertDictEqual(node._attributes(), dbnode_reloaded.attributes) # Extras self.assertDictEqual(node.get_extras(), dbnode_reloaded.extras) dbnode_reloaded.extras['test_extras'] = 'Boo!' custom_session.commit() self.assertDictEqual(node._attributes(), dbnode_reloaded.attributes)