def test_recommendations_via_manager():  # noqa
    ctx = get_test_ctx()

    EXPECTED_RESULTS = [('ghi', 3430.0), ('def', 3320.0), ('ijk', 3200.0),
                        ('hij', 3100.0), ('lmn', 420.0),
                        ('klm', 409.99999999999994), ('jkl', 400.0),
                        ('abc', 23.0), ('fgh', 22.0), ('efg', 21.0)]

    factory = MockRecommenderFactory()

    class MockProfileFetcher:
        def get(self, client_id):
            return {'client_id': client_id}

    ctx['recommender_factory'] = factory
    ctx['profile_fetcher'] = MockProfileFetcher()
    ctx['utils'] = Mocker()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend({'client_id': 'some_ignored_id'},
                                            10,
                                            extra_data={'branch': 'ensemble'})
    assert isinstance(recommendation_list, list)
    assert recommendation_list == EXPECTED_RESULTS
def test_recommendation_strategy():
    """The recommendation manager is currently very naive and just
    selects the first recommender which returns 'True' to
    can_recommend()."""

    EXPECTED_ADDONS = ["expected_id", "other-id"]

    # Create a stub ProfileFetcher that always returns the same
    # client data.
    class StubFetcher:
        def get(self, client_id):
            return {'client_id': '00000'}

    # Configure the recommender so that only the second model
    # can recommend and return the expected addons.
    factory = MockRecommenderFactory(
        legacy=lambda: StubRecommender(False, []),
        collaborative=lambda: StubRecommender(True, EXPECTED_ADDONS),
        similarity=lambda: StubRecommender(False, []),
        locale=lambda: StubRecommender(False, []))

    # Make sure the recommender returns the expected addons.
    ctx = get_test_ctx()
    ctx['recommender_factory'] = factory
    ctx['profile_fetcher'] = StubFetcher()
    ctx['utils'] = Mocker()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    manager = RecommendationManager(ctx.child())
    results = manager.recommend("client-id",
                                10,
                                extra_data={'branch': 'linear'})
    assert results == EXPECTED_ADDONS
Esempio n. 3
0
def test_fixed_client_id_empty_list(test_ctx):
    ctx = install_mocks(test_ctx)
    ctx = install_mock_curated_data(ctx)

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend(EMPTY_TEST_CLIENT_IDS[0], 10)

    assert len(recommendation_list) == 0
Esempio n. 4
0
def test_intervention_control(test_ctx):
    ctx = install_mocks(test_ctx)
    ctx = install_mock_curated_data(ctx)

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend('some_ignored_id',
                                            10,
                                            extra_data={'branch': INTERVENTION_CONTROL})

    assert len(recommendation_list) == 0
Esempio n. 5
0
def test_none_profile_returns_empty_list(test_ctx):
    ctx = install_mocks(test_ctx)

    class MockProfileFetcher:
        def get(self, client_id):
            return None
    ctx["profile_fetcher"] = MockProfileFetcher()

    rec_manager = RecommendationManager(ctx)
    assert rec_manager.recommend("random-client-id", 10) == []
def test_none_profile_returns_empty_list(monkeypatch):
    monkeypatch.setattr(hbase_client.HBaseClient, 'get_client_profile',
                        lambda x, y: None)

    monkeypatch.setattr(hbase_client.HBaseClient, '_get_hbase_hostname',
                        lambda x: 'master-ip-address')

    fetcher = ProfileFetcher()

    rec_manager = RecommendationManager(fetcher, ("fake-recommender", ))
    assert rec_manager.recommend("random-client-id", 10) == []
Esempio n. 7
0
def test_intervention_b(test_ctx):
    """The recommendation manager is currently very naive and just
    selects the first recommender which returns 'True' to
    can_recommend()."""

    ctx = install_mocks(test_ctx)
    ctx = install_mock_curated_data(ctx)

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend('some_ignored_id',
                                            4,
                                            extra_data={'branch': INTERVENTION_B})

    assert isinstance(recommendation_list, list)
    assert len(recommendation_list) == 4
Esempio n. 8
0
def test_fixed_client_id_empty_list(test_ctx):
    ctx = install_mocks(test_ctx)
    ctx = install_mock_curated_data(ctx)

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend(EMPTY_TEST_CLIENT_IDS[0],
                                            10,
                                            extra_data={'branch': INTERVENTION_A})

    assert len(recommendation_list) == 0

    recommendation_list = manager.recommend(EMPTY_TEST_CLIENT_IDS[0],
                                            10,
                                            extra_data={'branch': INTERVENTION_B})

    assert len(recommendation_list) == 0
