2013-12-17 14:23:34 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import pytest
|
2013-12-17 14:27:14 -08:00
|
|
|
import os
|
2013-12-17 15:35:23 -08:00
|
|
|
import subprocess # to access the script from shell
|
2013-12-17 14:23:34 -08:00
|
|
|
|
|
|
|
# will try as python 3 then default to python 2 modules
|
|
|
|
try:
|
|
|
|
import dbm
|
2013-12-20 13:51:34 -08:00
|
|
|
from dbm import ndbm
|
|
|
|
from dbm import gnu as gdbm
|
|
|
|
from dbm import dumb
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2013-12-17 14:23:34 -08:00
|
|
|
whichdb = dbm.whichdb
|
2013-12-17 16:04:26 -08:00
|
|
|
|
|
|
|
ndbm_type = "dbm.ndbm"
|
2013-12-20 13:51:34 -08:00
|
|
|
gdbm_type = "dbm.gnu"
|
2013-12-17 16:04:26 -08:00
|
|
|
dumb_type = "dbm.dumb"
|
|
|
|
|
2013-12-17 14:23:34 -08:00
|
|
|
except:
|
|
|
|
import dbm as ndbm
|
|
|
|
import gdbm
|
|
|
|
import dumbdbm as dumb
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2013-12-17 14:23:34 -08:00
|
|
|
from whichdb import whichdb
|
|
|
|
|
2013-12-17 16:04:26 -08:00
|
|
|
ndbm_type = "dbm"
|
|
|
|
gdbm_type = "gdbm"
|
|
|
|
dumb_type = "dumbdbm"
|
|
|
|
|
|
|
|
#global settings
|
|
|
|
key1 = 'very first key'
|
|
|
|
key2 = 'second key'
|
|
|
|
val1 = 'very first value'
|
|
|
|
val2 = 'second value'
|
2013-12-20 13:51:34 -08:00
|
|
|
dump_anydbm = "dump-anydbm"
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2013-12-17 14:27:14 -08:00
|
|
|
@pytest.fixture(scope = "function")
|
|
|
|
def make_gdbm_test_db(request):
|
2013-12-17 14:25:24 -08:00
|
|
|
db_name ="test_gdbm"
|
2013-12-20 13:51:34 -08:00
|
|
|
print("creating", db_name)
|
2013-12-17 14:25:24 -08:00
|
|
|
test_db = gdbm.open(db_name, "n")
|
2013-12-17 16:50:35 -08:00
|
|
|
test_db[key1] = val1
|
|
|
|
test_db[key2] = val2
|
2013-12-17 14:25:24 -08:00
|
|
|
test_db.close()
|
2013-12-17 14:27:14 -08:00
|
|
|
def delete_test_dumbdbm():
|
2013-12-20 13:51:34 -08:00
|
|
|
print("deleting", db_name)
|
2013-12-17 14:38:45 -08:00
|
|
|
os.remove(db_name)
|
2013-12-17 16:50:35 -08:00
|
|
|
|
2013-12-17 14:27:14 -08:00
|
|
|
request.addfinalizer(delete_test_dumbdbm)
|
2013-12-17 14:25:24 -08:00
|
|
|
return db_name
|
2013-12-17 14:26:00 -08:00
|
|
|
|
2013-12-17 17:29:50 -08:00
|
|
|
@pytest.fixture(scope = "function")
|
|
|
|
def make_ndbm_test_db(request):
|
|
|
|
db_name = "test_ndbm"
|
2013-12-20 13:51:34 -08:00
|
|
|
print("creating", db_name)
|
2013-12-18 18:23:15 -08:00
|
|
|
test_db = ndbm.open(db_name, "n")
|
2013-12-18 21:17:41 -08:00
|
|
|
test_db[key1] = val1
|
|
|
|
test_db[key2] = val2
|
2013-12-17 17:29:50 -08:00
|
|
|
test_db.close()
|
|
|
|
def delete_test_ndbm():
|
2013-12-20 13:51:34 -08:00
|
|
|
print("deleting", db_name)
|
2013-12-18 18:23:15 -08:00
|
|
|
os.remove(db_name+".db")
|
2013-12-17 17:29:50 -08:00
|
|
|
|
|
|
|
request.addfinalizer(delete_test_ndbm)
|
2013-12-18 18:23:15 -08:00
|
|
|
return db_name
|
2013-12-17 17:29:50 -08:00
|
|
|
|
2013-12-17 14:33:31 -08:00
|
|
|
@pytest.fixture(scope = "function")
|
|
|
|
def make_dumbdbm_test_db(request):
|
|
|
|
db_name ="test_dumbdbm"
|
2013-12-20 13:51:34 -08:00
|
|
|
print("creating", db_name)
|
2013-12-17 14:33:31 -08:00
|
|
|
test_db = dumb.open(db_name, "n")
|
2013-12-17 16:04:26 -08:00
|
|
|
test_db[key1] = val1
|
|
|
|
test_db[key2] = val2
|
2013-12-17 14:33:31 -08:00
|
|
|
test_db.close()
|
|
|
|
def delete_test_dumbdbm():
|
2013-12-20 13:51:34 -08:00
|
|
|
print("deleting", db_name)
|
2013-12-17 14:33:31 -08:00
|
|
|
os.remove(db_name+".dir")
|
|
|
|
os.remove(db_name+".bak")
|
|
|
|
os.remove(db_name+".dat")
|
|
|
|
|
|
|
|
request.addfinalizer(delete_test_dumbdbm)
|
2013-12-17 14:48:26 -08:00
|
|
|
return db_name
|
2013-12-17 14:33:31 -08:00
|
|
|
|
2013-12-17 16:50:35 -08:00
|
|
|
def test_dumpanydbm_identify_gdbm(make_gdbm_test_db):
|
2013-12-20 13:51:34 -08:00
|
|
|
print("running test_dumpanydbm_identify_gdbm")
|
|
|
|
output = subprocess.check_output([dump_anydbm, make_gdbm_test_db])
|
|
|
|
output = output.decode(encoding = 'UTF-8').strip().split("\n")
|
2013-12-17 16:04:26 -08:00
|
|
|
assert len(output) == 3 # 2 keys plus whichdb line
|
|
|
|
|
2013-12-17 15:35:23 -08:00
|
|
|
# split on space, then grab 4th word, which is db type
|
|
|
|
which = output[0].split(' ')[3]
|
2013-12-20 13:51:34 -08:00
|
|
|
print(which)
|
2013-12-17 16:04:26 -08:00
|
|
|
assert which == gdbm_type
|
|
|
|
|
|
|
|
#split remaining lines on ':' that separates key & value
|
|
|
|
db_dump_first_pair = output[1].split(':')
|
|
|
|
assert db_dump_first_pair[0] == key1
|
|
|
|
assert db_dump_first_pair[1] == val1
|
|
|
|
|
|
|
|
db_dump_second_pair = output[2].split(':')
|
|
|
|
assert db_dump_second_pair[0] == key2
|
|
|
|
assert db_dump_second_pair[1] == val2
|
|
|
|
|
2013-12-17 17:29:50 -08:00
|
|
|
def test_dumpanydbm_identify_ndbm(make_ndbm_test_db):
|
2013-12-20 13:51:34 -08:00
|
|
|
print("running test_dumpanydbm_identify_ndbm")
|
|
|
|
output = subprocess.check_output([dump_anydbm, make_ndbm_test_db])
|
|
|
|
output = output.decode(encoding = 'UTF-8').strip().split("\n")
|
2013-12-17 17:29:50 -08:00
|
|
|
|
|
|
|
assert len(output) == 3 # 2 keys plus whichdb line
|
|
|
|
|
|
|
|
# split on space, then grab 4th word, which is db type
|
|
|
|
which = output[0].split(' ')[3]
|
2013-12-20 13:51:34 -08:00
|
|
|
print(which)
|
2013-12-17 17:29:50 -08:00
|
|
|
assert which == ndbm_type
|
|
|
|
|
|
|
|
#split remaining lines on ':' that separates key & value
|
|
|
|
db_dump_first_pair = output[1].split(':')
|
|
|
|
assert db_dump_first_pair[0] == key1
|
|
|
|
assert db_dump_first_pair[1] == val1
|
|
|
|
|
|
|
|
db_dump_second_pair = output[2].split(':')
|
|
|
|
assert db_dump_second_pair[0] == key2
|
|
|
|
assert db_dump_second_pair[1] == val2
|
|
|
|
|
2013-12-17 16:50:35 -08:00
|
|
|
def test_dumpanydbm_identify_dumbdbm(make_dumbdbm_test_db):
|
2013-12-20 13:51:34 -08:00
|
|
|
print("running test_dumpanydbm_identify_dumbdbm")
|
|
|
|
output = subprocess.check_output([dump_anydbm, make_dumbdbm_test_db])
|
|
|
|
output = output.decode(encoding = 'UTF-8').strip().split("\n")
|
2013-12-18 21:17:41 -08:00
|
|
|
assert len(output) == 3 # 2 keys plus whichdb line
|
2013-12-20 13:51:34 -08:00
|
|
|
print(output)
|
2013-12-17 15:35:23 -08:00
|
|
|
|
2013-12-18 21:17:41 -08:00
|
|
|
# split on space, then grab 4th word, which is db type
|
|
|
|
which = output[0].split(' ')[3]
|
2013-12-20 13:51:34 -08:00
|
|
|
print(which)
|
2013-12-18 21:17:41 -08:00
|
|
|
assert which == dumb_type
|
|
|
|
|
|
|
|
#split remaining lines on ':' that separates key & value
|
|
|
|
db_dump_first_pair = output[1].split(':')
|
|
|
|
assert db_dump_first_pair[0] == key1
|
|
|
|
assert db_dump_first_pair[1] == val1
|
|
|
|
|
|
|
|
db_dump_second_pair = output[2].split(':')
|
|
|
|
assert db_dump_second_pair[0] == key2
|
|
|
|
assert db_dump_second_pair[1] == val2
|