コード例 #1
0
    def test_breakfast_recommendations(self):
        """It returns the recommended breakfasts for a user"""
        breakfasts = []
        for i in range(3):
            breakfasts.append(BreakfastFactory())

        ingredients = []
        for i in range(3):
            ingredients.append(IngredientFactory())

        user = UserFactory()

        breakfast_ingredients = (
            (breakfasts[0], ingredients[0], 0.8),
            (breakfasts[0], ingredients[1], 0.8),
            (breakfasts[0], ingredients[2], 0.2),
            (breakfasts[1], ingredients[0], 0.0),
            (breakfasts[1], ingredients[1], 0.2),
            (breakfasts[1], ingredients[2], 0.9),
            (breakfasts[2], ingredients[0], 0.9),
            (breakfasts[2], ingredients[1], 0.5),
            (breakfasts[2], ingredients[2], 0.1),
        )

        for breakfast, ingredient, coefficient in breakfast_ingredients:
            BreakfastIngredientFactory(breakfast=breakfast,
                                       ingredient=ingredient,
                                       coefficient=coefficient)

        ingredient_preferences = (
            (ingredients[0], 0.8),
            (ingredients[1], 0.4),
            (ingredients[2], 0.6),
        )

        for ingredient, coefficient in ingredient_preferences:
            UserPreferenceFactory(user=user,
                                  ingredient=ingredient,
                                  coefficient=coefficient)

        # We have to create the URL outside the context manager because user.id can trigger a query itself
        url = "/oop_orm/user/%d/breakfast_recommendations" % user.id
        with AssertNumQueries(db.session, 2):
            response = self.client.get(url, content_type='application/json')

        self.assertEqual(response.status_code, 200)
        response_data = json.loads(response.data)
        self.assertIn('breakfast_recs', response_data)
        self.assertEqual(len(response_data['breakfast_recs']), 3)
        self.assertTrue('breakfast_id' in response_data['breakfast_recs'][0])
        self.assertTrue('score' in response_data['breakfast_recs'][0])
コード例 #2
0
    def test_delete_user(self):
        """It deletes a user record in the database"""

        # expect a 404 error if invalid data is provided
        response = self.client.delete("/oop_orm/user/-1")
        self.assertEqual(response.status_code, 404)

        user = UserFactory(first_name="Alpha", last_name="Ardvark")

        # expect an object to be deleted if it is found
        response = self.client.delete("/oop_orm/user/{}".format(user.id))
        self.assertEqual(response.status_code, 204)
        self.assertIsNone(
            db.session.query(models.User).filter_by(id=user.id).scalar())
コード例 #3
0
    def test_to_dict(self):
        """It serlializes as a dict representation"""
        user = UserFactory(first_name='Adam', last_name='Anderson')
        ingredient = IngredientFactory(name='Spam')
        user_preference = UserPreferenceFactory.create(user=user,
                                                       ingredient=ingredient)

        # Force the session to flush. Otherwise I wasn't getting IDs for child objects
        db.session.flush()

        self.assertDictEqual(
            user_preference.to_dict(), {
                'user_id': user.id,
                'ingredient_id': ingredient.id,
                'coefficient': user_preference.coefficient
            })
コード例 #4
0
    def test_get_user(self):
        """It lists all the users in the database"""

        # expect to get a 404 response if no user found for ID
        response = self.client.get("/oop_orm/user/-1",
                                   content_type='application/json')
        self.assertEqual(response.status_code, 404)

        # expect to get a json object representation of a user to be returned
        user = UserFactory()
        response = self.client.get("/oop_orm/user/{}".format(user.id),
                                   content_type='application/json')
        self.assertEqual(response.status_code, 200)
        response_data = json.loads(response.data)
        self.assertEqual(response_data['id'], user.id)
        self.assertEqual(response_data['first_name'], user.first_name)
        self.assertEqual(response_data['last_name'], user.last_name)
