Example #1
0
def test_generic_create():
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
        "activities": [
            {"activity_type": "view", "activity_count": 10},
            {"activity_type": "read", "activity_count": 15},
        ]
    })

    serializer.is_valid(raise_exception=True)
    author = serializer.save()

    author_content_type = ContentType.objects.get_for_model(Author)
    assert Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        activity_type="view",
        activity_count=10,
    ).exists()
    assert Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        activity_type="read",
        activity_count=15,
    ).exists()
Example #2
0
def test_coded_generic_create():
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
        "profile_images": [
            {"filename": "profile_1.png"},
            {"filename": "profile_2.png"}
        ],
        "full_images": [
            {"filename": "full_1.png"}
        ]
    })

    serializer.is_valid(raise_exception=True)
    author = serializer.save()

    author_content_type = ContentType.objects.get_for_model(Author)
    assert list(Image.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        code="author_profile_image"
    ).values_list("filename", flat=True)) == ["profile_1.png", "profile_2.png"]
    assert list(Image.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        code="author_full_image"
    ).values_list("filename", flat=True)) == ["full_1.png"]
Example #3
0
def test_attachment_serializer_invalid_attachment(attachment_empty):
    assert not attachment_empty.code
    assert not attachment_empty.file_type
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
        "profile_image": 404,
    })
    assert not serializer.is_valid()
    assert serializer.errors == {
        "profile_image": ["Attachment does not exist"]
    }
Example #4
0
def test_attachment_serializer_invalid_value_error(attachment_empty):
    assert not attachment_empty.code
    assert not attachment_empty.file_type
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
        "profile_image": "wrong",
    })
    assert not serializer.is_valid()
    assert serializer.errors == {
        "profile_image": ["Attachment expects an integer"]
    }
Example #5
0
def test_attachment_serializer_no_attachment(attachment_empty):
    assert not attachment_empty.code
    assert not attachment_empty.file_type
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
    })
    assert serializer.is_valid()
    serializer.save()

    attachment = Attachment.objects.get(pk=attachment_empty.pk)
    assert not attachment.code
    assert not attachment.file_type
Example #6
0
def test_attachment_serializer_invalid_associated(attachment):
    file_type = AttachmentFileTypeFactory(code="author_profile_image")
    assert attachment.file_type != file_type
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
        "profile_image": attachment.pk
    })
    assert not serializer.is_valid()
    assert serializer.errors == {
        "profile_image": [
            "Attachment is already associated: {}".format(
                attachment.content_object)
        ]
    }
Example #7
0
def test_attachment_serializer_save(attachment_empty):
    file_type = AttachmentFileTypeFactory(code="author_profile_image")
    assert not attachment_empty.code
    assert not attachment_empty.file_type
    serializer = AuthorSerializer(
        data={
            "first_name": "Joe",
            "last_name": "Soap",
            "profile_image": attachment_empty.pk
        })
    assert serializer.is_valid()
    serializer.save()

    attachment = Attachment.objects.get(pk=attachment_empty.pk)
    assert attachment.code
    assert attachment.file_type == file_type
Example #8
0
def test_comma_separated_export_field_skip(author, reviews):
    reviews.get(author=author, rating=1)
    reviews.get(author=author, rating=2)
    mock_smart = Mock(side_effect=KeyError)
    with patch("unicef_restlib.fields.get_attribute_smart", mock_smart):
        serializer = AuthorSerializer(author)
        data = serializer.data
        assert "review_ratings" not in data
Example #9
0
def test_comma_separated_export_field_exception(author, reviews):
    reviews.get(author=author, rating=1)
    reviews.get(author=author, rating=2)
    mock_smart = Mock(side_effect=KeyError)
    with patch("unicef_restlib.fields.get_attribute_smart", mock_smart):
        serializer = AuthorSerializer(author)
        serializer.fields["review_ratings"].required = True
        with pytest.raises(KeyError):
            serializer.data
