| 1 |
import pytest |
| 2 |
|
| 3 |
import os |
| 4 |
|
| 5 |
dbm = pytest.importorskip('dbm') |
| 6 |
|
| 7 |
|
| 8 |
def test_get(tmpdir): |
| 9 |
path = str(tmpdir.join('test_dbm_extra.test_get')) |
| 10 |
d = dbm.open(path, 'c') |
| 11 |
x = d.get("42") |
| 12 |
assert x is None |
| 13 |
d.close() |
| 14 |
|
| 15 |
def test_delitem(tmpdir): |
| 16 |
path = str(tmpdir.join('test_dbm_extra.test_delitem')) |
| 17 |
d = dbm.open(path, 'c') |
| 18 |
with pytest.raises(KeyError): |
| 19 |
del d['xyz'] |
| 20 |
|
| 21 |
def test_nonstring(tmpdir): |
| 22 |
path = str(tmpdir.join('test_dbm_extra.test_nonstring')) |
| 23 |
d = dbm.open(path, 'c') |
| 24 |
if isinstance(d, dbm.dumb._Database): |
| 25 |
pytest.skip('Needs adpatation for failure of d[123]') |
| 26 |
with pytest.raises(TypeError): |
| 27 |
d[123] = 'xyz' |
| 28 |
with pytest.raises(TypeError): |
| 29 |
d['xyz'] = 123 |
| 30 |
with pytest.raises(TypeError): |
| 31 |
d['xyz'] = None |
| 32 |
with pytest.raises(TypeError): |
| 33 |
del d[123] |
| 34 |
with pytest.raises(TypeError): |
| 35 |
d[123] |
| 36 |
with pytest.raises(TypeError): |
| 37 |
123 in d |
| 38 |
with pytest.raises(AttributeError): |
| 39 |
d.has_key(123) |
| 40 |
with pytest.raises(TypeError): |
| 41 |
d.setdefault(123, 'xyz') |
| 42 |
with pytest.raises(TypeError): |
| 43 |
d.setdefault('xyz', 123) |
| 44 |
with pytest.raises(TypeError): |
| 45 |
d.get(123) |
| 46 |
assert dict(d) == {} |
| 47 |
d.setdefault('xyz', '123') |
| 48 |
assert dict(d) == {b'xyz': b'123'} |
| 49 |
d.close() |
| 50 |
|
| 51 |
def test_multiple_sets(tmpdir): |
| 52 |
path = str(tmpdir.join('test_dbm_extra.test_multiple_sets')) |
| 53 |
d = dbm.open(path, 'c') |
| 54 |
d['xyz'] = '12' |
| 55 |
d['xyz'] = '3' |
| 56 |
d['xyz'] = '546' |
| 57 |
assert dict(d) == {b'xyz': b'546'} |
| 58 |
assert d['xyz'] == b'546' |
| 59 |
|
| 60 |
@pytest.mark.skipif("'__pypy__' not in sys.modules") |
| 61 |
def test_extra(): |
| 62 |
try: |
| 63 |
import _dbm |
| 64 |
except ImportError: |
| 65 |
pytest.skip('no _dbm available') |
| 66 |
with pytest.raises(TypeError): |
| 67 |
_dbm.datum(123) |
| 68 |
with pytest.raises(TypeError): |
| 69 |
_dbm.datum(False) |
| 70 |
|
| 71 |
def test_null(): |
| 72 |
db = dbm.open('test', 'c') |
| 73 |
db['1'] = 'a\x00b' |
| 74 |
db.close() |
| 75 |
|
| 76 |
db = dbm.open('test', 'r') |
| 77 |
assert db['1'] == b'a\x00b' |
| 78 |
db.close() |
| 79 |
|
| 80 |
def test_key_with_empty_value(tmpdir): |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
path = str(tmpdir.join('test_dbm_extra.test_key_with_empty_value')) |
| 85 |
d = dbm.open(path, 'c') |
| 86 |
assert 'key_with_empty_value' not in d |
| 87 |
d['key_with_empty_value'] = '' |
| 88 |
assert 'key_with_empty_value' in d |
| 89 |
assert d['key_with_empty_value'] == b'' |
| 90 |
d.close() |
| 91 |
|
| 92 |
def test_unicode_filename(tmpdir): |
| 93 |
path = str(tmpdir) + os.sep + u'test_dbm_extra.test_unicode_filename' |
| 94 |
d = dbm.open(path, 'c') |
| 95 |
d.close() |