Esempio n. 1
0
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
Esempio n. 2
0
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()
Esempio n. 4
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"
def test_soft_fail():
    # Create a new instance of a SimilarityRecommender.
    ctx = Context()
    ctx['utils'] = MockNoDataUtils()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    r = SimilarityRecommender(ctx)

    # Don't recommend if the source files cannot be found.
    assert not r.can_recommend({})
Esempio n. 6
0
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
Esempio n. 7
0
def test_fetch_json():
    """ Just test a URL that we know will fail """
    ctx = Context()
    ctx['utils'] = utils = MockUtils()
    ctx['clock'] = Clock()
    cache = JSONCache(ctx)
    jdata = cache.fetch_json(
        "http://127.0.0.1:9001/some-nonexistant-url-foo.json")
    assert jdata == EXPECTED_JSON

    assert utils._fetch_count == 1
    for i in range(10):
        cache.fetch_json("http://127.0.0.1:9001/some-nonexistant-url-foo.json")
    assert utils._fetch_count == 1
Esempio n. 8
0
def test_get_s3_json_content():
    """ Just test an S3 bucket and key that doesn't exist """
    ctx = Context()
    ctx['utils'] = utils = MockUtils()
    ctx['clock'] = Clock()
    cache = JSONCache(ctx)
    jdata = cache.get_s3_json_content("taar_not_my_bucket",
                                      "this/is/not/a/valid/path")
    assert jdata == EXPECTED_S3_JSON

    assert utils._get_count == 1
    for i in range(10):
        cache.get_s3_json_content("taar_not_my_bucket",
                                  "this/is/not/a/valid/path")
    assert utils._get_count == 1
Esempio n. 9
0
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
Esempio n. 10
0
def test_can_recommend():
    ctx = Context()
    ctx = s3_mocker(ctx)
    r = LegacyRecommender(ctx)

    # 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)
Esempio n. 11
0
def test_recommendations():
    """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).
    """
    ctx = Context()
    ctx = s3_mocker(ctx)
    r = LegacyRecommender(ctx)

    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
Esempio n. 12
0
def test_expiry():
    """ Just test a URL that we know will fail """
    class MockClock:
        def __init__(self):
            self._now = 100

        def time(self):
            return self._now

    ctx = Context()
    utils = MockUtils()
    ctx['utils'] = utils
    ctx['clock'] = MockClock()

    cache = JSONCache(ctx)

    cache._ttl = 0  # Set TTL to nothing
    cache.refresh_expiry()

    jdata = cache.fetch_json(
        "http://127.0.0.1:9001/some-nonexistant-url-foo.json")
    assert jdata == EXPECTED_JSON
    jdata = cache.get_s3_json_content("taar_not_my_bucket",
                                      "this/is/not/a/valid/path")
    assert jdata == EXPECTED_S3_JSON

    assert utils._get_count == 1
    assert utils._fetch_count == 1

    for i in range(10):
        cache.fetch_json("http://127.0.0.1:9001/some-nonexistant-url-foo.json")
        cache.get_s3_json_content("taar_not_my_bucket",
                                  "this/is/not/a/valid/path")

    # Cache expires each time
    assert utils._get_count == 11
    assert utils._fetch_count == 11
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()
Esempio n. 15
0
def get_error_ctx():
    ctx = Context()
    ctx = activate_error_responses(ctx)
    return ctx
Esempio n. 16
0
def get_mocked_ctx():
    ctx = Context()
    ctx = activate_responses(ctx)
    return ctx
Esempio n. 17
0
def create_test_ctx():
    ctx = Context()
    ctx['utils'] = MockUtils()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx.child()