Example #10
0
def test_attachment_serializer_update(author):
    file_type = AttachmentFileTypeFactory(code="author_profile_image")
    attachment = AttachmentFactory(
        content_object=author,
        file_type=file_type,
        code=file_type.code,
        file="test.pdf",
    )
    serializer = AuthorSerializer(author,
                                  data={
                                      "first_name": "Joe",
                                      "last_name": "Soap",
                                      "profile_image": attachment.pk
                                  })
    assert serializer.is_valid()
    serializer.save()

    attachment_updated = Attachment.objects.get(pk=attachment.pk)
    assert attachment_updated.content_object == attachment.content_object
    assert attachment_updated.code == attachment.code
    assert attachment_updated.file_type == attachment.file_type
Example #11
0
def test_coded_generic_representation(author, images):
    image_profile = images.get(author=author, code="author_profile_image")
    image_full = images.get(author=author, code="author_full_image")
    serializer = AuthorSerializer(author)
    assert serializer.data["profile_images"] == [{
        "id": image_profile.pk,
        "filename": image_profile.filename
    }]
    assert serializer.data["full_images"] == [{
        "id": image_full.pk,
        "filename": image_full.filename
    }]
Example #12
0
def test_generic_create_through_update(author):
    author_content_type = ContentType.objects.get_for_model(Author)
    activity_qs = Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk
    )
    assert not activity_qs.exists()

    serializer = AuthorSerializer(author, partial=True, data={
        "activities": [
            {"activity_type": "view", "activity_count": 10},
            {"activity_type": "read", "activity_count": 15},
        ]
    })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    assert activity_qs.count() == 2
    assert Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        activity_type="view",
        activity_count=10,
    ).exists()
    assert Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        activity_type="read",
        activity_count=15,
    ).exists()
Example #13
0
def test_many_deleting_excess(author, books):
    book_1 = books.get(author=author)
    books.get(author=author)
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 2
    serializer = AuthorSerializer(author,
                                  data={
                                      "first_name":
                                      "Joe",
                                      "last_name":
                                      "Soap",
                                      "books": [
                                          {
                                              "id": book_1.pk,
                                              "name": "Scary Tales 1",
                                              "sku_number": "123"
                                          },
                                          {
                                              "name": "Scary Tales 3",
                                              "sku_number": "789"
                                          },
                                      ]
                                  })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    assert book_qs.count() == 2
    assert list(book_qs.values_list("name", flat=True)) == [
        "Scary Tales 1",
        "Scary Tales 3",
    ]
Example #14
0
def test_many_duplication(author, book):
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 1
    serializer = AuthorSerializer(author,
                                  partial=True,
                                  data={
                                      "books": [{
                                          "id": book.pk,
                                          "name": "Scary Tales",
                                          "sku_number": "123"
                                      }, {
                                          "id": book.pk,
                                          "name": "Scary Tales",
                                          "sku_number": "123"
                                      }]
                                  })

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()

        assert {
            "books": [{}, {
                "id": ["Duplication book with pk `{}`.".format(book.pk)]
            }]
        } == err.value.detail
    assert book_qs.count() == 1
Example #15
0
def test_many_update(author, books):
    book_1 = books.get()
    book_2 = books.get()
    assert author.first_name != "Changed"
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 2
    book_1_name = "New Scary Tales 1"
    book_2_name = "New Scary Tales 2"
    assert book_1.name != book_1_name
    assert book_2.name != book_2_name
    serializer = AuthorSerializer(author, data={
        "first_name": "Changed",
        "last_name": "Soap",
        "books": [
            {"id": book_1.pk, "name": book_1_name, "sku_number": "123"},
            {"id": book_2.pk, "name": book_2_name, "sku_number": "321"},
        ]
    })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    author_updated = Author.objects.get(pk=author.pk)
    assert author_updated.first_name == "Changed"
    assert book_qs.count() == 2
    book_1_updated = Book.objects.get(pk=book_1.pk)
    assert book_1_updated.name == book_1_name
    book_2_updated = Book.objects.get(pk=book_2.pk)
    assert book_2_updated.name == book_2_name
