def test_feature_should_work_with_cache(self): # Given def fn(tokens, token_index): value = tokens[token_index].value return "%s_%s" % (value, len(value)) mocked_fn = MagicMock(side_effect=fn) cache = [{ TOKEN_NAME: token } for token in tokenize("hello beautiful world", LANGUAGE_EN)] feature = Feature("test_feature", mocked_fn, offset=0) feature.compute(2, cache) feature1 = Feature("test_feature", mocked_fn, offset=1) feature2 = Feature("test_feature", mocked_fn, offset=2) # When res1 = feature1.compute(1, cache) res1_bis = feature1.compute(0, cache) res2 = feature2.compute(0, cache) # Then self.assertEqual(res1, "world_5") self.assertEqual(res1_bis, "beautiful_9") self.assertEqual(res2, "world_5") self.assertEqual(mocked_fn.call_count, 2)
def test_feature_should_work_with_cache(self): # Given def fn(tokens, token_index): value = tokens[token_index].value return "%s_%s" % (value, len(value)) mocked_fn = MagicMock(side_effect=fn) cache = [{TOKEN_NAME: token} for token in tokenize("hello beautiful world", LANGUAGE_EN)] feature = Feature("test_feature", mocked_fn, offset=0) feature.compute(2, cache) feature1 = Feature("test_feature", mocked_fn, offset=1) feature2 = Feature("test_feature", mocked_fn, offset=2) # When res1 = feature1.compute(1, cache) res1_bis = feature1.compute(0, cache) res2 = feature2.compute(0, cache) # Then self.assertEqual(res1, "world_5") self.assertEqual(res1_bis, "beautiful_9") self.assertEqual(res2, "world_5") self.assertEqual(mocked_fn.call_count, 2)
def test_feature_should_work_with_offset(self): # Given def fn(tokens, token_index): value = tokens[token_index].value return "%s_%s" % (value, len(value)) cache = [{TOKEN_NAME: token} for token in tokenize("hello beautiful world", LANGUAGE_EN)] feature = Feature("test_feature", fn, offset=1) # When res = feature.compute(1, cache) # Then self.assertEqual(res, "world_5")
def test_feature_should_work_with_offset(self): # Given def fn(tokens, token_index): value = tokens[token_index].value return "%s_%s" % (value, len(value)) cache = [{ TOKEN_NAME: token } for token in tokenize("hello beautiful world", LANGUAGE_EN)] feature = Feature("test_feature", fn, offset=1) # When res = feature.compute(1, cache) # Then self.assertEqual(res, "world_5")