def test_list_prefetch(self): prefetchable_fields = [ x[0] for x in _get_prefetchable_fields( self.viewset.serializer_class) ] response = self.client.get( self.url, data={"prefetch": ','.join(prefetchable_fields)}) self.assertEqual(200, response.status_code) objs = response.data self.assertTrue("results" in objs) self.assertTrue("prefetch" in objs) for obj in objs["results"]: for field in prefetchable_fields: field_value = obj.get(field, None) if field_value is None: continue self.assertTrue(field in objs["prefetch"]) values = field_value if type(field_value) is list else [ field_value ] for value in values: if type(value) is not int: value = value['id'] self.assertTrue(value in objs["prefetch"][field])
def test_detail_prefetch(self): print("=======================================================") prefetchable_fields = [ x[0] for x in _get_prefetchable_fields( self.viewset.serializer_class) ] current_objects = self.client.get(self.url, format='json').data relative_url = self.url + '%s/' % current_objects['results'][0][ 'id'] response = self.client.get( relative_url, data={"prefetch": ','.join(prefetchable_fields)}) self.assertEqual(200, response.status_code) obj = response.data self.assertTrue("prefetch" in obj) for field in prefetchable_fields: field_value = obj.get(field, None) if field_value is None: continue self.assertTrue(field in obj["prefetch"]) values = field_value if type(field_value) is list else [ field_value ] for value in values: self.assertTrue(value in obj["prefetch"][field])