def prepare_basic_data(self): pkg = m.Package(name='rnv') self.s.add(pkg) self.s.flush() build = m.Build(package_id=pkg.id, state=m.Build.RUNNING, task_id=666) self.s.add(build) self.s.commit() return pkg, build
def prepare_packages(self, pkg_names): pkgs = [] for name in pkg_names: pkg = self.s.query(m.Package).filter_by(name=name).first() if not pkg: pkg = m.Package(name=name) self.s.add(pkg) pkgs.append(pkg) self.s.commit() return pkgs
def prepare_builds(self, **kwargs): builds = {} for i, (name, state) in enumerate(sorted(kwargs.items())): pkg = m.Package(name=name) self.s.add(pkg) self.s.flush() build = m.Build(package_id=pkg.id, task_id=i + 1, state=state) self.s.add(build) builds[name] = build self.s.commit() return builds
def prio_table(self, tablename='tmp', **kwargs): try: table = Table(tablename, MetaData(), Column('pkg_id', Integer), Column('priority', Integer)) conn = self.s.connection() table.create(bind=conn) priorities = { name: prio for name, prio in kwargs.items() if '_' not in name } builds = { name[:-len("_build")]: state for name, state in kwargs.items() if name.endswith('_build') } states = { name[:-len('_state')]: state for name, state in kwargs.items() if name.endswith('_state') } pkgs = [] for name in priorities.keys(): pkg = self.s.query(m.Package).filter_by(name=name).first() if not pkg: pkg = m.Package(name=name, tracked=states.get(name) != 'ignored', collection_id=self.collection.id) self.s.add(pkg) self.s.flush() if states.get(name, True) is not None: pkg.resolved = states.get(name) != 'unresolved' pkgs.append((name, pkg)) if name in builds: self.s.add( m.Build(package_id=pkg.id, state=builds[name], task_id=self.task_id_counter, version='1', release='1.fc25', repo_id=1 if builds[name] != m.Build.RUNNING else None)) self.task_id_counter += 1 conn.execute(table.insert(), [{ 'pkg_id': pkg.id, 'priority': priorities[name] } for name, pkg in pkgs]) self.s.commit() yield table finally: self.s.rollback() conn = self.s.connection() table.drop(bind=conn, checkfirst=True) self.s.commit()
def test_time_priority(self): for days in [0, 2, 5, 7, 12]: pkg = m.Package(name='p{}'.format(days)) self.s.add(pkg) self.s.flush() build = m.Build(package_id=pkg.id, started=datetime.now() - timedelta(days, hours=1)) self.s.add(build) self.s.commit() query = self.get_scheduler().get_time_priority_query() self.assert_priority_query(query) res = sorted(query.all(), key=lambda x: x.priority) self.assertEqual(5, len(res)) expected_prios = [ -30.0, 161.339324401, 230.787748579, 256.455946637, 297.675251883 ] for item, exp in zip(res, expected_prios): self.assertAlmostEqual(exp, item.priority, places=1)