Compare commits
2 Commits
4068af462b
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 89c4bcae90 | |||
| 9775bc2cc6 |
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "megasniff"
|
name = "megasniff"
|
||||||
version = "0.2.6"
|
version = "0.2.6.post1"
|
||||||
description = "Library for in-time codegened type validation"
|
description = "Library for in-time codegened type validation"
|
||||||
authors = [
|
authors = [
|
||||||
{ name = "nikto_b", email = "niktob560@yandex.ru" }
|
{ name = "nikto_b", email = "niktob560@yandex.ru" }
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ from typing import get_args, Union, Annotated, Sequence, TypeAliasType, \
|
|||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
from .utils import *
|
from .utils import *
|
||||||
|
import uuid
|
||||||
|
from pathlib import Path
|
||||||
|
import tempfile
|
||||||
|
import importlib.util
|
||||||
|
|
||||||
JsonObject: TypeAlias = Union[None, bool, int, float, str, list['JsonObject'], dict[str, 'JsonObject']]
|
JsonObject: TypeAlias = Union[None, bool, int, float, str, list['JsonObject'], dict[str, 'JsonObject']]
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ if not isinstance({{from_container}}, dict):
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
for k_{{hashname(unwrapping)}}, v_{{hashname(unwrapping)}} in {{from_container}}.items():
|
for k_{{hashname(unwrapping)}}, v_{{hashname(unwrapping)}} in {{from_container}}.items():
|
||||||
{{ render_unwrap(unwrapping.key_unwrap, 'k_' + hashname(unwrapping), 'k_' + hashname(unwrapping)) | indent(4) }}
|
{{ render_unwrap(unwrapping.key_unwrap, 'k_' + hashname(unwrapping), 'k_' + hashname(unwrapping)) | indent(4) }}
|
||||||
{{ render_unwrap(unwrapping.value_unwrap, 'v_' + hashname(unwrapping), into_container + '[v_' + hashname(unwrapping) + ']') | indent(4) }}
|
{{ render_unwrap(unwrapping.value_unwrap, 'v_' + hashname(unwrapping), into_container + '[k_' + hashname(unwrapping) + ']') | indent(4) }}
|
||||||
{%- endset %}
|
{%- endset %}
|
||||||
{{out}}
|
{{out}}
|
||||||
{%- endmacro %}
|
{%- endmacro %}
|
||||||
|
|||||||
@@ -44,6 +44,20 @@ def test_unions():
|
|||||||
assert a['a'] == '42a'
|
assert a['a'] == '42a'
|
||||||
|
|
||||||
|
|
||||||
|
def test_dict_body():
|
||||||
|
@dataclass
|
||||||
|
class A:
|
||||||
|
a: dict[str, float]
|
||||||
|
|
||||||
|
defl = SchemaDeflatorGenerator()
|
||||||
|
fn = defl.schema_to_deflator(A)
|
||||||
|
|
||||||
|
a = fn(A({'1': 1.1, '2': 2.2}))
|
||||||
|
print(a)
|
||||||
|
assert a['a']['1'] == 1.1
|
||||||
|
assert a['a']['2'] == 2.2
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class CircA:
|
class CircA:
|
||||||
b: CircB
|
b: CircB
|
||||||
|
|||||||
53
tests/test_flat_types_deflator.py
Normal file
53
tests/test_flat_types_deflator.py
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from megasniff.deflator import SchemaDeflatorGenerator
|
||||||
|
from src.megasniff import SchemaInflatorGenerator
|
||||||
|
|
||||||
|
|
||||||
|
def test_str_deflator():
|
||||||
|
defl = SchemaDeflatorGenerator()
|
||||||
|
fn = defl.schema_to_deflator(str, explicit_casts_override=True)
|
||||||
|
a = fn('asdf')
|
||||||
|
|
||||||
|
assert a == 'asdf'
|
||||||
|
a = fn(1234)
|
||||||
|
|
||||||
|
assert a == '1234'
|
||||||
|
|
||||||
|
fn1 = defl.schema_to_deflator(str, strict_mode_override=True)
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
fn1(1234)
|
||||||
|
|
||||||
|
|
||||||
|
def test_int_deflator():
|
||||||
|
defl = SchemaDeflatorGenerator()
|
||||||
|
fn = defl.schema_to_deflator(int, explicit_casts_override=True)
|
||||||
|
a = fn(1234)
|
||||||
|
|
||||||
|
assert a == 1234
|
||||||
|
a = fn('1234')
|
||||||
|
|
||||||
|
assert a == 1234
|
||||||
|
|
||||||
|
fn1 = defl.schema_to_deflator(int, strict_mode_override=True)
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
fn1('1234')
|
||||||
|
|
||||||
|
|
||||||
|
def test_float_deflator():
|
||||||
|
defl = SchemaDeflatorGenerator()
|
||||||
|
fn = defl.schema_to_deflator(float, explicit_casts_override=True)
|
||||||
|
a = fn(1234.1)
|
||||||
|
|
||||||
|
assert a == 1234.1
|
||||||
|
a = fn('1234')
|
||||||
|
|
||||||
|
assert a == 1234.0
|
||||||
|
|
||||||
|
fn1 = defl.schema_to_deflator(float, strict_mode_override=True)
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
fn1(1234)
|
||||||
Reference in New Issue
Block a user