def test_todo_list_create(self):
     """
     Define some json data we expect to receive and use the serializer
     to parse it into models. Test the models to make sure they're correct.
     """
     # define data that resembles what would come through the API.
     # A Python dictionary is often what JSON data is parsed into initially.
     data = {
         'title': 'Test List',
         'items': [
             {
                 'title': 'Test Item 1',
                 'description': 'This is test item 1'
             },
             {
                 'title': 'Test Item 2',
                 'description': 'This is test item 2'
             }
         ]
     }
     
     # pass the data into the serializer to try and parse it
     serializer = serializers.ToDoList(data=data)
     # verify that the serializer thinks the data is valid
     self.assertTrue(serializer.is_valid())
     
     # get the object parsed from the serializer
     todo_list = serializer.save()
     
     # verify the title is correct
     self.assertEqual(todo_list.title, 'Test List')
     # verify it has two items
     self.assertEqual(todo_list.items.count(), 2)
Exemple #2
0
    def get(self, request, format=None):
        print('entered todolist get view')
        todo_lists = models.ToDoList.objects.all()

        serializer = serializers.ToDoList(todo_lists, many=True)

        return Response(serializer.data)
Exemple #3
0
 def post(self, request, format=None):
     print('post view')
     serializer = serializers.ToDoList(data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemple #4
0
    def get(self, request, format=None):
        """
        Return a list of all the ToDoList objects in the database.
        """
        # query the database for all instances of the ToDoList model
        todo_lists = models.ToDoList.objects.all()

        # serialize the data into a returnable format
        serializer = serializers.ToDoList(todo_lists, many=True)

        return Response(serializer.data)
Exemple #5
0
 def post(self, request, format=None):
     """
     Creates a new ToDoList with the given request data.
     """
     # deserialize the data from the request
     serializer = serializers.ToDoList(data=request.data)
     # if the data validation done by the serialize passes
     if serializer.is_valid():
         # then save the ToDoList to the database and return the resulting ToDoList
         serializer.save()
         return Response(serializer.data)
     else:
         # Throw a 404 error if the serializer detected the data wasn't valid
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
    def test_todo_list_create(self):
        data = {
            'title':
            'Test List',
            'items': [{
                'title': 'Test Item 1',
                'description': 'This is test item 1'
            }, {
                'title': 'Test Item 2',
                'description': 'This is test item 2'
            }]
        }

        serializer = serializers.ToDoList(data=data)
        self.assertTrue(serializer.is_valid())

        todo_list = serializer.save()

        self.assertEqual(todo_list.title, 'Test List')
        self.assertEqual(todo_list.items.count(), 2)