Пример #1
0
def test_clock():
    cl = Clock()
    actual = cl.time()
    expected = time.time()

    # The clock should be pretty accurate to now
    assert abs(actual - expected) < 0.1
Пример #2
0
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
Пример #3
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
Пример #4
0
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_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({})
Пример #6
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
Пример #7
0
def s3_mocker(ctx):
    ctx = ctx.child()

    class Mocker:
        def get_s3_json_content(self, *args, **kwargs):
            return FAKE_LEGACY_DATA

    ctx['utils'] = Mocker()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx
Пример #8
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
Пример #9
0
def activate_error_responses(ctx):
    """
    Overload the 'real' addon model and mapping URLs responses so that
    we always get 404 errors.
    """
    ctx = ctx.child()

    class ErrorUtils:
        def fetch_json(self, url):
            return None
    ctx['utils'] = ErrorUtils()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx
Пример #10
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
Пример #11
0
def activate_responses(ctx):
    """
    Overload the 'real' addon model and mapping URLs responses so that
    we always the fixture data at the top of this test module.
    """
    # responses.add(responses.GET, ADDON_MODEL_URL, json=FAKE_ADDON_MATRIX)
    # responses.add(responses.GET, ADDON_MAPPING_URL, json=FAKE_MAPPING)
    ctx = ctx.child()

    class MockUtils:
        def fetch_json(self, url):
            if url == ADDON_MODEL_URL:
                return FAKE_ADDON_MATRIX
            elif url == ADDON_MAPPING_URL:
                return FAKE_MAPPING

    ctx['utils'] = MockUtils()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx
Пример #12
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
Пример #13
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
Пример #14
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) == []
Пример #15
0
def create_cts_test_ctx():
    ctx = Context()
    ctx['utils'] = MockContinuousData()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx.child()
Пример #16
0
def create_cat_test_ctx():
    ctx = Context()
    ctx['utils'] = MockCategoricalData()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx.child()
Пример #17
0
def create_test_ctx():
    ctx = Context()
    ctx['utils'] = MockUtils()
    ctx['clock'] = Clock()
    ctx['cache'] = JSONCache(ctx)
    return ctx.child()