Example #16
0
def test_deleting(author, books):
    book_1 = books.get(author=author)
    book_2 = books.get(author=author)
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 2
    serializer = AuthorSerializer(author,
                                  partial=True,
                                  data={
                                      "books": [
                                          {
                                              "id": book_1.pk,
                                              "_delete": True
                                          },
                                          {
                                              "id": book_2.pk,
                                              "name": "Scary Tales 2",
                                              "sku_number": "123"
                                          },
                                      ]
                                  })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    assert book_qs.count() == 1
    assert book_qs.filter(pk=book_2.pk).exists()
Example #17
0
def test_unique_together(author, user):
    serializer = AuthorSerializer(author,
                                  partial=True,
                                  data={
                                      "reviews": [
                                          {
                                              "user": user.pk,
                                              "rating": 3
                                          },
                                          {
                                              "user": user.pk,
                                              "rating": 5
                                          },
                                      ]
                                  })

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()

    assert err.value.detail == {
        "reviews": [{
            "non_field_error":
            ["This fields author, user must make a unique set."]
        }]
    }
    assert not Review.objects.filter(author=author).exists()
Example #18
0
def test_unique_for_new(author):
    serializer = AuthorSerializer(author,
                                  partial=True,
                                  data={
                                      "books": [
                                          {
                                              "name": "Scary Tales 1",
                                              "sku_number": "123"
                                          },
                                          {
                                              "name": "Scary Tales 2",
                                              "sku_number": "123"
                                          },
                                      ]
                                  })

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()

    assert err.value.detail == {
        "books": [{}, {
            "sku_number": ["book with this sku number already exists."]
        }]
    }
    assert not Book.objects.filter(author=author).exists()
Example #19
0
def test_many_partial_update_mix(author, book):
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 1
    serializer = AuthorSerializer(author,
                                  partial=True,
                                  data={
                                      "books": [
                                          {
                                              "id": book.pk,
                                              "name": "Scary Tales 1",
                                              "sku_number": "123"
                                          },
                                          {
                                              "name": "Scary Tales 2",
                                              "sku_number": "456"
                                          },
                                      ]
                                  })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    assert book_qs.count() == 2
    assert list(book_qs.values_list("name", flat=True)) == [
        "Scary Tales 1",
        "Scary Tales 2",
    ]
Example #20
0
def test_many_nesting_raises(author, books):
    book_1 = books.get(author=author)
    book_2 = books.get(author=author)
    serializer = AuthorSerializer(author,
                                  partial=True,
                                  data={
                                      "books": [{
                                          "id": book_1.pk,
                                          "name": "Scary Tales",
                                          "sku_number": "123"
                                      }, {
                                          "id": book_2.pk,
                                          "name": "Scary Tales",
                                          "sku_number": "123"
                                      }]
                                  })
    serializer.fields["books"].child.update = Mock(
        side_effect=serializers.ValidationError(
            {'non_field_error': ['Some error.']}))

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()

    assert {
        "books": [{
            "non_field_error": ["Some error."]
        }, {
            "non_field_error": ["Some error."]
        }]
    } == err.value.detail
Example #21
0
def test_deleting_on_create(author):
    serializer = AuthorSerializer(author, partial=True, data={
        "books": [
            {"name": "Scary Tales", "sku_number": "123", '_delete': True}
        ]
    })

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()

    assert err.value.detail == {
        "books": [{"_delete": ["You can't delete not exist object."]}]
    }
Example #22
0
def test_many_create_through_update(author):
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 0
    serializer = AuthorSerializer(author, partial=True, data={
        "books": [
            {"name": "Book 1", "sku_number": "123"},
            {"name": "Book 2", "sku_number": "321"},
        ]
    })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    assert book_qs.count() == 2
    assert Book.objects.filter(author=author, name="Book 1").exists()
    assert Book.objects.filter(author=author, name="Book 2").exists()
