コード例 #1
0
def test_get_string_value_from_key():
    """Test that we can find value."""
    input_data = {'key': 'val1'}
    config = DataFetcher(**{'path': ['key']})

    assert handle_data_fetcher(
        input_data,
        config,
    ) == Success('val1')
コード例 #2
0
def test_get_object_value_from_key():
    """Test that we can find an object."""
    input_data = {'key': {'obj': 'val1'}}
    config = DataFetcher(**{'path': ['key']})

    assert handle_data_fetcher(
        input_data,
        config,
    ) == Success({'obj': 'val1'})
コード例 #3
0
def test_default_value_is_used():
    """Test that we get a default value when no path and no ifs."""
    input_data = {'key': 'val'}
    config = DataFetcher(**{'default': 'default'})

    assert handle_data_fetcher(
        input_data,
        config,
    ).unwrap() == 'default'
コード例 #4
0
def test_get_array_value_from_key():
    """Test that we can find an array."""
    input_data = {'key': ['array']}
    config = DataFetcher(**{'path': ['key']})

    assert handle_data_fetcher(
        input_data,
        config,
    ) == Success(['array'])
コード例 #5
0
ファイル: test_data_fetcher.py プロジェクト: kaiba-tech/kaiba
def test_only_int_and_str_in_path():
    """Test that giving an empty path is an error."""
    with pytest.raises(ValidationError) as ve:
        DataFetcher(path=[12.2])

    errors = ve.value.errors()[0]  # noqa: WPS441

    assert errors['loc'] == ('path', 0)
    assert errors['msg'] == 'str type expected'
コード例 #6
0
ファイル: test_data_fetcher.py プロジェクト: kaiba-tech/kaiba
def test_validates():  # noqa: WPS218
    """Test that dict is marshalled to pydantic object."""
    test = DataFetcher(
        path=['test', 123],
        default='Default',
    )
    assert test.path == ['test', 123]
    assert test.regex is None
    assert isinstance(test.if_statements, list)
    assert test.default == 'Default'
コード例 #7
0
def test_default_value_not_none():
    """Test that providing bad data returns Failure instance."""
    failure = handle_data_fetcher(
        {'fail': 'failure'},
        DataFetcher(**{
            'path': [],
            'default': None
        }),
    )
    assert not is_successful(failure)
    assert 'Default value should not be `None`' in str(failure.failure())
コード例 #8
0
def test_slicing_is_applied():
    """Test that applying slicing works."""
    input_data = {'key': 'value'}
    config = DataFetcher(**{
        'path': ['key'],
        'slicing': {
            'from': 2,
            'to': 3,
        },
    })
    assert handle_data_fetcher(
        input_data,
        config,
    ).unwrap() == 'l'
コード例 #9
0
def test_regex_is_applied_on_group_as_list():
    """Test that we can search by pattern when it is a list."""
    input_data: dict = {'game': '1. e4 e5 6. Qe2+ Qe7 7. Qxe7+ Kxe7 8. d4 Re8'}
    config = DataFetcher(
        **{
            'path': ['game'],
            'regex': {
                'expression': r'(e\d)+',
                'group': [0, 1, 6],
            },
        })
    assert handle_data_fetcher(
        input_data,
        config,
    ).unwrap() == ['e4', 'e5', 'e8']
コード例 #10
0
def test_regex_is_applied():
    """Test that we can search by pattern."""
    input_data: dict = {
        'game': '8. d4 Re8 ... 14. Rxe8+ Rxe8 15. h3'
    }  # noqa: E501
    config = DataFetcher(**{
        'path': ['game'],
        'regex': {
            'expression': '(Rxe8.*)',
        },
    })
    assert handle_data_fetcher(
        input_data,
        config,
    ).unwrap() == 'Rxe8+ Rxe8 15. h3'
コード例 #11
0
def test_if_statements_are_applied():
    """Test that applying if statements works."""
    input_data = {'key': 'val'}
    config = DataFetcher(
        **{
            'if_statements': [{
                'condition': 'is',
                'target': None,
                'then': 'otherval',
            }],
            'default':
            'bob',
        })
    assert handle_data_fetcher(
        input_data,
        config,
    ).unwrap() == 'otherval'