I have a dictionary A
that contains a single value that is also a dictionary B
B = {"k1": "v1", "k2": "v2", "k3": "v3",}
A = {"User": B}
This type of dictionary is used in one of my views
function
def views_function(request):
# request.data is a dictionary containing a single value that is also a dictionary. Just like A
I'm trying to write a unit test for views_function
from django.test.TestCase
.
from django.test import TestCase
class test_cases(TestCase):
def setUp(self):
self.B = {"k1": "v1", "k2": "v2", "k3": "v3",}
self.A = {"User": self.B}
self.url = reverse("views_function_url")
def test_views_function(self):
response = self.client.post(url, self.A, format="json")
But clients.post
seems unable to take A
as an input. When I print A
inside test_views_function
it shows the proper dictionary.
def test_views_function(self):
B = {"k1": "v1", "k2": "v2", "k3": "v3",}
A = {"User": B}
print(A) # {"User": {"k1": "v1", "k2": "v2", "k3": "v3"}}
response = self.client.post(url, A, format="json")
But when I add a print statement to views_function
for some reason only the last key of B
is there
def views_function(request):
print(request.data.dict()) # {'User': 'k3'}
Solution was simple, I had the wrong argument by accident. It's content_type="application/json"
not format="json"
in self.client.post()