def test_preinstalled_guids(): ctx = Context() ctx['utils'] = Mocker() ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) EXPECTED_RESULTS = [('ghi', 3430.0), ('ijk', 3200.0), ('lmn', 420.0), ('klm', 409.99999999999994), ('abc', 23.0)] factory = MockRecommenderFactory() ctx['recommender_factory'] = factory ctx['recommender_map'] = { 'collaborative': factory.create('collaborative'), 'similarity': factory.create('similarity'), 'locale': factory.create('locale') } r = EnsembleRecommender(ctx.child()) # 'hij' should be excluded from the suggestions list # The other two addon GUIDs 'def' and 'jkl' will never be # recommended anyway and should have no impact on results client = {'client_id': '12345', 'installed_addons': ['def', 'hij', 'jkl']} recommendation_list = r.recommend(client, 5) print(recommendation_list) assert isinstance(recommendation_list, list) assert recommendation_list == EXPECTED_RESULTS
def test_weight_cache(): # noqa ctx = Context() ctx['utils'] = Mocker() ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) wc = WeightCache(ctx.child()) actual = wc.getWeights() assert EXPECTED == actual
def get_test_ctx(): fetcher = ProfileFetcher(MockProfileController(None)) factory = MockRecommenderFactory() ctx = Context() ctx['profile_fetcher'] = fetcher ctx['recommender_factory'] = factory # Just populate the utils key for test when WeightCache is # instantiated ctx['utils'] = None return ctx.child()
def test_context(): ctx = Context() ctx['foo'] = 42 child_ctx = ctx.child() assert child_ctx['foo'] == 42 # Now clobber the local context, and demonstrate # that we haven't touched the parent child_ctx['foo'] = 'bar' assert child_ctx['foo'] == 'bar' assert child_ctx.get('foo', 'batz') == 'bar' assert ctx['foo'] == 42 assert ctx.get('foo', 'bar') == 42 # Revert the child back to the parent value del child_ctx['foo'] assert child_ctx['foo'] == 42 # Defaults work as expected assert child_ctx.get('foo', 'bar') == 42
def test_recommendations(): ctx = Context() ctx['utils'] = Mocker() ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) EXPECTED_RESULTS = [('ghi', 3430.0), ('def', 3320.0), ('ijk', 3200.0), ('hij', 3100.0), ('lmn', 420.0)] factory = MockRecommenderFactory() ctx['recommender_factory'] = factory ctx['recommender_map'] = { 'collaborative': factory.create('collaborative'), 'similarity': factory.create('similarity'), 'locale': factory.create('locale') } r = EnsembleRecommender(ctx.child()) client = {'client_id': '12345'} # Anything will work here recommendation_list = r.recommend(client, 5) assert isinstance(recommendation_list, list) assert recommendation_list == EXPECTED_RESULTS
def create_cts_test_ctx(): ctx = Context() ctx['utils'] = MockContinuousData() ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) return ctx.child()
def create_cat_test_ctx(): ctx = Context() ctx['utils'] = MockCategoricalData() ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) return ctx.child()
def create_test_ctx(): ctx = Context() ctx['utils'] = MockUtils() ctx['clock'] = Clock() ctx['cache'] = JSONCache(ctx) return ctx.child()