0

I am testing a view and while testing this I am getting this error

self = <django.db.models.fields.AutoField: id>, value = ''

    def get_prep_value(self, value):
        from django.db.models.expressions import OuterRef
        value = super().get_prep_value(value)
        if value is None or isinstance(value, OuterRef):
            return value
>       return int(value)
E       ValueError: invalid literal for int() with base 10: ''

/usr/local/lib/python3.6/site-packages/django/db/models/fields/__init__.py:965: ValueError

And I think I am getting this error because of request.user.profile.org cause mixer.blend(User) will pic some User which is not in my database means some User which hasn't a Profile, Org and many things. So, I want to know that how do I test this view, how I give that user some profile and Org(organisation). And also I was doing this like I was taking the info of some User which is in my database and passing it to #test_views.py -> test_dashboard_view()-> request.user which you can check in test_dashboard_view() the lines I have commented.......

views.py

@login_required
def dashboard_view(request):
    org = request.user.profile.org
    week_responses = day_wise_responses(7, org)
    user_org = request.user.profile.org.name
    sms_sent = org.sms_counter
    email_sent = org.email_counter
    today = datetime.today().date()
    responses_one_week = number_of_responses(7, org)
    responses_two_week = number_of_responses(14, org)
    average_rating = org_average_rating(org)
    responses_last_week = responses_two_week - responses_one_week
    if responses_last_week:
        responses_percent_change = (abs(responses_one_week - responses_last_week)/responses_last_week)*100
    else:
        responses_percent_change = responses_one_week*100
    # last n responses
    last_5_responses = last_n_responses(5, org)
    # print(last_5_responses)
    context = {'week_responses': week_responses, 'user_org': user_org, 'today': today,
               'responses_one_week': responses_one_week, 'responses_percent_change': responses_percent_change,
               'last_5_responses': last_5_responses, 'sms_sent': sms_sent, 'email_sent': email_sent,
               'average_rating': average_rating}
    return render(request, 'events/dashboard.html', context)

urls.py

path('dashboard/', include('fancy_tsunami.events.urls')),

test_views.py

from events.views import dashboard_view
from django.test import RequestFactory
from django.urls import resolve, reverse
from django import test
import pytest
from django.contrib.auth.models import User, AnonymousUser


@pytest.mark.django_db
class TestViews(test.TestCase):
    def test_dashboard_view(self):
        path = reverse('event-dashboard')
        request = RequestFactory().get(path)
        # Org = [{'id': 1, 'name': 'Company', 'logo': None, 'share_google_url': None, 'sharing_destinations_id': None, 'sms_counter': 0, 'email_counter': 0}]
        request.user = mixer.blend(User)
        # CheckUser = {'id': 3, 'password': 'argon2$argon2i$v=19$m=512,t=2,p=2$bHZkZ3Q0bmE2bEJU$N6x/LFqwI4guA', 'last_login': None, 'is_superuser': True, 'username': 'nitin', 'first_name': '', 'last_name': '', 'email': 'nitin@gmail.com', 'is_staff': True, 'is_active': True, 'date_joined': datetime.datetime(2019, 2, 21, 1, 10, 32, 146)}
        # request.user = (CheckUser)
        response = dashboard_view(request)
        self.assertEqual(response.status_code, 200)


Community
  • 1
  • 1
Nikhil Bhardwaj
  • 562
  • 10
  • 18
  • You can pass authorization to client via [`self.client.login()`](https://docs.djangoproject.com/en/2.1/topics/testing/tools/#django.test.Client.login) method. – hoefling Apr 07 '19 at 12:02
  • I have done but not working – Nikhil Bhardwaj Apr 07 '19 at 13:06
  • What have you done and what is not working? – hoefling Apr 07 '19 at 13:34
  • @hoefling I have slightly changed the question I tried self.client.login() but it didn't work and now I am using mixer.blend(User) which can be helpful but my problem is that mixer.blend()'s user is not in my database so that User doesn't have profile and Organisation. So before this I was thinking to get current user and pass that user in request.user or any user from my database which has Profile and Organisation. But it didn't work also because it was asking for is_authenticated=True. – Nikhil Bhardwaj Apr 07 '19 at 14:01
  • @hoefling check this https://stackoverflow.com/questions/55583069/testing-whether-a-url-is-giving-500-error-or-not-in-django – Nikhil Bhardwaj Apr 09 '19 at 07:53

3 Answers3

2

I got my answer actually the main problem was with redirection as @Brachamul suggested me in that question Testing whether a Url is giving 500 error or not in Django , I was being redirected somewhere else which was creating the problem. So the test was not getting passed because of that view. Thanks for giving me your precious time.

Nikhil Bhardwaj
  • 562
  • 10
  • 18
1

Instead of passing the request object directly to the function based views, you can try self.client.get() method which will simulate a real request coming to your views.

Django provides a test Client to simulate a user interacting with the code at the view level.

from django.test import Client
client = Client()
client.get('/path/to/your/views/')
Vishal Singh
  • 6,014
  • 2
  • 17
  • 33
1

I know you have found your answer already but for anyone else with this issue, I suggest using Client.force_login()

Here is an example of how it works:

profile_url = reverse("forum:profile")
forum_user = ForumUser.objects.get(username="admin")
       
profile_data = dict(
    user=forum_user
)

self.client.force_login(forum_user)    
response = self.client.post(profile_url, data=profile_data)

flyingfishcattle
  • 1,817
  • 3
  • 14
  • 25