Exemplo n.º 1
0
def test_can_recommend(mock_s3_json_downloader):
    r = LegacyRecommender()

    # Test that we can't recommend if we have not enough client info.
    assert not r.can_recommend({})
    assert not r.can_recommend({"disabled_addons_ids": []})

    # Check that we can not recommend if no *legacy* addons are detected,
    # but addon is in loaded resource.
    profile_without_legacy = dict(
        client_id="test-client-id",
        disabled_addons_ids=["test_guid_7", "test_guid_8"],
        locale="it-IT")

    assert not r.can_recommend(profile_without_legacy)
Exemplo n.º 2
0
def test_recommender_str():
    """Tests that the string representation of the recommender is correct
    """
    ctx = Context()
    ctx = s3_mocker(ctx)
    r = LegacyRecommender(ctx)
    assert str(r) == "LegacyRecommender"
Exemplo n.º 3
0
def test_recommender_str(mock_s3_json_downloader):
    """Tests that the string representation of the recommender is correct
    """
    # TODO: this test is brittle and should be removed once it is safe
    # to do so
    r = LegacyRecommender()
    assert str(r) == "LegacyRecommender"
Exemplo n.º 4
0
def test_recommendations(mock_s3_json_downloader):
    # Test that the legacy recommender returns the correct addons from the json loaded.
    r = LegacyRecommender()

    limit = 10
    profile_with_legacy = dict(
        client_id="test-client-id",
        disabled_addons_ids=["{test_guid_1}",
                             "test_guid_8"],
        locale="it-IT"
    )

    recommendations = r.recommend(profile_with_legacy, limit)

    # Make sure the structure of the recommendations is correct and that we recommended the the right addons.
    assert isinstance(recommendations, list)

    # Make sure that the reported addons are the ones from the fake data.
    assert "test_guid_2" in recommendations
    assert "test_guid_3" in recommendations
Exemplo n.º 5
0
def test_recommendations(mock_s3_json_downloader):
    """Test that the legacy recommender returns the correct addons from the json loaded.

    The JSON output for this recommender should be a list of 2-tuples
    of (GUID, weight).
    """
    r = LegacyRecommender()

    profile_with_many_legacy = dict(
        client_id="test-client-id",
        disabled_addons_ids=["guid-01", "guid-05", "guid-12"],
        locale="it-IT")

    recommendations = r.recommend(profile_with_many_legacy, LIMIT)

    assert len(recommendations) == LIMIT
    assert ("guid-13-1", 1) in recommendations
    assert ("guid-21-9", 1) not in recommendations
    assert ("guid-22-10", 1) not in recommendations
    assert ("guid-21-9", 1) not in recommendations
Exemplo n.º 6
0
def default_context():
    ctx = Context()
    from taar.recommenders import LegacyRecommender
    from taar.recommenders import CollaborativeRecommender
    from taar.recommenders import SimilarityRecommender
    from taar.recommenders import LocaleRecommender
    from taar.cache import Clock
    from taar.cache import JSONCache

    # Note that the EnsembleRecommender is *not* in this map as it
    # needs to ensure that the recommender_map key is installed in the
    # context
    ctx['recommender_factory_map'] = {
        'legacy': lambda: LegacyRecommender(ctx.child()),
        'collaborative': lambda: CollaborativeRecommender(ctx.child()),
        'similarity': lambda: SimilarityRecommender(ctx.child()),
        'locale': lambda: LocaleRecommender(ctx.child())
    }

    ctx['utils'] = utils
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx
Exemplo n.º 7
0
def test_recommender_str(mock_s3_json_downloader):
    # Tests that the string representation of the recommender is correct
    r = LegacyRecommender()
    assert str(r) == "LegacyRecommender"
Exemplo n.º 8
0
    masked, unmasked = random_partition(addons, num_mask)

    client['installed_addons'] = unmasked
    client['masked_addons'] = masked

    return client


training_masked = map(mask_addons, training)

recommenders = {
    "collaborative": CollaborativeRecommender(),
    "similarity": SimilarityRecommender(),
    "locale": LocaleRecommender("./top_addons_by_locale.json"),
    "legacy": LegacyRecommender()
}

def compute_features(client_data):
    recommendations = []
    matrix = []

    for _, recommender in recommenders.items():
        recommendations.append(recommender.get_weighted_recommendations(client_data))

    for addon in whitelist:
        matrix.append([features[addon] for features in recommendations])

    return client_data, np.array(matrix)