Esempio n. 9
0
def test_intervention_names(test_ctx):
    # verify that intervention names can use hyphens instead of
    # underscore
    ctx = install_mocks(test_ctx)
    ctx = install_mock_curated_data(ctx)

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend(TEST_CLIENT_IDS[0],
                                            10,
                                            extra_data={'branch': 'intervention-a'})

    assert len(recommendation_list) == 10

    recommendation_list = manager.recommend(TEST_CLIENT_IDS[0],
                                            10,
                                            extra_data={'branch': 'intervention-b'})

    assert len(recommendation_list) == 10
Esempio n. 10
0
def get_client_recommendations(request):
    form = AddonRecommendationsForm()
    recommendations = []
    if request.method == 'POST':
        form = AddonRecommendationsForm(data=request.POST)
        if form.is_valid():
            # Use addon recommender.
            value = form.cleaned_data['client_id']
            num_items = form.cleaned_data['num_items']
            recommendation_manager = RecommendationManager()
            recommendations = recommendation_manager.recommend(value,
                                                               num_items)
    context = {
        'form': form,
        'recommendations': recommendations
    }

    return render(request, template_name="taar/index.html", context=context)
def test_recommendation_strategy():
    EXPECTED_ADDONS = ["expected_id", "other-id"]

    # Create a stub ProfileFetcher that always returns the same
    # client data.
    class StubFetcher:
        def get(self, client_id):
            return {}

    # Configure the recommender so that only the second model
    # can recommend and return the expected addons.
    recommenders = (
        StubRecommender(False, []),
        StubRecommender(True, EXPECTED_ADDONS),
        StubRecommender(False, []),
    )

    # Make sure the recommender returns the expected addons.
    manager = RecommendationManager(StubFetcher(), recommenders)
    assert manager.recommend("client-id", 10) == EXPECTED_ADDONS
Esempio n. 12
0
def test_simple_recommendation(test_ctx):
    ctx = install_mocks(test_ctx)

    EXPECTED_RESULTS = [
        ("ghi", 3430.0),
        ("def", 3320.0),
        ("ijk", 3200.0),
        ("hij", 3100.0),
        ("lmn", 420.0),
        ("klm", 409.99999999999994),
        ("jkl", 400.0),
        ("abc", 23.0),
        ("fgh", 22.0),
        ("efg", 21.0),
    ]

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend("some_ignored_id", 10)

    assert isinstance(recommendation_list, list)
    assert recommendation_list == EXPECTED_RESULTS
Esempio n. 13
0
def test_intervention_a(test_ctx):
    ctx = install_mocks(test_ctx)

    EXPECTED_RESULTS = [('ghi', 3430.0),
                        ('def', 3320.0),
                        ('ijk', 3200.0),
                        ('hij', 3100.0),
                        ('lmn', 420.0),
                        ('klm', 409.99999999999994),
                        ('jkl', 400.0),
                        ('abc', 23.0),
                        ('fgh', 22.0),
                        ('efg', 21.0)]

    manager = RecommendationManager(ctx.child())
    recommendation_list = manager.recommend('some_ignored_id',
                                            10,
                                            extra_data={'branch': INTERVENTION_A})

    assert isinstance(recommendation_list, list)
    assert recommendation_list == EXPECTED_RESULTS
Esempio n. 14
0
def test_experimental_randomization(test_ctx):
    ctx = install_mocks(test_ctx)
    ctx = install_mock_curated_data(ctx)

    manager = RecommendationManager(ctx.child())
    raw_list = manager.recommend(TEST_CLIENT_IDS[0], 10)

    # Clobber the experiment probability to be 100% to force a
    # reordering.
    ctx["TAAR_EXPERIMENT_PROB"] = 1.0

    manager = RecommendationManager(ctx.child())
    rand_list = manager.recommend(TEST_CLIENT_IDS[0], 10)
    '''
    The two lists should be :

    * different (guid, weight) lists (possibly just order)
    * same length
    '''
    assert (reduce(
        operator.and_,
        [(t1[0] == t2[0] and t1[1] == t2[1])
         for t1, t2 in zip(rand_list, raw_list)],
    ) is False)
    assert len(rand_list) == len(raw_list)
Esempio n. 15
0
def test_none_profile_returns_empty_list(test_ctx):
    ctx = install_mocks(test_ctx)
    rec_manager = RecommendationManager(ctx)
    assert rec_manager.recommend("random-client-id", 10) == []
Esempio n. 16
0
def test_none_profile_returns_empty_list():
    ctx = get_test_ctx()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    rec_manager = RecommendationManager(ctx)
    assert rec_manager.recommend("random-client-id", 10) == []