コード例 #5
0
    def test_get_recommendations(self):
        """It gets recommended breakfasts"""
        user = UserFactory.create(first_name='Adam', last_name='Anderson')

        self.assertEqual(user.get_recommendations(), [])

        # expect a breakfast with no ingredients to be excluded included
        breakfast = BreakfastFactory.create()
        self.assertEqual(user.get_recommendations(), [])

        # expect a single recommendation with no breakfasts
        ingredient = IngredientFactory.create()
        BreakfastIngredientFactory(breakfast=breakfast,
                                   ingredient=ingredient,
                                   coefficient=1)
        self.assertEqual(len(user.get_recommendations()), 1)
        self.assertDictEqual(user.get_recommendations()[0], {
            'breakfast_id': breakfast.id,
            'score': 0.0
        })

        # expect a single recommendation with no breakfasts
        UserPreferenceFactory.create(user=user,
                                     ingredient=ingredient,
                                     coefficient=1)
        self.assertEqual(len(user.get_recommendations()), 1)
        self.assertDictEqual(user.get_recommendations()[0], {
            'breakfast_id': breakfast.id,
            'score': 1
        })

        # expect a second breakfast with less relvency to show up in recommendations position #2
        breakfast_2 = BreakfastFactory.create()
        BreakfastIngredientFactory(breakfast=breakfast_2,
                                   ingredient=ingredient,
                                   coefficient=0.1)
        self.assertEqual(len(user.get_recommendations()), 2)
        self.assertDictEqual(user.get_recommendations()[0], {
            'breakfast_id': breakfast.id,
            'score': 1
        })
        self.assertDictEqual(user.get_recommendations()[1], {
            'breakfast_id': breakfast_2.id,
            'score': 0.1
        })
コード例 #6
0
    def test_list_users(self):
        """It lists all the users in the database"""

        # expect no users to return an empty json object
        response = self.client.get("/oop_orm/user/",
                                   content_type='application/json')
        self.assertEqual(response.status_code, 200)
        response_data = json.loads(response.data)
        self.assertIn("users", response_data)
        self.assertEqual(len(response_data["users"]), 0)

        # create 3 users
        user_ids = []
        for i in range(3):
            user_ids.append(UserFactory().id)

        # exepect 3 user objects to be returned
        response = self.client.get("/oop_orm/user/",
                                   content_type='application/json')
        self.assertEqual(response.status_code, 200)
        response_data = json.loads(response.data)
        for user in response_data['users']:
            self.assertIn(user['id'], user_ids)
コード例 #7
0
    def test_update_user(self):
        """It updates a user in the database"""

        user = UserFactory(first_name="Alpha", last_name="Ardvark")
        request_data = {"first_name": "Beta", "last_name": "Baboon"}

        # expect to get a 404 response if no user found for ID
        response = self.client.put("/oop_orm/user/-1",
                                   content_type='application/json')
        self.assertEqual(response.status_code, 404)

        # expect a json representation of the updated user to be returned
        response = self.client.put("/oop_orm/user/{}".format(user.id),
                                   content_type='application/json',
                                   data=json.dumps(request_data))
        self.assertEqual(response.status_code, 200)
        response_data = json.loads(response.data)
        self.assertEqual(response_data['first_name'], 'Beta')
        self.assertEqual(response_data['last_name'], 'Baboon')

        # expect the record to be updated
        user = models.User.query.get(user.id)
        self.assertEqual(user.first_name, request_data['first_name'])
        self.assertEqual(user.last_name, request_data['last_name'])
コード例 #8
0
    def setUp(self):
        super(UserPreferencesTestCase, self).setUp()

        # create a user for each test
        self.user = UserFactory()
        self.list_url = self.list_url_template.format(user_id=self.user.id)
コード例 #9
0
 def test_full_name(self):
     """It returns a person's full name"""
     user = UserFactory.build(first_name='Adam', last_name='Anderson')
     self.assertEqual(user.full_name, 'Adam Anderson')
コード例 #10
0
 def test_to_dict(self):
     """It serlializes as a dict representation"""
     user_props = {'id': 1, 'first_name': 'Karma', 'last_name': 'Kabana'}
     user = UserFactory.build(**user_props)
     self.assertDictEqual(user.to_dict(), user_props)