def setUp(self):
        super(CFRecommendationsViewsTestCase, self).setUp()

        self.user = db_user.get_or_create(1, 'vansika_1')
        self.user2 = db_user.get_or_create(2, 'vansika_2')
        self.user3 = db_user.get_or_create(3, 'vansika_3')

        # generate test data
        data = {"recording_mbid": []}

        for score in range(1500, 0, -1):
            data["recording_mbid"].append({
                "recording_mbid": str(uuid.uuid4()),
                "score": score
            })

        db_recommendations_cf_recording.insert_user_recommendation(
            1,
            UserRecommendationsJson(**{
                'top_artist': data['recording_mbid'],
                'similar_artist': []
            }))

        db_recommendations_cf_recording.insert_user_recommendation(
            2,
            UserRecommendationsJson(**{
                'top_artist': [],
                'similar_artist': data['recording_mbid']
            }))

        # get recommendations
        self.user_recommendations = db_recommendations_cf_recording.get_user_recommendation(
            1)
        self.user2_recommendations = db_recommendations_cf_recording.get_user_recommendation(
            2)
Beispiel #2
0
    def setUp(self):
        self.server_url = "https://labs.api.listenbrainz.org/recording-mbid-lookup/json"
        super(CFRecommendationsViewsTestCase, self).setUp()
        self.user = db_user.get_or_create(1, 'vansika')
        db_user.agree_to_gdpr(self.user['musicbrainz_id'])
        self.user2 = db_user.get_or_create(2, 'vansika_1')
        self.user3 = db_user.get_or_create(3, 'vansika_2')

        # generate test data
        data = {"recording_mbid": []}

        for score in range(1500, 0, -1):
            data["recording_mbid"].append(
                {
                    "recording_mbid": str(uuid.uuid4()),
                    "score": score
                }
            )

        db_recommendations_cf_recording.insert_user_recommendation(
            2,
            UserRecommendationsJson(**{
                'top_artist': data['recording_mbid'],
                'similar_artist': []
            })
        )

        db_recommendations_cf_recording.insert_user_recommendation(
            3,
            UserRecommendationsJson(**{
                'top_artist': [],
                'similar_artist': data['recording_mbid']
            })
        )
    def setUp(self):
        super(CFRecommendationsViewsTestCase, self).setUp()

        self.user = db_user.get_or_create(1, 'vansika_1')
        self.user2 = db_user.get_or_create(2, 'vansika_2')
        self.user3 = db_user.get_or_create(3, 'vansika_3')
        # insert recommendations
        with open(self.path_to_data_file('cf_recommendations_db_data_for_api_test_recording.json'), 'r') as f:
            self.payload = json.load(f)

        db_recommendations_cf_recording.insert_user_recommendation(
            1,
            UserRecommendationsJson(**{
                'top_artist': self.payload['recording_mbid'],
                'similar_artist': []
            })
        )

        db_recommendations_cf_recording.insert_user_recommendation(
            2,
            UserRecommendationsJson(**{
                'top_artist': [],
                'similar_artist': self.payload['recording_mbid']
            })
        )

        # get recommendations
        self.user_recommendations = db_recommendations_cf_recording.get_user_recommendation(1)
        self.user2_recommendations = db_recommendations_cf_recording.get_user_recommendation(2)
    def setUp(self):
        self.server_url = "https://labs.api.listenbrainz.org/recording-mbid-lookup/json"
        ServerTestCase.setUp(self)
        DatabaseTestCase.setUp(self)
        self.user = db_user.get_or_create(1, 'vansika')
        db_user.agree_to_gdpr(self.user['musicbrainz_id'])
        self.user2 = db_user.get_or_create(2, 'vansika_1')
        self.user3 = db_user.get_or_create(3, 'vansika_2')

        # insert recommendations
        with open(self.path_to_data_file('cf_recommendations_db_data_for_api_test_recording.json'), 'r') as f:
            self.payload = json.load(f)

        db_recommendations_cf_recording.insert_user_recommendation(
            2,
            UserRecommendationsJson(**{
                'top_artist': self.payload['recording_mbid'],
                'similar_artist': []
            })
        )

        db_recommendations_cf_recording.insert_user_recommendation(
            3,
            UserRecommendationsJson(**{
                'top_artist': [],
                'similar_artist': self.payload['recording_mbid']
            })
        )
Beispiel #5
0
def handle_recommendations(data):
    """ Take recommended recordings for a user and save it in the db.
    """
    musicbrainz_id = data['musicbrainz_id']
    user = db_user.get_by_mb_id(musicbrainz_id)
    if not user:
        current_app.logger.critical(
            "Generated recommendations for a user that doesn't exist in the Postgres database: {}"
            .format(musicbrainz_id))
        return

    current_app.logger.debug(
        "inserting recommendation for {}".format(musicbrainz_id))
    recommendations = data['recommendations']

    try:
        db_recommendations_cf_recording.insert_user_recommendation(
            user['id'],
            UserRecommendationsJson(
                **{
                    'top_artist': recommendations['top_artist'],
                    'similar_artist': recommendations['similar_artist']
                }))
    except ValidationError:
        current_app.logger.error(
            """ValidationError while inserting recommendations for user with musicbrainz_id:
                                 {musicbrainz_id}. \nData: {data}""".format(
                musicbrainz_id=musicbrainz_id, data=json.dumps(data,
                                                               indent=3)))

    current_app.logger.debug(
        "recommendation for {} inserted".format(musicbrainz_id))
