Beispiel #1
0
def test_missing_data_gives_nothing():
    """Test that we get an array if we set array = true in object."""
    input_data = {'key': 'test name'}
    config = KaibaObject(
        **{
            'name':
            'root',
            'array':
            True,
            'attributes': [
                {
                    'name': 'name',
                    'data_fetchers': [
                        {
                            'path': ['missing'],
                        },
                    ],
                },
            ],
        })

    assert not is_successful(map_data(
        input_data,
        config,
    ))
Beispiel #2
0
def test_array_true_but_no_loop_gives_array():
    """Test that we get an array if we set array = true in object."""
    input_data = {'key': 'test name'}
    config = KaibaObject(
        **{
            'name':
            'root',
            'array':
            True,
            'attributes': [
                {
                    'name': 'name',
                    'data_fetchers': [
                        {
                            'path': ['key'],
                        },
                    ],
                },
            ],
        })

    assert map_data(
        input_data,
        config,
    ).unwrap() == [{
        'name': 'test name'
    }]
Beispiel #3
0
def process(
    input_data: dict,
    configuration: dict,
) -> ResultE[Union[list, dict]]:
    """Validate configuration then process data."""
    try:
        cfg = KaibaObject(**configuration)
    except ValidationError as ve:
        return Failure(ve)

    return map_data(input_data, cfg)
Beispiel #4
0
def test_missing_data_creates_no_object():
    """Test that if an object mapping result is empty we create now 'key'."""
    input_data = {'key': 'test name'}
    config = KaibaObject(
        **{
            'name':
            'root',
            'array':
            True,
            'attributes': [
                {
                    'name': 'an_attribute',
                    'default': 'val',
                },
            ],
            'objects': [
                {
                    'name':
                    'test',
                    'array':
                    False,
                    'attributes': [
                        {
                            'name': 'name',
                            'data_fetchers': [
                                {
                                    'path': ['missing'],
                                },
                            ],
                        },
                    ],
                },
            ],
        })

    expected_result = [{
        'an_attribute': 'val',
    }]

    assert map_data(
        input_data,
        config,
    ).unwrap() == expected_result
Beispiel #5
0
def test_creating_key_to_name():
    """Test that we can fetch key in dict."""
    input_data = {'key': 'test name'}
    config = KaibaObject(
        name='root',
        array=False,
        attributes=[
            {
                'name': 'name',
                'data_fetchers': [
                    {
                        'path': ['key'],
                    },
                ],
            },
        ],
    )

    assert map_data(
        input_data,
        config,
    ).unwrap() == {
        'name': 'test name'
    }
Beispiel #6
0
def test_most_features():
    """Test that we can fetch key in dict."""
    config = KaibaObject(
        **{
            'name':
            'schema',
            'array':
            False,
            'attributes': [
                {
                    'name':
                    'name',
                    'data_fetchers': [
                        {
                            'path': ['key'],
                            'if_statements': [
                                {
                                    'condition': 'is',
                                    'target': 'val1',
                                    'then': None,
                                },
                            ],
                            'default':
                            'default',
                        },
                        {
                            'path': ['key2'],
                            'if_statements': [
                                {
                                    'condition': 'is',
                                    'target': 'val2',
                                    'then': 'if',
                                },
                            ],
                        },
                    ],
                    'separator':
                    '-',
                    'if_statements': [
                        {
                            'condition': 'is',
                            'target': 'default-if',
                            'then': None,
                        },
                    ],
                    'default':
                    'default2',
                },
            ],
            'objects': [
                {
                    'name':
                    'address',
                    'array':
                    False,
                    'attributes': [
                        {
                            'name': 'address1',
                            'data_fetchers': [
                                {
                                    'path': ['a1'],
                                },
                            ],
                        },
                        {
                            'name': 'address2',
                            'data_fetchers': [
                                {
                                    'path': ['a2'],
                                },
                            ],
                        },
                    ],
                },
                {
                    'name':
                    'people',
                    'array':
                    True,
                    'iterators': [
                        {
                            'alias': 'persons',
                            'path': ['persons'],
                        },
                    ],
                    'attributes': [
                        {
                            'name': 'firstname',
                            'data_fetchers': [
                                {
                                    'path': ['persons', 'name'],
                                },
                            ],
                        },
                    ],
                },
            ],
            'branching_objects': [
                {
                    'name':
                    'extrafield',
                    'array':
                    True,
                    'branching_attributes': [
                        [
                            {
                                'name': 'dataname',
                                'default': 'one',
                            },
                            {
                                'name':
                                'datavalue',
                                'data_fetchers': [
                                    {
                                        'path': ['extra', 'extra1'],
                                    },
                                ],
                            },
                        ],
                        [
                            {
                                'name': 'dataname',
                                'default': 'two',
                            },
                            {
                                'name':
                                'datavalue',
                                'data_fetchers': [
                                    {
                                        'path': ['extra', 'extra2'],
                                    },
                                ],
                            },
                        ],
                    ],
                },
            ],
        })
    input_data = {
        'key': 'val1',
        'key2': 'val2',
        'a1': 'a1',
        'a2': 'a2',
        'persons': [{
            'name': 'john'
        }, {
            'name': 'bob'
        }],
        'extra': {
            'extra1': 'extra1val',
            'extra2': 'extra2val',
        },
    }
    expected_result = {
        'name':
        'default2',
        'address': {
            'address1': 'a1',
            'address2': 'a2',
        },
        'people': [
            {
                'firstname': 'john'
            },
            {
                'firstname': 'bob'
            },
        ],
        'extrafield': [
            {
                'dataname': 'one',
                'datavalue': 'extra1val'
            },
            {
                'dataname': 'two',
                'datavalue': 'extra2val'
            },
        ],
    }

    assert map_data(
        input_data,
        config,
    ).unwrap() == expected_result
