Esempio n. 1
0
        def test_should_display_top_rated_recipes(self, api_client):
            for i in range(10):
                new_user = UserFactory()
                api_client.force_authenticate(new_user)
                recipe_data = RecipeFactory.build()
                data = {
                    'title': {recipe_data.title},
                    'description': {recipe_data.description},
                    'flavor_type': {recipe_data.flavor_type},
                }
                api_client.post(create_recipe_url, data)
                new_recipe = Recipe.objects.all().get(title=recipe_data.title)
                new_recipe.stars = 5
                new_recipe.save()
                api_client.logout()

            for i in range(10):
                new_user = UserFactory()
                api_client.force_authenticate(new_user)
                recipe_data = RecipeFactory.build()
                data = {
                    'title': {recipe_data.title},
                    'description': {recipe_data.description},
                    'flavor_type': {recipe_data.flavor_type},
                }
                api_client.post(create_recipe_url, data)
                new_recipe = Recipe.objects.all().get(title=recipe_data.title)
                new_recipe.stars = 4
                new_recipe.save()
                api_client.logout()

            api_client.force_authenticate(new_user)
            response = api_client.get(top_rated_recipes_url)
            top_rated_recipes = Recipe.objects.all().filter(stars=5)
            bottom_rated_recipes = Recipe.objects.all().filter(stars=4)

            for recipe in top_rated_recipes:
                assert f'{recipe}' in f'{response.content}'

            for recipe in bottom_rated_recipes:
                assert f'{recipe}' not in f'{response.content}'
Esempio n. 2
0
        def test_recipe_create_post_request_not_allowed(self, api_client):
            new_user = UserFactory()
            recipe_data = RecipeFactory.build()
            data = {
                'author': {new_user.id},
                'title': {recipe_data.title},
                'description': {recipe_data.description},
                'flavor_type': {recipe_data.flavor_type},
            }
            response = api_client.post(create_recipe_url, data)

            assert response.status_code == 401
Esempio n. 3
0
        def test_recipe_create_post_request(self, api_client):
            new_user = UserFactory()
            api_client.force_authenticate(new_user)
            recipe_data = RecipeFactory.build()
            data = {
                'title': {recipe_data.title},
                'description': {recipe_data.description},
                'flavor_type': {recipe_data.flavor_type},
            }
            response = api_client.post(create_recipe_url, data)

            assert response.status_code == 201
Esempio n. 4
0
        def test_rating_recipe_calculate_account_stars(self, api_client):
            first_recipe = RecipeFactory()
            recipe_author = first_recipe.author
            api_client.force_authenticate(recipe_author)

            second_recipe = RecipeFactory.build()
            data = {
                'title': {second_recipe.title},
                'description': {second_recipe.description},
                'flavor_type': {second_recipe.flavor_type}, 
            }
            api_client.post(create_recipe_url, data)
            second_recipe = Recipe.objects.all().get(title=second_recipe.title)

            data = {
                'recipe': first_recipe.id,
                'stars': 5
            }

            api_client.post(rating_rate_url, data)
            
            data = {
                'recipe': second_recipe.id,
                'stars': 0
            }
            api_client.post(rating_rate_url, data)
            api_client.logout()


            second_user = UserFactory()
            api_client.force_authenticate(second_user)
            data = {
                'recipe': first_recipe.id,
                'stars': 5
            }
            api_client.post(rating_rate_url, data)

            data = {
                'recipe': second_recipe.id,
                'stars': 5
            }
            api_client.post(rating_rate_url, data)
            api_client.logout()


            first_recipe = Recipe.objects.all().get(title=first_recipe.title)
            second_recipe = Recipe.objects.all().get(title=second_recipe.title)
            user = UserAccount.objects.all().get(id=recipe_author.id)

            assert first_recipe.stars == '5.0'
            assert second_recipe.stars == '2.5'
            assert Rating.get_account_stars_score(user=user) == 3.75
            assert user.stars == '3.75'
Esempio n. 5
0
def test_get_account_stars_score(api_client):
    user = UserFactory()
    api_client.force_authenticate(user)

    first_recipe = RecipeFactory.build()
    create_recipe_url = reverse('recipes:create')
    data = {
        'title': {first_recipe.title},
        'description': {first_recipe.description},
        'flavor_type': {first_recipe.flavor_type},
    }
    api_client.post(create_recipe_url, data)

    second_recipe = RecipeFactory.build()
    data = {
        'title': {second_recipe.title},
        'description': {second_recipe.description},
        'flavor_type': {second_recipe.flavor_type},
    }
    api_client.post(create_recipe_url, data)

    first_recipe = Recipe.objects.all().get(title__exact=first_recipe.title)
    data = {'recipe': first_recipe.id, 'stars': 5}
    api_client.post(rating_create_url, data)
    second_recipe = Recipe.objects.all().get(title__exact=second_recipe.title)
    data = {'recipe': second_recipe.id, 'stars': 0}
    api_client.post(rating_create_url, data)

    second_user = UserFactory()
    api_client.force_authenticate(second_user)

    data = {'recipe': first_recipe.id, 'stars': 1}
    api_client.post(rating_create_url, data)
    data = {'recipe': second_recipe.id, 'stars': 3}
    api_client.post(rating_create_url, data)

    account_avg_stars = Rating.get_account_stars_score(user=user)

    assert account_avg_stars == 2.25
Esempio n. 6
0
        def test_recipe_author_is_current_logged_in_user(self, api_client):
            ''' testing the method perform_create '''
            first_user = UserFactory()
            api_client.force_authenticate(first_user)
            recipe_data = RecipeFactory.build()
            data = {
                'title': {recipe_data.title},
                'description': {recipe_data.description},
                'flavor_type': {recipe_data.flavor_type},
            }
            api_client.post(create_recipe_url, data)
            new_recipe = Recipe.objects.get(title=recipe_data.title)

            assert new_recipe.author == first_user
Esempio n. 7
0
        def test_should_display_all_recipes_of_users_that_in_account_following_list(
                self, api_client):
            first_user = RecipeFactory().author

            second_user = UserFactory()
            api_client.force_authenticate(second_user)
            for i in range(10):
                recipe_data = RecipeFactory.build()
                data = {
                    'title': {recipe_data.title},
                    'description': {recipe_data.description},
                    'flavor_type': {recipe_data.flavor_type},
                }
                api_client.post(create_recipe_url, data)

            api_client.logout()
            api_client.force_authenticate(first_user)
            first_user.following.add(second_user)

            response = api_client.get(followed_users_recipes_url)
            recipes = Recipe.objects.all().filter(author=second_user)

            for recipe in recipes:
                assert f'{recipe}' in f'{response.content}'