def post_new_spec(request, dbp, obj, resource): require_permission(request.req, resource, dbp.env, operation="CREATE") if obj is Entity: # instantiating Entity (i.e., creating a spec) pass elif obj is Instance or isinstance(obj, Entity): # instantiating an existing spec return post_new_artifact(request.req, dbp, obj, resource) else: raise Exception("Trying to instantiate something that can't be instantiated '%s'" % (obj,)) name = request.req.args.get('name') parent_name = request.req.args.get('parent') attributes = [Attribute(n,m,t) for n,t,m in _group_spec_attributes(request.req)] if parent_name: dbp.load_spec(parent_name) bases = (dbp.pool.get_item(parent_name),) else: bases = tuple() brand_new_inst = Entity(name=name, attributes=attributes, bases=bases) dbp.pool.add(brand_new_inst) dbp.save(get_reporter_id(request.req), 'comment', request.req.remote_addr) add_notice(request.req, 'Your changes have been saved.') url = request.req.href.customartifacts('spec', brand_new_inst.get_name(), action='view') request.req.redirect(url)
def post_new_spec(request, dbp, obj, resource): require_permission(request.req, resource, dbp.env, operation="CREATE") if obj is Entity: # instantiating Entity (i.e., creating a spec) pass elif obj is Instance or isinstance( obj, Entity): # instantiating an existing spec return post_new_artifact(request.req, dbp, obj, resource) else: raise Exception( "Trying to instantiate something that can't be instantiated '%s'" % (obj, )) name = request.req.args.get('name') parent_name = request.req.args.get('parent') attributes = [ Attribute(n, m, t) for n, t, m in _group_spec_attributes(request.req) ] if parent_name: dbp.load_spec(parent_name) bases = (dbp.pool.get_item(parent_name), ) else: bases = tuple() brand_new_inst = Entity(name=name, attributes=attributes, bases=bases) dbp.pool.add(brand_new_inst) dbp.save(get_reporter_id(request.req), 'comment', request.req.remote_addr) add_notice(request.req, 'Your changes have been saved.') url = request.req.href.customartifacts('spec', brand_new_inst.get_name(), action='view') request.req.redirect(url)
def load_spec(self, spec_name, db=None): # Ignore requests to load the top-most classes of the instantiation chain (Entity and Instance), # as these will not be persisted to the database and will be always available from the pool. if spec_name in (Entity.get_name(), Instance.get_name()): return if not db: db = self.env.get_read_db() version = self._get_latest_spec_version(spec_name, db) if version is None: raise ValueError("No version found for spec with name '%s'" % (spec_name,)) # get the baseclass base_class = None cursor = db.cursor() rows = cursor.execute( """ SELECT base_class FROM asa_spec WHERE name='%s' AND version_id='%d' GROUP BY name""" % (spec_name, version) ) base_class_name = rows.fetchone() if not base_class_name is None and len(base_class_name) > 0: base_class_name = base_class_name[0] # load base classes (recursively until the root is reached) if base_class_name == Instance.get_name(): base_class = Instance else: self.load_spec(base_class_name, db) base_class = self.pool.get_item(base_class_name) bases = (base_class,) if not base_class is None else tuple() # get the attributes attributes = [] cursor = db.cursor() rows = cursor.execute( """ SELECT name, multplicity_low, multplicity_high, type, uiorder FROM asa_spec_attribute WHERE spec_name='%s' AND version_id='%d'""" % (spec_name, version) ) for row in rows.fetchall(): attributes.append(Attribute(name=row[0], multiplicity=(row[1], row[2]), atype=row[3], order=row[4])) # create the entity spec = Entity(name=spec_name, bases=bases, version=version, persisted=True, attributes=attributes) if self.pool.get_item(spec.get_name()) is None: self.pool.add(spec)
def load_spec(self, spec_name, db=None): # Ignore requests to load the top-most classes of the instantiation chain (Entity and Instance), # as these will not be persisted to the database and will be always available from the pool. if spec_name in (Entity.get_name(), Instance.get_name()): return if not db: db = self.env.get_read_db() version = self._get_latest_spec_version(spec_name, db) if version is None: raise ValueError("No version found for spec with name '%s'" % (spec_name,)) # get the baseclass base_class = None cursor = db.cursor() rows = cursor.execute(""" SELECT base_class FROM asa_spec WHERE name='%s' AND version_id='%d' GROUP BY name""" % (spec_name, version)) base_class_name = rows.fetchone() if not base_class_name is None and len(base_class_name) > 0: base_class_name = base_class_name[0] # load base classes (recursively until the root is reached) if base_class_name == Instance.get_name(): base_class = Instance else: self.load_spec(base_class_name, db) base_class = self.pool.get_item(base_class_name) bases = (base_class,) if not base_class is None else tuple() # get the attributes attributes = [] cursor = db.cursor() rows = cursor.execute(""" SELECT name, multplicity_low, multplicity_high, type, uiorder FROM asa_spec_attribute WHERE spec_name='%s' AND version_id='%d'""" % (spec_name, version)) for row in rows.fetchall(): attributes.append(Attribute(name=row[0], multiplicity=(row[1], row[2]), atype=row[3], order=row[4])) # create the entity spec = Entity(name=spec_name, bases=bases, version=version, persisted=True, attributes=attributes) if self.pool.get_item(spec.get_name()) is None: self.pool.add(spec)
def setUp(self): self.env = EnvironmentStub(enable=['trac.*', 'AdaptiveArtifacts.*', 'AdaptiveArtifacts.persistence.db.*']) Setup(self.env).upgrade_environment(self.env.get_db_cnx()) self.Vehicle = Entity(name="Vehicle") self.Car = Entity(name="Car", bases=(self.Vehicle,), attributes=[Attribute(name="Wheels", multiplicity=4, atype=str)] ) self.lightningMcQueen = self.Car( values={"Wheels": ['front left wheel', 'front right wheel', 'rear left wheel', 'front right wheel']} ) pool = InstancePool() pool.add(self.Vehicle) pool.add(self.Car) pool.add(self.lightningMcQueen) dbp = DBPool(self.env, pool) dbp.save('anonymous',"","120.0.0.1")
def build_saved_and_reloaded_pool(testcase): testcase.env = EnvironmentStub(enable=['trac.*', 'AdaptiveArtifacts.*', 'AdaptiveArtifacts.persistence.db.*']) Setup(testcase.env).upgrade_environment(testcase.env.get_db_cnx()) # this works as far as no one inherits from MetaModelInstancesStructureAfterLoad and ModelInstancesStructureAfterLoad super(testcase.__class__, testcase).setUp() dbp = DBPool(testcase.env, testcase.pool) dbp.save('anonymous',"","120.0.0.1") new_pool = InstancePool() new_dbp = DBPool(testcase.env, new_pool) for instance in testcase.pool.get_instances_of(Instance.get_name()): new_dbp.load_artifact(instance.get_id()) for entity in testcase.pool.get_instances_of(Entity.get_name()): new_dbp.load_spec(entity.get_name()) testcase.pool = new_pool
class TestBasicEntityBehaviour(unittest.TestCase): def setUp(self): self.env = EnvironmentStub(enable=['trac.*', 'AdaptiveArtifacts.*', 'AdaptiveArtifacts.persistence.db.*']) Setup(self.env).upgrade_environment(self.env.get_db_cnx()) self.Vehicle = Entity(name="Vehicle") self.Car = Entity(name="Car", bases=(self.Vehicle,), attributes=[Attribute(name="Wheels", multiplicity=4, atype=str)] ) self.lightningMcQueen = self.Car( values={"Wheels": ['front left wheel', 'front right wheel', 'rear left wheel', 'front right wheel']} ) pool = InstancePool() pool.add(self.Vehicle) pool.add(self.Car) pool.add(self.lightningMcQueen) dbp = DBPool(self.env, pool) dbp.save('anonymous',"","120.0.0.1") def test_load_one(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) lightningMcQueen = pool.get_item(self.lightningMcQueen.get_id()) self.assertEqual(lightningMcQueen.get_id(), self.lightningMcQueen.get_id()) self.assertEqual(len(lightningMcQueen.get_value("Wheels")), 4) def test_load_related(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) lightningMcQueen = pool.get_item(self.lightningMcQueen.get_id()) self.assertTrue(not pool.get_item(self.Car.get_id()) is None) self.assertTrue(not pool.get_item(self.Vehicle.get_id()) is None) def test_load_all_specs(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_specs() self.assertEqual(len(pool.get_items((1,))), 2) self.assertTrue(not pool.get_item(self.Car.get_id()) is None) self.assertTrue(not pool.get_item(self.Vehicle.get_id()) is None) def test_load_artifacts_of_spec(self): # add a couple more instances pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_specs() car1 = self.Car(values={"License": "GO-42-42"}) plane2 = self.Vehicle(values={"License": "GO-55-55"}) pool.add(car1) pool.add(plane2) dbp.save('me', 'a couple more instances', '127.0.0.1') # load cars only pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifacts_of(self.Car.get_name()) self.assertEqual(len(pool.get_items((0,))), 2) self.assertTrue(not pool.get_item(self.lightningMcQueen.get_id()) is None) self.assertTrue(not pool.get_item(car1.get_id()) is None) def test_edit_spec_name(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_spec("Car") Car = dbp.pool.get_item("Car") # create three more versions Car._is_modified = True dbp.save('me', 'a new version', '127.0.0.1') Car._is_modified = True dbp.save('me', 'a new version', '127.0.0.1') Car._is_modified = True dbp.save('me', 'a new version', '127.0.0.1') # so, there should be 4 versions now ch = [(version, timestamp, author, ipnr, comment) for version, timestamp, author, ipnr, comment in dbp.get_history(Car)] self.assertEqual(len(ch), 4) # edit the name Car._replace_name("Automobile") dbp.save('me', 'a couple more instances', '127.0.0.1') # querying by the new name should render 5 versions self.assertEqual(Car.get_name(), "Automobile") ch = [(version, timestamp, author, ipnr, comment) for version, timestamp, author, ipnr, comment in dbp.get_history(Car)] self.assertEqual(len(ch), 5) def test_delete_unmodified(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) lightningMcQueen = pool.get_item(self.lightningMcQueen.get_id()) dbp.delete(lightningMcQueen, 'me', 'deleted stuff', '127.0.0.1') self.assertTrue(pool.get_item(lightningMcQueen.get_id()) is None) pool2 = InstancePool() dbp2 = DBPool(self.env, pool2) self.assertRaises(ValueError, dbp2.load_artifact, self.lightningMcQueen.get_id()) self.assertTrue(pool2.get_item(self.lightningMcQueen.get_id()) is None) def test_delete_new(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) sallyCarrera = self.Car() pool.add(sallyCarrera) dbp = DBPool(self.env, pool) dbp.delete(sallyCarrera, 'me', 'deleted stuff', '127.0.0.1') self.assertEqual(3, len(dbp.pool.get_items((0,1)))) def test_delete_changed(self): pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) lightningMcQueen = pool.get_item(self.lightningMcQueen.get_id()) self.lightningMcQueen.set_value('Wheels', ['front middle wheel', 'rear left wheel', 'front right wheel']) dbp.delete(lightningMcQueen, 'me', 'deleted stuff', '127.0.0.1') self.assertTrue(pool.get_item(lightningMcQueen.get_id()) is None) self.assertEqual(2, len(dbp.pool.get_items((0,1)))) pool2 = InstancePool() dbp2 = DBPool(self.env, pool2) self.assertRaises(ValueError, dbp2.load_artifact, self.lightningMcQueen.get_id()) self.assertTrue(pool2.get_item(self.lightningMcQueen.get_id()) is None) def test_retrieve_history(self): # make change pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) lm = pool.get_item(self.lightningMcQueen.get_id()) lm.set_value('License', 'GO-42-42') dbp.save('me', 'added license information', '127.0.0.1') # reload and inspect pool = InstancePool() dbp = DBPool(self.env, pool) dbp.load_artifact(self.lightningMcQueen.get_id()) lm = pool.get_item(self.lightningMcQueen.get_id()) h = [(version, timestamp, author, ipnr, comment) for version, timestamp, author, ipnr, comment in dbp.get_history(lm)] self.assertEqual(len(h), 2)