<commit_before>from datetime import datetime, timedelta

import pytest

from jobcontrol.exceptions import NotFound


def test_job_crud(storage):  # todo: test list/iter too!
    job_id = storage.create_job(
        'datacat.utils.testing:job_simple_echo',
        args=['foo', 'bar', 'baz'],
        kwargs={'spam': 100, 'eggs': 200, 'bacon': 500})

    assert isinstance(job_id, int)

    job_info = storage.get_job(job_id)
    assert job_info['id'] == job_id
    assert job_info['function'] == 'datacat.utils.testing:job_simple_echo'
    assert job_info['args'] == ['foo', 'bar', 'baz']
    assert job_info['kwargs'] == {'spam': 100, 'eggs': 200, 'bacon': 500}
    assert job_info['dependencies'] == []
    assert storage.get_job_deps(job_id) == []

    assert isinstance(job_info['ctime'], datetime)
    assert datetime.now() - job_info['ctime'] <= timedelta(seconds=2)

    storage.update_job(job_id, function='foo:bar')
    storage.update_job(job_id, args=['foo', 'bar'], kwargs={'A': 1})
    storage.update_job(job_id, dependencies=[1, 2, 3])

    job_info = storage.get_job(job_id)
    assert job_info['id'] == job_id
    assert job_info['function'] == 'foo:bar'
    assert job_info['args'] == ['foo', 'bar']
    assert job_info['kwargs'] == {'A': 1}
    assert job_info['dependencies'] == [1, 2, 3]
    # assert len(storage.get_job_deps(job_id)) == 1

    storage.delete_job(job_id)

    with pytest.raises(NotFound):
        storage.get_job(job_id)


def test_job_list_iter_mget(storage):
    # With empty db
    assert storage.list_jobs() == []
    assert list(storage.iter_jobs()) == []
    assert storage.mget_jobs([1, 2, 3, 4]) == []

    # Create jobs
    job_ids = [storage.create_job('foo:bar', args=[x]) for x in xrange(5)]

    # Query..
    assert storage.list_jobs() == job_ids

    _jobs = list(storage.iter_jobs())
    assert [x['id'] for x in _jobs] == job_ids
    for x in _jobs:
        assert x['function'] == 'foo:bar'

    _jobs2 = storage.mget_jobs(job_ids)
    assert _jobs2 == _jobs

    for jid in job_ids:
        storage.delete_job(jid)


def test_job_default_values(storage):
    job_id = storage.create_job('datacat.utils.testing:job_simple_echo')

    assert isinstance(job_id, int)

    job_info = storage.get_job(job_id)
    assert job_info['id'] == job_id
    assert job_info['function'] == 'datacat.utils.testing:job_simple_echo'
    assert job_info['args'] == []
    assert job_info['kwargs'] == {}
    assert job_info['dependencies'] == []
    assert storage.get_job_deps(job_id) == []

    storage.delete_job(job_id)


def test_job_deps(storage):
    job_id1 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['A'])
    job_id2 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['B'])
    job_id3 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['C'])
    job_id4 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['C'])<commit_msg>args is a tuple, not list<commit_after>from datetime import datetime, timedelta

import pytest

from jobcontrol.exceptions import NotFound


def test_job_crud(storage):  # todo: test list/iter too!
    job_id = storage.create_job(
        'datacat.utils.testing:job_simple_echo',
        args=['foo', 'bar', 'baz'],
        kwargs={'spam': 100, 'eggs': 200, 'bacon': 500})

    assert isinstance(job_id, int)

    job_info = storage.get_job(job_id)
    assert job_info['id'] == job_id
    assert job_info['function'] == 'datacat.utils.testing:job_simple_echo'
    assert job_info['args'] == ['foo', 'bar', 'baz']
    assert job_info['kwargs'] == {'spam': 100, 'eggs': 200, 'bacon': 500}
    assert job_info['dependencies'] == []
    assert storage.get_job_deps(job_id) == []

    assert isinstance(job_info['ctime'], datetime)
    assert datetime.now() - job_info['ctime'] <= timedelta(seconds=2)

    storage.update_job(job_id, function='foo:bar')
    storage.update_job(job_id, args=['foo', 'bar'], kwargs={'A': 1})
    storage.update_job(job_id, dependencies=[1, 2, 3])

    job_info = storage.get_job(job_id)
    assert job_info['id'] == job_id
    assert job_info['function'] == 'foo:bar'
    assert job_info['args'] == ['foo', 'bar']
    assert job_info['kwargs'] == {'A': 1}
    assert job_info['dependencies'] == [1, 2, 3]
    # assert len(storage.get_job_deps(job_id)) == 1

    storage.delete_job(job_id)

    with pytest.raises(NotFound):
        storage.get_job(job_id)


def test_job_list_iter_mget(storage):
    # With empty db
    assert storage.list_jobs() == []
    assert list(storage.iter_jobs()) == []
    assert storage.mget_jobs([1, 2, 3, 4]) == []

    # Create jobs
    job_ids = [storage.create_job('foo:bar', args=[x]) for x in xrange(5)]

    # Query..
    assert storage.list_jobs() == job_ids

    _jobs = list(storage.iter_jobs())
    assert [x['id'] for x in _jobs] == job_ids
    for x in _jobs:
        assert x['function'] == 'foo:bar'

    _jobs2 = storage.mget_jobs(job_ids)
    assert _jobs2 == _jobs

    for jid in job_ids:
        storage.delete_job(jid)


def test_job_default_values(storage):
    job_id = storage.create_job('datacat.utils.testing:job_simple_echo')

    assert isinstance(job_id, int)

    job_info = storage.get_job(job_id)
    assert job_info['id'] == job_id
    assert job_info['function'] == 'datacat.utils.testing:job_simple_echo'
    assert job_info['args'] == ()
    assert job_info['kwargs'] == {}
    assert job_info['dependencies'] == []
    assert storage.get_job_deps(job_id) == []

    storage.delete_job(job_id)


def test_job_deps(storage):
    job_id1 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['A'])
    job_id2 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['B'])
    job_id3 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['C'])
    job_id4 = storage.create_job(
        'datacat.utils.testing:job_simple_echo', args=['C'])
{'fraction_non_alphanumeric': 0.15813253012048192, 'fraction_numerical': 0.013052208835341365, 'mean_word_length': 3.883169934640523, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5286745', 'n_tokens_mistral': 2223, 'n_tokens_neox': 2100, 'n_words': 400}