• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python utils.dump_json函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tests.utils.dump_json函数的典型用法代码示例。如果您正苦于以下问题:Python dump_json函数的具体用法?Python dump_json怎么用?Python dump_json使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了dump_json函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_invalid_reverse_relation

def test_invalid_reverse_relation(client):
    author = models.Person.objects.create(name="The Author")
    assert not models.Comment.objects.exists()
    data = dump_json({
        "posts": {
            "title": "This is the title",
            "author": "http://testserver/people/%d/" % author.pk,
            "comments": ["http://testserver/comments/1/"]
        }
    })

    response = client.post(
        reverse("post-list"), data=data,
        content_type="application/vnd.api+json")

    assert response.status_code == 400, response.content
    assert response['content-type'] == 'application/vnd.api+json'

    results = {
        "errors": [{
            "status": "400",
            "path": "/comments",
            "detail": "Invalid hyperlink - object does not exist."
        }]
    }
    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:26,代码来源:test_errors.py


示例2: test_drf_non_field_validation_error

def test_drf_non_field_validation_error(rf):
    '''DRF uses 'non_field_errors' as the key for non-field errors'''
    class LazyPersonSerializer(PersonSerializer):
        def validate(self, attr):
            raise ValidationError("Feeling lazy. Try again later.")

    class LazyPersonViewSet(PersonViewSet):
        serializer_class = LazyPersonSerializer

    data = dump_json({"people": {"name": "Jason Api"}})

    request = rf.post(
        reverse("person-list"), data=data,
        content_type="application/vnd.api+json")
    view = LazyPersonViewSet.as_view({'post': 'create'})
    response = view(request)
    response.render()

    assert response.status_code == 400, response.content
    assert not models.Person.objects.exists()

    results = {
        "errors": [{
            "status": "400",
            "path": "/-",
            "detail": "Feeling lazy. Try again later."
        }]
    }
    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:29,代码来源:test_errors.py


示例3: test_django_non_field_validation_error

def test_django_non_field_validation_error(rf, monkeypatch):
    '''Django uses __all__ as the key for non-field errors

    Constant is django.core.exceptions.NON_FIELD_ERRORS
    '''
    def clean(self):
        raise ValidationError("I'm not taking any new people today")

    monkeypatch.setattr(models.Person, 'clean', clean)
    data = dump_json({"people": {"name": "Jason Api"}})

    request = rf.post(
        reverse("person-list"), data=data,
        content_type="application/vnd.api+json")
    view = PersonViewSet.as_view({'post': 'create'})
    response = view(request)
    response.render()

    assert response.status_code == 400, response.content
    assert not models.Person.objects.exists()

    results = {
        "errors": [{
            "status": "400",
            "path": "/-",
            "detail": "I'm not taking any new people today"
        }]
    }
    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:29,代码来源:test_errors.py


示例4: test_update_attribute

def test_update_attribute(client):

    models.Person.objects.create(name="test")

    data = dump_json({
        "people": {
            "name": "new test",
        },
    })
    results = {
        "people": {
            "id": "1",
            "href": "http://testserver/people/1/",
            "name": "new test",
            "links": {
                "favorite_post": None,
                "liked_comments": [],
            }
        },
        "links": {
            "people.favorite_post": {
                "href": "http://testserver/posts/{people.favorite_post}/",
                "type": "posts"
            },
            "people.liked_comments": {
                "href": "http://testserver/comments/{people.liked_comments}/",
                "type": "comments"
            },
        }
    }

    response = client.put(
        reverse("people-full-detail", args=[1]), data,
        content_type="application/vnd.api+json")
    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:35,代码来源:test_detail.py


示例5: test_update_to_many_link

def test_update_to_many_link(client):
    models.Person.objects.create(name="test")
    author = models.Person.objects.create(name="author")
    post = models.Post.objects.create(title="The Post", author=author)
    comment1 = models.Comment.objects.create(body="Comment 1", post=post)
    comment2 = models.Comment.objects.create(body="Comment 2", post=post)

    data = dump_json(
        {
            "people": {
                "name": "test",
                "links": {"favorite_post": None, "liked_comments": [str(comment1.pk), str(comment2.pk)]},
            }
        }
    )
    results = {
        "people": {
            "id": "1",
            "href": "http://testserver/people/1/",
            "name": "test",
            "links": {"favorite_post": None, "liked_comments": [str(comment1.pk), str(comment2.pk)]},
        },
        "links": {
            "people.favorite_post": {"href": "http://testserver/posts/{people.favorite_post}/", "type": "posts"},
            "people.liked_comments": {
                "href": "http://testserver/comments/{people.liked_comments}/",
                "type": "comments",
            },
        },
    }

    response = client.put(reverse("people-full-detail", args=[1]), data, content_type="application/vnd.api+json")
    assert response.content == dump_json(results)
