Exemple #1
0
def test_instance_instance_serialisation(reset_registry) -> None:
    class Name123(static.StaticData):
        """Test class."""

        a: int = static.STATIC
        b: int
        c: Dict[str, int]

    @dataclasses.dataclass
    class GoodBoy(metaclass=Name123):
        """Test class."""

        __species__ = 'WolfoInDogClothes'

        a = 1
        b: int
        c: Dict[str, int]

    doggo = GoodBoy(b=2, c={
        'age_in_dog_years': 100,
    })

    assert serialisation.jsonify(doggo) == {'$module': GoodBoy.__module__,
                                            '$type': GoodBoy.__name__,
                                            'b': 2,
                                            'c': {'ageInDogYears': 100}}
    assert serialisation.jsonify(
        doggo,
        camel_case_keys=True,
        arg_struct=True) == {'$module': GoodBoy.__module__,
                             '$type': GoodBoy.__name__,
                             'b': 2,
                             'c': {'ageInDogYears': 100}}
    assert serialisation.jsonify(
        doggo,
        camel_case_keys=False,
        arg_struct=True) == {'$module': GoodBoy.__module__,
                             '$type': GoodBoy.__name__,
                             'b': 2,
                             'c': {'age_in_dog_years': 100}
                             }
    assert serialisation.jsonify(
        doggo,
        camel_case_keys=True,
        arg_struct=False) == {'b': 2,
                              'c': {'ageInDogYears': 100}}

    assert serialisation.jsonify(
        doggo,
        camel_case_keys=False,
        arg_struct=False) == {'b': 2,
                              'c': {'age_in_dog_years': 100}}
Exemple #2
0
def test_jsonify(obj: Any, expected: Any) -> None:
    val = serialisation.jsonify(obj)
    try:
        assert val == expected
    except AssertionError:
        pass
    else:
        return
    assert _replace_nan(val) == _replace_nan(expected)
Exemple #3
0
def test_jsonify_camel_meta(obj: Any, expected: Any) -> None:
    val = serialisation.jsonify(obj, camel_case_keys=True, arg_struct=True)
    try:
        assert val == expected
    except AssertionError:
        pass
    else:
        return
    assert _replace_nan(val) == _replace_nan(expected)
Exemple #4
0
def _jsonify_static_data(obj: StaticData,
                         camel_case_keys: bool = True,
                         arg_struct: bool = True) -> serialisation.JSONType:
    d = {f: getattr(obj, f) for f in obj.__static_fields__}
    d['name'] = obj.__name__
    d = serialisation.jsonify(d,
                              camel_case_keys=camel_case_keys,
                              arg_struct=arg_struct)
    if arg_struct:
        d[serialisation.MODULE_KEY] = type(obj).__module__
        d[serialisation.NAME_KEY] = type(obj).__name__
    return d
Exemple #5
0
def test_instance_serialisation(reset_registry) -> None:
    class Name123(static.StaticData):
        """Test class."""

        b: int = static.STATIC

    class GoodBoy(metaclass=Name123):
        """Test class."""

        __species__ = 'WolfoInDogClothes'

        b = 2
        c: Dict[str, int] = {'age_in_dog_years': 100}

    class BadBoy(metaclass=Name123):
        """He's a bad boy."""

        surname = 'Eilish'
        b = 2
        c: int
        d: str

    # GoodBoy
    assert serialisation.jsonify(GoodBoy) == {
        '$module': Name123.__module__,
        '$type': Name123.__name__,
        'name': 'GoodBoy',
        'b': 2,
        'c': {'ageInDogYears': 100}
    }
    assert serialisation.jsonify(
        GoodBoy,
        camel_case_keys=True,
        arg_struct=True) == {'$module': Name123.__module__,
                             '$type': Name123.__name__,
                             'name': 'GoodBoy',
                             'b': 2,
                             'c': {'ageInDogYears': 100}}
    assert serialisation.jsonify(
        GoodBoy,
        camel_case_keys=False,
        arg_struct=True) == {'$module': Name123.__module__,
                             '$type': Name123.__name__,
                             'name': 'GoodBoy',
                             'b': 2,
                             'c': {'age_in_dog_years': 100}}
    assert serialisation.jsonify(
        GoodBoy,
        camel_case_keys=True,
        arg_struct=False) == {'name': 'GoodBoy',
                              'b': 2,
                              'c': {'ageInDogYears': 100}}

    assert serialisation.jsonify(
        GoodBoy,
        camel_case_keys=False,
        arg_struct=False) == {'name': 'GoodBoy',
                              'b': 2,
                              'c': {'age_in_dog_years': 100}}

    # BadBoy
    assert serialisation.jsonify(BadBoy) == {'$module': Name123.__module__,
                                             '$type': Name123.__name__,
                                             'name': 'BadBoy',
                                             'b': 2,
                                             'surname': 'Eilish'}
    assert serialisation.jsonify(
        BadBoy,
        camel_case_keys=True,
        arg_struct=True) == {'$module': Name123.__module__,
                             '$type': Name123.__name__,
                             'name': 'BadBoy',
                             'b': 2,
                             'surname': 'Eilish'}
    assert serialisation.jsonify(
        BadBoy,
        camel_case_keys=False,
        arg_struct=True) == {'$module': Name123.__module__,
                             '$type': Name123.__name__,
                             'name': 'BadBoy',
                             'b': 2,
                             'surname': 'Eilish'}
    assert serialisation.jsonify(
        BadBoy,
        camel_case_keys=True,
        arg_struct=False) == {'name': 'BadBoy',
                              'b': 2,
                              'surname': 'Eilish'}

    assert serialisation.jsonify(
        BadBoy,
        camel_case_keys=False,
        arg_struct=False) == {'name': 'BadBoy',
                              'b': 2,
                              'surname': 'Eilish'}