2013-06-25 01:00:56 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
|
|
|
# Copyright (C) 2000-2007 Donald N. Allingham
|
|
|
|
#
|
|
|
|
# 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
# test/GrampsDb/Cursor_Test.py
|
|
|
|
# $Id$
|
|
|
|
|
2006-02-19 21:41:18 +05:30
|
|
|
import unittest
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import tempfile
|
|
|
|
import shutil
|
|
|
|
import time
|
|
|
|
import traceback
|
|
|
|
import sys
|
|
|
|
from bsddb import dbshelve, db
|
|
|
|
|
Reviving RunAllTests.py on gramps40/trunk
Porting from gramps34 my fix for 6935, 6937, 6938.
Import with full "gramps." qualification from the tests,
to overcome the relative import problem (see bug# 6938 for context).
All testing code has been adjusted to the filename and paths
changes since gramps34.
Same as on gramps34, one test fails still, will investigate if
it is a test or a code bug:
ERROR: test_buffer_recall
(ErrorReportAssistant_Test.ErrorReportAssistantTest)
Test that simple recall of messages works.
----------------------------------------------------------------------
Traceback (most recent call last):
File "./GrampsLogger/ErrorReportAssistant_Test.py", line 60, in
test_buffer_recall
rotate_handler=rh)
File "../gramps/gui/logger/_errorreportassistant.py", line 81, in
__init__
self.build_page1()
File "../gramps/gui/logger/_errorreportassistant.py", line 267, in
build_page1
self._reset_error_details()
File "../gramps/gui/logger/_errorreportassistant.py", line 178, in
_reset_error_details
self._error_detail.get_record()))
AttributeError: 'str' object has no attribute 'get_record'
svn: r22729
2013-07-24 11:53:33 +05:30
|
|
|
sys.path.append('../../gramps')
|
2006-02-19 21:41:18 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
set()
|
|
|
|
except NameError:
|
|
|
|
from sets import Set as set
|
|
|
|
|
Reviving RunAllTests.py on gramps40/trunk
Porting from gramps34 my fix for 6935, 6937, 6938.
Import with full "gramps." qualification from the tests,
to overcome the relative import problem (see bug# 6938 for context).
All testing code has been adjusted to the filename and paths
changes since gramps34.
Same as on gramps34, one test fails still, will investigate if
it is a test or a code bug:
ERROR: test_buffer_recall
(ErrorReportAssistant_Test.ErrorReportAssistantTest)
Test that simple recall of messages works.
----------------------------------------------------------------------
Traceback (most recent call last):
File "./GrampsLogger/ErrorReportAssistant_Test.py", line 60, in
test_buffer_recall
rotate_handler=rh)
File "../gramps/gui/logger/_errorreportassistant.py", line 81, in
__init__
self.build_page1()
File "../gramps/gui/logger/_errorreportassistant.py", line 267, in
build_page1
self._reset_error_details()
File "../gramps/gui/logger/_errorreportassistant.py", line 178, in
_reset_error_details
self._error_detail.get_record()))
AttributeError: 'str' object has no attribute 'get_record'
svn: r22729
2013-07-24 11:53:33 +05:30
|
|
|
import gramps.gen.const
|
2006-02-19 21:41:18 +05:30
|
|
|
|
|
|
|
logger = logging.getLogger('Gramps.GrampsDbBase_Test')
|
|
|
|
|
|
|
|
from GrampsDbTestBase import GrampsDbBaseTest
|
|
|
|
|
|
|
|
class Data(object):
|
|
|
|
|
2008-02-24 19:25:55 +05:30
|
|
|
def __init__(self, handle,surname, name):
|
2006-02-19 21:41:18 +05:30
|
|
|
self.handle = handle
|
|
|
|
self.surname = surname
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
## def __repr__(self):
|
|
|
|
## return repr((self.handle,self.surname,self.name))
|
|
|
|
|
|
|
|
class CursorTest(unittest.TestCase):
|
|
|
|
"""Test the cursor handling."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._tmpdir = tempfile.mkdtemp()
|
|
|
|
self.full_name = os.path.join(self._tmpdir,'test.grdb')
|
|
|
|
self.env = db.DBEnv()
|
|
|
|
self.env.set_cachesize(0,0x2000000)
|
|
|
|
self.env.set_lk_max_locks(25000)
|
|
|
|
self.env.set_lk_max_objects(25000)
|
|
|
|
self.env.set_flags(db.DB_LOG_AUTOREMOVE,1) # clean up unused logs
|
|
|
|
# The DB_PRIVATE flag must go if we ever move to multi-user setup
|
|
|
|
env_flags = db.DB_CREATE|db.DB_RECOVER|db.DB_PRIVATE|\
|
|
|
|
db.DB_INIT_MPOOL|db.DB_INIT_LOCK|\
|
|
|
|
db.DB_INIT_LOG|db.DB_INIT_TXN
|
|
|
|
|
|
|
|
env_name = "%s/env" % (self._tmpdir,)
|
|
|
|
if not os.path.isdir(env_name):
|
|
|
|
os.mkdir(env_name)
|
|
|
|
self.env.open(env_name,env_flags)
|
|
|
|
(self.person_map,self.surnames) = self._open_tables()
|
|
|
|
|
|
|
|
def _open_tables(self):
|
|
|
|
dbmap = dbshelve.DBShelf(self.env)
|
|
|
|
dbmap.db.set_pagesize(16384)
|
|
|
|
dbmap.open(self.full_name, 'person', db.DB_HASH,
|
|
|
|
db.DB_CREATE|db.DB_AUTO_COMMIT, 0666)
|
|
|
|
person_map = dbmap
|
|
|
|
|
|
|
|
table_flags = db.DB_CREATE|db.DB_AUTO_COMMIT
|
|
|
|
|
|
|
|
surnames = db.DB(self.env)
|
|
|
|
surnames.set_flags(db.DB_DUP|db.DB_DUPSORT)
|
|
|
|
surnames.open(self.full_name, "surnames", db.DB_BTREE,
|
|
|
|
flags=table_flags)
|
|
|
|
|
|
|
|
def find_surname(key,data):
|
|
|
|
return data.surname
|
|
|
|
|
|
|
|
person_map.associate(surnames, find_surname, table_flags)
|
|
|
|
|
|
|
|
return (person_map,surnames)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
shutil.rmtree(self._tmpdir)
|
|
|
|
|
|
|
|
def test_simple_insert(self):
|
|
|
|
"""test insert and retrieve works."""
|
|
|
|
|
|
|
|
data = Data(str(1),'surname1','name1')
|
|
|
|
the_txn = self.env.txn_begin()
|
|
|
|
self.person_map.put(data.handle,data,txn=the_txn)
|
|
|
|
the_txn.commit()
|
|
|
|
|
|
|
|
v = self.person_map.get(data.handle)
|
|
|
|
|
|
|
|
assert v.handle == data.handle
|
|
|
|
|
|
|
|
def test_insert_with_curor_closed(self):
|
|
|
|
"""test_insert_with_curor_closed"""
|
|
|
|
|
|
|
|
cursor_txn = self.env.txn_begin()
|
|
|
|
|
|
|
|
cursor = self.surnames.cursor(txn=cursor_txn)
|
|
|
|
cursor.first()
|
|
|
|
cursor.next()
|
|
|
|
cursor.close()
|
|
|
|
cursor_txn.commit()
|
|
|
|
|
|
|
|
data = Data(str(2),'surname2','name2')
|
|
|
|
the_txn = self.env.txn_begin()
|
|
|
|
self.person_map.put(data.handle,data,txn=the_txn)
|
|
|
|
the_txn.commit()
|
|
|
|
|
|
|
|
v = self.person_map.get(data.handle)
|
|
|
|
|
|
|
|
assert v.handle == data.handle
|
|
|
|
|
|
|
|
def test_insert_with_curor_open(self):
|
|
|
|
"""test_insert_with_curor_open"""
|
|
|
|
|
|
|
|
cursor_txn = self.env.txn_begin()
|
|
|
|
cursor = self.surnames.cursor(txn=cursor_txn)
|
|
|
|
cursor.first()
|
|
|
|
cursor.next()
|
|
|
|
|
|
|
|
data = Data(str(2),'surname2','name2')
|
|
|
|
the_txn = self.env.txn_begin()
|
|
|
|
self.person_map.put(data.handle,data,txn=the_txn)
|
|
|
|
the_txn.commit()
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
cursor_txn.commit()
|
|
|
|
|
|
|
|
v = self.person_map.get(data.handle)
|
|
|
|
|
|
|
|
assert v.handle == data.handle
|
|
|
|
|
|
|
|
def xtest_insert_with_curor_open_and_db_open(self):
|
|
|
|
"""test_insert_with_curor_open_and_db_open"""
|
|
|
|
|
|
|
|
(person2,surnames2) = self._open_tables()
|
|
|
|
|
|
|
|
cursor_txn = self.env.txn_begin()
|
|
|
|
cursor = surnames2.cursor(txn=cursor_txn)
|
|
|
|
cursor.first()
|
|
|
|
cursor.next()
|
|
|
|
|
|
|
|
data = Data(str(2),'surname2','name2')
|
|
|
|
the_txn = self.env.txn_begin()
|
|
|
|
self.person_map.put(data.handle,data,txn=the_txn)
|
|
|
|
the_txn.commit()
|
|
|
|
|
|
|
|
cursor.close()
|
|
|
|
cursor_txn.commit()
|
|
|
|
|
|
|
|
v = self.person_map.get(data.handle)
|
|
|
|
|
|
|
|
assert v.handle == data.handle
|
|
|
|
|
|
|
|
|
|
|
|
def testSuite():
|
|
|
|
suite = unittest.makeSuite(CursorTest,'test')
|
|
|
|
return suite
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.TextTestRunner().run(testSuite())
|