开发者ID:kevin-brown,项目名称:drf-json-api,代码行数:33,代码来源:test_detail.py


示例6: test_invalid_forward_relation

def test_invalid_forward_relation(client):
    assert not models.Person.objects.exists()

    data = dump_json({"posts": {"title": "This is the title", "author": "http://testserver/people/1/", "comments": []}})

    response = client.post(reverse("post-list"), data=data, content_type="application/vnd.api+json")

    assert response.status_code == 400, response.content
    assert response["content-type"] == "application/vnd.api+json"
    assert not models.Post.objects.exists()

    results = {"errors": [{"status": "400", "path": "/author", "detail": does_not_exist}]}

    assert response.content == dump_json(results)
开发者ID:kevin-brown,项目名称:drf-json-api,代码行数:14,代码来源:test_errors.py


示例7: test_required_field_omitted

def test_required_field_omitted(client):
    data = {}
    data_json_api = dump_json({"people": data})

    response = client.post(reverse("person-list"), data=data_json_api, content_type="application/vnd.api+json")

    assert response.status_code == 400, response.content
    assert not models.Person.objects.exists()

    result_data = {"name": ["This field is required."]}
    assert response.data == result_data

    results = {"errors": [{"path": "/name", "detail": "This field is required.", "status": "400"}]}

    assert response.content == dump_json(results)
开发者ID:kevin-brown,项目名称:drf-json-api,代码行数:15,代码来源:test_errors.py


示例8: test_pagination

def test_pagination(rf):
    models.Person.objects.create(name="test")

    class PaginatedPersonViewSet(PersonViewSet):
        paginate_by = 10

    request = rf.get(
        reverse("person-list"), content_type="application/vnd.api+json")
    view = PaginatedPersonViewSet.as_view({'get': 'list'})
    response = view(request)
    response.render()

    assert response.status_code == 200, response.content

    results = {
        "people": [
            {
                "id": "1",
                "href": "http://testserver/people/1/",
                "name": "test",
            },
        ],
        "meta": {
            "pagination": {
                "people": {
                    "count": 1,
                    "next": None,
                    "previous": None,
                }
            }
        }
    }
    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:33,代码来源:test_list.py


示例9: test_multiple_links

def test_multiple_links(client):
    author = models.Person.objects.create(name="test")
    post = models.Post.objects.create(author=author, title="Test post title")
    models.Comment.objects.create(post=post, body="Test comment one.")
    models.Comment.objects.create(post=post, body="Test comment two.")

    results = {
        "links": {
            "posts.author": {
                "href": "http://testserver/people/{posts.author}/",
                "type": "people",
            },
            "posts.comments": {
                "href": "http://testserver/comments/{posts.comments}/",
                "type": "comments",
            }
        },
        "posts": [
            {
                "id": "1",
                "title": "Test post title",
                "href": "http://testserver/posts/1/",
                "links": {
                    "author": "1",
                    "comments": ["1", "2"]
                }
            },
        ],
    }

    response = client.get(reverse("post-list"))

    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:33,代码来源:test_links.py


示例10: test_single_links

def test_single_links(client):
    author = models.Person.objects.create(name="test")
    post = models.Post.objects.create(author=author, title="Test post title.")
    models.Comment.objects.create(post=post, body="Some text for testing.")

    results = {
        "links": {
            "comments.post": {
                "href": "http://testserver/posts/{comments.post}/",
                "type": "posts",
            },
        },
        "comments": [
            {
                "id": "1",
                "body": "Some text for testing.",
                "href": "http://testserver/comments/1/",
                "links": {
                    "post": "1",
                }
            },
        ],
    }

    response = client.get(reverse("comment-list"))

    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:27,代码来源:test_links.py


示例11: test_multiple

