Example #1
0
def test_filter():
    is_odd = lambda x: x % 2
    odd_spec = Iter().filter(is_odd)
    out = glom(RANGE_5, odd_spec)
    assert list(out) == [1, 3]

    # let's just make sure we're actually streaming just in case
    counter = count()
    out = glom(counter, odd_spec)
    assert next(out) == 1
    assert next(out) == 3
    assert next(counter) == 4
    assert next(counter) == 5
    assert next(out) == 7

    bools = [True, False, False, True, False]
    spec = Iter().filter().all()
    out = glom(bools, spec)
    assert out == [True, True]

    imags = [0j, 1j, 2, 2j, 3j]
    spec = Iter().filter(
        Check(T.imag.real, type=float, one_of=(0, 2), default=SKIP)).all()
    out = glom(imags, spec)
    assert out == [0j, 2j]

    assert repr(Iter().filter(T.a.b)).startswith('Iter().filter(T.a.b)')
Example #2
0
def test_check_signature():
    with raises(ValueError):
        Check(instance_of=())
    with raises(ValueError):
        Check(type=())

    with raises(TypeError):
        Check(fake_kwarg=True)

    with raises(ValueError):
        Check(one_of=1)
    with raises(ValueError):
        Check(one_of=())
    with raises(TypeError):
        Check(one_of=(1, 2), equal_to=3)

    with raises(ValueError):
        Check(validate='bad, not callable, value')
Example #3
0
def test_check_multi():
    target = 1
    with raises(CheckError) as exc_info:
        glom(target, Check(instance_of=float, validate=lambda x: x > 3.14))

    assert "2 errors" in str(exc_info.value)
Example #4
0
def test_check_basic():
    assert glom([0, SKIP], [T]) == [0]  # sanity check SKIP

    target = [{'id': 0}, {'id': 1}, {'id': 2}]

    # check that skipping non-passing values works
    assert glom(target, ([Coalesce(Check('id', equal_to=0), default=SKIP)], T[0])) == {'id': 0}
    assert glom(target, ([Check('id', equal_to=0, default=SKIP)], T[0])) == {'id': 0}

    # check that stopping iteration on non-passing values works
    assert glom(target, [Check('id', equal_to=0, default=STOP)]) == [{'id': 0}]

    # check that stopping chain execution on non-passing values works
    spec = (Check(validate=lambda x: len(x) > 0, default=STOP), T[0])
    assert glom('hello', spec) == 'h'
    assert glom('', spec) == ''  # would fail with IndexError if STOP didn't work

    assert repr(Check()) == 'Check()'
    assert repr(Check(T.a)) == 'Check(T.a)'
    assert repr(Check(equal_to=1)) == 'Check(equal_to=1)'
    assert repr(Check(instance_of=dict)) == 'Check(instance_of=dict)'
    assert repr(Check(T(len), validate=sum)) == 'Check(T(len), validate=sum)'

    target = [1, u'a']
    assert glom(target, [Check(type=unicode, default=SKIP)]) == ['a']
    assert glom(target, [Check(type=(unicode, int))]) == [1, 'a']
    assert glom(target, [Check(instance_of=unicode, default=SKIP)]) == ['a']
    assert glom(target, [Check(instance_of=(unicode, int))]) == [1, 'a']

    target = ['1']
    assert glom(target, [Check(validate=(int, float))])
    assert glom(target, [Check()])  # bare check does a truthy check

    failing_checks = [
        ({'a': {'b': 1}}, {'a': ('a', 'b', Check(type=str))},
         '''target at path ['a', 'b'] failed check, got error: "expected type to be 'str', found type 'int'"'''),
        ({'a': {'b': 1}}, {'a': ('a', Check('b', type=str))},
         '''target at path ['a'] failed check, subtarget at 'b' got '''
         '''error: "expected type to be 'str', found type 'int'"'''),
        (1, Check(type=(unicode, bool))),
        (1, Check(instance_of=unicode)),
        (1, Check(instance_of=(unicode, bool))),
        (1, Check(equal_to=0)),
        (1, Check(one_of=(0,))),
        (1, Check(one_of=(0, 2))),
        ('-3.14', Check(validate=int)),
        ('', Check(validate=lambda x: False))]

    for fc in failing_checks:
        if len(fc) == 2:
            target, check = fc
            msg = None
        else:
            target, check, msg = fc

        with raises(CheckError) as exc_info:
            glom(target, check)

        if msg is not None:
            assert str(exc_info.value).find(msg) != -1
        assert repr(exc_info.value)
Example #5
0
}

GAPS_COLUMNS = [
    ["Company Name", "company_name", "blue"],
    ["Control Name", "name", "orange"],
    ["Control Number", "number", "orange"],
    ["Level", "impact_level", "orange"],
    ["Remedy", "remedy", "orange"],
]

GAPS_SUMMARY = {
    "total_findings": (Coalesce("residual_risk.findings", default=[]), len),
    "total_high_findings": (
        Coalesce("residual_risk.findings", default=[]),
        ["impact_level"],
        [Check(equal_to="High", default=SKIP)],
        len,
    ),
    "total_medium_findings": (
        Coalesce("residual_risk.findings", default=[]),
        ["impact_level"],
        [Check(equal_to="Medium", default=SKIP)],
        len,
    ),
    "total_low_findings": (
        Coalesce("residual_risk.findings", default=[]),
        ["impact_level"],
        [Check(equal_to="Low", default=SKIP)],
        len,
    ),
}
Example #6
0
import json
from datetime import datetime

import glom
from glom import Coalesce, Check, glom

from .db import Smappee

spec = {
    'utc_timestamp':
    ('utcTimeStamp', lambda x: datetime.fromtimestamp(x / 1000)),
    'total_power': ('totalPower', Check(type=int)),
    'total_reactive_power': ('totalReactivePower', Check(type=int)),
    'total_export_energy': ('totalExportEnergy', Check(type=int)),
    'total_import_energy': ('totalImportEnergy', Check(type=int)),
    'monitor_status': ('monitorStatus', Check(type=int)),
}

# Channel Power
for i in range(6):
    spec.update({
        'ct_input{}'.format(i):
        ('channelPowers.{}.ctInput'.format(i), Check(type=int)),
        'power{}'.format(i): ('channelPowers.{}.power'.format(i),
                              Check(type=int)),
        'export_energy{}'.format(i):
        ('channelPowers.{}.exportEnergy'.format(i), Check(type=int)),
        'import_energy{}'.format(i):
        ('channelPowers.{}.importEnergy'.format(i), Check(type=int)),
        'phase_id{}'.format(i): ('channelPowers.{}.phaseId'.format(i),
                                 Check(type=int)),
Example #7
0
"""Module with data schemas."""
from glom import glom, Check, GlomError, Coalesce, OMIT
from app.models import enums
from app.processors import validation
from app.errors import SchemaError


def parse_id(t):
    """Parse mongo id."""
    return str(t) if t is not OMIT else OMIT


CALL_RECORD_START = {
    "id": (Coalesce("_id", default=OMIT), parse_id),
    "type": (Coalesce("type.value", "type"),
             Check(validate=lambda x: enums.CallRecordType(x))),
    "timestamp": ("timestamp",
                  Check(type=float)),
    "call_id": ("call_id",
                Check(type=int)),
    "source": ("source",
               Check(type=str,
                     validate=validation.phone_number)),
    "destination": ("destination",
                    Check(type=str,
                          validate=validation.phone_number))
}

CALL_RECORD_END = {
    "id": (Coalesce("_id", default=OMIT), parse_id),
    "type": (Coalesce("type.value", "type"),