def setUpNotebook(self, name='notebook', mock=MOCK_ALWAYS_MOCK, content={}, folder=None): ''' @param name: name postfix for the folder, see L{setUpFolder} @param mock: see L{setUpFolder}, default is C{MOCK_ALWAYS_MOCK} @param content: dictionary where the keys are page names and the values the page content. If a tuple or list is given, pages are created with default text. L{Path} objects are allowed instead of page names @param folder: determine the folder to be used, only needed in special cases where the folder must be outside of the project folder, like when testing version control logic ''' import datetime from zim.newfs.mock import MockFolder from zim.notebook.notebook import NotebookConfig, Notebook from zim.notebook.page import Path from zim.notebook.layout import FilesLayout from zim.notebook.index import Index from zim.formats.wiki import WIKI_FORMAT_VERSION if folder is None: folder = self.setUpFolder(name, mock) folder.touch() # Must exist for sane notebook cache_dir = folder.folder('.zim') layout = FilesLayout(folder, endofline='unix') if isinstance(folder, MockFolder): conffile = folder.file('notebook.zim') config = NotebookConfig(conffile) index = Index(':memory:', layout) else: conffile = folder.file('notebook.zim') config = NotebookConfig(conffile) cache_dir.touch() index = Index(cache_dir.file('index.db').path, layout) if isinstance(content, (list, tuple)): content = dict((p, 'test 123') for p in content) notebook = Notebook(cache_dir, config, folder, layout, index) for name, text in list(content.items()): path = Path(name) if isinstance(name, str) else name file, folder = layout.map_page(path) file.write( ('Content-Type: text/x-zim-wiki\n' 'Wiki-Format: %s\n' 'Creation-Date: %s\n\n') % (WIKI_FORMAT_VERSION, datetime.datetime.now().isoformat()) + text) notebook.index.check_and_update() assert notebook.index.is_uptodate return notebook
def testWithBrokenFile(self): file = self.folder.file('index.db') file.write('this is not a database file...\n') self.assertTrue(file.exists()) with tests.LoggingFilter('zim.notebook.index', 'Overwriting'): with tests.LoggingFilter('zim.notebook.index', 'Could not access'): index = Index(file.path, self.layout) self.assertTrue(file.exists()) self.assertEqual(index.get_property('db_version'), DB_VERSION)
def testWithLockedFile(self): file = self.folder.file('index.db') file.write('this is not a database file...\n') os.chmod(file.path, 0o000) # make read-only self.addCleanup(lambda: os.chmod(file.path, 0o700)) self.assertTrue(file.exists()) with tests.LoggingFilter('zim.notebook.index', 'Overwriting'): with tests.LoggingFilter('zim.notebook.index', 'Could not access'): index = Index(file.path, self.layout) self.assertTrue(file.exists()) self.assertEqual(index.get_property('db_version'), DB_VERSION)
def testWithValidDBFile(self): # E.g. old index, not conforming our table layout file = self.folder.file('index.db') self.assertFalse(file.exists()) db = sqlite3.Connection(file.path) db.execute('CREATE TABLE zim_index (key TEXT);') db.close() self.assertTrue(file.exists()) index = Index(file.path, self.layout) self.assertTrue(file.exists()) self.assertEqual(index.get_property('db_version'), DB_VERSION)
def setUpNotebook(cls, name=None, mock=MOCK_ALWAYS_MOCK, content={}): ''' @param name: basename for the folder, use class name if C{None} @param mock: see L{setUpFolder}, default is C{MOCK_ALWAYS_MOCK} @param content: dictionary where the keys are page names and the values the page content. ''' import datetime from zim.newfs.mock import MockFolder from zim.config import VirtualConfigBackend from zim.notebook.notebook import NotebookConfig, Notebook from zim.notebook.page import Path from zim.notebook.layout import FilesLayout from zim.notebook.index import Index from zim.formats.wiki import WIKI_FORMAT_VERSION folder = cls.setUpFolder(name, mock) cache_dir = folder.folder('.zim') layout = FilesLayout(folder, endofline='unix') if isinstance(folder, MockFolder): ### XXX - Big HACK here - Get better classes for this - XXX ### dir = VirtualConfigBackend() file = dir.file('notebook.zim') file.dir = dir file.dir.basename = folder.basename ### config = NotebookConfig(file) index = Index(':memory:', layout) else: from zim.fs import Dir conffile = Dir(folder.path).file('notebook.zim') config = NotebookConfig(conffile) cache_dir.touch() index = Index(cache_dir.file('index.db').path, layout) notebook = Notebook(None, cache_dir, config, folder, layout, index) for name, text in content.items(): file, folder = layout.map_page(Path(name)) file.write( ('Content-Type: text/x-zim-wiki\n' 'Wiki-Format: %s\n' 'Creation-Date: %s\n\n') % (WIKI_FORMAT_VERSION, datetime.datetime.now().isoformat()) + text + '\n') notebook.index.check_and_update() return notebook
def setUp(self): folder = self.setUpFolder(mock=tests.MOCK_ALWAYS_MOCK) layout = FilesLayout(folder, endofline='unix') index = Index(':memory:', layout) ### XXX - Big HACK here - Get better classes for this - XXX ### dir = VirtualConfigBackend() file = dir.file('notebook.zim') file.dir = dir file.dir.basename = 'Unnamed Notebook' ### config = NotebookConfig(file) dir = None cache_dir = None self.notebook = Notebook(dir, cache_dir, config, folder, layout, index) index.check_and_update()
def testWithoutFileAndWithValidFile(self): # Two tests combined because first needed as init for the second file = self.folder.file('index.db') self.assertFalse(file.exists()) index = Index(file.path, self.layout) self.assertTrue(file.exists()) self.assertEqual(index.get_property('db_version'), DB_VERSION) index._db.close() del (index) index = Index(file.path, self.layout) self.assertTrue(file.exists()) self.assertEqual(index.get_property('db_version'), DB_VERSION)
def new_notebook(fakedir=None): '''Returns a new Notebook object with all data in memory Uses data from L{WikiTestData} @param fakedir: optional parameter to set the 'dir' attribute for the notebook which allows you to resolve file paths etc. It will not automatically touch the dir (hence it being 'fake'). ''' import sqlite3 from zim.fs import Dir from zim.config import VirtualConfigBackend from zim.notebook import Notebook, Path from zim.notebook.notebook import NotebookConfig from zim.notebook.index import Index from zim.notebook.layout import FilesLayout from zim.newfs.mock import MockFolder, clone_mock_object, os_native_path global _notebook_data if not _notebook_data: # run this one time only templfolder = MockFolder('/mock/notebook') layout = FilesLayout(templfolder, endofline='unix') manifest = [] for name, text in WikiTestData: manifest.append(name) file, x = layout.map_page(Path(name)) file.write(text) manifest = frozenset(_expand_manifest(manifest)) index = Index(':memory:', layout) index.check_and_update() lines = list(index._db.iterdump()) sql = '\n'.join(lines) _notebook_data = (templfolder, sql, manifest) if fakedir: fakedir = fakedir if isinstance(fakedir, basestring) else fakedir.path fakedir = os_native_path(fakedir) templfolder, sql, manifest = _notebook_data folder = MockFolder(fakedir or templfolder.path) clone_mock_object(templfolder, folder) #~ print ">>>>>>>>>>>>>" #~ for path in folder._fs.tree(): #~ print path layout = FilesLayout(folder, endofline='unix') index = Index(':memory:', layout) tables = [ r[0] for r in index._db.execute( 'SELECT name FROM sqlite_master ' 'WHERE type="table" and name NOT LIKE "sqlite%"') ] for table in tables: index._db.execute('DROP TABLE %s' % table) index._db.executescript(sql) index._db.commit() ### XXX - Big HACK here - Get better classes for this - XXX ### dir = VirtualConfigBackend() file = dir.file('notebook.zim') file.dir = dir file.dir.basename = 'Unnamed Notebook' ### config = NotebookConfig(file) notebook = Notebook(None, None, config, folder, layout, index) if fakedir: notebook.dir = Dir(fakedir) notebook.testdata_manifest = manifest return notebook