def test_modify_search_filter_hook(self): # Prep an empty result. mock_api = self.generate_mock_api() # With an empty recommendation result, the Filter is set up # to return nothing. lane = RecommendationLane( self._default_library, self.work, "", novelist_api=mock_api ) filter = Filter() assert False == filter.match_nothing modified = lane.modify_search_filter_hook(filter) assert modified == filter assert True == filter.match_nothing # When there are recommendations, the Filter is modified to # match only those ISBNs. i1 = self._identifier() i2 = self._identifier() lane.recommendations = [i1, i2] filter = Filter() assert [] == filter.identifiers modified = lane.modify_search_filter_hook(filter) assert modified == filter assert [i1, i2] == filter.identifiers assert False == filter.match_nothing
def test_works_query_with_source_audience(self): # If the lane is created with a source audience, it filters the # recommendations appropriately. works = self.sample_works_for_each_audience() [children, ya, adult, adults_only] = works recommendations = list() for work in works: recommendations.append(work.license_pools[0].identifier) expected = { Classifier.AUDIENCE_CHILDREN : [children], Classifier.AUDIENCE_YOUNG_ADULT : [children, ya], Classifier.AUDIENCE_ADULTS_ONLY : works } for audience, results in expected.items(): self.work.audience = audience SessionManager.refresh_materialized_views(self._db) mock_api = self.generate_mock_api() lane = RecommendationLane( self._default_library, self.work, '', novelist_api=mock_api ) lane.recommendations = recommendations self.assert_works_queries(lane, results)
def test_modify_search_filter_hook(self): # Prep an empty result. mock_api = self.generate_mock_api() # With an empty recommendation result, the Filter is set up # to return nothing. lane = RecommendationLane(self._default_library, self.work, '', novelist_api=mock_api) filter = Filter() eq_(False, filter.match_nothing) modified = lane.modify_search_filter_hook(filter) eq_(modified, filter) eq_(True, filter.match_nothing) # When there are recommendations, the Filter is modified to # match only those ISBNs. i1 = self._identifier() i2 = self._identifier() lane.recommendations = [i1, i2] filter = Filter() eq_([], filter.identifiers) modified = lane.modify_search_filter_hook(filter) eq_(modified, filter) eq_([i1, i2], filter.identifiers) eq_(False, filter.match_nothing)
def test_works_query_with_source_language(self): # Prepare a number of works with different languages. # TODO: Setting a data source name is necessary because # Gutenberg books get filtered out when children or ya # is one of the lane's audiences. eng = self._work(with_license_pool=True, language='eng', data_source_name=DataSource.OVERDRIVE) fre = self._work(with_license_pool=True, language='fre', data_source_name=DataSource.OVERDRIVE) spa = self._work(with_license_pool=True, language='spa', data_source_name=DataSource.OVERDRIVE) SessionManager.refresh_materialized_views(self._db) # They're all returned as recommendations from NoveList Select. recommendations = list() for work in [eng, fre, spa]: recommendations.append(work.license_pools[0].identifier) # But only the work that matches the source work is included. mock_api = self.generate_mock_api() lane = RecommendationLane(self._default_library, self.work, '', novelist_api=mock_api) lane.recommendations = recommendations self.assert_works_queries(lane, [eng]) # It doesn't matter the language. self.work.presentation_edition.language = 'fre' SessionManager.refresh_materialized_views(self._db) mock_api = self.generate_mock_api() lane = RecommendationLane(self._default_library, self.work, '', novelist_api=mock_api) lane.recommendations = recommendations self.assert_works_queries(lane, [fre])
def test_works_query(self): # Prep an empty result. mock_api = self.generate_mock_api() # With an empty recommendation result, the lane is empty. lane = RecommendationLane(self._db, self.lp, '', novelist_api=mock_api) eq_(None, lane.works()) eq_(None, lane.materialized_works()) # Resulting recommendations are returned when available, though. result = self._work(with_license_pool=True) lane.recommendations = [result.license_pools[0].identifier] SessionManager.refresh_materialized_views(self._db) self.assert_works_queries(lane, [result])
def test_overview_facets(self): # A FeaturedFacets object is adapted to a Facets object with # specific settings. featured = FeaturedFacets(0.44, entrypoint=AudiobooksEntryPoint) lane = RecommendationLane( self._default_library, self.work, "", novelist_api=self.generate_mock_api() ) overview = lane.overview_facets(self._db, featured) assert isinstance(overview, Facets) assert Facets.COLLECTION_FULL == overview.collection assert Facets.AVAILABLE_ALL == overview.availability assert Facets.ORDER_AUTHOR == overview.order # Entry point was preserved. assert AudiobooksEntryPoint == overview.entrypoint
def test_works_query(self): # Prep an empty result. mock_api = self.generate_mock_api() # With an empty recommendation result, the lane is empty. lane = RecommendationLane(self._default_library, self.work, '', novelist_api=mock_api) eq_(None, lane.works(self._db)) # Resulting recommendations are returned when available, though. # TODO: Setting a data source name is necessary because Gutenberg # books get filtered out when children or ya is one of the lane's # audiences. result = self._work(with_license_pool=True, data_source_name=DataSource.OVERDRIVE) lane.recommendations = [result.license_pools[0].identifier] SessionManager.refresh_materialized_views(self._db) self.assert_works_queries(lane, [result])
def test_works_query_with_source_language(self): # Prepare a number of works with different languages. eng = self._work(with_license_pool=True, language='eng') fre = self._work(with_license_pool=True, language='fre') spa = self._work(with_license_pool=True, language='spa') SessionManager.refresh_materialized_views(self._db) # They're all returned as recommendations from NoveList Select. recommendations = list() for work in [eng, fre, spa]: recommendations.append(work.license_pools[0].identifier) # But only the work that matches the source work is included. mock_api = self.generate_mock_api() lane = RecommendationLane(self._db, self.lp, '', novelist_api=mock_api) lane.recommendations = recommendations self.assert_works_queries(lane, [eng]) # It doesn't matter the language. self.lp.presentation_edition.language = 'fre' SessionManager.refresh_materialized_views(self._db) mock_api = self.generate_mock_api() lane = RecommendationLane(self._db, self.lp, '', novelist_api=mock_api) lane.recommendations = recommendations self.assert_works_queries(lane, [fre])