# Path: eulcommon/djangoextras/taskresult/models.py
# class TaskResult(models.Model):
#     label = models.CharField(max_length=100)
#     object_id = models.CharField(max_length=50)
#     url = models.URLField()
#     created = models.DateTimeField(auto_now_add=True)
#     task_id = models.CharField(max_length=100)
#     task_start = models.DateTimeField(blank=True, null=True)
#     task_end = models.DateTimeField(blank=True, null=True)
# 
#     @property
#     def task(self):
#         return AsyncResult(self.task_id)
# 
#     def __unicode__(self):
#         return self.label
# 
#     @property
#     def duration(self):
#         if self.task_end and self.task_start:
#             return self.task_end - self.task_start
#         else:
#             return '-'
# 
#     @property
#     def status(self):
#         return self.task.status
# 
#     @property
#     def result(self):
#         return self.task.result or ''
# 
#     # use font-awesome icons for short-hand status display?
#     unknown_icon = 'glyphicon-question-sign'
#     status_icon_map = {
#         'pending': 'glyphicon-time',
#         'success': 'glyphicon-ok',
#         'failure': 'glyphicon-remove',
#         # 'failure': 'glyphicon-alert',  # doesn't display in keep; version?
#         '': unknown_icon
#     }
#     status_style = {
#         'pending': 'text-muted',
#         'success': 'text-success',
#         'failure': 'text-danger',
#         '': 'text-warning',
#     }
# 
#     def status_icon(self):
#         'glyphicon for task status; requires bootstrap'
#         icon = self.status_icon_map.get(self.status.lower(),
#                                         self.unknown_icon)
#         style = self.status_style.get(self.status.lower(), '')
#         return mark_safe(
#             '<span class="glyphicon %s %s" aria-hidden="true"></span>' %
#             (icon, style))
#     status_icon.short_description = 'Status'


from time import sleep
from celery.signals import task_prerun, task_postrun
from django import VERSION as django_version
from django.core.urlresolvers import reverse
from django.test import TestCase
from .models import TaskResult
import pytest
import test_setup
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.




# ensure testsettings are loaded
# NOTE: could cause problems if tests are run in another context (?)


# if django_version < (1, 8):


class TaskResultTestCase(TestCase):

    def test_start_end_duration(self):
        # create a task and send pre/post run signals to test signal handlers
        id = 'foo'
        tr = TaskResult(label='test task', object_id='id', url='/foo', task_id=id)
        tr.save()
        task_time = 2
        task_prerun.send(sender=TaskResultTestCase, task_id=id)
        sleep(task_time)
        task_postrun.send(sender=TaskResultTestCase, task_id=id)


# Path: eulcommon/djangoextras/taskresult/models.py
# class TaskResult(models.Model):
#     label = models.CharField(max_length=100)
#     object_id = models.CharField(max_length=50)
#     url = models.URLField()
#     created = models.DateTimeField(auto_now_add=True)
#     task_id = models.CharField(max_length=100)
#     task_start = models.DateTimeField(blank=True, null=True)
#     task_end = models.DateTimeField(blank=True, null=True)
# 
#     @property
#     def task(self):
#         return AsyncResult(self.task_id)
# 
#     def __unicode__(self):
#         return self.label
# 
#     @property
#     def duration(self):
#         if self.task_end and self.task_start:
#             return self.task_end - self.task_start
#         else:
#             return '-'
# 
#     @property
#     def status(self):
#         return self.task.status
# 
#     @property
#     def result(self):
#         return self.task.result or ''
# 
#     # use font-awesome icons for short-hand status display?
#     unknown_icon = 'glyphicon-question-sign'
#     status_icon_map = {
#         'pending': 'glyphicon-time',
#         'success': 'glyphicon-ok',
#         'failure': 'glyphicon-remove',
#         # 'failure': 'glyphicon-alert',  # doesn't display in keep; version?
#         '': unknown_icon
#     }
#     status_style = {
#         'pending': 'text-muted',
#         'success': 'text-success',
#         'failure': 'text-danger',
#         '': 'text-warning',
#     }
# 
#     def status_icon(self):
#         'glyphicon for task status; requires bootstrap'
#         icon = self.status_icon_map.get(self.status.lower(),
#                                         self.unknown_icon)
#         style = self.status_style.get(self.status.lower(), '')
#         return mark_safe(
#             '<span class="glyphicon %s %s" aria-hidden="true"></span>' %
#             (icon, style))
#     status_icon.short_description = 'Status'


# Path: eulcommon/djangoextras/taskresult/tests.py
from time import sleep
from celery.signals import task_prerun, task_postrun
from django import VERSION as django_version
from django.core.urlresolvers import reverse
from django.test import TestCase
from .models import TaskResult
import pytest
import test_setup

#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.




# ensure testsettings are loaded
# NOTE: could cause problems if tests are run in another context (?)


# if django_version < (1, 8):


class TaskResultTestCase(TestCase):

    def test_start_end_duration(self):
        # create a task and send pre/post run signals to test signal handlers
        id = 'foo'
        tr = TaskResult(label='test task', object_id='id', url='/foo', task_id=id)
        tr.save()
        task_time = 2
        task_prerun.send(sender=TaskResultTestCase, task_id=id)
        sleep(task_time)
        task_postrun.send(sender=TaskResultTestCase, task_id=id)


        tr = TaskResult.objects.get(task_id=id)