def test_multiple(client):
    test_data = dump_json({
        "people": [
            {
                "name": "first",
            },
            {
                "name": "second",
            },
        ],
    })

    output_data = [
        {
            "name": "first",
        },
        {
            "name": "second",
        },
    ]

    response = client.generic("echo",
        reverse("person-list"), data=test_data,
        content_type="application/vnd.api+json",
    )

    assert response.data == output_data
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:27,代码来源:test_parsing.py


示例12: test_object_with_optional_links

def test_object_with_optional_links(client):
    models.Person.objects.create(name="test")

    results = {
        "people": {
            "id": "1",
            "href": "http://testserver/people/1/",
            "name": "test",
            "links": {
                "favorite_post": None,
                "liked_comments": [],
            }
        },
        "links": {
            "people.favorite_post": {
                "href": "http://testserver/posts/{people.favorite_post}/",
                "type": "posts"
            },
            "people.liked_comments": {
                "href": "http://testserver/comments/{people.liked_comments}/",
                "type": "comments"
            },
        }
    }

    response = client.get(reverse("people-full-detail", args=[1]))

    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:28,代码来源:test_detail.py


示例13: test_auth_required

def test_auth_required(rf):
    class RestrictedPersonViewSet(PersonViewSet):
        permission_classes = [IsAuthenticated]

    data = dump_json({"people": {"name": "Jason Api"}})

    request = rf.post(reverse("person-list"), data=data, content_type="application/vnd.api+json")
    view = RestrictedPersonViewSet.as_view({"post": "create"})
    response = view(request)
    response.render()

    assert response.status_code == 403, response.content
    assert not models.Person.objects.exists()

    results = {"errors": [{"status": "403", "title": "Authentication credentials were not provided."}]}
    assert response.content == dump_json(results)
开发者ID:kevin-brown,项目名称:drf-json-api,代码行数:16,代码来源:test_errors.py


示例14: test_empty_list

def test_empty_list(client):
    results = {
        "posts": [],
    }

    response = client.get(reverse("post-list"))

    assert response.content == dump_json(results)
开发者ID:canufeel,项目名称:drf-json-api,代码行数:8,代码来源:test_list.py


示例15: test_object

def test_object(client):
    models.Person.objects.create(name="test")

    results = {"people": {"id": "1", "href": "http://testserver/people/1/", "name": "test"}}

    response = client.get(reverse("person-detail", args=[1]))

    assert response.content == dump_json(results)
开发者ID:kevin-brown,项目名称:drf-json-api,代码行数:8,代码来源:test_detail.py


示例16: test_update_to_one_pk_link

def test_update_to_one_pk_link(client):
    models.Person.objects.create(name="test")
    author = models.Person.objects.create(name="author")
    post = models.Post.objects.create(title="The Post", author=author)

    data = dump_json({"people": {"name": "test", "links": {"favorite_post": str(post.pk), "liked_comments": []}}})
    results = {
        "people": {
            "id": "1",
            "href": "http://testserver/people/1/",
            "name": "test",
            "links": {"favorite_post": str(post.pk), "liked_comments": []},
        },
        "links": {"people.favorite_post": {"type": "posts"}, "people.liked_comments": {"type": "comments"}},
    }

    response = client.put(reverse("pk-people-full-detail", args=[1]), data, content_type="application/vnd.api+json")
    assert response.content == dump_json(results)
开发者ID:kevin-brown,项目名称:drf-json-api,代码行数:18,代码来源:test_detail.py


示例17: test_options

def test_options(client):
    # DRF 3.x representation
    results = {
        "meta": {
            "actions": {
                "POST": {
                    "author": {
                        "choices": [],
                        "label": "Author",
                        "read_only": False,
                        "required": True,
                        "type": "field"
                    },
                    "comments": {
                        "choices": [],
                        "label": "Comments",
                        "read_only": False,
                        "required": True,
                        "type": "field"
                    },
                    "id": {
                        "label": "ID",
                        "read_only": True,
                        "required": False,
                        "type": "integer"
                    },
                    "title": {
                        "label": "Title",
                        "read_only": False,
                        "required": True,
                        "type": "string"
                    },
                    "url": {
                        "label": "Url",
                        "read_only": True,
                        "required": False,
                        "type": "field"
                    }
                }
            },
            "description": "",
            "name": "Post List",
            "parses": ["application/vnd.api+json"],
            "renders": ["application/vnd.api+json"],
        }
    }

    # DRF 2.x representation - fields labels are lowercase, no choices
    ps = PostSerializer()
    if hasattr(ps, 'metadata'):
        results['meta']['actions']['POST'] = ps.metadata()

    response = client.options(reverse("post-list"))

    assert response.status_code == 200
    assert response.content == dump_json(results)