Example #23
0
def test_many_create():
    assert Book.objects.count() == 0
    author_qs = Author.objects.filter(first_name="Joe")
    assert not author_qs.exists()
    serializer = AuthorSerializer(data={
        "first_name": "Joe",
        "last_name": "Soap",
        "books": [
            {"name": "Scary Tales 1", "sku_number": "123"},
            {"name": "Scary Tales 2", "sku_number": "321"}
        ]
    })

    serializer.is_valid(raise_exception=True)
    serializer.save()
    assert author_qs.exists()
Example #24
0
def test_many_missed(author):
    book_qs = Book.objects.filter(author=author)
    assert not book_qs.exists()
    serializer = AuthorSerializer(author, partial=True, data={
        "books": [
            {"id": 404, "name": "Scary Tales", "sku_number": "123"}
        ]
    })

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()

    assert err.value.detail == {"books": [
        {"id": ["Book with pk `404` doesn't exists."]}
    ]}
    assert not book_qs.exists()
Example #25
0
def test_unique(author, book):
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 1
    serializer = AuthorSerializer(author, partial=True, data={
        "books": [
            {"name": "Scary Tales", "sku_number": book.sku_number}
        ]
    })

    serializer.is_valid(raise_exception=True)
    with pytest.raises(serializers.ValidationError) as err:
        serializer.save()
    assert err.value.detail == {"books": [
        {"sku_number": [
            "book with this sku number already exists."
        ]}
    ]}
    assert book_qs.count() == 1
Example #26
0
def test_generic_update(author, activities):
    activity_1 = activities.get()
    activity_2 = activities.get()
    activity_1_type = "".join([activity_1.activity_type, "view"])
    activity_2_type = "".join([activity_2.activity_type, "read"])
    assert activity_1.activity_type != activity_1_type
    assert activity_2.activity_type != activity_2_type
    serializer = AuthorSerializer(author,
                                  data={
                                      "first_name":
                                      "Changed",
                                      "last_name":
                                      "Soap",
                                      "activities": [
                                          {
                                              "id": activity_1.pk,
                                              "activity_type": activity_1_type,
                                              "activity_count": 10,
                                          },
                                          {
                                              "id": activity_2.pk,
                                              "activity_type": activity_2_type,
                                              "activity_count": 15,
                                          },
                                      ]
                                  })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    author_content_type = ContentType.objects.get_for_model(Author)
    assert Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        activity_type=activity_1_type,
        activity_count=10,
    ).exists()
    assert Activity.objects.filter(
        content_type=author_content_type,
        object_id=author.pk,
        activity_type=activity_2_type,
        activity_count=15,
    ).exists()
Example #27
0
def test_many_partial_update(author, books):
    book_1 = books.get()
    book_2 = books.get()
    book_qs = Book.objects.filter(author=author)
    assert book_qs.count() == 2
    book_1_name = "New Scary Tales 1"
    assert book_1.name != book_1_name
    serializer = AuthorSerializer(author, partial=True, data={
        "books": [
            {"id": book_1.pk, "name": book_1_name}
        ]
    })

    serializer.is_valid(raise_exception=True)
    serializer.save()

    assert book_qs.count() == 2
    book_1_updated = Book.objects.get(pk=book_1.pk)
    assert book_1_updated.name == book_1_name
    book_2_updated = Book.objects.get(pk=book_2.pk)
    assert book_2_updated.name == book_2.name
Example #28
0
def test_comma_separated_export_field(author, reviews):
    reviews.get(author=author, rating=1)
    reviews.get(author=author, rating=2)
    serializer = AuthorSerializer(author)
    data = serializer.data
    assert data["review_ratings"] == "1, 2"