Beispiel #6
0
    def test_handle_recommendations(self, mock_get_by_mb_id, mock_db_insert):
        data = {
            'musicbrainz_id': 'vansika',
            'type': 'cf_recording_recommendations',
            'recommendations': {
                'top_artist': [{
                    'recording_mbid': "2acb406f-c716-45f8-a8bd-96ca3939c2e5",
                    'score': 1.8
                }, {
                    'recording_mbid': "8acb406f-c716-45f8-a8bd-96ca3939c2e5",
                    'score': -0.8
                }],
                'similar_artist': []
            }
        }

        mock_get_by_mb_id.return_value = {'id': 1, 'musicbrainz_id': 'vansika'}
        with self.app.app_context():
            handle_recommendations(data)

        mock_db_insert.assert_called_with(
            1,
            UserRecommendationsJson(top_artist=[
                UserRecommendationsRecord(
                    recording_mbid="2acb406f-c716-45f8-a8bd-96ca3939c2e5",
                    score=1.8),
                UserRecommendationsRecord(
                    recording_mbid="8acb406f-c716-45f8-a8bd-96ca3939c2e5",
                    score=-0.8),
            ],
                                    similar_artist=[]))
    def insert_test_data(self):
        top_artist_recording_mbids = [{
            'recording_mbid': 'x36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 1.0
        }, {
            'recording_mbid': 'h36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 8.7
        }]

        similar_artist_recording_mbids = [{
            'recording_mbid': 'v36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 2.6
        }, {
            'recording_mbid': 'i36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 3.7
        }]

        db_recommendations_cf_recording.insert_user_recommendation(
            self.user['id'],
            UserRecommendationsJson(
                **{
                    'top_artist': top_artist_recording_mbids,
                    'similar_artist': similar_artist_recording_mbids
                }))

        return {
            'top_artist_recording_mbids': top_artist_recording_mbids,
            'similar_artist_recording_mbids': similar_artist_recording_mbids,
        }
    def test_insert_user_recommendation(self):
        top_artist_recording_mbids = [{
            'recording_mbid': 'a36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 2.3
        }, {
            'recording_mbid': 'b36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 3.0
        }]

        similar_artist_recording_mbids = [{
            'recording_mbid': 'c36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 1.9
        }, {
            'recording_mbid': 'd36d6fc9-49d0-4789-a7dd-a2b72369ca45',
            'score': 7.6
        }]

        db_recommendations_cf_recording.insert_user_recommendation(
            self.user['id'],
            UserRecommendationsJson(
                **{
                    'top_artist': top_artist_recording_mbids,
                    'similar_artist': similar_artist_recording_mbids
                }))

        result = db_recommendations_cf_recording.get_user_recommendation(
            self.user['id'])
        self.assertEqual(
            getattr(result, 'recording_mbid').dict()['top_artist'],
            top_artist_recording_mbids)
        self.assertEqual(
            getattr(result, 'recording_mbid').dict()['similar_artist'],
            similar_artist_recording_mbids)
        self.assertGreater(int(getattr(result, 'created').strftime('%s')), 0)
    def insert_test_data(self):
        top_artist_recording_mbids = [{
            'recording_mbid': '17009e7b-11cb-46fa-9a42-e72937d05ee5',
            'score': 1.0
        }, {
            'recording_mbid': 'c8f2edaa-6da8-471a-8c45-069852176104',
            'score': 8.7
        }]

        similar_artist_recording_mbids = [{
            'recording_mbid': '81925173-6863-44c3-afd8-e1023b69969d',
            'score': 2.6
        }, {
            'recording_mbid': '62413d46-6bac-4bad-96b9-1b062da236a2',
            'score': 3.7
        }]

        db_recommendations_cf_recording.insert_user_recommendation(
            self.user['id'],
            UserRecommendationsJson(
                **{
                    'top_artist': top_artist_recording_mbids,
                    'similar_artist': similar_artist_recording_mbids
                }))

        return {
            'top_artist_recording_mbids': top_artist_recording_mbids,
            'similar_artist_recording_mbids': similar_artist_recording_mbids,
        }
Beispiel #10
0
def insert_user_recommendation(user_id: int,
                               recommendations: UserRecommendationsJson):
    """ Insert recommended recording for a user in the db.

        Args:
            user_id (int): row id of the user.
            recommendations (dict): User recommendations.
    """
    with db.engine.connect() as connection:
        connection.execute(
            sqlalchemy.text("""
            INSERT INTO recommendation.cf_recording (user_id, recording_mbid)
                 VALUES (:user_id, :recommendation)
            ON CONFLICT (user_id)
          DO UPDATE SET user_id = :user_id,
                        recording_mbid = :recommendation,
                        created = NOW()
            """), {
                'user_id': user_id,
                'recommendation': ujson.dumps(recommendations.dict()),
            })