예제 #1
0
 def test_list_ordering(self):
     restaurants: List[Restaurant] = RestaurantFactory.create_batch(5)
     url = reverse('api:restaurant:list-create')
     response = self.client.get(f'{url}?ordering=-rating')
     self.assertEqual(response.status_code, 200, response.content)
     data = response.json()
     self.assertEqual([r['rating'] for r in data],
                      sorted((r.rating for r in restaurants), reverse=True))
예제 #2
0
    def test_list(self):
        restaurants: List[Restaurant] = RestaurantFactory.create_batch(3)
        url = reverse('api:restaurant:list-create')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200, response.content)
        data = response.json()
        self.assertEqual(len(data), 3)

        for i, restaurant in enumerate(data):
            # this will work because no explicit sorting in place
            self.assertEqual(restaurant['name'], restaurants[i].name)
            self.assertEqual(
                restaurant['food_types'],
                list(restaurants[i].food_types.values('id', 'name')))
            self.assertEqual(restaurant['address'], restaurants[i].address)
            self.assertEqual(restaurant['rating'], restaurants[i].rating)