File: test_dbm.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (95 lines) | stat: -rw-r--r-- 2,634 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pytest

import os

dbm = pytest.importorskip('dbm')


def test_get(tmpdir):
    path = str(tmpdir.join('test_dbm_extra.test_get'))
    d = dbm.open(path, 'c')
    x = d.get("42")
    assert x is None
    d.close()

def test_delitem(tmpdir):
    path = str(tmpdir.join('test_dbm_extra.test_delitem'))
    d = dbm.open(path, 'c')
    with pytest.raises(KeyError):
        del d['xyz']

def test_nonstring(tmpdir):
    path = str(tmpdir.join('test_dbm_extra.test_nonstring'))
    d = dbm.open(path, 'c')
    if isinstance(d, dbm.dumb._Database):
        pytest.skip('Needs adpatation for failure of d[123]')
    with pytest.raises(TypeError):
        d[123] = 'xyz'
    with pytest.raises(TypeError):
        d['xyz'] = 123
    with pytest.raises(TypeError):
        d['xyz'] = None
    with pytest.raises(TypeError):
        del d[123]
    with pytest.raises(TypeError):
        d[123]
    with pytest.raises(TypeError):
        123 in d
    with pytest.raises(AttributeError):
        d.has_key(123)
    with pytest.raises(TypeError):
        d.setdefault(123, 'xyz')
    with pytest.raises(TypeError):
        d.setdefault('xyz', 123)
    with pytest.raises(TypeError):
        d.get(123)
    assert dict(d) == {}
    d.setdefault('xyz', '123')
    assert dict(d) == {b'xyz': b'123'}
    d.close()

def test_multiple_sets(tmpdir):
    path = str(tmpdir.join('test_dbm_extra.test_multiple_sets'))
    d = dbm.open(path, 'c')
    d['xyz'] = '12'
    d['xyz'] = '3'
    d['xyz'] = '546'
    assert dict(d) == {b'xyz': b'546'}
    assert d['xyz'] == b'546'

@pytest.mark.skipif("'__pypy__' not in sys.modules")
def test_extra():
    try:
        import _dbm
    except ImportError:
        pytest.skip('no _dbm available')
    with pytest.raises(TypeError):
        _dbm.datum(123)
    with pytest.raises(TypeError):
        _dbm.datum(False)

def test_null():
    db = dbm.open('test', 'c')
    db['1'] = 'a\x00b'
    db.close()

    db = dbm.open('test', 'r')
    assert db['1'] == b'a\x00b'
    db.close()

def test_key_with_empty_value(tmpdir):
    # this test fails on CPython too (at least on tannit), and the
    # case shows up when gdbm is not installed and test_anydbm.py
    # falls back dbm.
    path = str(tmpdir.join('test_dbm_extra.test_key_with_empty_value'))
    d = dbm.open(path, 'c')
    assert 'key_with_empty_value' not in d
    d['key_with_empty_value'] = ''
    assert 'key_with_empty_value' in d
    assert d['key_with_empty_value'] == b''
    d.close()

def test_unicode_filename(tmpdir):
    path = str(tmpdir) + os.sep + u'test_dbm_extra.test_unicode_filename'
    d = dbm.open(path, 'c')
    d.close()