Esempio n. 1
0
def test_purge(session):
    job_id_1 = ops.create_new_job([], session)
    ops.finish_job(job_id_1, 'result', session)

    job_id_2 = ops.create_new_job([], session)
    ops.finish_job(job_id_2, 'result', session)
    job_2 = session.query(model.Job).filter_by(id=job_id_2).first()
    job_2.modification_time = datetime.datetime.utcnow() - datetime.timedelta(
        hours=2)
    session.commit()

    job_id_3 = ops.create_new_job([], session)
    job_3 = session.query(model.Job).filter_by(id=job_id_3).first()
    job_3.modification_time = datetime.datetime.utcnow() - datetime.timedelta(
        hours=2)
    session.commit()

    job_id_4 = ops.create_new_job([], session)
    ops.finish_job(job_id_4, 'result', session)
    job_4 = session.query(model.Job).filter_by(id=job_id_4).first()
    job_4.modification_time = datetime.datetime.utcnow() + datetime.timedelta(
        hours=2)
    session.commit()

    ops.purge_old_jobs(session)

    jobs = session.query(model.Job).order_by(model.Job.modification_time).all()

    assert len(jobs) == 3
    assert jobs[0].id == job_id_3
    assert jobs[1].id == job_id_1
    assert jobs[2].id == job_id_4
Esempio n. 2
0
def test_get_next_job(session):
    job_id_1 = ops.create_new_job([], session)
    ops.finish_job(job_id_1, 'result', session)

    job_id_2 = ops.create_new_job([], session)
    ops.finish_job(job_id_2, 'result', session)
    job_2 = session.query(model.Job).filter_by(id=job_id_2).first()
    job_2.creation_time = datetime.datetime.utcnow() - datetime.timedelta(
        hours=2)
    session.commit()

    job_id_3 = ops.create_new_job([], session)
    job_2 = session.query(model.Job).filter_by(id=job_id_3).first()
    job_2.creation_time = datetime.datetime.utcnow() - datetime.timedelta(
        hours=2)
    session.commit()

    job_id_4 = ops.create_new_job([], session)
    ops.finish_job(job_id_4, 'result', session)
    job_4 = session.query(model.Job).filter_by(id=job_id_4).first()
    job_4.creation_time = datetime.datetime.utcnow() + datetime.timedelta(
        hours=2)
    session.commit()

    job = ops.get_next_job(session)
    assert job.id == job_id_3
Esempio n. 3
0
def test_update_status(session):
    job_id = ops.create_new_job([], session)
    ops.update_status(job_id, 3.14159, session)

    result = session.query(model.Job).first()
    assert result.status == pytest.approx(3.14159)
    assert result.finished == 0
Esempio n. 4
0
def test_get_status(session):
    job_id = ops.create_new_job([], session)
    job = session.query(model.Job).first()
    job.status = 3.14159
    session.commit()

    status = ops.get_status(job_id, session)
    assert status.status == pytest.approx(3.14159)
Esempio n. 5
0
def request_training():
    """Request handler for training requests."""
    log = logger.new()
    request_content = flask.request.get_json()
    if request_content is None:
        log.error('frontend::train_request::invalid_json')
        flask.abort(415)

    training_request = extract_training_request(request_content)
    if training_request is None:
        log.error('frontend::train_request::invalid_request')
        flask.abort(400)

    job_id = _database_operations.create_new_job(training_request, Session())
    log.info('frontend::train_request::request_training', job_id=job_id)
    return job_id
Esempio n. 6
0
def test_finish(session):
    modification_time = datetime.datetime.utcnow()

    job_id = ops.create_new_job([], session)
    job = session.query(model.Job).filter_by(id=job_id).first()
    job.modification_time = datetime.datetime.utcnow() - datetime.timedelta(
        hours=2)
    session.commit()

    ops.finish_job(job_id, 'result', session)

    result = session.query(model.Job).first()
    assert result.finished == 1
    assert len(result.training_results) == 1
    assert result.training_results[0].result == b'result'
    assert abs(result.modification_time - modification_time) \
           < datetime.timedelta(seconds=10)
Esempio n. 7
0
def test_create_new_job(session):
    job_id = ops.create_new_job(['foobarbaz'], session)

    results = session.query(model.Job)
    assert results.count() == 1

    job = results.first()
    assert job.id == job_id
    assert job.status == 0.0
    assert job.finished == 0

    assert abs(job.creation_time -
               datetime.datetime.utcnow()) < datetime.timedelta(minutes=1)

    assert len(job.training_results) == 0
    assert len(job.training_jobs) == 1
    assert job.training_jobs[0].training_job == b'["foobarbaz"]'
Esempio n. 8
0
def test_get_results_not_available(session):
    job_id = ops.create_new_job([], session)

    result = ops.get_results(job_id, session)
    assert result is None
Esempio n. 9
0
def test_get_results(session):
    job_id = ops.create_new_job([], session)
    ops.finish_job(job_id, 'result', session)

    result = ops.get_results(job_id, session)
    assert result == 'result'