def _get_sql(files): folder = MockFolder('/mock/notebook/') indexer = buildUpdateIter(folder) for path, text in files: folder.file(path).write(text) indexer.check_and_update() lines = list(indexer.db.iterdump()) indexer.db.close() return '\n'.join(lines)
def setUpFolder(cls, name=None, mock=MOCK_DEFAULT_MOCK): '''Convenience method to create a temporary folder for testing Default use of "C{MOCK_DEFAULT_MOCK}" means that about 20% of the cases will use real filesystem at random while the rest will mock. (Thus giving a balance between overall test speed and the whish to detect cases where mock and real filesystem give different results.) This behavior is overruled by "--fast" and "--full" in the test script. @param name: basename for the folder, use class name if C{None} @param mock: mock level for this test, one of C{MOCK_ALWAYS_MOCK}, C{MOCK_DEFAULT_MOCK}, C{MOCK_DEFAULT_REAL} or C{MOCK_ALWAYS_REAL}. @returns: a L{Folder} object (either L{LocalFolder} or L{MockFolder}) that is guarenteed non-existing ''' path = cls._get_tmp_name(name) if mock == MOCK_ALWAYS_MOCK: use_mock = True elif mock == MOCK_ALWAYS_REAL: use_mock = False elif mock == MOCK_DEFAULT_REAL: if FAST_TEST: use_mock = True else: use_mock = False else: # MOCK_DEFAULT_MOCK: if FULL_TEST: use_mock = False elif FAST_TEST: use_mock = True elif random.random() < 0.2: logger.info("Random dice throw: use real file system") use_mock = False else: use_mock = True if use_mock: from zim.newfs.mock import MockFolder folder = MockFolder(path) else: from zim.newfs import LocalFolder if os.path.exists(path): logger.debug('Clear tmp folder: %s', path) shutil.rmtree(path) assert not os.path.exists(path) # make real sure folder = LocalFolder(path) assert not folder.exists() return folder
def makeConfigManagerVirtual(): # Used in test suite to turn ConfigManager singleton in blank virtual state # _set() also resets internal state, but objects that already have a # reference to a config file or config dict will not see this from zim.newfs.mock import MockFolder folder = MockFolder('/<VirtualConfigManager>/') ConfigManager._set(folder)
def new_test_database(): global _SQL if _SQL is None: folder = MockFolder('/mock/notebook/') indexer = buildUpdateIter(folder) for path, text in FILES: folder.file(path).write(text) indexer.check_and_update() lines = list(indexer.db.iterdump()) _SQL = '\n'.join(lines) indexer.db.close() db = sqlite3.Connection(':memory:') db.row_factory = sqlite3.Row db.executescript(_SQL) return db
def setUpFolder(self, name=None, mock=MOCK_DEFAULT_MOCK): '''Convenience method to create a temporary folder for testing @param name: basename for the folder, uses test name if C{None} @param mock: mock level for this test, one of C{MOCK_ALWAYS_MOCK}, C{MOCK_DEFAULT_MOCK}, C{MOCK_DEFAULT_REAL} or C{MOCK_ALWAYS_REAL}. The C{MOCK_ALWAYS_*} arguments force the use of a real folder or a mock object. The C{MOCK_DEFAULT_*} arguments give a preference but for these the behavior is overruled by "--fast" and "--full" in the test script. @returns: a L{Folder} object (either L{LocalFolder} or L{MockFolder}) that is guarenteed non-existing ''' if name is None and self._testMethodName != 'runTest': name = self._testMethodName path = self._get_tmp_name(name) if mock == MOCK_ALWAYS_MOCK: use_mock = True elif mock == MOCK_ALWAYS_REAL: use_mock = False else: if FULL_TEST: use_mock = False elif FAST_TEST: use_mock = True else: use_mock = (mock == MOCK_DEFAULT_MOCK) if use_mock: from zim.newfs.mock import MockFolder folder = MockFolder(path) else: from zim.newfs import LocalFolder if os.path.exists(path): logger.debug('Clear tmp folder: %s', path) shutil.rmtree(path) assert not os.path.exists(path) # make real sure folder = LocalFolder(path) assert not folder.exists() return folder
def testWithDefaults(self): from zim.newfs.mock import MockFolder folder = MockFolder('/<mock>/config/') userfile = folder.file('foo.conf') defaultfile = folder.file('default.conf') defaultfile.write('default!\n') file = ConfigFile(userfile, [defaultfile]) self.assertEqual(file.read(), 'default!\n') self.assertEqual(file.readlines(), ['default!\n']) file.touch() self.assertEqual(file.read(), 'default!\n') self.assertEqual(file.readlines(), ['default!\n']) file.write('foo!\n') self.assertEqual(file.read(), 'foo!\n') self.assertEqual(file.readlines(), ['foo!\n']) file.remove() self.assertEqual(file.read(), 'default!\n') self.assertEqual(file.readlines(), ['default!\n'])
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