def test_export_import(self): import tempfile from os import mkdir from os import rmdir from os import unlink from os.path import join from os.path import split from OFS.Folder import Folder from OFS.Image import File from transaction import commit from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage try: tf = None # temporary file required for export/import # export/import needs the object manager in ZODB s = DemoStorage() db = DB(s) c = db.open() root = c.root() top = Folder("top") f = File("f", "", b"") top._setObject(f.getId(), f) root["top"] = top tmp = Folder("tmp") top._setObject(tmp.getId(), tmp) commit() exported = top.manage_exportObject("f", True) tdir = tempfile.mkdtemp() idir = join(tdir, "import") mkdir(idir) tf = tempfile.NamedTemporaryFile(dir=idir, delete=False) tf.write(exported) tf.close() unused, tname = split(tf.name) tmp._getImportPaths = _CallResult((tdir, )) tmp.manage_importObject(tname, set_owner=False, suppress_events=True) imp_f = tmp["f"] # exception if import unsuccessful self.assertIsInstance(imp_f, File) commit() finally: if tf is not None: # pragma: no cover unlink(tf.name) rmdir(idir) rmdir(tdir) c.close() db.close() s.close()
def factory(): filestorage = FileStorage(*args, **kw) demostorage = DemoStorage(base=filestorage) blobstorage = BlobStorage(blobstorage_dir, demostorage, layout=blobstorage_layout) return DB(blobstorage, **dbkw)
def reset(self, storage=None, uri=None): """Close and reopen a database connection.""" if self.db: self.close() if storage is not None: self.storage = storage elif uri: storage_factory, db_args = zodburi.resolve_uri(uri) self.storage = storage_factory() db_args.update(self.db_args) self.db_args = db_args else: self.storage = DemoStorage() self.name = self.db_args.get("database_name", Database.DEFAULT_DATABASE_NAME) if self.name in LocalData.get().databases: last_context = LocalData.get().last_database_context.get(self.name) raise KeyError( "A database named '{}' already exists. Last opening was on {} at line {}" .format(self.name, last_context[0], last_context[1]) if last_context else "A database named '{}' already exists.". format(self.name)) self.db = ZODB.DB(self.storage, **self.db_args) LocalData.get().databases[self.name] = self
def sandbox(base=None): '''Returns a sandbox copy of the base ZODB.''' if base is None: base = Zope2.DB base_storage = base._storage quota = getattr(base_storage, '_quota', None) storage = DemoStorage(base=base_storage, quota=quota) return ZODB.DB(storage)
def getStorage(): """ Return a storage instance for running ZopeTestCase based tests. By default a DemoStorage is used. Setting $TEST_ZEO_HOST/TEST_ZEO_PORT environment variables allows you to use a ZEO server instead. A file storage can be configured by settting the $TEST_FILESTORAGE environment variable. """ get = os.environ.get if os.environ.has_key('TEST_ZEO_HOST') and os.environ.has_key( 'TEST_ZEO_PORT'): from ZEO.ClientStorage import ClientStorage zeo_host = get('TEST_ZEO_HOST') zeo_port = int(get('TEST_ZEO_PORT')) LOG.info('Using ZEO server (%s:%d)' % (zeo_host, zeo_port)) return ClientStorage((zeo_host, zeo_port)) elif os.environ.has_key('TEST_FILESTORAGE'): import ZODB.FileStorage datafs = get('TEST_FILESTORAGE') LOG.info('Using Filestorage at (%s)' % datafs) return ZODB.FileStorage.FileStorage(datafs) else: from ZODB.DemoStorage import DemoStorage LOG.info('Using DemoStorage') return DemoStorage()
def profiled_filter_with_demostorage(numdata, clazz, filter_value): sheraf.Database(storage=DemoStorage(name="demo")) init_data(clazz, numdata) result = profiled_filter(clazz, filter_value) nzeros = str(numdata).count("0") print("10^{nzeros} objects : {result} secs".format(nzeros=nzeros, result=result)) return ("10^{nzeros}".format(nzeros=nzeros), result)
def open(self): from ZODB.DemoStorage import DemoStorage if self.config.base: base = self.config.base.open() else: base = None return DemoStorage(self.config.name, base=base, quota=self.config.quota)
def DemoDatabaseConfigurationFactory(key, dbconfig): storage = DemoStorage(name=dbconfig['name']) db = DB(storage) alsoProvides(db.open().root(), IDatabase) transaction.commit() db.close() # Set request aware database for app db = RequestAwareDB(storage) return Database(key, db)
def createStorage(self): """Create a new storage. You may want to subclass this class when creating a custom layer. You can then override this method to create a different storage. The default is an empty DemoStorage. """ from ZODB.DemoStorage import DemoStorage return DemoStorage(name='EmptyZODB')
def open(self): base = changes = None for factory in self.config.factories: if factory.name == 'changes': changes = factory.open() else: if base is None: base = factory.open() else: raise ValueError("Too many base storages defined!") from ZODB.DemoStorage import DemoStorage return DemoStorage(self.config.name, base=base, changes=changes)
def setUp(test): testing.setUp(test) # Register adapters and handlers: # This query interface relies on a zope.app.catalog to # do the job. Before we can use this catalog, we'll need to register an # IIntIds utility and wire in a couple of adatpers defined in the # subscribe module. This is what 'create_subscriptions' does for us: from zope.component import provideUtility, provideAdapter, provideHandler for adapter in (subscribe.catalog_data, subscribe.SubscriptionSearchableText): provideAdapter(adapter) from zope.component.event import objectEventNotify for handler in (subscribe.subscription_modified, subscribe.subscription_removed, objectEventNotify): provideHandler(handler) # Set up an IIntIds utility: try: from zope.intid import IntIds from zope.intid.interfaces import IIntIds IntIds, IIntIds # pyflakes except ImportError: # BBB Plone 4.0 and earlier. from zope.app.intid import IntIds from zope.app.intid.interfaces import IIntIds intids = IntIds() provideUtility(intids, IIntIds) # We'll register a slight variation of the subscription_added # handler that commits the transaction, so that a later lookup of # IKeyReference for our subscription will work: from ZODB.DemoStorage import DemoStorage from ZODB import DB global root db = DB(DemoStorage()) root = db.open().root() subscription_added.__component_adapts__ = ( subscribe.subscription_added.__component_adapts__) provideHandler(subscription_added) # As a last step, we'll register the IKeyReference adapter for all # persistent objects: from zope.keyreference.persistent import KeyReferenceToPersistent from persistent.interfaces import IPersistent provideAdapter(KeyReferenceToPersistent, adapts=(IPersistent, )) provideAdapter(subscribe.get_subscription_label) provideAdapter(subscribe.get_subscription_key)
def setUp(self): import io import transaction from AccessControl import SecurityManager from AccessControl.SecurityManagement import newSecurityManager from OFS.Application import Application from OFS.Folder import manage_addFolder from OFS.Image import manage_addFile from Testing.makerequest import makerequest from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage s = DemoStorage() self.connection = DB(s).open() try: r = self.connection.root() a = Application() r['Application'] = a self.root = a responseOut = self.responseOut = io.BytesIO() self.app = makerequest(self.root, stdout=responseOut) manage_addFolder(self.app, 'folder1') folder1 = getattr(self.app, 'folder1') setattr(folder1, '+something', 'plus') folder1.all_meta_types = ({ 'name': 'File', 'action': 'manage_addFile', 'permission': 'Add images and files' }, ) manage_addFile(folder1, 'file', file=b'', content_type='text/plain') # Hack, we need a _p_mtime for the file, so we make sure that it # has one. We use a subtransaction, which means we can rollback # later and pretend we didn't touch the ZODB. transaction.commit() except Exception: self.connection.close() raise transaction.begin() self.folder1 = getattr(self.app, 'folder1') self.policy = UnitTestSecurityPolicy() self.oldPolicy = SecurityManager.setSecurityPolicy(self.policy) newSecurityManager(None, self._makeUser().__of__(self.root))
def _getDB(): db = stuff.get('db') if not db: ds = DemoStorage(quota=(1 << 20)) db = ZODB.DB(ds, pool_size=60) conn = db.open() root = conn.root() app = Application() root['Application'] = app transaction.savepoint(optimistic=True) _populate(app) stuff['db'] = db conn.close() return db
def _getApp(): app = stuff.get('app', None) if not app: ds = DemoStorage() db = ZODB.DB(ds) conn = db.open() root = conn.root() app = Application() root['Application'] = app transaction.commit() stuff['app'] = app stuff['conn'] = conn stuff['db'] = db return app
def _getDB(): db = stuff.get('db') if not db: ds = DemoStorage() db = ZODB.DB(ds) conn = db.open() root = conn.root() app = Application() root['Application'] = app _populate(app) transaction.commit() stuff['db'] = db conn.close() return db
def stackDemoStorage(db=None, name=None): """Create a new DemoStorage that has the given database as a base. ``db`` may be none, in which case a base demo storage will be created. ``name`` is optional, but can be used to name the storage. The usual pattern in a layer is:: def setUp(self): self['zodbDB'] = stackDemoStorage(self.get('zodbDB'), name='mylayer') # noqa def tearDown(self): self['zodbDB'].close() del self['zodbDB'] """ from ZODB.DemoStorage import DemoStorage from ZODB.DB import DB if db is not None: storage = DemoStorage(name=name, base=db.storage) else: storage = DemoStorage(name=name) return DB(storage)
def _makeJarAndRoot(self): import ZODB from OFS.Folder import Folder from ZODB.DemoStorage import DemoStorage CACHESIZE = 5 # something tiny LOOPCOUNT = CACHESIZE * 10 storage = DemoStorage("Test") db = ZODB.DB(storage, cache_size=CACHESIZE) connection = db.open() root = connection.root() app = Folder('app') root['app'] = app return connection, app
def test_closedConnection(self): from ZODB import DB from ZODB.DemoStorage import DemoStorage class Conn(DB.klass): loads = 1 stores = 2 def db(self): return db def getTransferCounts(self, clear=False): l, s = self.loads, self.stores if clear: self.loads = self.stores = 0 return l, s class MyDB(DB): klass = Conn db = MyDB(DemoStorage()) database_name = 'DB' db.database_name = database_name self.addCleanup(db.close) db.klass = Conn called_component = [] def component(data): called_component.append(1) assert_that(data.loads, is_(1)) assert_that(data.stores, is_(2)) assert_that(data.db_name, is_(database_name)) assert_that(data.pool_all_count, is_(1)) assert_that(data.pool_idle_count, is_(0)) mon = ActivityMonitor(components=[component]) conn = db.open() self.addCleanup(conn.close) mon.closedConnection(conn) assert_that(conn.loads, is_(0)) assert_that(conn.stores, is_(0)) assert_that(called_component, is_([1]))
def _getDB(): from OFS.Application import Application import transaction db = stuff.get('db') if not db: from ZODB import DB from ZODB.DemoStorage import DemoStorage ds = DemoStorage() db = DB(ds, pool_size=60) conn = db.open() root = conn.root() app = Application() root['Application'] = app transaction.savepoint(optimistic=True) _populate(app) stuff['db'] = db conn.close() return db
def createDatabase(): # XXX We have to import and init products in order for PluginIndexes to # be registered. OFS.Application.import_products() # Create a DemoStorage and put an Application in it db = DB(DemoStorage()) conn = db.open() root = conn.root() app = OFS.Application.Application() root['Application'] = app transaction.commit() # Init products #OFS.Application.initialize(app) OFS.Application.install_products(app) # XXX: this is still icky return app
def zlibfactory(): # Wraps uri in :class:`zc.slibstorage.ZlibStorage` and returns a # :class:`ZODB.DB` # Delay setting the client name until the very end so whatever is going to # set environment variables will have done so. if 'client' not in storage_kw: name = os.environ.get("DATASERVER_ZEO_CLIENT_NAME") if name: # pragma: no cover This isn't documented. # storage name is automatically part of it storage_kw['client'] = name # ClientCache docs say 200MB is good storage_kw.setdefault('cache_size', 200 * 1024 * 1024) # Client storage is very picky: a Unix path must be bytes, not # unicode client = ClientStorage(*args, **storage_kw) if 'demostorage' in orig_kw: # pragma: no cover client = DemoStorage(base=client) zlib = ZlibStorage(client) return DB(zlib, **dbkw)
def setUp(self): super(BasePublicationTests, self).setUp() from zope.security.management import endInteraction endInteraction() ztapi.provideAdapter(IHTTPRequest, IUserPreferredCharsets, HTTPCharsets) self.policy = setSecurityPolicy( simplepolicies.PermissiveSecurityPolicy) self.storage = DemoStorage('test_storage') self.db = db = DB(self.storage) ztapi.provideUtility(IAuthentication, principalRegistry) connection = db.open() root = connection.root() app = getattr(root, ZopePublication.root_name, None) if app is None: from zope.app.folder import rootFolder app = rootFolder() root[ZopePublication.root_name] = app transaction.commit() connection.close() self.app = app from zope.traversing.namespace import view, resource, etc ztapi.provideNamespaceHandler('view', view) ztapi.provideNamespaceHandler('resource', resource) ztapi.provideNamespaceHandler('etc', etc) self.request = TestRequest('/f1/f2') self.user = Principal('test.principal') self.request.setPrincipal(self.user) from zope.interface import Interface self.presentation_type = Interface self.request._presentation_type = self.presentation_type self.object = object() self.publication = ZopePublication(self.db)
def test_visit_sublocations_check_class_only_not_activate(self): from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage import transaction from zope.interface import alsoProvides cat = self._makeOne() content = PersistentContent() alsoProvides(content, INoAutoIndex) cat.mock_catalog_data.append((4, content)) db = DB(DemoStorage()) self.addCleanup(db.close) transaction.begin() conn = db.open() self.addCleanup(conn.close) conn.root.cat = cat transaction.commit() transaction.begin() conn.cacheMinimize() assert_that(conn.root.cat.mock_catalog_data[1][1], is_(NoIndexContent)) assert_that(conn.root.cat.mock_catalog_data[1][1]._p_status, is_('ghost')) assert_that(conn.root.cat.mock_catalog_data[-1][1], is_(PersistentContent)) assert_that(conn.root.cat.mock_catalog_data[-1][1]._p_status, is_('ghost')) locs = list(cat._visitSublocations()) assert_that(locs, has_length(1)) assert_that(locs[0], contains(1, is_(Content))) # Still a ghost assert_that(conn.root.cat.mock_catalog_data[1][1]._p_status, is_('ghost')) # But the one that alsoProvides() had to wake up assert_that(conn.root.cat.mock_catalog_data[-1][1]._p_status, is_('saved'))
def sandbox(base=None): '''Returns a sandbox copy of the base ZODB.''' if base is None: base = Zope2.DB storage = DemoStorage(base=base._storage) return ZODB.DB(storage)
import tarfile import xml.parsers.expat import xml.dom.minidom from urllib import url2pathname from ZODB.DemoStorage import DemoStorage from ZODB import DB from OFS.XMLExportImport import importXML if int(os.environ.get('erp5_report_new_simulation_failures') or 0): newSimulationExpectedFailure = lambda test: test else: from unittest import expectedFailure as newSimulationExpectedFailure # Keep a global reference to a ZODB storage so that we can import business # template xml files. XXX this connection will remain open. db = DB(DemoStorage()) connection = db.open() class BusinessTemplateInfoBase: def __init__(self, target): self.target = target self.setUp() def setUp(self): self.title = '' self.modules = {} self.allowed_content_types = {} self.actions = {}
neo_cluster = NEOCluster(db_list, partitions=4, name='erp5/unit_test', temp_dir=cwd, logger=save, adapter='SQLite', clear_databases=not load) forkNodes() if node_pid_list is None: save_mysql = None else: cluster = bool(node_pid_list) sigint = signal.signal(signal.SIGINT, signal.SIG_IGN) try: neo_cluster.start() finally: signal.signal(signal.SIGINT, sigint) Storage = neo_cluster.getZODBStorage(read_only=demo_storage) if demo_storage: Storage = DemoStorage(base=Storage) else: neo_cluster = None while not zeo_client: if activity_node: r, zeo_client = os.pipe() zeo_server_pid = fork() if zeo_server_pid: save_mysql = None os.close(zeo_client) zeo_client = eval(os.fdopen(r).read()) continue else: node_pid_list = activity_node = None os.close(r) signal.signal(signal.SIGINT, signal.SIG_IGN)
def makeConnection(): import ZODB from ZODB.DemoStorage import DemoStorage s = DemoStorage() return ZODB.DB(s).open()
def factory(): filestorage = FileStorage(*args, **kw) demostorage = DemoStorage(base=filestorage) return DB(demostorage, **dbkw)
def setUp(self): self.db = ZODB.DB(DemoStorage()) self.tm1 = transaction.TransactionManager() self.conn1 = self.db.open(transaction_manager=self.tm1) self.tm2 = transaction.TransactionManager() self.conn2 = self.db.open(transaction_manager=self.tm2)
def factory(): from ZEO.ClientStorage import ClientStorage from ZODB.DB import DB from ZODB.DemoStorage import DemoStorage demostorage = DemoStorage(base=ClientStorage(*args, **kw)) return DB(demostorage, **dbkw) #pragma NO COVERAGE