from datetime import datetime, timedelta
from rq.job import Job

from rq_retry_scheduler import queue


def target_function(*args, **kwargs):
    pass


def test_logger_name():
    assert queue.logger.name == 'rq:retryscheduler:queue'


def test_job_key():
    assert queue.Queue.scheduler_jobs_key == 'rq:retryscheduler:scheduled_jobs'


def test_enqueue_at(mock, connection):
    q = queue.Queue('unittest', connection=connection)

    enqueue = mock.patch.object(q, 'enqueue')

    dt = datetime(2016, 1, 1, 0, 0, 0)

    args = 'unit', 'tests'
    kwargs = {'are': 'cool'}
    meta = {'enqueue_at': dt}

    q.enqueue_at(dt, target_function, *args, **kwargs)

    enqueue.assert_called_with(
        target_function, args=args, kwargs=kwargs, meta=meta)


def test_enqueue_in(mock, connection):
    q = queue.Queue('unittest', connection=connection)

    dt = datetime.utcnow().replace(microsecond=0)
    q.current_time = lambda: dt

    td = timedelta(minutes=5)

    enqueue = mock.patch.object(q, 'enqueue')

    args = 'unit', 'tests'
    kwargs = {'are': 'cool'}
    meta = {'enqueue_at': dt + td}

    q.enqueue_in(td, target_function, *args, **kwargs)

    enqueue.assert_called_with(
        target_function, args=args, kwargs=kwargs, meta=meta)


def test_enqueue_job_at(mock, connection):
    q = queue.Queue('unittest', connection=connection)

    enqueue = mock.patch.object(q, 'enqueue_job')

    dt = datetime(2016, 1, 1, 0, 0, 0)

    args = 'unit', 'tests'
    kwargs = {'are': 'cool'}

    job = Job.create(
        target_function, args=args, kwargs=kwargs, connection=connection)

    assert 'enqueue_at' not in job.meta

    q.enqueue_job_at(dt, job)

    enqueue.assert_called_with(job)

    assert 'enqueue_at' in job.meta
    assert job.meta['enqueue_at'] == dt


def test_enqueue_job_in(mock, connection):
    q = queue.Queue('unittest', connection=connection)

    dt = datetime.utcnow().replace(microsecond=0)
    q.current_time = lambda: dt

    td = timedelta(minutes=5)

    enqueue = mock.patch.object(q, 'enqueue_job')

    args = 'unit', 'tests'
    kwargs = {'are': 'cool'}

    job = Job.create(
        target_function, args=args, kwargs=kwargs, connection=connection)

    assert 'enqueue_at' not in job.meta

    q.enqueue_job_in(td, job)

    enqueue.assert_called_with(job)

    assert 'enqueue_at' in job.meta
    assert job.meta['enqueue_at'] == dt + td

@@ -56,16 +56,30 @@
 port Job
+%0Aimport pytest
 %0A%0Afrom r
@@ -108,16 +108,117 @@
 rt queue
+, util%0A%0A%0A@pytest.fixture%0Adef q(connection):%0A    return queue.Queue('unittest', connection=connection)
 %0A%0A%0Adef t
@@ -477,76 +477,11 @@
 ck, 
-connection):%0A    q = queue.Queue('unittest', connection=connection)%0A
+q):
 %0A   
@@ -833,76 +833,11 @@
 ck, 
-connection):%0A    q = queue.Queue('unittest', connection=connection)%0A
+q):
 %0A   
@@ -1263,24 +1263,27 @@
 job_at(mock,
+ q,
  connection)
@@ -1288,64 +1288,8 @@
 n):%0A
-    q = queue.Queue('unittest', connection=connection)%0A%0A
     
@@ -1745,16 +1745,19 @@
 in(mock,
+ q,
  connect
@@ -1766,64 +1766,8 @@
 n):%0A
-    q = queue.Queue('unittest', connection=connection)%0A%0A
     

Use a fixture for making the Queue