Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. ## View:
  2. def testView(request, param_INT):
  3.     user_ID = request.user.id
  4.     context_DIC = {'contextKeyFromView_STR': param_INT, 'user_ID':user_ID}
  5.     return render(request, 'sprache/test.html', context_DIC)
  6.  
  7. ## Test:
  8. @pytest.mark.django_db(transaction=True)
  9. def test_x():
  10.     User.objects.create_user(
  11.         username='Testuser2@xxx.com'
  12.         , email='testuser2@xxx.com'
  13.         , password='xxx'
  14.         , is_superuser=0
  15.         , last_name='Walker'
  16.         , is_staff=0
  17.         , is_active=1
  18.     )
  19.     user = User.objects.get_by_natural_key(username='Testuser2@xxx.com')
  20.  
  21.     url = reverse('sprache:testView', kwargs={'param_INT': 11})
  22.  
  23.     contextValueFromPost_INT = 55
  24.     context_DIC = {'contextKeyFromPost_STR': contextValueFromPost_INT}
  25.  
  26.     request = RequestFactory().post(
  27.         path = url,
  28.         data=context_DIC,
  29.     )
  30.     request.user = user
  31.  
  32.     response = testView(request, param_INT=11)
  33.  
  34.     assert response.context['contextKeyFromView_STR'] == 11
  35.     assert response.context['user_ID'] == 1
  36.  
  37. ## Error:
  38. #    @pytest.mark.django_db(transaction=True)
  39. #    def test_x():
  40. #        User.objects.create_user(
  41. #            username='Testuser2@xxx.com'
  42. #            , email='testuser2@xxx.com'
  43. #            , password='xxx'
  44. #            , is_superuser=0
  45. #            , last_name='Walker'
  46. #            , is_staff=0
  47. #            , is_active=1
  48. #        )
  49. #        user = User.objects.get_by_natural_key(username='Testuser2@xxx.com')
  50. #
  51. #        url = reverse('sprache:testView', kwargs={'param_INT': 11})
  52. #
  53. #        contextValueFromPost_INT = 55
  54. #        context_DIC = {'contextKeyFromPost_STR': contextValueFromPost_INT}
  55. #
  56. #        request = RequestFactory().post(
  57. #            path = url,
  58. #            data=context_DIC,
  59. #        )
  60. #        request.user = user
  61. #
  62. #        response = testView(request, param_INT=11)
  63. #
  64. #>       assert response.context['contextKeyFromView_STR'] == 11
  65. #E       AttributeError: 'HttpResponse' object has no attribute 'context'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement