Exemple #1
0
    def test_invalid_update_news_without_description(self):
        """
        Can't Update news with description empty.
        """

        self.client.force_authenticate(self.superuser)
        data = NewsSerializer(self.news).data
        data.update({'description': ''})
        response = self.client.put(self.url, data)
        self.assertEquals(response.status_code, status.HTTP_400_BAD_REQUEST)
Exemple #2
0
    def test_valid_update_news(self):
        """
        Test to admin update the news.
        """

        self.client.force_authenticate(self.superuser)
        data = NewsSerializer(self.news).data
        data.update({'title': 'News 01'})
        response = self.client.put(self.url, data)
        self.assertEquals(response.status_code, status.HTTP_200_OK)
Exemple #3
0
    def test_news_not_found(self):
        """
        Test to find news that not exists.
        """

        self.client.force_authenticate(self.superuser)
        url_invalid = reverse('news:keep-news', kwargs={'pk': 30})
        data = NewsSerializer(self.news).data
        data.update({'title': 'News 02'})
        response = self.client.put(url_invalid, data)
        self.assertEquals(response.status_code, status.HTTP_404_NOT_FOUND)
Exemple #4
0
    def test_invalid_update_news(self):
        """
        Only superuser can update news.
        """

        self.client.force_authenticate(self.user)
        data = NewsSerializer(self.news).data
        data.update(
            {'description': 'Important news...'}
        )
        response = self.client.put(self.url, data)
        self.assertEquals(response.status_code, status.HTTP_403_FORBIDDEN)
Exemple #5
0
    def test_valid_news_detail(self):
        """
        Test found the specific news.
        """

        url = reverse('news:keep-news', kwargs={'pk': self.news.pk})
        serializer = NewsSerializer(self.news)
        response = self.client.get(url)
        self.assertEquals(response.data, serializer.data)
        self.assertEquals(response.status_code, status.HTTP_200_OK)
Exemple #6
0
    def test_valid_news_list(self):
        """
        Test found the user list.
        """

        news = News.objects.all()
        serializer = NewsSerializer(news, many=True)
        url = reverse('news:list-create')
        response = self.client.get(url)
        self.assertEquals(News.objects.count(), 1)
        self.assertEquals(response.data, serializer.data)
        self.assertEquals(response.status_code, status.HTTP_200_OK)
class JobResultSerializer(serializers.Serializer):
    """
    Core job result serializer. Serializes the
    results from a particular job.
    """

    # Id of job (always present in deserialization)
    id = serializers.CharField(required=True)
    # State of the job (always present in deserialization)
    state = serializers.CharField(required=True)
    # Traceback in case of failed task (only present if task fails)
    traceback = serializers.CharField(required=False)
    # Timestamp of when the job was completed (might be omitted in deserialization)
    date_done = serializers.DateTimeField(required=False)
    # Timestamp of when the job will expire (might be omitted in deserialization)
    expires_at = serializers.DateTimeField(required=False)
    # Original job keyword arguments (might be omitted in deserialization)
    job_arguments = serializers.DictField(required=False)
    # Number of found news (might be omitted in deserialization)
    number_of_news = serializers.SerializerMethodField(read_only=True)
    # The list of found news (might be omitted in deserialization)
    news = NewsSerializer(many=True, required=False, allow_null=True)

    def get_number_of_news(self, obj) -> int:
        """
        `obj` is the dict created in `to_internal_value`.
        We acess `news` key.
        """
        news = obj.get("news", None)
        return len(news) if news else None

    def to_internal_value(self, job: AsyncResult):

        # Define the inital data (these fields are always present in deserialization)
        data = {"id": job.id, "state": job.state}

        # Check if state is 'PENDING'. If so, the task
        # is unknown (does not exist).
        if job.state == "PENDING":
            raise NotFound({"id": job.id, "state": "NOT_FOUND"})

        # If job failed, include the traceback
        if job.state == "FAILURE":
            data |= {"traceback": job.traceback}

        # If job is 'STARTED', or 'FAILURE' include 'job_arguments'
        if job.state in ["STARTED", "FAILURE"]:
            data |= {"job_arguments": job.kwargs}

        # If job is 'SUCCESS' include every field
        if job.state == "SUCCESS":
            data |= {
                "job_arguments":
                job.kwargs,
                "date_done":
                job.date_done,
                "expires_at":
                (job.date_done +
                 datetime.timedelta(seconds=job.backend.expires)),
                "news":
                job.result.news,  # result is of type 'NewsFactory'
            }

        return data