# Path: pq/api.py

# 
# Path: pq/utils/time.py
# def format_time(dt):
#     dt = timestamp_to_datetime(dt)
#     return dt.isoformat() if dt else '?'
# 
# Path: pq/tasks/consumer.py
# def poll_time(a, b, x, lag=0):
#     a = max(a, 0)   # 0 minimum pool gap
#     b = max(a, b)   # b cannot be less than a
#     return max(a + (b-a) * (exp(x) - x - 1)/FACTOR - lag, 0)
# 
# Path: tests/app.py
# def simple_task(self, value=0):
#     return self.v0 + value


import unittest
from datetime import datetime
from unittest import mock
from pq import api
from pq.utils.time import format_time
from pq.tasks.consumer import poll_time
from tests.app import simple_task




class TestTasks(unittest.TestCase):

    def app(self, task_paths=None, **kwargs):
        task_paths = task_paths or ['tests.example.sampletasks.*']
        app = api.QueueApp(task_paths=task_paths, **kwargs)
        app.backend.tasks.queue = mock.MagicMock()
        return app

    def test_decorator(self):
        job_cls = api.job('bla foo', v0=6)(simple_task)
        job = job_cls()
        self.assertIsInstance(job, api.Job)
        self.assertEqual(job(value=4), 10)
        self.assertEqual(str(job), 'bla.foo')
        self.assertFalse(job.task)

    def test_unknown_state(self):
        self.assertEqual(api.status_string(243134), 'UNKNOWN')
        self.assertEqual(api.status_string('jhbjhbj'), 'UNKNOWN')
        self.assertEqual(api.status_string(1), 'SUCCESS')

    def test_format_time(self):
        dt = datetime.now()
        st = format_time(dt)
        self.assertIsInstance(st, str)
        timestamp = dt.timestamp()

# Path: pq/api.py

# 
# Path: pq/utils/time.py
# def format_time(dt):
#     dt = timestamp_to_datetime(dt)
#     return dt.isoformat() if dt else '?'
# 
# Path: pq/tasks/consumer.py
# def poll_time(a, b, x, lag=0):
#     a = max(a, 0)   # 0 minimum pool gap
#     b = max(a, b)   # b cannot be less than a
#     return max(a + (b-a) * (exp(x) - x - 1)/FACTOR - lag, 0)
# 
# Path: tests/app.py
# def simple_task(self, value=0):
#     return self.v0 + value


# Path: tests/test_api.py
import unittest
from datetime import datetime
from unittest import mock
from pq import api
from pq.utils.time import format_time
from pq.tasks.consumer import poll_time
from tests.app import simple_task





class TestTasks(unittest.TestCase):

    def app(self, task_paths=None, **kwargs):
        task_paths = task_paths or ['tests.example.sampletasks.*']
        app = api.QueueApp(task_paths=task_paths, **kwargs)
        app.backend.tasks.queue = mock.MagicMock()
        return app

    def test_decorator(self):
        job_cls = api.job('bla foo', v0=6)(simple_task)
        job = job_cls()
        self.assertIsInstance(job, api.Job)
        self.assertEqual(job(value=4), 10)
        self.assertEqual(str(job), 'bla.foo')
        self.assertFalse(job.task)

    def test_unknown_state(self):
        self.assertEqual(api.status_string(243134), 'UNKNOWN')
        self.assertEqual(api.status_string('jhbjhbj'), 'UNKNOWN')
        self.assertEqual(api.status_string(1), 'SUCCESS')

    def test_format_time(self):
        dt = datetime.now()
        st = format_time(dt)
        self.assertIsInstance(st, str)
        timestamp = dt.timestamp()

        st2 = format_time(timestamp)