Merge lp:~canonical-ca-hackers/ubuntu-recommender/top-up-recommendations into lp:ubuntu-recommender

Proposed by Łukasz Czyżykowski
Status: Merged
Approved by: Anthony Lenton
Approved revision: 57
Merged at revision: 56
Proposed branch: lp:~canonical-ca-hackers/ubuntu-recommender/top-up-recommendations
Merge into: lp:ubuntu-recommender
Diff against target: 63 lines (+19/-2)
2 files modified
src/recommender/api/handlers.py (+3/-2)
src/recommender/tests/test_api.py (+16/-0)
To merge this branch: bzr merge lp:~canonical-ca-hackers/ubuntu-recommender/top-up-recommendations
Reviewer Review Type Date Requested Status
Anthony Lenton (community) Approve
Review via email: mp+102708@code.launchpad.net

Commit message

Always return RECOMMENDATION_LIMIT number of recommendations from recommend me api call.

Description of the change

Overview
========
Added code to always return RECOMMENDATION_LIMIT number of recommendations from recommend me api call.

To post a comment you must log in.
Revision history for this message
Anthony Lenton (elachuni) wrote :

Thanks lukazs!

Revision history for this message
Anthony Lenton (elachuni) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/recommender/api/handlers.py'
2--- src/recommender/api/handlers.py 2012-03-28 19:36:23 +0000
3+++ src/recommender/api/handlers.py 2012-04-19 14:43:18 +0000
4@@ -107,8 +107,9 @@
5 predictions = get_top_rated_apps(settings.RECOMMENDATION_LIMIT)
6 return UserRecommendation(rid='', data=json.dumps(predictions))
7 predictions = slopeone.predict_for_user(request.user.username)
8- if not predictions:
9- predictions = get_top_rated_apps(settings.RECOMMENDATION_LIMIT)
10+ missing_no = settings.RECOMMENDATION_LIMIT - len(predictions)
11+ if missing_no > 0:
12+ predictions += get_top_rated_apps(missing_no)
13 rec = UserRecommendation.objects.create(
14 user=request.user,
15 rid=get_random_id(),
16
17=== modified file 'src/recommender/tests/test_api.py'
18--- src/recommender/tests/test_api.py 2012-03-28 19:36:23 +0000
19+++ src/recommender/tests/test_api.py 2012-04-19 14:43:18 +0000
20@@ -49,6 +49,7 @@
21 Package,
22 RemovedApp,
23 RatingsAndReviewsDataItem,
24+ PersonIdentification,
25 Token,
26 UserProfile,
27 UserRecommendation,
28@@ -58,6 +59,7 @@
29 header_from_token,
30 patch_settings,
31 )
32+from recommender.predict import slopeone
33
34 from django_factory import TestCase
35
36@@ -275,6 +277,7 @@
37 def setUp(self):
38 super(RecommendMeTestCase, self).setUp()
39 token, consumer = create_oauth_token_and_consumer(self.factory)
40+ self.consumer = consumer
41 self.url = reverse('api-recommend-me',
42 kwargs={'uuid': '12345678123456781234567812345678'})
43 self.header = header_from_token(self.url, token, consumer)
44@@ -313,6 +316,19 @@
45 data = json.loads(response.content)
46 self.assertNotEqual(data['data'], [])
47
48+ @patch('recommender.api.handlers.slopeone')
49+ def test_user_will_always_gets_max_number_of_predictions(
50+ self, mock_slopeone):
51+ mock_slopeone.predict_for_user.return_value = [
52+ {'package_name': 'a', 'rating': 5}
53+ ]
54+ self.factory.make(settings.RECOMMENDATION_LIMIT, Package)
55+
56+ response = self.client.get(self.url, **self.header)
57+
58+ data = json.loads(response.content)
59+ self.assertEqual(len(data['data']), settings.RECOMMENDATION_LIMIT)
60+
61
62 class RecommendAppTestCase(TestCase):
63 def setUp(self):

Subscribers

People subscribed via source and target branches