In the following test I get one of two errors, which I have not been able to resolve. This is my first project in Django, and therefore first time testing with it. I read that the APIRequestFactory will create its own database for the test then wipe it clean. There is no interaction with the middleware.
Is it possible to check that the data is being retrieved properly using GET calls? So far I can only check that the status is 200 or not. The content of the GET or POST calls that I am making is empty. The examples that I have seen with the RequestFactory have not displayed any data comparssions. If you know of any examples, I should be able to resolve my problem.
When created Report object is used to test against the data that was posted
self.assertEqual(response.data['module'], report.module) TypeError: list indices must be integers, not str
I am not sure what to change to make this assertion true. I have searched the error message, but it does not seem like I am using a ListField as report.module, since that is a direct field of the Report.
When replacing report.module with the expected json data from /dashboard/modules/
AssertionError: [] != '[{"module":"testModule"}]'
class GetModules(TestCase):
def setUp(self):
#access to the request factory
self.factory = APIRequestFactory()
def test_get_modules(self):
self.assertEqual(Report.objects.count(), 0)
Report.objects.create(module="testModule", severity=3, title="Get Modules", data="{}")
report = Report.objects.get(module="testModule")
self.assertEqual(Report.objects.count(), 1)
url = '/dashboard/modules/'
request = self.factory.get(url)
response = moduleList(request)
self.assertEqual(Report.objects.count(), 1)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, '[{"module":"testModule" }]')
I know that the object exists because the test of count returns 0 before and after the object is created it is 1.
response.data is already a python object and not a string so I would guess it would be equal to
{"module":"testModule" }