def test_should_save_db_then_load_db():
    i = {
        'a': UdbIndex(['a']),
        'b': UdbIndex(['b']),
    }

    s = UdbJsonFileStorage('ignore.test')

    s.drop()

    s.save(i, 4, {1: {'a': 1}, 3: {'a': 2}})

    db = s.load()

    assert db == {
        'data': {
            '1': {
                'a': 1
            },
            '3': {
                'a': 2
            },
        },
        'indexes': {
            'a': [['a'], None],
            'b': [['b'], None],
        },
        'revision': 4,
    }
Beispiel #2
0
def test_should_get_cover_key_or_raise_on_not_covered_data_with_default_value(
):
    i = UdbIndex((('a', required), ('b', 2), ('c', required)))
    d = {'a': 1, 'c': 3}

    assert i.get_cover_key_or_raise(d) == ''.join(
        type_formatter_iter([1, 2, 3]))
Beispiel #3
0
def test_should_get_cover_key_on_not_covered_data_with_default_value_as_callable(
):
    i = UdbIndex(
        (('a', required), ('b', lambda key, values: 2), ('c', required)))
    d = {'a': 1, 'c': 3}

    assert i.get_cover_key(d) == ''.join(type_formatter_iter([1, 2, 3]))
Beispiel #4
0
def test_should_validate_query_primitive_value():
    assert UdbIndex.validate_query({'a': None})
    assert UdbIndex.validate_query({'a': False})
    assert UdbIndex.validate_query({'a': True})
    assert UdbIndex.validate_query({'a': 0})
    assert UdbIndex.validate_query({'a': 0.5})
    assert UdbIndex.validate_query({'a': '0'})

    with pytest.raises(InvalidScanOperationValueError):
        UdbIndex.validate_query({'a': []})

    with pytest.raises(InvalidScanOperationValueError):
        UdbIndex.validate_query({'a': UdbIndex})
def test_should_save_db_then_load_db():
    i = {
        'a': UdbIndex(['a']),
        'b': UdbIndex(['b']),
    }

    s = UdbWalStorage('ignore.test')

    s.drop()

    s.save_meta(i, 0)

    s.load()

    s.on_insert(1, {'a': 1})
    s.on_insert(2, {'a': 2})
    s.on_insert(3, {'a': 3})
    s.on_delete(2)
    s.on_update(3, {'a': 3}, {'b': 3})

    s.load()

    s.on_update(3, {'a': 3}, {'b': 333})

    db = s.load()

    assert db == {
        'data': {
            1: {
                'a': 1,
                '__rev__': 1
            },
            3: {
                'a': 3,
                'b': 333,
                '__rev__': 3
            },
        },
        'indexes': {
            'a': [['a'], None],
            'b': [['b'], None],
        },
        'revision': 4,
    }
Beispiel #6
0
def test_should_be_constructed_with_schema_as_list():
    i = UdbIndex(['a', 'b', 'c'])

    assert i.schema == {'a': EMPTY, 'b': EMPTY, 'c': EMPTY}
Beispiel #7
0
def test_should_validate_query_in_nin_ops():
    for op in ('$in', '$nin'):
        assert UdbIndex.validate_query({'a': {op: [None]}})
        assert UdbIndex.validate_query({'a': {op: [False]}})
        assert UdbIndex.validate_query({'a': {op: [True]}})
        assert UdbIndex.validate_query({'a': {op: [0]}})
        assert UdbIndex.validate_query({'a': {op: [0.5]}})
        assert UdbIndex.validate_query({'a': {op: ['0']}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: None}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: False}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: True}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: 0}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: 0.5}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: '0'}})
Beispiel #8
0
def test_should_validate_query_eq_gt_gte_lt_lte_ne_ops():
    for op in ('$eq', '$gt', '$gte', '$lt', '$lte', '$ne'):
        assert UdbIndex.validate_query({'a': {op: None}})
        assert UdbIndex.validate_query({'a': {op: False}})
        assert UdbIndex.validate_query({'a': {op: True}})
        assert UdbIndex.validate_query({'a': {op: 0}})
        assert UdbIndex.validate_query({'a': {op: 0.5}})
        assert UdbIndex.validate_query({'a': {op: '0'}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: []}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: {}}})

        with pytest.raises(InvalidScanOperationValueError):
            UdbIndex.validate_query({'a': {op: UdbIndex}})
Beispiel #9
0
def test_should_be_constructed_with_schema_as_dict():
    i = UdbIndex({'d': EMPTY, 'e': EMPTY, 'f': EMPTY})

    assert i.schema == {'d': EMPTY, 'e': EMPTY, 'f': EMPTY}
Beispiel #10
0
def test_should_not_get_cover_or_raise_key_on_fully_uncovered_data_as_sparsed(
):
    i = UdbIndex(['a', 'b', 'c'], sparsed=True)
    d = {}

    assert i.get_cover_key_or_raise(d) is None
Beispiel #11
0
def test_should_get_cover_key_or_raise_on_not_covered_data_as_sparsed():
    i = UdbIndex(['a', 'b', 'c'], sparsed=True)
    d = {'a': 1, 'c': 3}

    assert i.get_cover_key_or_raise(d) == ''.join(
        type_formatter_iter([1, None, 3]))
Beispiel #12
0
def test_should_get_cover_key_on_full_covered_data():
    i = UdbIndex(['a', 'b', 'c'])
    d = {'a': 1, 'b': 2, 'c': 3}

    assert i.get_cover_key(d) == ''.join(type_formatter_iter([1, 2, 3]))
Beispiel #13
0
def test_should_raise_exception_on_required_field_in_get_cover_key_or_raise():
    i = UdbIndex(['a', 'b', 'c'])

    with pytest.raises(FieldRequiredError):
        i.get_cover_key_or_raise({'a': 1, 'c': 3})
Beispiel #14
0
def test_should_not_get_cover_key_on_not_covered_data():
    i = UdbIndex(['a', 'b', 'c'])
    d = {'a': 1, 'c': 3}

    assert i.get_cover_key(d) is None