Skip to content

Commit

Permalink
Add callback tests - improve coverage (#228)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanwharris authored and MattPainter01 committed Jul 20, 2018
1 parent 3027854 commit 2527da6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
31 changes: 29 additions & 2 deletions tests/callbacks/test_callbacks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
from unittest import TestCase
from unittest.mock import MagicMock

from torchbearer.callbacks import CallbackList
from torchbearer.callbacks import CallbackList, Callback


class TestCallbackList(TestCase):
class TestCallback(TestCase):
def test_empty_methods(self):
callback = Callback()

self.assertIsNone(callback.on_start({}))
self.assertIsNone(callback.on_start_epoch({}))
self.assertIsNone(callback.on_start_training({}))
self.assertIsNone(callback.on_sample({}))
self.assertIsNone(callback.on_forward({}))
self.assertIsNone(callback.on_criterion({}))
self.assertIsNone(callback.on_backward({}))
self.assertIsNone(callback.on_step_training({}))
self.assertIsNone(callback.on_end_training({}))
self.assertIsNone(callback.on_end_epoch({}))
self.assertIsNone(callback.on_end({}))
self.assertIsNone(callback.on_start_validation({}))
self.assertIsNone(callback.on_sample_validation({}))
self.assertIsNone(callback.on_forward_validation({}))
self.assertIsNone(callback.on_end_validation({}))
self.assertIsNone(callback.on_step_validation({}))


class TestCallbackList(TestCase):
def __init__(self, methodName='runTest'):
super().__init__(methodName)
self.callback_1 = MagicMock()
Expand All @@ -17,3 +38,9 @@ def test_for_list(self):
self.list.on_start({})
self.assertTrue(self.callback_1.method_calls[0][0] == 'on_start')
self.assertTrue(self.callback_2.method_calls[0][0] == 'on_start')

def test_list_in_list(self):
callback = 'test'
clist = CallbackList([callback])
clist2 = CallbackList([clist])
self.assertTrue(clist2.callback_list[0] == 'test')
16 changes: 14 additions & 2 deletions tests/callbacks/test_checkpointers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

import torchbearer
from torchbearer import Model
from torchbearer.callbacks.checkpointers import _Checkpointer, MostRecent, Interval, Best
import os
from torchbearer.callbacks.checkpointers import _Checkpointer, ModelCheckpoint, MostRecent, Interval, Best


class TestCheckpointer(TestCase):
@patch('os.makedirs')
def test_make_dirs(self, mock_dirs):
_Checkpointer('thisdirectoryshouldntexist/norshouldthis/model.pt')
mock_dirs.assert_called_once_with('thisdirectoryshouldntexist/norshouldthis')

@patch("torch.save")
def test_save_checkpoint_save_filename(self, mock_save):
torchmodel = Mock()
Expand Down Expand Up @@ -115,6 +119,14 @@ def test_save_checkpoint_overwrite_recent(self, _, __):
self.assertTrue(check.most_recent == 'test_file_1.pt')


class TestModelCheckpoint(TestCase):
def test_best_only(self):
self.assertTrue(isinstance(ModelCheckpoint(save_best_only=True), Best))

def test_not_best_only(self):
self.assertTrue(isinstance(ModelCheckpoint(save_best_only=False), Interval))


class TestMostRecent(TestCase):
@patch('torchbearer.callbacks.checkpointers._Checkpointer.save_checkpoint')
def test_save(self, mock_save_check):
Expand Down

0 comments on commit 2527da6

Please sign in to comment.