2013-12-17 14:23:34 -08:00
|
|
|
#!/usr/bin/env python
|
2016-04-06 19:37:55 -07:00
|
|
|
#
|
|
|
|
# tests/test_dump-anydbm.py - tests for dump-anydbm
|
|
|
|
#
|
|
|
|
# Copyright (C) 2013-2016 Internet Archive
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
|
|
|
# USA.
|
|
|
|
#
|
2013-12-17 14:23:34 -08:00
|
|
|
|
|
|
|
import pytest
|
2013-12-17 14:27:14 -08:00
|
|
|
import os
|
2014-01-02 18:18:46 -08:00
|
|
|
import tempfile
|
2013-12-17 15:35:23 -08:00
|
|
|
import subprocess # to access the script from shell
|
2014-01-17 15:35:25 -08:00
|
|
|
import sys
|
2014-08-01 17:40:34 -07:00
|
|
|
import glob
|
2015-09-22 05:45:05 +00:00
|
|
|
import distutils
|
2013-12-17 14:23:34 -08:00
|
|
|
|
|
|
|
# will try as python 3 then default to python 2 modules
|
2014-01-02 16:29:15 -08:00
|
|
|
try:
|
|
|
|
import dbm
|
|
|
|
from dbm import ndbm
|
|
|
|
from dbm import gnu as gdbm
|
|
|
|
from dbm import dumb
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2014-01-02 16:29:15 -08:00
|
|
|
whichdb = dbm.whichdb
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
ndbm_type = b"dbm.ndbm"
|
|
|
|
gdbm_type = b"dbm.gnu"
|
|
|
|
dumb_type = b"dbm.dumb"
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2013-12-17 14:23:34 -08:00
|
|
|
except:
|
2014-01-02 16:29:15 -08:00
|
|
|
import dbm as ndbm
|
|
|
|
import gdbm
|
|
|
|
import dumbdbm as dumb
|
2013-12-17 16:04:26 -08:00
|
|
|
|
2014-01-02 16:29:15 -08:00
|
|
|
from whichdb import whichdb
|
2013-12-17 14:23:34 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
ndbm_type = b"dbm"
|
|
|
|
gdbm_type = b"gdbm"
|
|
|
|
dumb_type = b"dumbdbm"
|
2013-12-17 16:04:26 -08:00
|
|
|
|
|
|
|
#global settings
|
|
|
|
key1 = 'very first key'
|
|
|
|
key2 = 'second key'
|
|
|
|
val1 = 'very first value'
|
|
|
|
val2 = 'second value'
|
|
|
|
|
2014-01-17 16:20:16 -08:00
|
|
|
py = sys.executable
|
2015-09-22 05:45:05 +00:00
|
|
|
dump_anydbm_loc = distutils.spawn.find_executable("dump-anydbm")
|
2014-01-02 16:29:15 -08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2014-01-06 17:22:49 -08:00
|
|
|
def gdbm_test_db(request):
|
2014-08-01 17:40:34 -07:00
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
2014-08-01 17:32:16 -07:00
|
|
|
print("creating test gdbm file {}".format(temp_file.name))
|
2014-01-02 18:18:46 -08:00
|
|
|
test_db = gdbm.open(temp_file.name, "n")
|
2014-01-02 16:29:15 -08:00
|
|
|
test_db[key1] = val1
|
|
|
|
test_db[key2] = val2
|
|
|
|
test_db.close()
|
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
def delete_gdbm_test_db():
|
2014-01-02 18:18:46 -08:00
|
|
|
temp_file.close()
|
2014-08-01 17:40:34 -07:00
|
|
|
for f in glob.glob("{}*".format(temp_file.name)):
|
|
|
|
print("deleting test gdbm file {}".format(f))
|
|
|
|
os.remove(f)
|
2014-01-02 16:29:15 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
request.addfinalizer(delete_gdbm_test_db)
|
2014-01-02 18:18:46 -08:00
|
|
|
return temp_file.name
|
2014-01-02 16:29:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2014-01-06 17:22:49 -08:00
|
|
|
def ndbm_test_db(request):
|
2014-08-01 17:40:34 -07:00
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
2014-01-02 18:18:46 -08:00
|
|
|
test_db = ndbm.open(temp_file.name, "n")
|
2014-01-02 16:29:15 -08:00
|
|
|
test_db[key1] = val1
|
|
|
|
test_db[key2] = val2
|
|
|
|
test_db.close()
|
|
|
|
|
|
|
|
def delete_test_ndbm():
|
2014-01-02 18:18:46 -08:00
|
|
|
temp_file.close()
|
2014-08-01 17:40:34 -07:00
|
|
|
for f in glob.glob("{}*".format(temp_file.name)):
|
|
|
|
print("deleting test ndbm file {}".format(f))
|
|
|
|
os.remove(f)
|
2014-01-02 16:29:15 -08:00
|
|
|
|
|
|
|
request.addfinalizer(delete_test_ndbm)
|
2014-01-02 18:18:46 -08:00
|
|
|
return temp_file.name
|
2014-01-02 16:29:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2014-01-06 17:22:49 -08:00
|
|
|
def dumbdbm_test_db(request):
|
2014-08-01 17:40:34 -07:00
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
2014-08-01 17:32:16 -07:00
|
|
|
print("creating test dumbdbm file {}".format(temp_file.name))
|
2014-01-02 18:18:46 -08:00
|
|
|
test_db = dumb.open(temp_file.name, "n")
|
2014-01-02 16:29:15 -08:00
|
|
|
test_db[key1] = val1
|
|
|
|
test_db[key2] = val2
|
|
|
|
test_db.close()
|
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
def delete_dumbdbm_test_db():
|
2014-01-02 18:18:46 -08:00
|
|
|
temp_file.close()
|
2014-08-01 17:40:34 -07:00
|
|
|
for f in glob.glob("{}*".format(temp_file.name)):
|
|
|
|
print("deleting test dumbdbm file {}".format(f))
|
|
|
|
os.remove(f)
|
2014-01-02 16:29:15 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
request.addfinalizer(delete_dumbdbm_test_db)
|
2014-01-02 18:18:46 -08:00
|
|
|
return temp_file.name
|
2014-01-02 16:29:15 -08:00
|
|
|
|
2013-12-17 14:33:31 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
def test_dumpanydbm_identify_gdbm(gdbm_test_db):
|
2014-01-02 16:29:15 -08:00
|
|
|
print("running test_dumpanydbm_identify_gdbm")
|
2014-01-17 16:20:16 -08:00
|
|
|
output = subprocess.check_output([py, dump_anydbm_loc, gdbm_test_db])
|
2014-08-01 17:32:16 -07:00
|
|
|
print("script printout: ")
|
2014-01-17 15:35:25 -08:00
|
|
|
print(output)
|
2014-08-01 17:32:16 -07:00
|
|
|
print("check_one: ")
|
2014-01-17 15:35:25 -08:00
|
|
|
print(gdbm_test_db.encode(encoding='UTF-8') + b' is a ' + gdbm_type + b' db\nvery first key:very first value\nsecond key:second value\n')
|
2014-01-02 16:29:15 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
assert (output == gdbm_test_db.encode(encoding='UTF-8') + b' is a ' + gdbm_type + b' db\nvery first key:very first value\nsecond key:second value\n' or
|
|
|
|
output == gdbm_test_db.encode(encoding='UTF-8') + b' is a ' + gdbm_type + b' db\nsecond key:second value\nvery first key:very first value\n')
|
2013-12-17 16:04:26 -08:00
|
|
|
|
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
def test_dumpanydbm_identify_ndbm(ndbm_test_db):
|
2014-01-02 16:29:15 -08:00
|
|
|
print("running test_dumpanydbm_identify_ndbm")
|
2014-01-17 16:20:16 -08:00
|
|
|
output = subprocess.check_output([py, dump_anydbm_loc, ndbm_test_db])
|
2014-08-01 17:32:16 -07:00
|
|
|
print("script printout: ")
|
2014-01-17 15:35:25 -08:00
|
|
|
print(output)
|
2014-08-01 17:32:16 -07:00
|
|
|
print("check_one: ")
|
2014-01-17 15:35:25 -08:00
|
|
|
print(ndbm_test_db.encode(encoding='UTF-8') + b' is a ' + ndbm_type + b' db\nvery first key:very first value\nsecond key:second value\n')
|
2013-12-17 17:29:50 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
assert (output == ndbm_test_db.encode(encoding='UTF-8') + b' is a ' + ndbm_type + b' db\nvery first key:very first value\nsecond key:second value\n' or
|
|
|
|
output == ndbm_test_db.encode(encoding='UTF-8') + b' is a ' + ndbm_type + b' db\nsecond key:second value\nvery first key:very first value\n')
|
2013-12-17 17:29:50 -08:00
|
|
|
|
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
def test_dumpanydbm_identify_dumbdbm(dumbdbm_test_db):
|
2014-01-02 16:29:15 -08:00
|
|
|
print("running test_dumpanydbm_identify_dumbdbm")
|
2014-01-17 15:35:25 -08:00
|
|
|
|
2014-01-17 16:20:16 -08:00
|
|
|
output = subprocess.check_output([py, dump_anydbm_loc, dumbdbm_test_db])
|
2014-08-01 17:32:16 -07:00
|
|
|
print("script printout: ")
|
2014-01-17 15:35:25 -08:00
|
|
|
print(output)
|
2014-08-01 17:32:16 -07:00
|
|
|
print("check_one: ")
|
2014-01-17 15:35:25 -08:00
|
|
|
print(dumbdbm_test_db.encode(encoding='UTF-8') + b' is a ' + dumb_type + b' db\nvery first key:very first value\nsecond key:second value\n')
|
2014-01-02 17:05:45 -08:00
|
|
|
|
2014-01-06 17:22:49 -08:00
|
|
|
assert (output == dumbdbm_test_db.encode(encoding='UTF-8') + b' is a ' + dumb_type + b' db\nvery first key:very first value\nsecond key:second value\n' or
|
|
|
|
output == dumbdbm_test_db.encode(encoding='UTF-8') + b' is a ' + dumb_type + b' db\nsecond key:second value\nvery first key:very first value\n')
|