def test_flow():
    """Verify that the whole flow works."""

    data = {"toaster": "bidule"}
    amount = 10
    job = JobProgress(data=data, amount=amount)

    expected = {
        'amount': 10,
        'data': {
            'toaster': 'bidule'
        },
        'id': mock.ANY,
        'is_ready': False,
        'progress': {
            'PENDING': 10
        },
        'state': 'PENDING',
    }
    assert job.state == states.PENDING
    assert job.to_dict() == expected

    # Add all units as success
    for _ in range(amount):
        job.add_one_success()

    expected = {
        'amount': 10,
        'data': {
            'toaster': 'bidule'
        },
        'id': mock.ANY,
        'is_ready': False,
        'progress': {
            'SUCCESS': 10
        },
        'state': 'PENDING',
    }
    assert job.state == states.PENDING
    assert job.to_dict() == expected

    # Set the job itself to success
    job.state = states.SUCCESS

    expected = {
        'amount': 10,
        'data': {
            'toaster': 'bidule'
        },
        'id': mock.ANY,
        'is_ready': True,
        'progress': {
            'SUCCESS': 10
        },
        'state': 'SUCCESS',
    }
    assert job.state == states.SUCCESS
    assert job.to_dict() == expected
def test_flow():
    """Verify that the whole flow works."""

    data = {"toaster": "bidule"}
    amount = 10
    job = JobProgress(data=data, amount=amount)

    expected = {
        'amount': 10,
        'data': {'toaster': 'bidule'},
        'id': mock.ANY,
        'is_ready': False,
        'progress': {'PENDING': 10},
        'state': 'PENDING',
    }
    assert job.state == states.PENDING
    assert job.to_dict() == expected

    # Add all units as success
    for _ in range(amount):
        job.add_one_success()

    expected = {
        'amount': 10,
        'data': {'toaster': 'bidule'},
        'id': mock.ANY,
        'is_ready': False,
        'progress': {'SUCCESS': 10},
        'state': 'PENDING',
    }
    assert job.state == states.PENDING
    assert job.to_dict() == expected

    # Set the job itself to success
    job.state = states.SUCCESS

    expected = {
        'amount': 10,
        'data': {'toaster': 'bidule'},
        'id': mock.ANY,
        'is_ready': True,
        'progress': {'SUCCESS': 10},
        'state': 'SUCCESS',
    }
    assert job.state == states.SUCCESS
    assert job.to_dict() == expected
def test_staled_job():
    """Verify that we can check if a job is staled."""
    job = JobProgress({}, amount=1)
    assert job.is_staled is False

    job.state = states.STARTED
    job.add_one_success()
    assert job.is_staled is False

    # delete the heartbeat key.
    job.backend.client.delete(job.backend._get_metadata_key(
        job.backend._get_key_for_job_id(job.id), "heartbeat"
    ))
    assert job.is_staled is True

    utils.fail_staled_jobs(JobProgress.session)
    assert job.state == states.FAILURE

    jobs = JobProgress.query(state=states.STARTED)
    assert jobs == []

    jobs = JobProgress.query(state=states.FAILURE)
    assert jobs == [job]
def test_staled_job():
    """Verify that we can check if a job is staled."""
    job = JobProgress({}, amount=1)
    assert job.is_staled is False

    job.state = states.STARTED
    job.add_one_success()
    assert job.is_staled is False

    # delete the heartbeat key.
    job.backend.client.delete(
        job.backend._get_metadata_key(job.backend._get_key_for_job_id(job.id),
                                      "heartbeat"))
    assert job.is_staled is True

    utils.fail_staled_jobs(JobProgress.session)
    assert job.state == states.FAILURE

    jobs = JobProgress.query(state=states.STARTED)
    assert jobs == []

    jobs = JobProgress.query(state=states.FAILURE)
    assert jobs == [job]