Home / File/ test_json_tag.py — flask Source File

test_json_tag.py — flask Source File

Architecture documentation for test_json_tag.py, a python file in the flask codebase. 5 imports, 0 dependents.

File python ApplicationCore ExtensionRegistry 5 imports 5 functions 5 classes

Entity Profile

Dependency Diagram

graph LR
  9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2["test_json_tag.py"]
  bdb4799f_8b0c_fcfd_51cc_f0b833652f8f["datetime"]
  9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2 --> bdb4799f_8b0c_fcfd_51cc_f0b833652f8f
  c26d67c5_e5bf_05b7_bcb4_ac69089e1109["uuid"]
  9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2 --> c26d67c5_e5bf_05b7_bcb4_ac69089e1109
  da94d511_b8b8_a450_f67f_fc28ea9b648a["pytest"]
  9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2 --> da94d511_b8b8_a450_f67f_fc28ea9b648a
  73ef7241_8c5f_09fd_18d9_bc9f2aafacb1["markupsafe"]
  9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2 --> 73ef7241_8c5f_09fd_18d9_bc9f2aafacb1
  53843c45_198f_d7a7_496e_083e05bc9238["flask.json.tag"]
  9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2 --> 53843c45_198f_d7a7_496e_083e05bc9238
  style 9ee4537d_ef85_ee15_7a39_4a9f1c7ee7e2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

from datetime import datetime
from datetime import timezone
from uuid import uuid4

import pytest
from markupsafe import Markup

from flask.json.tag import JSONTag
from flask.json.tag import TaggedJSONSerializer


@pytest.mark.parametrize(
    "data",
    (
        {" t": (1, 2, 3)},
        {" t__": b"a"},
        {" di": " di"},
        {"x": (1, 2, 3), "y": 4},
        (1, 2, 3),
        [(1, 2, 3)],
        b"\xff",
        Markup("<html>"),
        uuid4(),
        datetime.now(tz=timezone.utc).replace(microsecond=0),
    ),
)
def test_dump_load_unchanged(data):
    s = TaggedJSONSerializer()
    assert s.loads(s.dumps(data)) == data


def test_duplicate_tag():
    class TagDict(JSONTag):
        key = " d"

    s = TaggedJSONSerializer()
    pytest.raises(KeyError, s.register, TagDict)
    s.register(TagDict, force=True, index=0)
    assert isinstance(s.tags[" d"], TagDict)
    assert isinstance(s.order[0], TagDict)


def test_custom_tag():
    class Foo:  # noqa: B903, for Python2 compatibility
        def __init__(self, data):
            self.data = data

    class TagFoo(JSONTag):
        __slots__ = ()
        key = " f"

        def check(self, value):
            return isinstance(value, Foo)

        def to_json(self, value):
            return self.serializer.tag(value.data)

        def to_python(self, value):
            return Foo(value)

    s = TaggedJSONSerializer()
    s.register(TagFoo)
    assert s.loads(s.dumps(Foo("bar"))).data == "bar"


def test_tag_interface():
    t = JSONTag(None)
    pytest.raises(NotImplementedError, t.check, None)
    pytest.raises(NotImplementedError, t.to_json, None)
    pytest.raises(NotImplementedError, t.to_python, None)


def test_tag_order():
    class Tag1(JSONTag):
        key = " 1"

    class Tag2(JSONTag):
        key = " 2"

    s = TaggedJSONSerializer()

    s.register(Tag1, index=-1)
    assert isinstance(s.order[-2], Tag1)

    s.register(Tag2, index=None)
    assert isinstance(s.order[-1], Tag2)

Subdomains

Dependencies

  • datetime
  • flask.json.tag
  • markupsafe
  • pytest
  • uuid

Frequently Asked Questions

What does test_json_tag.py do?
test_json_tag.py is a source file in the flask codebase, written in python. It belongs to the ApplicationCore domain, ExtensionRegistry subdomain.
What functions are defined in test_json_tag.py?
test_json_tag.py defines 5 function(s): test_custom_tag, test_dump_load_unchanged, test_duplicate_tag, test_tag_interface, test_tag_order.
What does test_json_tag.py depend on?
test_json_tag.py imports 5 module(s): datetime, flask.json.tag, markupsafe, pytest, uuid.
Where is test_json_tag.py in the architecture?
test_json_tag.py is located at tests/test_json_tag.py (domain: ApplicationCore, subdomain: ExtensionRegistry, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free