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 expand_macro(self, formatter, name, content):
        # Example: [[ASA(42)]]
        args, kw = parse_args(content)
        args = [arg.strip() for arg in args]
        if not args or not args[0].isdigit():
            raise TracError('Custom artifact id not specified')
        args, kw = parse_args(content)
        if not args or not args[0].isdigit():
            raise TracError('Custom artifact id not specified')
        artifact_id = int(args[0])
        dbp = DBPool(self.env, InstancePool())
        try:
            dbp.load_artifact(id=artifact_id)
        except ValueError:
            return system_message("Custom Artifact not found", "No custom artifact was found for id '{0}'.".format(artifact_id))
        artifact = dbp.pool.get_item(id=artifact_id)
        artifact_url = formatter.req.href.customartifacts('artifact/{0}'.format(artifact.get_id()))
        res = Core._get_resource(artifact) if not artifact in (Entity, Instance, None) and not type(artifact)==unicode else None
        spec_name, spec_url, values = _get_artifact_details(artifact, formatter.req)

        tpl='view_artifact_dialog.html'
        data = {
            'context': Context.from_request(formatter.req, res),
            'spec_name': spec_name,
            'spec_url': spec_url,
            'artifact': artifact,
            'artifact_url': artifact_url,
            'artifacts_values': values,
        }
        return Chrome(self.env).render_template(formatter.req, tpl, data, None, fragment=True)
 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_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_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 get_search_results(self, req, terms, filters):
        if 'asa-filter' in filters:
            for a_id, attr_name, attr_value, vid, time, author in Searcher.search(self.env, terms):
                dbp = DBPool(self.env, InstancePool())
                dbp.load_artifact(a_id)
                artifact = dbp.pool.get_item(a_id)

                res = Resource('asa', a_id, vid)
                link = get_resource_url(self.env, res, req.href)
                title = unicode(artifact)
                text = u"Custom Artifact of the type {0}.".format(artifact.__class__.get_name())
                text += u" {0}: {1}".format(attr_name, shorten_result(attr_value, terms))
                yield (link, title, time, author, text)
        return
    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)
    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
    def _get_link(self, href, artifact_id, label=None, art_attr=None):
        try:
            pool = InstancePool()
            dbp = DBPool(self.env, pool)
            dbp.load_artifact(id=artifact_id)
            artifact = pool.get_item(id=artifact_id)
            spec_name = artifact.__class__.get_name() if not artifact.__class__ is Instance else None
            if not art_attr is None:
                label = artifact.get_value(art_attr)
                if label is None:
                    return inline_system_message(u"Error: The attribute '{0}' was not found.".format(art_attr))

            if label is None:
                label = str(artifact)
            if spec_name is None:
                title = "Custom Software Artifact '%s'" % (label,)
            else:
                title = "Custom Software Artifact '%s' of type '%s'" % (label, spec_name)
        except ValueError:
            title = "Custom Software Artifact with ID '%s' does not exist" % (artifact_id,)
        return tag.a(label, href=href.customartifacts('artifact', artifact_id), class_="asa-link", title=title)