Skip to content

Commit

Permalink
add unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrogr committed Oct 31, 2017
1 parent 89ef51e commit 5399aa5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 16 deletions.
4 changes: 1 addition & 3 deletions deeptracy/tasks/base_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import logging
from celery import Task

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
logger = logging.getLogger('deeptracy')


class DeeptracyTask(Task):
"""Default class for all task in deeptracy. It has error handling for logging all celery failures in tasks"""
def on_failure(self, exc, task_id, args, kwargs, einfo):
logger.info('-----ON FAILURE-----')
logger.exception('celery task failure', exc_info=exc)
super().on_failure(exc, task_id, args, kwargs, einfo)

Expand Down
13 changes: 0 additions & 13 deletions tests/__init__.py

This file was deleted.

32 changes: 32 additions & 0 deletions tests/unit/test_base_task.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2017 BBVA
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 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.

from unittest import TestCase, mock
from unittest.mock import ANY

from deeptracy.tasks.base_task import DeeptracyTask


class TestBaseTask(TestCase):

@mock.patch('deeptracy.tasks.base_task.logger')
def test_base_task_on_failure(self, mock_logger):
"""After initialization metadata should create all models"""
base_task = DeeptracyTask()
# on_failure(self, exc, task_id, args, kwargs, einfo):
exc = ValueError('test_base_task_on_failure')
base_task.on_failure(exc, 'task_id', None, None, None)

self.assertTrue(mock_logger.exception.called)
mock_logger.exception.assert_called_with(ANY, exc_info=exc)

0 comments on commit 5399aa5

Please sign in to comment.