<commit_before>import json
import pytest
from validr import T, Schema, Compiler, SchemaError, modelclass

from .helper import skipif_dict_not_ordered

EXPECT = {
    '$self': "dict.optional.desc('a dict')",
    'key': [
        'list.unique',
        'int.min(0).max(9)'
    ],
    'tag': "str.desc('a tag')"
}


@skipif_dict_not_ordered()
def test_str_copy_and_to_primitive():
    schema = T.dict(
        key=T.list(T.int.min(0).max(9)).unique.optional(False),
        tag=T.str.desc('a tag'),
    ).optional.desc('a dict').__schema__
    assert schema.to_primitive() == EXPECT
    assert json.loads(str(schema)) == EXPECT
    copy = schema.copy()
    assert copy.to_primitive() == EXPECT
    # verify copy is deep copy
    schema.items['key'].items = T.int
    assert copy.to_primitive() == EXPECT


def test_param_type():
    T.str.default(None)
    T.str.default("")
    T.int.default(0)
    T.bool.default(True)
    T.float.default(1.5)
    with pytest.raises(SchemaError):
        T.xxx.param([])
    with pytest.raises(SchemaError):
        T.xxx.param({})
    with pytest.raises(SchemaError):
        T.xxx.param(object)


def test_repr():
    schema = T.dict(
        key=T.list(T.int).unique,
    ).optional.desc('a dict')
    assert repr(schema) == "T.dict({key}).optional.desc('a dict')"
    schema = T.list(T.int.min(0)).unique
    assert repr(schema) == 'T.list(int).unique'
    schema = T.str.minlen(10).optional(False)
    assert repr(schema) == 'T.str.minlen(10)'
    assert repr(Schema()) == 'Schema<>'


def test_compiled_items():
    compiler = Compiler()
    value = compiler.compile(T.int.min(0))
    assert repr(T.dict(key=value)) == 'T.dict({key})'
    assert repr(T.list(value)) == 'T.list(int)'


def test_load_schema():
    compiler = Compiler()
    schema = T.list(T.int.min(0))
    assert T(schema) == schema
    assert T(compiler.compile(schema)) == schema
    assert T(['int.min(0)']) == schema


def test_slice():
    schema = T.dict(
        id=T.int,
        age=T.int.min(0),
        name=T.str,
    ).optional
    assert schema['id'] == T.dict(id=T.int).optional
    assert schema['age', 'name'] == T.dict(
        age=T.int.min(0),
        name=T.str,
    ).optional


def test_union_list():
    schema = T.union([
        T.str,
        T.list(T.str),
        T.dict(key1=T.str),
        T.dict(key1=T.str, key2=T.str),
    ]).optional
    assert T(json.loads(str(schema))) == schema
    assert T(schema.__schema__.to_primitive()) == schema
    with pytest.raises(SchemaError):
        T.union(T.str, T.int)
    with pytest.raises(SchemaError):
        T.union([T.str, int])


def test_union_dict():
    schema = T.union(
        type1=T.dict(key1=T.str),
        type2=T.dict(key2=T.str, key3=T.list(T.int)),
    ).by('type').optional
    assert T(json.loads(str(schema))) == schema
    assert T(schema.__schema__.to_primitive()) == schema
    with pytest.raises(SchemaError):
        T.union(type1=T.dict, type2=dict)


def test_dynamic_dict():
    schema = T.dict(labels=T.list(T.str)).key(
        T.str.minlen(2)
    ).value(
        T.list(T.int)
    ).optional
    assert T(json.loads(str(schema))) == schema<commit_msg>add test on union schema copy<commit_after>import json
import pytest
from validr import T, Schema, Compiler, SchemaError, modelclass

from .helper import skipif_dict_not_ordered

EXPECT = {
    '$self': "dict.optional.desc('a dict')",
    'key': [
        'list.unique',
        'int.min(0).max(9)'
    ],
    'tag': "str.desc('a tag')"
}


@skipif_dict_not_ordered()
def test_str_copy_and_to_primitive():
    schema = T.dict(
        key=T.list(T.int.min(0).max(9)).unique.optional(False),
        tag=T.str.desc('a tag'),
    ).optional.desc('a dict').__schema__
    assert schema.to_primitive() == EXPECT
    assert json.loads(str(schema)) == EXPECT
    copy = schema.copy()
    assert copy.to_primitive() == EXPECT
    # verify copy is deep copy
    schema.items['key'].items = T.int
    assert copy.to_primitive() == EXPECT


def test_param_type():
    T.str.default(None)
    T.str.default("")
    T.int.default(0)
    T.bool.default(True)
    T.float.default(1.5)
    with pytest.raises(SchemaError):
        T.xxx.param([])
    with pytest.raises(SchemaError):
        T.xxx.param({})
    with pytest.raises(SchemaError):
        T.xxx.param(object)


def test_repr():
    schema = T.dict(
        key=T.list(T.int).unique,
    ).optional.desc('a dict')
    assert repr(schema) == "T.dict({key}).optional.desc('a dict')"
    schema = T.list(T.int.min(0)).unique
    assert repr(schema) == 'T.list(int).unique'
    schema = T.str.minlen(10).optional(False)
    assert repr(schema) == 'T.str.minlen(10)'
    assert repr(Schema()) == 'Schema<>'


def test_compiled_items():
    compiler = Compiler()
    value = compiler.compile(T.int.min(0))
    assert repr(T.dict(key=value)) == 'T.dict({key})'
    assert repr(T.list(value)) == 'T.list(int)'


def test_load_schema():
    compiler = Compiler()
    schema = T.list(T.int.min(0))
    assert T(schema) == schema
    assert T(compiler.compile(schema)) == schema
    assert T(['int.min(0)']) == schema


def test_slice():
    schema = T.dict(
        id=T.int,
        age=T.int.min(0),
        name=T.str,
    ).optional
    assert schema['id'] == T.dict(id=T.int).optional
    assert schema['age', 'name'] == T.dict(
        age=T.int.min(0),
        name=T.str,
    ).optional


def test_union_list():
    schema = T.union([
        T.str,
        T.list(T.str),
        T.dict(key1=T.str),
        T.dict(key1=T.str, key2=T.str),
    ]).optional
    assert schema.__schema__.copy() == schema.__schema__
    assert T(json.loads(str(schema))) == schema
    assert T(schema.__schema__.to_primitive()) == schema
    with pytest.raises(SchemaError):
        T.union(T.str, T.int)
    with pytest.raises(SchemaError):
        T.union([T.str, int])


def test_union_dict():
    schema = T.union(
        type1=T.dict(key1=T.str),
        type2=T.dict(key2=T.str, key3=T.list(T.int)),
    ).by('type').optional
    assert T(json.loads(str(schema))) == schema
    assert T(schema.__schema__.to_primitive()) == schema
    with pytest.raises(SchemaError):
        T.union(type1=T.dict, type2=dict)


def test_dynamic_dict():
    schema = T.dict(labels=T.list(T.str)).key(
        T.str.minlen(2)
    ).value(
        T.list(T.int)
    ).optional
    assert T(json.loads(str(schema))) == schema
{'fraction_non_alphanumeric': 0.17720530835284934, 'fraction_numerical': 0.008743169398907104, 'mean_word_length': 2.8800726832222896, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5963327', 'n_tokens_mistral': 2346, 'n_tokens_neox': 2331, 'n_words': 397}