Beispiel #7
0
def test_mapping_where_data_is_not_found():
    """Test that when we map and don't find data its okay."""
    config = KaibaObject(
        **{
            'name':
            'root',
            'array':
            True,
            'iterators': [
                {
                    'alias': 'journals',
                    'path': ['journals'],
                },
            ],
            'attributes': [
                {
                    'name': 'journal_id',
                    'data_fetchers': [
                        {
                            'path': ['journals', 'journal', 'id'],
                        },
                    ],
                },
            ],
            'objects': [
                {
                    'name':
                    'invoices',
                    'array':
                    True,
                    'iterators': [
                        {
                            'alias': 'invoices',
                            'path': ['journals', 'journal', 'invoices'],
                        },
                    ],
                    'attributes': [
                        {
                            'name':
                            'amount',
                            'data_fetchers': [
                                {
                                    'path': ['invoices', 'amount'],
                                },
                            ],
                        },
                    ],
                },
            ],
            'branching_objects': [
                {
                    'name':
                    'extrafield',
                    'array':
                    True,
                    'branching_attributes': [
                        [
                            {
                                'name':
                                'datavalue',
                                'data_fetchers': [
                                    {
                                        'path': ['extra', 'extra1'],
                                    },
                                ],
                            },
                        ],
                    ],
                },
            ],
        })
    input_data = {
        'journals': [
            {
                'journal': {
                    'id': 1,
                    'invoices': [{}, {
                        'amount': 1.2
                    }],
                },
            },
            {
                'journal': {
                    'id': 2,
                },
            },
        ],
    }
    expected_result = [
        {
            'journal_id': 1,
            'invoices': [
                {
                    'amount': 1.2
                },
            ],
        },
        {
            'journal_id': 2,
            'invoices': [],
        },
    ]

    assert map_data(
        input_data,
        config,
    ).unwrap() == expected_result
Beispiel #8
0
def test_double_repeatable():
    """Test that we can map nested repeatable objects."""
    config = KaibaObject(
        **{
            'name':
            'root',
            'array':
            True,
            'iterators': [
                {
                    'alias': 'journals',
                    'path': ['journals'],
                },
            ],
            'attributes': [
                {
                    'name': 'journal_id',
                    'data_fetchers': [
                        {
                            'path': ['journals', 'journal', 'id'],
                        },
                    ],
                },
            ],
            'objects': [
                {
                    'name':
                    'invoices',
                    'array':
                    True,
                    'iterators': [
                        {
                            'alias': 'invoices',
                            'path': ['journals', 'journal', 'invoices'],
                        },
                    ],
                    'attributes': [
                        {
                            'name':
                            'amount',
                            'data_fetchers': [
                                {
                                    'path': ['invoices', 'amount'],
                                },
                            ],
                        },
                    ],
                },
            ],
        })
    input_data = {
        'journals': [
            {
                'journal': {
                    'id': 1,
                    'invoices': [{
                        'amount': 1.1
                    }, {
                        'amount': 1.2
                    }],
                },
            },
            {
                'journal': {
                    'id': 2,
                    'invoices': [{
                        'amount': 1.3
                    }, {
                        'amount': 1.4
                    }],
                },
            },
        ],
    }
    expected_result = [
        {
            'journal_id': 1,
            'invoices': [
                {
                    'amount': 1.1
                },
                {
                    'amount': 1.2
                },
            ],
        },
        {
            'journal_id': 2,
            'invoices': [
                {
                    'amount': 1.3
                },
                {
                    'amount': 1.4
                },
            ],
        },
    ]

    assert map_data(
        input_data,
        config,
    ).unwrap() == expected_result