-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better comment address comments address latest comments
- Loading branch information
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""Tests for the fex FeatureExtractor Collection.""" | ||
|
||
import os | ||
import tempfile | ||
import unittest | ||
|
||
import fex | ||
import mock_extractor as me | ||
|
||
|
||
# Python mock library cannot be used because cannot be pickled for caching. | ||
class _CallCounterMock(object): | ||
"""Mocking class to count function calls.""" | ||
|
||
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 = me.MockExtractor('1', 'col_1', 42) | ||
test_extractor.extract = _CallCounterMock() | ||
|
||
# Create collection, add extractor and run (extract should be called). | ||
collection = fex.Collection(self.cache_file) | ||
collection.add_feature_extractor(test_extractor) | ||
collection.run(self.dataset_file) | ||
counter_first_run = test_extractor.extract.counter | ||
|
||
# Create a new collection with same extractor and run. | ||
collection2 = fex.Collection(self.cache_file) | ||
collection2.add_feature_extractor(test_extractor) | ||
collection2.run(self.dataset_file) | ||
self.assertEqual(counter_first_run, test_extractor.extract.counter) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() # pragma: no cover |