Skip to content

Commit

Permalink
Add tests for caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
dedan committed Mar 28, 2016
1 parent 07e0af6 commit ae93691
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/collection_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"""Tests for the fex FeatureExtractor Collection."""

import os
import tempfile
import unittest

import fex
import test_classes as tc


class CallCounterMock(object):
"""Mocking class to count function calls.
Python mock library cannot be used because cannot be pickled for caching.
"""

def __init__(self):
"""Set counter to zero."""
super(CallCounterMock, self).__init__()
self.counter = 0

def __call__(self):
"""Record calls to class instances."""
self.counter = self.counter + 1


class CollectionTest(unittest.TestCase):
"""Test feature extractor collection."""

def setUp(self):
"""Create temporary file and test data."""
self.dataset_file = tempfile.mktemp()
self.cache_file = tempfile.mktemp()

def tearDown(self):
"""Remove the file after the test."""
os.remove(self.dataset_file)
os.remove(self.cache_file)

def test_extract_should_not_be_called_if_hash_unchanged(self):
"""Feature extractor should only be re-computed when source changed."""
test_extractor = tc.TestExtractor('1', 'col_1', 42)
test_extractor.extract = CallCounterMock()
collection = fex.Collection(self.cache_file)
collection.add_feature_extractor(test_extractor)
collection.run(self.dataset_file)
self.assertEqual(test_extractor.extract.counter, 1)
collection = fex.Collection(self.cache_file)
collection.add_feature_extractor(test_extractor)
collection.run(self.dataset_file)
self.assertEqual(test_extractor.extract.counter, 1)
collection = fex.Collection(self.cache_file)
collection.add_feature_extractor(test_extractor)
test_extractor._source_hash = 'new_value'
collection.run(self.dataset_file)
self.assertEqual(test_extractor.extract.counter, 2)

if __name__ == '__main__':
unittest.main() # pragma: no cover

0 comments on commit ae93691

Please sign in to comment.