开发者ID:canufeel,项目名称:drf-json-api,代码行数:56,代码来源:test_list.py


示例18: test_create_post_success

def test_create_post_success(client):
    author = models.Person.objects.create(name="The Author")

    data = dump_json({
        "posts": {
            "title": "This is the title",
            "links": {
                "author": author.pk,
                "comments": [],
            },
        }
    })

    response = client.post(
        reverse("post-list"), data=data,
        content_type="application/vnd.api+json")
    assert response.status_code == 201
    assert response['content-type'] == 'application/vnd.api+json'

    post = models.Post.objects.get()
    results = {
        "posts": {
            "id": str(post.pk),
            "href": "http://testserver/posts/%s/" % post.pk,
            "title": "This is the title",
            "links": {
                "author": str(author.pk),
                "comments": []
            }
        },
        "links": {
            "posts.author": {
                "href": "http://testserver/people/{posts.author}/",
                "type": "people"
            },
            "posts.comments": {
                "href": "http://testserver/comments/{posts.comments}/",
                "type": "comments"
            }
        },
    }
    assert response.content == dump_json(results)
开发者ID:canufeel,项目名称:drf-json-api,代码行数:42,代码来源:test_list.py


示例19: test_options

def test_options(client):
    results = {
        "meta": {
            "actions": {
                "POST": {
                    "author": {
                        "choices": [],
                        "label": "Author",
                        "read_only": False,
                        "required": True,
                        "type": "field"
                    },
                    "comments": {
                        "choices": [],
                        "label": "Comments",
                        "read_only": False,
                        "required": True,
                        "type": "field"
                    },
                    "id": {
                        "label": "ID",
                        "read_only": True,
                        "required": False,
                        "type": "integer"
                    },
                    "title": {
                        "label": "Title",
                        "read_only": False,
                        "required": True,
                        "type": "string"
                    },
                    "url": {
                        "label": "Url",
                        "read_only": True,
                        "required": False,
                        "type": "field"
                    }
                }
            },
            "description": "",
            "name": "Post List",
            "parses": ["application/vnd.api+json"],
            "renders": ["application/vnd.api+json"],
        }
    }

    response = client.options(reverse("post-list"))

    assert response.status_code == 200
    assert response.content == dump_json(results)
开发者ID:jwhitlock,项目名称:drf-json-api,代码行数:50,代码来源:test_list.py


示例20: test_multiple_linked

def test_multiple_linked(client):
    author = models.Person.objects.create(name="test")
    post = models.Post.objects.create(
        author=author, title="One amazing test post.")
    models.Comment.objects.create(
        post=post, body="This is a test comment.")
    models.Comment.objects.create(
        post=post, body="One more comment.")

    results = {
        "posts": [
            {
                "id": "1",
                "href": "http://testserver/posts/1/",
                "title": "One amazing test post.",
                "links": {
                    "author": "1",
                    "comments": ["1", "2"],
                },
            },
        ],
        "links": {
            "posts.author": {
                "href": "http://testserver/people/{posts.author}/",
                "type": "people",
            },
            "posts.comments": {
                "href": "http://testserver/comments/{posts.comments}/",
                "type": "comments",
            }
        },
        "linked": {
            "comments": [
                {
                    "id": "1",
                    "href": "http://testserver/comments/1/",
                    "body": "This is a test comment.",
                },
                {
                    "id": "2",
                    "href": "http://testserver/comments/2/",
                    "body": "One more comment.",
                },
            ],
        },
    }

    response = client.get(reverse("nested-post-list"))

    assert response.content == dump_json(results)
开发者ID:clhamilton,项目名称:drf-json-api,代码行数:50,代码来源:test_nested.py



注:本文中的tests.utils.dump_json函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python utils.expect函数代码示例发布时间:2022-05-27
下一篇:
Python utils.disconnect_signals函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap