示例#1
0
 def test_find_by_attributes(self):
     """ Find Recommendation by some attributes """
     Recommendation(customer_id=2,
                    product_id=3,
                    recommend_product_id=4,
                    recommend_type="upsell").save()
     Recommendation(customer_id=2,
                    product_id=3,
                    recommend_product_id=4,
                    recommend_type="downsell").save()
     Recommendation(customer_id=5,
                    product_id=6,
                    recommend_product_id=7,
                    recommend_type="downsell").save()
     recommendations = Recommendation.find_by_attributes(3, 2, "downsell")
     self.assertEqual(recommendations[0].recommend_type, "downsell")
     self.assertEqual(recommendations[0].product_id, 3)
     self.assertEqual(recommendations[0].customer_id, 2)
     recommendations = Recommendation.find_by_attributes(3, 2, None)
     self.assertEqual(recommendations[0].recommend_type, "upsell")
     self.assertEqual(recommendations[0].product_id, 3)
     self.assertEqual(recommendations[0].customer_id, 2)
     self.assertEqual(recommendations[1].recommend_type, "downsell")
     self.assertEqual(recommendations[1].product_id, 3)
     self.assertEqual(recommendations[1].customer_id, 2)
 def get(self):
     """ List all or query the Recommendations"""
     app.logger.info('Request to list Recommendations...')
     product_id = request.args.get('product-id')
     customer_id = request.args.get('customer-id')
     recommend_type = request.args.get('recommend-type')
     recommendations = Recommendation.find_by_attributes(
         product_id, customer_id, recommend_type)
     if not recommendations:
         raise NotFound(
             "Recommendation with product_id {}, "
             "customer_id {}, recommend_type {} was not found.".format(
                 product_id, customer_id, recommend_type))
     results = [
         recommendation.serialize() for recommendation in recommendations
     ]
     return results, status.HTTP_200_OK