<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging
import time
from twisted.internet import defer

from nova import exception
from nova import flags
from nova import test
from nova import utils
from nova.compute import model
from nova.compute import node

FLAGS = flags.FLAGS

class ModelTestCase(test.TrialTestCase):
    def setUp(self):
        super(ModelTestCase, self).setUp()
        self.flags(fake_libvirt=True,
                   fake_storage=True,
                   fake_users=True)

    def tearDown(self):
        model.Instance('i-test').destroy()
        model.Host('testhost').destroy()
        model.Daemon('testhost', 'nova-testdaemon').destroy()

    def create_instance(self):
        inst = model.Instance('i-test')
        inst['reservation_id'] = 'r-test'
        inst['launch_time'] = '10'
        inst['user_id'] = 'fake'
        inst['project_id'] = 'fake'
        inst['instance_type'] = 'm1.tiny'
        inst['node_name'] = FLAGS.node_name
        inst['mac_address'] = utils.generate_mac()
        inst['ami_launch_index'] = 0
        inst.save()
        return inst

    def create_host(self):
        host = model.Host('testhost')
        host.save()
        return host

    def create_daemon(self):
        daemon = model.Daemon('testhost', 'nova-testdaemon')
        daemon.save()
        return daemon

    @defer.inlineCallbacks
    def test_create_instance(self):
        """store with create_instace, then test that a load finds it"""
        instance = yield self.create_instance()
        old = yield model.Instance(instance.identifier)
        self.assertFalse(old.is_new_record())

    @defer.inlineCallbacks
    def test_delete_instance(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_instance()
        yield instance.destroy()
        newinst = yield model.Instance('i-test')
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_instance_added_to_set(self):
        """create, then check that it is listed for the project"""
        instance = yield self.create_instance()
        found = False
        for x in model.InstanceDirectory().all:
            if x.identifier == 'i-test':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_instance_associates_project(self):
        """create, then check that it is listed for the project"""
        instance = yield self.create_instance()
        found = False
        for x in model.InstanceDirectory().by_project(instance.project):
            if x.identifier == 'i-test':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_host_class_finds_hosts(self):
        host = yield self.create_host()
        self.assertEqual('testhost', model.Host.lookup('testhost').identifier)

    @defer.inlineCallbacks
    def test_host_class_doesnt_find_missing_hosts(self):
        rv = yield model.Host.lookup('woahnelly')
        self.assertEqual(None, rv)

    @defer.inlineCallbacks
    def test_create_host(self):
        """store with create_host, then test that a load finds it"""
        host = yield self.create_host()
        old = yield model.Host(host.identifier)
        self.assertFalse(old.is_new_record())

    @defer.inlineCallbacks
    def test_delete_host(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_host()
        yield instance.destroy()
        newinst = yield model.Host('testhost')
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_host_added_to_set(self):
        """create, then check that it is included in list"""
        instance = yield self.create_host()
        found = False
        for x in model.Host.all():
            if x.identifier == 'testhost':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_create_daemon_two_args(self):
        """create a daemon with two arguments"""
        d = yield self.create_daemon()
        d = model.Daemon('testhost', 'nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_create_daemon_single_arg(self):
        """Create a daemon using the combined host:bin format"""
        d = yield model.Daemon("testhost:nova-testdaemon")
        d.save()
        d = model.Daemon('testhost:nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_equality_of_daemon_single_and_double_args(self):
        """Create a daemon using the combined host:bin arg, find with 2"""
        d = yield model.Daemon("testhost:nova-testdaemon")
        d.save()
        d = model.Daemon('testhost', 'nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_equality_daemon_of_double_and_single_args(self):
        """Create a daemon using the combined host:bin arg, find with 2"""
        d = yield self.create_daemon()
        d = model.Daemon('testhost:nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_delete_daemon(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_daemon()
        yield instance.destroy()
        newinst = yield model.Daemon('testhost', 'nova-testdaemon')
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_daemon_heartbeat(self):
        """Create a daemon, sleep, heartbeat, check for update"""
        d = yield self.create_daemon()
        ts = d['updated_at']
        time.sleep(2)
        d.heartbeat()
        d2 = model.Daemon('testhost', 'nova-testdaemon')
        ts2 = d2['updated_at']
        self.assert_(ts2 > ts)

    @defer.inlineCallbacks
    def test_daemon_added_to_set(self):
        """create, then check that it is included in list"""
        instance = yield self.create_daemon()
        found = False
        for x in model.Daemon.all():
            if x.identifier == 'testhost:nova-testdaemon':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_daemon_associates_host(self):
        """create, then check that it is listed for the host"""
        instance = yield self.create_daemon()
        found = False
        for x in model.Daemon.by_host('testhost'):
            if x.identifier == 'testhost:nova-testdaemon':
                found = True
        self.assertTrue(found)
<commit_msg>Basic standup of SessionToken model for shortlived auth tokens.<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2010 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import logging
import time
from twisted.internet import defer

from nova import exception
from nova import flags
from nova import test
from nova import utils
from nova.compute import model
from nova.compute import node

FLAGS = flags.FLAGS

class ModelTestCase(test.TrialTestCase):
    def setUp(self):
        super(ModelTestCase, self).setUp()
        self.flags(fake_libvirt=True,
                   fake_storage=True,
                   fake_users=True)

    def tearDown(self):
        model.Instance('i-test').destroy()
        model.Host('testhost').destroy()
        model.Daemon('testhost', 'nova-testdaemon').destroy()

    def create_instance(self):
        inst = model.Instance('i-test')
        inst['reservation_id'] = 'r-test'
        inst['launch_time'] = '10'
        inst['user_id'] = 'fake'
        inst['project_id'] = 'fake'
        inst['instance_type'] = 'm1.tiny'
        inst['node_name'] = FLAGS.node_name
        inst['mac_address'] = utils.generate_mac()
        inst['ami_launch_index'] = 0
        inst.save()
        return inst

    def create_host(self):
        host = model.Host('testhost')
        host.save()
        return host

    def create_daemon(self):
        daemon = model.Daemon('testhost', 'nova-testdaemon')
        daemon.save()
        return daemon

    def create_session_token(self):
        session_token = model.SessionToken('tk12341234')
        session_token['user'] = 'testuser'
        session_token.save()
        return session_token

    @defer.inlineCallbacks
    def test_create_instance(self):
        """store with create_instace, then test that a load finds it"""
        instance = yield self.create_instance()
        old = yield model.Instance(instance.identifier)
        self.assertFalse(old.is_new_record())

    @defer.inlineCallbacks
    def test_delete_instance(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_instance()
        yield instance.destroy()
        newinst = yield model.Instance('i-test')
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_instance_added_to_set(self):
        """create, then check that it is listed for the project"""
        instance = yield self.create_instance()
        found = False
        for x in model.InstanceDirectory().all:
            if x.identifier == 'i-test':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_instance_associates_project(self):
        """create, then check that it is listed for the project"""
        instance = yield self.create_instance()
        found = False
        for x in model.InstanceDirectory().by_project(instance.project):
            if x.identifier == 'i-test':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_host_class_finds_hosts(self):
        host = yield self.create_host()
        self.assertEqual('testhost', model.Host.lookup('testhost').identifier)

    @defer.inlineCallbacks
    def test_host_class_doesnt_find_missing_hosts(self):
        rv = yield model.Host.lookup('woahnelly')
        self.assertEqual(None, rv)

    @defer.inlineCallbacks
    def test_create_host(self):
        """store with create_host, then test that a load finds it"""
        host = yield self.create_host()
        old = yield model.Host(host.identifier)
        self.assertFalse(old.is_new_record())

    @defer.inlineCallbacks
    def test_delete_host(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_host()
        yield instance.destroy()
        newinst = yield model.Host('testhost')
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_host_added_to_set(self):
        """create, then check that it is included in list"""
        instance = yield self.create_host()
        found = False
        for x in model.Host.all():
            if x.identifier == 'testhost':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_create_daemon_two_args(self):
        """create a daemon with two arguments"""
        d = yield self.create_daemon()
        d = model.Daemon('testhost', 'nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_create_daemon_single_arg(self):
        """Create a daemon using the combined host:bin format"""
        d = yield model.Daemon("testhost:nova-testdaemon")
        d.save()
        d = model.Daemon('testhost:nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_equality_of_daemon_single_and_double_args(self):
        """Create a daemon using the combined host:bin arg, find with 2"""
        d = yield model.Daemon("testhost:nova-testdaemon")
        d.save()
        d = model.Daemon('testhost', 'nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_equality_daemon_of_double_and_single_args(self):
        """Create a daemon using the combined host:bin arg, find with 2"""
        d = yield self.create_daemon()
        d = model.Daemon('testhost:nova-testdaemon')
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_delete_daemon(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_daemon()
        yield instance.destroy()
        newinst = yield model.Daemon('testhost', 'nova-testdaemon')
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_daemon_heartbeat(self):
        """Create a daemon, sleep, heartbeat, check for update"""
        d = yield self.create_daemon()
        ts = d['updated_at']
        time.sleep(2)
        d.heartbeat()
        d2 = model.Daemon('testhost', 'nova-testdaemon')
        ts2 = d2['updated_at']
        self.assert_(ts2 > ts)

    @defer.inlineCallbacks
    def test_daemon_added_to_set(self):
        """create, then check that it is included in list"""
        instance = yield self.create_daemon()
        found = False
        for x in model.Daemon.all():
            if x.identifier == 'testhost:nova-testdaemon':
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_daemon_associates_host(self):
        """create, then check that it is listed for the host"""
        instance = yield self.create_daemon()
        found = False
        for x in model.Daemon.by_host('testhost'):
            if x.identifier == 'testhost:nova-testdaemon':
                found = True
        self.assertTrue(found)

    @defer.inlineCallbacks
    def test_create_session_token(self):
        """create"""
        d = yield self.create_session_token()
        d = model.SessionToken(d.token)
        self.assertFalse(d.is_new_record())

    @defer.inlineCallbacks
    def test_delete_session_token(self):
        """create, then destroy, then make sure loads a new record"""
        instance = yield self.create_session_token()
        yield instance.destroy()
        newinst = yield model.SessionToken(instance.token)
        self.assertTrue(newinst.is_new_record())

    @defer.inlineCallbacks
    def test_session_token_added_to_set(self):
        """create, then check that it is included in list"""
        instance = yield self.create_session_token()
        found = False
        for x in model.SessionToken.all():
            if x.identifier == instance.token:
                found = True
        self.assert_(found)

    @defer.inlineCallbacks
    def test_session_token_associates_user(self):
        """create, then check that it is listed for the user"""
        instance = yield self.create_session_token()
        found = False
        for x in model.SessionToken.associated_to('user', 'testuser'):
            if x.identifier == instance.identifier:
                found = True
        self.assertTrue(found)

    @defer.inlineCallbacks
    def test_session_token_generation(self):
        instance = yield model.SessionToken.generate('username', 'TokenType')
        self.assertFalse(instance.is_new_record())

    @defer.inlineCallbacks
    def test_find_generated_session_token(self):
        instance = yield model.SessionToken.generate('username', 'TokenType')
        found = yield model.SessionToken.lookup(instance.identifier)
        self.assert_(found)
<|endoftext|>


<commit_before>from functools import wraps
import glob
import os
import sys
import threading
import uuid

try:
    from redis import Redis
    from redis.client import pairs_to_dict
    from redis.client import zset_score_pairs
    from redis.exceptions import ConnectionError
    from redis.exceptions import TimeoutError
except ImportError:
    Redis = object
    zset_score_pairs = None
    ConnectionError = TimeoutError = Exception

from walrus.autocomplete import Autocomplete
from walrus.cache import Cache
from walrus.containers import Array
from walrus.containers import BitField
from walrus.containers import BloomFilter
from walrus.containers import ConsumerGroup
from walrus.containers import Hash
from walrus.containers import HyperLogLog
from walrus.containers import List
from walrus.containers import Set
from walrus.containers import Stream
from walrus.containers import ZSet
from walrus.counter import Counter
from walrus.fts import Index
from walrus.graph import Graph
from walrus.lock import Lock
from walrus.rate_limit import RateLimit
from walrus.streams import TimeSeries

class TransactionLocal(threading.local):
    def __init__(self, **kwargs):
        super(TransactionLocal, self).__init__(**kwargs)
        self.pipes = []

    @property
    def pipe(self):
        if len(self.pipes):
            return self.pipes[-1]

    def commit(self):
        pipe = self.pipes.pop()
        return pipe.execute()

    def abort(self):
        pipe = self.pipes.pop()
        pipe.reset()

class Database(Redis):
    """
    Redis-py client with some extras.
    """
    def __init__(self, *args, **kwargs):
        """
        :param args: Arbitrary positional arguments to pass to the
            base ``Redis`` instance.
        :param kwargs: Arbitrary keyword arguments to pass to the
            base ``Redis`` instance.
        :param str script_dir: Path to directory containing walrus
            scripts. Use "script_dir=False" to disable loading any scripts.
        """
        script_dir = kwargs.pop('script_dir', None)
        super(Database, self).__init__(*args, **kwargs)
        self.__mapping = {
            b'list': self.List,
            b'set': self.Set,
            b'zset': self.ZSet,
            b'hash': self.Hash}
        self._transaction_local = TransactionLocal()
        self._transaction_lock = threading.RLock()
        if script_dir is not False:
            self.init_scripts(script_dir=script_dir)

    def xsetid(self, name, id):
        """
        Set the last ID of the given stream.

        :param name: stream identifier
        :param id: new value for last ID
        """
        return self.execute_command('XSETID', name, id) == b'OK'

    def xpending_summary(self, key, group):
        """
        Pending message summary report.

        :param key: stream identifier
        :param group: consumer group name
        :returns: dictionary of information about pending messages
        """
        return self.xpending(key, group)

    def get_transaction(self):
        with self._transaction_lock:
            local = self._transaction_local
            local.pipes.append(self.pipeline())
            return local.pipe

    def commit_transaction(self):
        """
        Commit the currently active transaction (Pipeline). If no
        transaction is active in the current thread, an exception
        will be raised.

        :returns: The return value of executing the Pipeline.
        :raises: ``ValueError`` if no transaction is active.
        """
        with self._transaction_lock:
            local = self._transaction_local
            if not local.pipes:
                raise ValueError('No transaction is currently active.')
            return local.commit()

    def clear_transaction(self):
        """
        Clear the currently active transaction (if exists). If the
        transaction stack is not empty, then a new pipeline will
        be initialized.

        :returns: No return value.
        :raises: ``ValueError`` if no transaction is active.
        """
        with self._transaction_lock:
            local = self._transaction_local
            if not local.pipes:
                raise ValueError('No transaction is currently active.')
            local.abort()

    def atomic(self):
        return _Atomic(self)

    def init_scripts(self, script_dir=None):
        self._scripts = {}
        if not script_dir:
            script_dir = os.path.join(os.path.dirname(__file__), 'scripts')
        for filename in glob.glob(os.path.join(script_dir, '*.lua')):
            with open(filename, 'r') as fh:
                script_obj = self.register_script(fh.read())
                script_name = os.path.splitext(os.path.basename(filename))[0]
                self._scripts[script_name] = script_obj

    def run_script(self, script_name, keys=None, args=None):
        """
        Execute a walrus script with the given arguments.

        :param script_name: The base name of the script to execute.
        :param list keys: Keys referenced by the script.
        :param list args: Arguments passed in to the script.
        :returns: Return value of script.

        .. note:: Redis scripts require two parameters, ``keys``
            and ``args``, which are referenced in lua as ``KEYS``
            and ``ARGV``.
        """
        return self._scripts[script_name](keys, args)

    def get_temp_key(self):
        """
        Generate a temporary random key using UUID4.
        """
        return 'temp.%s' % uuid.uuid4()

    def __iter__(self):
        """
        Iterate over the keys of the selected database.
        """
        return iter(self.scan_iter())

    def __len__(self):
        return self.dbsize()

    def search(self, pattern):
        """
        Search the keyspace of the selected database using the
        given search pattern.

        :param str pattern: Search pattern using wildcards.
        :returns: Iterator that yields matching keys.
        """
        return self.scan_iter(pattern)

    def get_key(self, key):
        """
        Return a rich object for the given key. For instance, if
        a hash key is requested, then a :py:class:`Hash` will be
        returned.

        Note: only works for Hash, List, Set and ZSet.

        :param str key: Key to retrieve.
        :returns: A hash, set, list, zset or array.
        """
        return self.__mapping.get(self.type(key), self.__getitem__)(key)

    def hash_exists(self, key):
        return self.exists(key)

    def autocomplete(self, namespace='autocomplete', **kwargs):
        return Autocomplete(self, namespace, **kwargs)

    def cache(self, name='cache', default_timeout=3600):
        """
        Create a :py:class:`Cache` instance.

        :param str name: The name used to prefix keys used to
            store cached data.
        :param int default_timeout: The default key expiry.
        :returns: A :py:class:`Cache` instance.
        """
        return Cache(self, name=name, default_timeout=default_timeout)

    def counter(self, name):
        """
        Create a :py:class:`Counter` instance.

        :param str name: The name used to store the counter's value.
        :returns: A :py:class:`Counter` instance.
        """
        return Counter(self, name=name)

    def graph(self, name, *args, **kwargs):
        """
        Creates a :py:class:`Graph` instance.

        :param str name: The namespace for the graph metadata.
        :returns: a :py:class:`Graph` instance.
        """
        return Graph(self, name, *args, **kwargs)

    def lock(self, name, ttl=None, lock_id=None):
        """
        Create a named :py:class:`Lock` instance. The lock implements
        an API similar to the standard library's ``threading.Lock``,
        and can also be used as a context manager or decorator.

        :param str name: The name of the lock.
        :param int ttl: The time-to-live for the lock in milliseconds
            (optional). If the ttl is ``None`` then the lock will not
            expire.
        :param str lock_id: Optional identifier for the lock instance.
        """
        return Lock(self, name, ttl, lock_id)

    def rate_limit(self, name, limit=5, per=60, debug=False):
        """
        Rate limit implementation. Allows up to `limit` of events every `per`
        seconds.

        See :ref:`rate-limit` for more information.
        """
        return RateLimit(self, name, limit, per, debug)

    def Index(self, name, **options):
        """
        Create a :py:class:`Index` full-text search index with the given
        name and options.
        """
        return Index(self, name, **options)

    def List(self, key):
        """
        Create a :py:class:`List` instance wrapping the given key.
        """
        return List(self, key)

    def Hash(self, key):
        """
        Create a :py:class:`Hash` instance wrapping the given key.
        """
        return Hash(self, key)

    def Set(self, key):
        """
        Create a :py:class:`Set` instance wrapping the given key.
        """
        return Set(self, key)

    def ZSet(self, key):
        """
        Create a :py:class:`ZSet` instance wrapping the given key.
        """
        return ZSet(self, key)

    def HyperLogLog(self, key):
        """
        Create a :py:class:`HyperLogLog` instance wrapping the given
        key.
        """
        return HyperLogLog(self, key)

    def Array(self, key):
        """
        Create a :py:class:`Array` instance wrapping the given key.
        """
        return Array(self, key)

    def Stream(self, key):
        """
        Create a :py:class:`Stream` instance wrapping the given key.
        """
        return Stream(self, key)

    def consumer_group(self, group, keys, consumer=None):
        """
        Create a named :py:class:`ConsumerGroup` instance for the given key(s).

        :param group: name of consumer group
        :param keys: stream identifier(s) to monitor. May be a single stream
            key, a list of stream keys, or a key-to-minimum id mapping. The
            minimum id for each stream should be considered an exclusive
            lower-bound. The '$' value can also be used to only read values
            added *after* our command started blocking.
        :param consumer: name for consumer within group
        :returns: a :py:class:`ConsumerGroup` instance
        """
        return ConsumerGroup(self, group, keys, consumer=consumer)

    def time_series(self, group, keys, consumer=None):
        """
        Create a named :py:class:`TimeSeries` consumer-group for the
        given key(s). TimeSeries objects are almost identical to
        :py:class:`ConsumerGroup` except they offer a higher level of
        abstraction and read/write message ids as datetimes.

        :param group: name of consumer group
        :param keys: stream identifier(s) to monitor. May be a single stream
            key, a list of stream keys, or a key-to-minimum id mapping. The
            minimum id for each stream should be considered an exclusive
            lower-bound. The '$' value can also be used to only read values
            added *after* our command started blocking.
        :param consumer: name for consumer within group
        :returns: a :py:class:`TimeSeries` instance
        """
        return TimeSeries(self, group, keys, consumer=consumer)

    def bit_field(self, key):
        """
        Container for working with the Redis BITFIELD command.

        :returns: a :py:class:`BitField` instance.
        """
        return BitField(self, key)

    def bloom_filter(self, key, size=64 * 1024):
        """
        Create a :py:class:`BloomFilter` container type.

        Bloom-filters are probabilistic data-structures that are used to answer
        the question: "is X a member of set S?" It is possible to receive a
        false positive, but impossible to receive a false negative (in other
        words, if the bloom filter contains a value, it will never erroneously
        report that it does *not* contain such a value). The accuracy of the
        bloom-filter and the likelihood of a false positive can be reduced by
        increasing the size of the bloomfilter. The default size is 64KB (or
        524,288 bits).
        """
        return BloomFilter(self, key, size)

    def cas(self, key, value, new_value):
        """
        Perform an atomic compare-and-set on the value in "key", using a prefix
        match on the provided value.
        """
        return self.run_script('cas', keys=[key], args=[value, new_value])

    def listener(self, channels=None, patterns=None, is_async=False):
        """
        Decorator for wrapping functions used to listen for Redis
        pub-sub messages.

        The listener will listen until the decorated function
        raises a ``StopIteration`` exception.

        :param list channels: Channels to listen on.
        :param list patterns: Patterns to match.
        :param bool is_async: Whether to start the listener in a
            separate thread.
        """
        def decorator(fn):
            _channels = channels or []
            _patterns = patterns or []

            @wraps(fn)
            def inner():
                pubsub = self.pubsub()

                def listen():
                    for channel in _channels:
                        pubsub.subscribe(channel)
                    for pattern in _patterns:
                        pubsub.psubscribe(pattern)

                    for data_dict in pubsub.listen():
                        try:
                            ret = fn(**data_dict)
                        except StopIteration:
                            pubsub.close()
                            break

                if is_async:
                    worker = threading.Thread(target=listen)
                    worker.start()
                    return worker
                else:
                    listen()

            return inner
        return decorator

    def stream_log(self, callback, connection_id='monitor'):
        """
        Stream Redis activity one line at a time to the given
        callback.

        :param callback: A function that accepts a single argument,
            the Redis command.
        """
        conn = self.connection_pool.get_connection(connection_id, None)
        conn.send_command('monitor')
        while callback(conn.read_response()):
            pass

class _Atomic(object):
    def __init__(self, db):
        self.db = db

    @property
    def pipe(self):
        return self.db._transaction_local.pipe

    def __enter__(self):
        self.db.get_transaction()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type:
            self.clear(False)
        else:
            self.commit(False)

    def commit(self, begin_new=True):
        ret = self.db.commit_transaction()
        if begin_new:
            self.db.get_transaction()
        return ret

    def clear(self, begin_new=True):
        self.db.clear_transaction()
        if begin_new:
            self.db.get_transaction()
<commit_msg>Fixes #178<commit_after>from functools import wraps
import glob
import os
import sys
import threading
import uuid

try:
    from redis import Redis
    from redis.client import pairs_to_dict
    from redis.client import zset_score_pairs
    from redis.exceptions import ConnectionError
    from redis.exceptions import TimeoutError
except ImportError:
    Redis = object
    zset_score_pairs = None
    ConnectionError = TimeoutError = Exception

from walrus.autocomplete import Autocomplete
from walrus.cache import Cache
from walrus.containers import Array
from walrus.containers import BitField
from walrus.containers import BloomFilter
from walrus.containers import ConsumerGroup
from walrus.containers import Hash
from walrus.containers import HyperLogLog
from walrus.containers import List
from walrus.containers import Set
from walrus.containers import Stream
from walrus.containers import ZSet
from walrus.counter import Counter
from walrus.fts import Index
from walrus.graph import Graph
from walrus.lock import Lock
from walrus.rate_limit import RateLimit
from walrus.streams import TimeSeries

class TransactionLocal(threading.local):
    def __init__(self, **kwargs):
        super(TransactionLocal, self).__init__(**kwargs)
        self.pipes = []

    @property
    def pipe(self):
        if len(self.pipes):
            return self.pipes[-1]

    def commit(self):
        pipe = self.pipes.pop()
        return pipe.execute()

    def abort(self):
        pipe = self.pipes.pop()
        pipe.reset()

class Database(Redis):
    """
    Redis-py client with some extras.
    """
    def __init__(self, *args, **kwargs):
        """
        :param args: Arbitrary positional arguments to pass to the
            base ``Redis`` instance.
        :param kwargs: Arbitrary keyword arguments to pass to the
            base ``Redis`` instance.
        :param str script_dir: Path to directory containing walrus
            scripts. Use "script_dir=False" to disable loading any scripts.
        """
        script_dir = kwargs.pop('script_dir', None)
        super(Database, self).__init__(*args, **kwargs)
        self.__mapping = {
            b'list': self.List,
            b'set': self.Set,
            b'zset': self.ZSet,
            b'hash': self.Hash}
        self._transaction_local = TransactionLocal()
        self._transaction_lock = threading.RLock()
        if script_dir is not False:
            self.init_scripts(script_dir=script_dir)

    def __bool__(self):
        return True  # Avoid falling back to __len__().

    def xsetid(self, name, id):
        """
        Set the last ID of the given stream.

        :param name: stream identifier
        :param id: new value for last ID
        """
        return self.execute_command('XSETID', name, id) == b'OK'

    def xpending_summary(self, key, group):
        """
        Pending message summary report.

        :param key: stream identifier
        :param group: consumer group name
        :returns: dictionary of information about pending messages
        """
        return self.xpending(key, group)

    def get_transaction(self):
        with self._transaction_lock:
            local = self._transaction_local
            local.pipes.append(self.pipeline())
            return local.pipe

    def commit_transaction(self):
        """
        Commit the currently active transaction (Pipeline). If no
        transaction is active in the current thread, an exception
        will be raised.

        :returns: The return value of executing the Pipeline.
        :raises: ``ValueError`` if no transaction is active.
        """
        with self._transaction_lock:
            local = self._transaction_local
            if not local.pipes:
                raise ValueError('No transaction is currently active.')
            return local.commit()

    def clear_transaction(self):
        """
        Clear the currently active transaction (if exists). If the
        transaction stack is not empty, then a new pipeline will
        be initialized.

        :returns: No return value.
        :raises: ``ValueError`` if no transaction is active.
        """
        with self._transaction_lock:
            local = self._transaction_local
            if not local.pipes:
                raise ValueError('No transaction is currently active.')
            local.abort()

    def atomic(self):
        return _Atomic(self)

    def init_scripts(self, script_dir=None):
        self._scripts = {}
        if not script_dir:
            script_dir = os.path.join(os.path.dirname(__file__), 'scripts')
        for filename in glob.glob(os.path.join(script_dir, '*.lua')):
            with open(filename, 'r') as fh:
                script_obj = self.register_script(fh.read())
                script_name = os.path.splitext(os.path.basename(filename))[0]
                self._scripts[script_name] = script_obj

    def run_script(self, script_name, keys=None, args=None):
        """
        Execute a walrus script with the given arguments.

        :param script_name: The base name of the script to execute.
        :param list keys: Keys referenced by the script.
        :param list args: Arguments passed in to the script.
        :returns: Return value of script.

        .. note:: Redis scripts require two parameters, ``keys``
            and ``args``, which are referenced in lua as ``KEYS``
            and ``ARGV``.
        """
        return self._scripts[script_name](keys, args)

    def get_temp_key(self):
        """
        Generate a temporary random key using UUID4.
        """
        return 'temp.%s' % uuid.uuid4()

    def __iter__(self):
        """
        Iterate over the keys of the selected database.
        """
        return iter(self.scan_iter())

    def __len__(self):
        return self.dbsize()

    def search(self, pattern):
        """
        Search the keyspace of the selected database using the
        given search pattern.

        :param str pattern: Search pattern using wildcards.
        :returns: Iterator that yields matching keys.
        """
        return self.scan_iter(pattern)

    def get_key(self, key):
        """
        Return a rich object for the given key. For instance, if
        a hash key is requested, then a :py:class:`Hash` will be
        returned.

        Note: only works for Hash, List, Set and ZSet.

        :param str key: Key to retrieve.
        :returns: A hash, set, list, zset or array.
        """
        return self.__mapping.get(self.type(key), self.__getitem__)(key)

    def hash_exists(self, key):
        return self.exists(key)

    def autocomplete(self, namespace='autocomplete', **kwargs):
        return Autocomplete(self, namespace, **kwargs)

    def cache(self, name='cache', default_timeout=3600):
        """
        Create a :py:class:`Cache` instance.

        :param str name: The name used to prefix keys used to
            store cached data.
        :param int default_timeout: The default key expiry.
        :returns: A :py:class:`Cache` instance.
        """
        return Cache(self, name=name, default_timeout=default_timeout)

    def counter(self, name):
        """
        Create a :py:class:`Counter` instance.

        :param str name: The name used to store the counter's value.
        :returns: A :py:class:`Counter` instance.
        """
        return Counter(self, name=name)

    def graph(self, name, *args, **kwargs):
        """
        Creates a :py:class:`Graph` instance.

        :param str name: The namespace for the graph metadata.
        :returns: a :py:class:`Graph` instance.
        """
        return Graph(self, name, *args, **kwargs)

    def lock(self, name, ttl=None, lock_id=None):
        """
        Create a named :py:class:`Lock` instance. The lock implements
        an API similar to the standard library's ``threading.Lock``,
        and can also be used as a context manager or decorator.

        :param str name: The name of the lock.
        :param int ttl: The time-to-live for the lock in milliseconds
            (optional). If the ttl is ``None`` then the lock will not
            expire.
        :param str lock_id: Optional identifier for the lock instance.
        """
        return Lock(self, name, ttl, lock_id)

    def rate_limit(self, name, limit=5, per=60, debug=False):
        """
        Rate limit implementation. Allows up to `limit` of events every `per`
        seconds.

        See :ref:`rate-limit` for more information.
        """
        return RateLimit(self, name, limit, per, debug)

    def Index(self, name, **options):
        """
        Create a :py:class:`Index` full-text search index with the given
        name and options.
        """
        return Index(self, name, **options)

    def List(self, key):
        """
        Create a :py:class:`List` instance wrapping the given key.
        """
        return List(self, key)

    def Hash(self, key):
        """
        Create a :py:class:`Hash` instance wrapping the given key.
        """
        return Hash(self, key)

    def Set(self, key):
        """
        Create a :py:class:`Set` instance wrapping the given key.
        """
        return Set(self, key)

    def ZSet(self, key):
        """
        Create a :py:class:`ZSet` instance wrapping the given key.
        """
        return ZSet(self, key)

    def HyperLogLog(self, key):
        """
        Create a :py:class:`HyperLogLog` instance wrapping the given
        key.
        """
        return HyperLogLog(self, key)

    def Array(self, key):
        """
        Create a :py:class:`Array` instance wrapping the given key.
        """
        return Array(self, key)

    def Stream(self, key):
        """
        Create a :py:class:`Stream` instance wrapping the given key.
        """
        return Stream(self, key)

    def consumer_group(self, group, keys, consumer=None):
        """
        Create a named :py:class:`ConsumerGroup` instance for the given key(s).

        :param group: name of consumer group
        :param keys: stream identifier(s) to monitor. May be a single stream
            key, a list of stream keys, or a key-to-minimum id mapping. The
            minimum id for each stream should be considered an exclusive
            lower-bound. The '$' value can also be used to only read values
            added *after* our command started blocking.
        :param consumer: name for consumer within group
        :returns: a :py:class:`ConsumerGroup` instance
        """
        return ConsumerGroup(self, group, keys, consumer=consumer)

    def time_series(self, group, keys, consumer=None):
        """
        Create a named :py:class:`TimeSeries` consumer-group for the
        given key(s). TimeSeries objects are almost identical to
        :py:class:`ConsumerGroup` except they offer a higher level of
        abstraction and read/write message ids as datetimes.

        :param group: name of consumer group
        :param keys: stream identifier(s) to monitor. May be a single stream
            key, a list of stream keys, or a key-to-minimum id mapping. The
            minimum id for each stream should be considered an exclusive
            lower-bound. The '$' value can also be used to only read values
            added *after* our command started blocking.
        :param consumer: name for consumer within group
        :returns: a :py:class:`TimeSeries` instance
        """
        return TimeSeries(self, group, keys, consumer=consumer)

    def bit_field(self, key):
        """
        Container for working with the Redis BITFIELD command.

        :returns: a :py:class:`BitField` instance.
        """
        return BitField(self, key)

    def bloom_filter(self, key, size=64 * 1024):
        """
        Create a :py:class:`BloomFilter` container type.

        Bloom-filters are probabilistic data-structures that are used to answer
        the question: "is X a member of set S?" It is possible to receive a
        false positive, but impossible to receive a false negative (in other
        words, if the bloom filter contains a value, it will never erroneously
        report that it does *not* contain such a value). The accuracy of the
        bloom-filter and the likelihood of a false positive can be reduced by
        increasing the size of the bloomfilter. The default size is 64KB (or
        524,288 bits).
        """
        return BloomFilter(self, key, size)

    def cas(self, key, value, new_value):
        """
        Perform an atomic compare-and-set on the value in "key", using a prefix
        match on the provided value.
        """
        return self.run_script('cas', keys=[key], args=[value, new_value])

    def listener(self, channels=None, patterns=None, is_async=False):
        """
        Decorator for wrapping functions used to listen for Redis
        pub-sub messages.

        The listener will listen until the decorated function
        raises a ``StopIteration`` exception.

        :param list channels: Channels to listen on.
        :param list patterns: Patterns to match.
        :param bool is_async: Whether to start the listener in a
            separate thread.
        """
        def decorator(fn):
            _channels = channels or []
            _patterns = patterns or []

            @wraps(fn)
            def inner():
                pubsub = self.pubsub()

                def listen():
                    for channel in _channels:
                        pubsub.subscribe(channel)
                    for pattern in _patterns:
                        pubsub.psubscribe(pattern)

                    for data_dict in pubsub.listen():
                        try:
                            ret = fn(**data_dict)
                        except StopIteration:
                            pubsub.close()
                            break

                if is_async:
                    worker = threading.Thread(target=listen)
                    worker.start()
                    return worker
                else:
                    listen()

            return inner
        return decorator

    def stream_log(self, callback, connection_id='monitor'):
        """
        Stream Redis activity one line at a time to the given
        callback.

        :param callback: A function that accepts a single argument,
            the Redis command.
        """
        conn = self.connection_pool.get_connection(connection_id, None)
        conn.send_command('monitor')
        while callback(conn.read_response()):
            pass

class _Atomic(object):
    def __init__(self, db):
        self.db = db

    @property
    def pipe(self):
        return self.db._transaction_local.pipe

    def __enter__(self):
        self.db.get_transaction()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        if exc_type:
            self.clear(False)
        else:
            self.commit(False)

    def commit(self, begin_new=True):
        ret = self.db.commit_transaction()
        if begin_new:
            self.db.get_transaction()
        return ret

    def clear(self, begin_new=True):
        self.db.clear_transaction()
        if begin_new:
            self.db.get_transaction()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

""" Test VQE """

import unittest
import warnings
from test.aqua import QiskitAquaTestCase
import numpy as np
from ddt import ddt, unpack, data
from qiskit import BasicAer, QuantumCircuit
from qiskit.circuit.library import TwoLocal

from qiskit.aqua import QuantumInstance, aqua_globals, AquaError
from qiskit.aqua.operators import WeightedPauliOperator, PrimitiveOp
from qiskit.aqua.components.variational_forms import RY, RYRZ
from qiskit.aqua.components.optimizers import L_BFGS_B, COBYLA, SPSA, SLSQP
from qiskit.aqua.algorithms import VQE

@ddt
class TestVQE(QiskitAquaTestCase):
    """ Test VQE """

    def setUp(self):
        super().setUp()
        self.seed = 50
        aqua_globals.random_seed = self.seed
        pauli_dict = {
            'paulis': [{"coeff": {"imag": 0.0, "real": -1.052373245772859}, "label": "II"},
                       {"coeff": {"imag": 0.0, "real": 0.39793742484318045}, "label": "IZ"},
                       {"coeff": {"imag": 0.0, "real": -0.39793742484318045}, "label": "ZI"},
                       {"coeff": {"imag": 0.0, "real": -0.01128010425623538}, "label": "ZZ"},
                       {"coeff": {"imag": 0.0, "real": 0.18093119978423156}, "label": "XX"}
                       ]
        }
        self.qubit_op = WeightedPauliOperator.from_dict(pauli_dict).to_opflow()

        num_qubits = self.qubit_op.num_qubits
        ansatz = TwoLocal(num_qubits, rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        self.ryrz_wavefunction = {'wrapped': RYRZ(num_qubits),
                                  'circuit': QuantumCircuit(num_qubits).compose(ansatz),
                                  'library': ansatz}

        ansatz = ansatz.copy()
        ansatz.rotation_blocks = 'ry'
        self.ry_wavefunction = {'wrapped': RY(num_qubits),
                                'circuit': QuantumCircuit(num_qubits).compose(ansatz),
                                'library': ansatz}
        warnings.filterwarnings('always', category=DeprecationWarning)

    @data('wrapped', 'circuit', 'library')
    def test_vqe(self, mode):
        """ VQE test """
        wavefunction = self.ryrz_wavefunction[mode]
        if mode == 'wrapped':
            warnings.filterwarnings('ignore', category=DeprecationWarning)
        vqe = VQE(self.qubit_op, wavefunction, L_BFGS_B())
        if mode == 'wrapped':
            warnings.filterwarnings('always', category=DeprecationWarning)

        result = vqe.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                                         basis_gates=['u1', 'u2', 'u3', 'cx', 'id'],
                                         coupling_map=[[0, 1]],
                                         seed_simulator=aqua_globals.random_seed,
                                         seed_transpiler=aqua_globals.random_seed))
        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503)
        np.testing.assert_array_almost_equal(result.eigenvalue.real, -1.85727503, 5)
        self.assertEqual(len(result.optimal_point), 16)
        self.assertIsNotNone(result.cost_function_evals)
        self.assertIsNotNone(result.optimizer_time)

    def test_vqe_no_varform_params(self):
        """Test specifying a variational form with no parameters raises an error."""
        circuit = QuantumCircuit(self.qubit_op.num_qubits)
        for i in range(circuit.num_qubits):
            circuit.h(i)
            circuit.cx(i, (i + 1) % circuit.num_qubits)
            circuit.rx(0.2, i)

        vqe = VQE(self.qubit_op, circuit)
        with self.assertRaises(RuntimeError):
            vqe.run(BasicAer.get_backend('statevector_simulator'))

    @data(
        (SLSQP, 5, 4),
        (SLSQP, 5, 1),
        (SPSA, 3, 2),  # max_evals_grouped=n is considered as max_evals_grouped=2 if n>2
        (SPSA, 3, 1)
    )
    @unpack
    def test_vqe_optimizers(self, optimizer_cls, places, max_evals_grouped):
        """ VQE Optimizers test """
        result = VQE(self.qubit_op,
                     TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz'),
                     optimizer_cls(),
                     max_evals_grouped=max_evals_grouped).run(
                         QuantumInstance(BasicAer.get_backend('statevector_simulator'), shots=1,
                                         seed_simulator=aqua_globals.random_seed,
                                         seed_transpiler=aqua_globals.random_seed))

        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=places)

    @data('wrapped', 'circuit', 'library')
    def test_vqe_qasm(self, mode):
        """ VQE QASM test """
        backend = BasicAer.get_backend('qasm_simulator')
        optimizer = SPSA(max_trials=300, last_avg=5)
        wavefunction = self.ry_wavefunction[mode]

        if mode == 'wrapped':
            warnings.filterwarnings('ignore', category=DeprecationWarning)
            reference_energy = -1.86823
        else:
            reference_energy = -1.87019
        vqe = VQE(self.qubit_op, wavefunction, optimizer, max_evals_grouped=1)
        if mode == 'wrapped':
            warnings.filterwarnings('always', category=DeprecationWarning)

        # TODO benchmark this later.
        quantum_instance = QuantumInstance(backend, shots=1000,
                                           seed_simulator=self.seed,
                                           seed_transpiler=self.seed)
        result = vqe.run(quantum_instance)
        self.assertAlmostEqual(result.eigenvalue.real, reference_energy, places=2)

    @data('wrapped', 'circuit', 'library')
    def test_vqe_statevector_snapshot_mode(self, mode):
        """ VQE Aer statevector_simulator snapshot mode test """
        try:
            # pylint: disable=import-outside-toplevel
            from qiskit import Aer
        except Exception as ex:  # pylint: disable=broad-except
            self.skipTest("Aer doesn't appear to be installed. Error: '{}'".format(str(ex)))
            return
        backend = Aer.get_backend('statevector_simulator')
        wavefunction = self.ry_wavefunction[mode]
        optimizer = L_BFGS_B()

        if mode == 'wrapped':
            warnings.filterwarnings('ignore', category=DeprecationWarning)
        vqe = VQE(self.qubit_op, wavefunction, optimizer, max_evals_grouped=1)
        if mode == 'wrapped':
            warnings.filterwarnings('always', category=DeprecationWarning)

        quantum_instance = QuantumInstance(backend,
                                           seed_simulator=aqua_globals.random_seed,
                                           seed_transpiler=aqua_globals.random_seed)
        result = vqe.run(quantum_instance)
        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=6)

    @data('wrapped', 'circuit', 'library')
    def test_vqe_qasm_snapshot_mode(self, mode):
        """ VQE Aer qasm_simulator snapshot mode test """
        try:
            # pylint: disable=import-outside-toplevel
            from qiskit import Aer
        except Exception as ex:  # pylint: disable=broad-except
            self.skipTest("Aer doesn't appear to be installed. Error: '{}'".format(str(ex)))
            return
        backend = Aer.get_backend('qasm_simulator')
        optimizer = L_BFGS_B()
        wavefunction = self.ry_wavefunction[mode]

        if mode == 'wrapped':
            warnings.filterwarnings('ignore', category=DeprecationWarning)
        vqe = VQE(self.qubit_op, wavefunction, optimizer, max_evals_grouped=1)
        if mode == 'wrapped':
            warnings.filterwarnings('always', category=DeprecationWarning)

        quantum_instance = QuantumInstance(backend, shots=1,
                                           seed_simulator=aqua_globals.random_seed,
                                           seed_transpiler=aqua_globals.random_seed)
        result = vqe.run(quantum_instance)
        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=6)

    @data('wrapped', 'circuit', 'library')
    def test_vqe_callback(self, mode):
        """ VQE Callback test """
        history = {'eval_count': [], 'parameters': [], 'mean': [], 'std': []}

        def store_intermediate_result(eval_count, parameters, mean, std):
            history['eval_count'].append(eval_count)
            history['parameters'].append(parameters)
            history['mean'].append(mean)
            history['std'].append(std)

        backend = BasicAer.get_backend('qasm_simulator')
        optimizer = COBYLA(maxiter=3)
        wavefunction = self.ry_wavefunction[mode]

        if mode == 'wrapped':
            warnings.filterwarnings('ignore', category=DeprecationWarning)
        vqe = VQE(self.qubit_op, wavefunction, optimizer, callback=store_intermediate_result)
        if mode == 'wrapped':
            warnings.filterwarnings('always', category=DeprecationWarning)

        quantum_instance = QuantumInstance(backend,
                                           seed_transpiler=50,
                                           shots=1024,
                                           seed_simulator=50)
        vqe.run(quantum_instance)

        self.assertTrue(all(isinstance(count, int) for count in history['eval_count']))
        self.assertTrue(all(isinstance(mean, float) for mean in history['mean']))
        self.assertTrue(all(isinstance(std, float) for std in history['std']))
        for params in history['parameters']:
            self.assertTrue(all(isinstance(param, float) for param in params))

    def test_vqe_reuse(self):
        """ Test vqe reuse """
        vqe = VQE()
        with self.assertRaises(AquaError):
            _ = vqe.run()

        var_form = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')
        vqe.var_form = var_form
        with self.assertRaises(AquaError):
            _ = vqe.run()

        vqe.operator = self.qubit_op
        with self.assertRaises(AquaError):
            _ = vqe.run()

        qinst = QuantumInstance(BasicAer.get_backend('statevector_simulator'))
        vqe.quantum_instance = qinst
        result = vqe.run()
        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=5)

        operator = PrimitiveOp(np.array([[1, 0, 0, 0],
                                         [0, -1, 0, 0],
                                         [0, 0, 2, 0],
                                         [0, 0, 0, 3]]))
        vqe.operator = operator
        result = vqe.run()
        self.assertAlmostEqual(result.eigenvalue.real, -1.0, places=5)

    def test_vqe_optimizer(self):
        """ Test running same VQE twice to re-use optimizer, then switch optimizer """
        vqe = VQE(self.qubit_op, optimizer=SLSQP(),
                  quantum_instance=QuantumInstance(BasicAer.get_backend('statevector_simulator')))

        def run_check():
            result = vqe.compute_minimum_eigenvalue()
            self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=5)

        run_check()

        with self.subTest('Optimizer re-use'):
            run_check()

        with self.subTest('Optimizer replace'):
            vqe.optimizer = L_BFGS_B()
            run_check()

    def test_vqe_mes(self):
        """ Test vqe minimum eigen solver interface """
        ansatz = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')
        vqe = VQE(var_form=ansatz, optimizer=COBYLA())
        vqe.set_backend(BasicAer.get_backend('statevector_simulator'))
        result = vqe.compute_minimum_eigenvalue(self.qubit_op)
        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=5)

    @unittest.skip(reason="IBMQ testing not available in general.")
    def test_ibmq_vqe(self):
        """ IBMQ VQE Test """
        from qiskit import IBMQ
        provider = IBMQ.load_account()
        backend = provider.get_backend('ibmq_qasm_simulator')
        ansatz = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')

        opt = SLSQP(maxiter=1)
        opt.set_max_evals_grouped(100)
        vqe = VQE(self.qubit_op, ansatz, SLSQP(maxiter=2))

        result = vqe.run(backend)
        print(result)
        self.assertAlmostEqual(result.eigenvalue.real, -1.85727503)
        np.testing.assert_array_almost_equal(result.eigenvalue.real, -1.85727503, 5)
        self.assertEqual(len(result.optimal_point), 16)
        self.assertIsNotNone(result.cost_function_evals)
        self.assertIsNotNone(result.optimizer_time)

if __name__ == '__main__':
    unittest.main()
<commit_msg>Remove deprecated VQ tests (Qiskit/qiskit-aqua#987)<commit_after># -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2018, 2020.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

""" Test VQE """

import unittest
import warnings
from test.aqua import QiskitAquaTestCase
import numpy as np
from ddt import ddt, unpack, data
from qiskit import BasicAer, QuantumCircuit
from qiskit.circuit.library import TwoLocal, EfficientSU2

from qiskit.aqua import QuantumInstance, aqua_globals, AquaError
from qiskit.aqua.operators import WeightedPauliOperator, PrimitiveOp, X, Z, I
from qiskit.aqua.components.variational_forms import RYRZ
from qiskit.aqua.components.optimizers import L_BFGS_B, COBYLA, SPSA, SLSQP
from qiskit.aqua.algorithms import VQE

@ddt
class TestVQE(QiskitAquaTestCase):
    """ Test VQE """

    def setUp(self):
        super().setUp()
        self.seed = 50
        aqua_globals.random_seed = self.seed
        self.h2_op = -1.052373245772859 * (I ^ I) \
            + 0.39793742484318045 * (I ^ Z) \
            - 0.39793742484318045 * (Z ^ I) \
            - 0.01128010425623538 * (Z ^ Z) \
            + 0.18093119978423156 * (X ^ X)
        self.h2_energy = -1.85727503

        self.ryrz_wavefunction = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')
        self.ry_wavefunction = TwoLocal(rotation_blocks='ry', entanglement_blocks='cz')

        self.qasm_simulator = QuantumInstance(BasicAer.get_backend('qasm_simulator'),
                                              shots=1024,
                                              seed_simulator=self.seed,
                                              seed_transpiler=self.seed)
        self.statevector_simulator = QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                                                     shots=1,
                                                     seed_simulator=self.seed,
                                                     seed_transpiler=self.seed)

    def test_basic_aer_statevector(self):
        """Test the VQE on BasicAer's statevector simulator."""
        wavefunction = self.ryrz_wavefunction
        vqe = VQE(self.h2_op, wavefunction, L_BFGS_B())

        result = vqe.run(QuantumInstance(BasicAer.get_backend('statevector_simulator'),
                                         basis_gates=['u1', 'u2', 'u3', 'cx', 'id'],
                                         coupling_map=[[0, 1]],
                                         seed_simulator=aqua_globals.random_seed,
                                         seed_transpiler=aqua_globals.random_seed))

        with self.subTest(msg='test eigenvalue'):
            self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy)

        with self.subTest(msg='test dimension of optimal point'):
            self.assertEqual(len(result.optimal_point), 16)

        with self.subTest(msg='assert cost_function_evals is set'):
            self.assertIsNotNone(result.cost_function_evals)

        with self.subTest(msg='assert optimizer_time is set'):
            self.assertIsNotNone(result.optimizer_time)

    def test_deprecated_variational_forms(self):
        """Test running the VQE on a deprecated VariationalForm object."""
        warnings.filterwarnings('ignore', category=DeprecationWarning)
        wavefunction = RYRZ(2)
        vqe = VQE(self.h2_op, wavefunction, L_BFGS_B())
        warnings.filterwarnings('always', category=DeprecationWarning)
        result = vqe.run(self.statevector_simulator)
        self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy)

    def test_circuit_input(self):
        """Test running the VQE on a plain QuantumCircuit object."""
        wavefunction = QuantumCircuit(2).compose(EfficientSU2(2))
        optimizer = SLSQP(maxiter=50)
        vqe = VQE(self.h2_op, wavefunction, optimizer=optimizer)
        result = vqe.run(self.statevector_simulator)
        self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5)

    def test_legacy_operator(self):
        """Test the VQE accepts and converts the legacy WeightedPauliOperator."""
        pauli_dict = {
            'paulis': [{"coeff": {"imag": 0.0, "real": -1.052373245772859}, "label": "II"},
                       {"coeff": {"imag": 0.0, "real": 0.39793742484318045}, "label": "IZ"},
                       {"coeff": {"imag": 0.0, "real": -0.39793742484318045}, "label": "ZI"},
                       {"coeff": {"imag": 0.0, "real": -0.01128010425623538}, "label": "ZZ"},
                       {"coeff": {"imag": 0.0, "real": 0.18093119978423156}, "label": "XX"}
                       ]
        }
        h2_op = WeightedPauliOperator.from_dict(pauli_dict)
        vqe = VQE(h2_op)
        self.assertEqual(vqe.operator, self.h2_op)

    def test_missing_varform_params(self):
        """Test specifying a variational form with no parameters raises an error."""
        circuit = QuantumCircuit(self.h2_op.num_qubits)
        vqe = VQE(self.h2_op, circuit)
        with self.assertRaises(RuntimeError):
            vqe.run(BasicAer.get_backend('statevector_simulator'))

    @data(
        (SLSQP(maxiter=50), 5, 4),
        (SPSA(max_trials=150), 3, 2),  # max_evals_grouped=n or =2 if n>2
    )
    @unpack
    def test_max_evals_grouped(self, optimizer, places, max_evals_grouped):
        """ VQE Optimizers test """
        vqe = VQE(self.h2_op, self.ryrz_wavefunction, optimizer,
                  max_evals_grouped=max_evals_grouped,
                  quantum_instance=self.statevector_simulator)
        result = vqe.run()
        self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=places)

    def test_basic_aer_qasm(self):
        """Test the VQE on BasicAer's QASM simulator."""
        optimizer = SPSA(max_trials=300, last_avg=5)
        wavefunction = self.ry_wavefunction

        vqe = VQE(self.h2_op, wavefunction, optimizer, max_evals_grouped=1)

        # TODO benchmark this later.
        result = vqe.run(self.qasm_simulator)
        self.assertAlmostEqual(result.eigenvalue.real, -1.86823, places=2)

    def test_statevector_snapshot_mode(self):
        """Test the VQE using Aer's statevector_simulator snapshot mode."""
        try:
            # pylint: disable=import-outside-toplevel
            from qiskit import Aer
        except Exception as ex:  # pylint: disable=broad-except
            self.skipTest("Aer doesn't appear to be installed. Error: '{}'".format(str(ex)))
            return
        backend = Aer.get_backend('statevector_simulator')
        wavefunction = self.ry_wavefunction
        optimizer = L_BFGS_B()

        vqe = VQE(self.h2_op, wavefunction, optimizer, max_evals_grouped=1)

        quantum_instance = QuantumInstance(backend,
                                           seed_simulator=aqua_globals.random_seed,
                                           seed_transpiler=aqua_globals.random_seed)
        result = vqe.run(quantum_instance)
        self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=6)

    def test_qasm_snapshot_mode(self):
        """Test the VQE using Aer's qasm_simulator snapshot mode."""
        try:
            # pylint: disable=import-outside-toplevel
            from qiskit import Aer
        except Exception as ex:  # pylint: disable=broad-except
            self.skipTest("Aer doesn't appear to be installed. Error: '{}'".format(str(ex)))
            return
        backend = Aer.get_backend('qasm_simulator')
        optimizer = L_BFGS_B()
        wavefunction = self.ry_wavefunction

        vqe = VQE(self.h2_op, wavefunction, optimizer, max_evals_grouped=1)

        quantum_instance = QuantumInstance(backend, shots=1,
                                           seed_simulator=aqua_globals.random_seed,
                                           seed_transpiler=aqua_globals.random_seed)
        result = vqe.run(quantum_instance)
        self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=6)

    def test_callback(self):
        """Test the callback on VQE."""
        history = {'eval_count': [], 'parameters': [], 'mean': [], 'std': []}

        def store_intermediate_result(eval_count, parameters, mean, std):
            history['eval_count'].append(eval_count)
            history['parameters'].append(parameters)
            history['mean'].append(mean)
            history['std'].append(std)

        optimizer = COBYLA(maxiter=3)
        wavefunction = self.ry_wavefunction

        vqe = VQE(self.h2_op, wavefunction, optimizer, callback=store_intermediate_result)
        vqe.run(self.qasm_simulator)

        self.assertTrue(all(isinstance(count, int) for count in history['eval_count']))
        self.assertTrue(all(isinstance(mean, float) for mean in history['mean']))
        self.assertTrue(all(isinstance(std, float) for std in history['std']))
        for params in history['parameters']:
            self.assertTrue(all(isinstance(param, float) for param in params))

    def test_reuse(self):
        """Test re-using a VQE algorithm instance."""
        vqe = VQE()
        with self.subTest(msg='assert running empty raises AquaError'):
            with self.assertRaises(AquaError):
                _ = vqe.run()

        var_form = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')
        vqe.var_form = var_form
        with self.subTest(msg='assert missing operator raises AquaError'):
            with self.assertRaises(AquaError):
                _ = vqe.run()

        vqe.operator = self.h2_op
        with self.subTest(msg='assert missing backend raises AquaError'):
            with self.assertRaises(AquaError):
                _ = vqe.run()

        vqe.quantum_instance = self.statevector_simulator
        with self.subTest(msg='assert VQE works once all info is available'):
            result = vqe.run()
            self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy, places=5)

        operator = PrimitiveOp(np.array([[1, 0, 0, 0],
                                         [0, -1, 0, 0],
                                         [0, 0, 2, 0],
                                         [0, 0, 0, 3]]))

        with self.subTest(msg='assert minimum eigensolver interface works'):
            result = vqe.compute_minimum_eigenvalue(operator)
            self.assertAlmostEqual(result.eigenvalue.real, -1.0, places=5)

    def test_vqe_optimizer(self):
        """ Test running same VQE twice to re-use optimizer, then switch optimizer """
        vqe = VQE(self.h2_op, optimizer=SLSQP(),
                  quantum_instance=QuantumInstance(BasicAer.get_backend('statevector_simulator')))

        def run_check():
            result = vqe.compute_minimum_eigenvalue()
            self.assertAlmostEqual(result.eigenvalue.real, -1.85727503, places=5)

        run_check()

        with self.subTest('Optimizer re-use'):
            run_check()

        with self.subTest('Optimizer replace'):
            vqe.optimizer = L_BFGS_B()
            run_check()

    @unittest.skip(reason="IBMQ testing not available in general.")
    def test_ibmq(self):
        """ IBMQ VQE Test """
        from qiskit import IBMQ
        provider = IBMQ.load_account()
        backend = provider.get_backend('ibmq_qasm_simulator')
        ansatz = TwoLocal(rotation_blocks=['ry', 'rz'], entanglement_blocks='cz')

        opt = SLSQP(maxiter=1)
        opt.set_max_evals_grouped(100)
        vqe = VQE(self.h2_op, ansatz, SLSQP(maxiter=2))

        result = vqe.run(backend)
        print(result)
        self.assertAlmostEqual(result.eigenvalue.real, self.h2_energy)
        np.testing.assert_array_almost_equal(result.eigenvalue.real, self.h2_energy, 5)
        self.assertEqual(len(result.optimal_point), 16)
        self.assertIsNotNone(result.cost_function_evals)
        self.assertIsNotNone(result.optimizer_time)

if __name__ == '__main__':
    unittest.main()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import copy

from django.core.exceptions import ImproperlyConfigured
from django.db.models import fields

from . import settings
from .models import Translation
from .utils.i18n import (get_cache_key,
                         get_language,
                         build_localized_field_name,
                         build_localized_verbose_name)

SUPPORTED_FIELDS = (
    fields.CharField,
    fields.TextField,
)

class TranslationFieldMixin(object):
    """
    Common logic for translated and translation fields.
    """

    def getter_cache(self, instance, field_name, original_field_name, language):
        if instance is None:
            raise AttributeError('Can only be accessed via instance')

        kwargs = dict(
            identifier=instance._linguist.identifier,
            object_id=instance.pk,
            language=language,
            field_name=original_field_name)

        cache_key = get_cache_key(**kwargs)
        if cache_key in instance._linguist:
            return instance._linguist[cache_key].field_value

        translation = Translation.objects.get_translation(**kwargs)

        if translation:
            instance._linguist[cache_key] = translation
            return translation.field_value

    def setter_cache(self, instance, field_name, original_field_name, language, value):
        if instance is None:
            raise AttributeError('Can only be accessed via instance')

        if not instance.pk:
            return

        kwargs = dict(
            identifier=instance._linguist.identifier,
            object_id=instance.pk,
            language=language,
            field_name=original_field_name,
            field_value=value)

        cache_kwargs = copy.copy(kwargs)
        del cache_kwargs['field_value']
        cache_key = get_cache_key(**cache_kwargs)

        obj, created = Translation.objects.set_translation(**kwargs)
        instance._linguist[cache_key] = obj

    def db_type(self, connection):
        """
        Returning None will cause Django to exclude this field from the concrete
        field list (``_meta.concrete_fields``) resulting in the fact that syncdb
        will skip this field when creating tables in PostgreSQL.
        """
        return None

class TranslationField(TranslationFieldMixin):
    """
    Translation field.
    """

    def __init__(self, field, language):
        self.field = field
        self.language = language
        self.attname = build_localized_field_name(self.field.name, language)
        self.name = self.attname
        self.verbose_name = build_localized_verbose_name(field.verbose_name, language)
        self.null = True
        self.blank = True
        self.column = None
        self.editable = True

    def __get__(self, instance, instance_type=None):
        return self.getter_cache(instance=instance,
                                 field_name=self.name,
                                 original_field_name=self.field.name,
                                 language=self.language)

    def __set__(self, instance, value):
        self.setter_cache(instance=instance,
                          field_name=self.name,
                          original_field_name=self.field.name,
                          language=self.language,
                          value=value)

class CacheDescriptor(dict):

    def __init__(self, identifier):
        self._identifier = identifier
        self['identifier'] = self._identifier
        self['language'] = get_language()

    @property
    def language(self):
        return self['language']

    @language.setter
    def language(self, value):
        self['language'] = value

    @property
    def identifier(self):
        return self['identifier']

    @identifier.setter
    def identifier(self, value):
        self['identifier'] = value

def contribute_to_model(translation_class):
    """
    Temporarly adds translation class to model to pass it to metaclass.
    """
    model.add_to_class('linguist_translation_class', translation_class)
<commit_msg>Remove obsolete TranslationFieldMixin and contribute_to_model function.<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

import copy

from django.core.exceptions import ImproperlyConfigured
from django.db.models import fields

from . import settings
from .models import Translation
from .utils.i18n import (get_cache_key,
                         get_language,
                         build_localized_field_name,
                         build_localized_verbose_name)

SUPPORTED_FIELDS = (
    fields.CharField,
    fields.TextField,
)

class TranslationField(TranslationFieldMixin):
    """
    Translation field.
    """

    def __init__(self, field, language):
        self.field = field
        self.language = language
        self.attname = build_localized_field_name(self.field.name, language)
        self.name = self.attname
        self.verbose_name = build_localized_verbose_name(field.verbose_name, language)
        self.null = True
        self.blank = True
        self.column = None
        self.editable = True

    def __get__(self, instance, instance_type=None):
        if instance is None:
            raise AttributeError('Can only be accessed via instance')

        kwargs = dict(
            identifier=instance._linguist.identifier,
            object_id=instance.pk,
            language=self.language,
            field_name=self.field.name)

        cache_key = get_cache_key(**kwargs)
        if cache_key in instance._linguist:
            return instance._linguist[cache_key].field_value

        translation = Translation.objects.get_translation(**kwargs)

        if translation:
            instance._linguist[cache_key] = translation
            return translation.field_value

    def __set__(self, instance, value):
        if instance is None:
            raise AttributeError('Can only be accessed via instance')

        kwargs = dict(
            identifier=instance._linguist.identifier,
            object_id=instance.pk,
            language=self.language,
            field_name=self.field.name,
            field_value=value)

        cache_kwargs = copy.copy(kwargs)
        del cache_kwargs['field_value']
        cache_key = get_cache_key(**cache_kwargs)

        obj, created = Translation.objects.set_translation(**kwargs)
        instance._linguist[cache_key] = obj

    def db_type(self, connection):
        """
        Returning None will cause Django to exclude this field from the concrete
        field list (``_meta.concrete_fields``) resulting in the fact that syncdb
        will skip this field when creating tables in PostgreSQL.
        """
        return None

class CacheDescriptor(dict):

    def __init__(self, identifier):
        self._identifier = identifier
        self['identifier'] = self._identifier
        self['language'] = get_language()

    @property
    def language(self):
        return self['language']

    @language.setter
    def language(self, value):
        self['language'] = value

    @property
    def identifier(self):
        return self['identifier']

    @identifier.setter
    def identifier(self, value):
        self['identifier'] = value
<|endoftext|>


<commit_before>import sys
sys.path.insert(0,'../')
import unittest
import os

import crawl
from crawl_test import FIXTURE_ROOT

class TestCrawl(unittest.TestCase):

	def new_crawl(self,callback=None):
		search_path = crawl.Crawl(FIXTURE_ROOT)
		search_path.append_paths("app/views","vendor/plugins/signal_id/app/views",".")
		search_path.append_extensions("builder","coffee","str",".erb")
		search_path.alias_extension('htm',"html")
		search_path.alias_extension('xhtml',"html")
		search_path.alias_extension('php',"html")
		search_path.alias_extension('coffee',"html")

		return callback(search_path) if callback else search_path

	def setUp(self):
		self.crawl = self.new_crawl()

	def fixture_path(self,path):
		return os.path.abspath(os.path.join(FIXTURE_ROOT,path))

	def testRoot(self):
		self.assertEqual(FIXTURE_ROOT,self.crawl.root)

	def testPaths(self):
		self.assertEqual(
			[
				self.fixture_path('app/views'),
				self.fixture_path('vendor/plugins/signal_id/app/views'),
				self.fixture_path('.')
			],
			self.crawl.paths
		)

	def testExtensions(self):
		self.assertEqual([".builder",".coffee",".str",".erb"],self.crawl.extensions)

	def testIndex(self):
		self.assertIsInstance(self.crawl.index(),crawl.index.Index)

	def testFindNonexistantFile(self):
		self.assertIsNone(self.crawl.find("people/show.html"))

	def testFindWithoutExtension(self):
		self.assertEqual(
				self.fixture_path("app/views/projects/index.html.erb"),
				self.crawl.find("projects/index.html")
			)<commit_msg>Added testFindWithExtension test<commit_after>import sys
sys.path.insert(0,'../')
import unittest
import os

import crawl
from crawl_test import FIXTURE_ROOT

class TestCrawl(unittest.TestCase):

	def new_crawl(self,callback=None):
		search_path = crawl.Crawl(FIXTURE_ROOT)
		search_path.append_paths("app/views","vendor/plugins/signal_id/app/views",".")
		search_path.append_extensions("builder","coffee","str",".erb")
		search_path.alias_extension('htm',"html")
		search_path.alias_extension('xhtml',"html")
		search_path.alias_extension('php',"html")
		search_path.alias_extension('coffee',"html")

		return callback(search_path) if callback else search_path

	def setUp(self):
		self.crawl = self.new_crawl()

	def fixture_path(self,path):
		return os.path.abspath(os.path.join(FIXTURE_ROOT,path))

	def testRoot(self):
		self.assertEqual(FIXTURE_ROOT,self.crawl.root)

	def testPaths(self):
		self.assertEqual(
			[
				self.fixture_path('app/views'),
				self.fixture_path('vendor/plugins/signal_id/app/views'),
				self.fixture_path('.')
			],
			self.crawl.paths
		)

	def testExtensions(self):
		self.assertEqual([".builder",".coffee",".str",".erb"],self.crawl.extensions)

	def testIndex(self):
		self.assertIsInstance(self.crawl.index(),crawl.index.Index)

	def testFindNonexistantFile(self):
		self.assertIsNone(self.crawl.find("people/show.html"))

	def testFindWithoutExtension(self):
		self.assertEqual(
				self.fixture_path("app/views/projects/index.html.erb"),
				self.crawl.find("projects/index.html")
			)

	def testFindWithExtension(self):
		self.assertEqual(
				self.fixture_path("app/views/projects/index.html.erb"),
				self.crawl.find("projects/index.html.erb")
			)<|endoftext|>


<commit_before>from nose2 import events, loader, session
from nose2.tests._common import TestCase

class TestPluggableTestLoader(TestCase):

    def setUp(self):
        self.session = session.Session()
        self.loader = loader.PluggableTestLoader(self.session)

    def test_load_from_module_calls_hook(self):
        self.session.hooks.register('loadTestsFromModule', FakePlugin())
        evt = events.LoadFromModuleEvent(self.loader, 'some_module')
        self.session.hooks.loadTestsFromModule(evt)
        self.assertTrue(evt.fakeLoadFromModule,
                        "FakePlugin.loadTestsFromModule() was not called")

    def test_load_from_name_calls_hook(self):
        self.session.hooks.register('loadTestsFromName', FakePlugin())
        evt = events.LoadFromNameEvent(self.loader,
                                       'some_name',
                                       'some_module')
        self.session.hooks.loadTestsFromName(evt)
        self.assertTrue(evt.fakeLoadFromName,
                        "FakePlugin.fakeLoadFromName() was not called")

    def test_load_from_names_calls_hook(self):
        self.session.hooks.register('loadTestsFromNames', FakePlugin())
        evt = events.LoadFromNamesEvent(self.loader,
                                        ['name1', 'name2'],
                                        'some_module')
        self.session.hooks.loadTestsFromNames(evt)
        self.assertTrue(evt.fakeLoadFromNames,
                        "FakePlugin.fakeLoadFromNames() was not called")

class FakePlugin(object):

    def loadTestsFromModule(self, event):
        event.fakeLoadFromModule = True

    def loadTestsFromName(self, event):
        event.fakeLoadFromName = True

    def loadTestsFromNames(self, event):
        event.fakeLoadFromNames = True
<commit_msg>Tests for PluggableTestLoader.<commit_after>from nose2 import events, loader, session
from nose2.tests._common import TestCase

class TestPluggableTestLoader(TestCase):

    def setUp(self):
        self.session = session.Session()
        self.loader = loader.PluggableTestLoader(self.session)

    def test_load_from_module_calls_hook(self):
        self.session.hooks.register('loadTestsFromModule', FakePlugin())
        evt = events.LoadFromModuleEvent(self.loader, 'some_module')
        self.session.hooks.loadTestsFromModule(evt)
        self.assertTrue(evt.fakeLoadFromModule,
                        "FakePlugin.loadTestsFromModule() was not called")

    def test_load_from_name_calls_hook(self):
        self.session.hooks.register('loadTestsFromName', FakePlugin())
        evt = events.LoadFromNameEvent(self.loader,
                                       'some_name',
                                       'some_module')
        self.session.hooks.loadTestsFromName(evt)
        self.assertTrue(evt.fakeLoadFromName,
                        "FakePlugin.fakeLoadFromName() was not called")

    def test_load_from_names_calls_hook(self):
        self.session.hooks.register('loadTestsFromNames', FakePlugin())
        evt = events.LoadFromNamesEvent(self.loader,
                                        ['some_name'],
                                        'some_module')
        self.session.hooks.loadTestsFromNames(evt)
        self.assertTrue(evt.fakeLoadFromNames,
                        "FakePlugin.fakeLoadFromNames() was not called")

    def test_loader_from_names_calls_names_hook(self):
        fake_plugin = FakePlugin()
        self.session.hooks.register('loadTestsFromNames', fake_plugin)
        self.loader.loadTestsFromNames(['some_name'])
        self.assertTrue(fake_plugin.fakeLoadFromNames)

    def test_loader_from_names_calls_name_hook(self):
        fake_plugin = FakePlugin()
        self.session.hooks.register('loadTestsFromName', fake_plugin)
        self.loader.loadTestsFromNames(['some_name'])
        self.assertTrue(fake_plugin.fakeLoadFromName)

class FakePlugin(object):

    def loadTestsFromModule(self, event):
        event.fakeLoadFromModule = True
        self.fakeLoadFromModule = True

    def loadTestsFromName(self, event):
        event.fakeLoadFromName = True
        self.fakeLoadFromName = True

    def loadTestsFromNames(self, event):
        event.fakeLoadFromNames = True
        self.fakeLoadFromNames = True<|endoftext|>


<commit_before>from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.views.generic import ListView
from django.forms import BaseModelFormSet
from django.forms import ModelForm
from django.forms import modelformset_factory, modelform_factory
from django import forms

from django.core.urlresolvers import reverse

from Degree360.models import FeedbackProvider, Survey, QuestionSection, Question, MultiChoiceAnswer, OpenAnswer
from _overlapped import NULL

def workInProgress(request):
    return HttpResponse("Generic view for Degree360. Work in progress.")

def requestFeedback(request, pk):
    feedbackProviders = FeedbackProvider.objects.all()
    context = {
        'pk': pk,
        'feedbackProviders': feedbackProviders
        }

    return render(request, 'Degree360/requestFeedback.html', context)

def _processQuestionSectionPost():
    return NULL

def questionSectionView(request, pk, email, section):
    if request.method == "POST":
        return HttpResponse("questionSectionView after post. Work in progress.")

    else:
        feedbackProvider = get_object_or_404(FeedbackProvider, survey=pk, email=email)

        multiChoiceAnswers = MultiChoiceAnswer.objects.filter(feedback_provider = feedbackProvider, question__section__description = section)

        form = modelformset_factory(MultiChoiceAnswer, form=MultiChoiceAnswerForm, extra=0)
        formSet = form(queryset = multiChoiceAnswers, )

        context = {
            'pk': pk,
            'section': section,
            'formSet': formSet,
            }

        return render(request, 'Degree360/questionSection.html', context)

def _processFeedbackProviderFormAndRedirect(form, pk):
    if form.is_valid():
        feedbackProvider = form.save(commit=False)
        feedbackProvider.survey = Survey.objects.get(id=pk)
        feedbackProvider.save()
        return HttpResponseRedirect(reverse('Degree360:requestFeedback', args=(pk,)))

def _renderFeedbackProviderTemplate(request, form, pk):
    template = 'Degree360/FeedbackProvider.html'
    context = {
        'form':form,
         'pk':pk
         }

    return render(request, template, context)

def addFeedbackProvider(request, pk):
    if request.method == "POST":
        form = FeedbackProviderForm(request.POST)
        _processFeedbackProviderFormAndRedirect(form, pk)
    else:
        form = FeedbackProviderForm()

    return _renderFeedbackProviderTemplate(request, form, pk)

def editFeedbackProvider(request, pk, email):
    feedbackProvider = get_object_or_404(FeedbackProvider, survey=pk, email=email)

    if request.method == "POST":
        form = FeedbackProviderForm(request.POST, instance=feedbackProvider)
        _processFeedbackProviderFormAndRedirect(form, pk)
    else:
        initial = {
            'name': feedbackProvider.name,
            'last_name': feedbackProvider.last_name,
            'email': feedbackProvider.email,
            'relation_type': feedbackProvider.relation_type
            }
        form = FeedbackProviderForm(initial)

    return _renderFeedbackProviderTemplate(request, form, pk)

class SurveyIndexView(ListView):
    template_name = 'Degree360/SurveyIndex.html'
    context_object_name = 'surveys_list'

    def get_queryset(self):
        return Survey.objects.all()

<commit_msg>removed _overlapped<commit_after>from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib import messages
from django.views.generic import ListView
from django.forms import BaseModelFormSet
from django.forms import ModelForm
from django.forms import modelformset_factory, modelform_factory
from django import forms

from django.core.urlresolvers import reverse

from Degree360.models import FeedbackProvider, Survey, QuestionSection, Question, MultiChoiceAnswer, OpenAnswer

def workInProgress(request):
    return HttpResponse("Generic view for Degree360. Work in progress.")

def requestFeedback(request, pk):
    feedbackProviders = FeedbackProvider.objects.all()
    context = {
        'pk': pk,
        'feedbackProviders': feedbackProviders
        }

    return render(request, 'Degree360/requestFeedback.html', context)

def questionSectionView(request, pk, email, section):
    if request.method == "POST":
        return HttpResponse("questionSectionView after post. Work in progress.")

    else:
        feedbackProvider = get_object_or_404(FeedbackProvider, survey=pk, email=email)

        multiChoiceAnswers = MultiChoiceAnswer.objects.filter(feedback_provider = feedbackProvider, question__section__description = section)

        form = modelformset_factory(MultiChoiceAnswer, form=MultiChoiceAnswerForm, extra=0)
        formSet = form(queryset = multiChoiceAnswers, )

        context = {
            'pk': pk,
            'section': section,
            'formSet': formSet,
            }

        return render(request, 'Degree360/questionSection.html', context)

def _processFeedbackProviderFormAndRedirect(form, pk):
    if form.is_valid():
        feedbackProvider = form.save(commit=False)
        feedbackProvider.survey = Survey.objects.get(id=pk)
        feedbackProvider.save()
        return HttpResponseRedirect(reverse('Degree360:requestFeedback', args=(pk,)))

def _renderFeedbackProviderTemplate(request, form, pk):
    template = 'Degree360/FeedbackProvider.html'
    context = {
        'form':form,
         'pk':pk
         }

    return render(request, template, context)

def addFeedbackProvider(request, pk):
    if request.method == "POST":
        form = FeedbackProviderForm(request.POST)
        _processFeedbackProviderFormAndRedirect(form, pk)
    else:
        form = FeedbackProviderForm()

    return _renderFeedbackProviderTemplate(request, form, pk)

def editFeedbackProvider(request, pk, email):
    feedbackProvider = get_object_or_404(FeedbackProvider, survey=pk, email=email)

    if request.method == "POST":
        form = FeedbackProviderForm(request.POST, instance=feedbackProvider)
        _processFeedbackProviderFormAndRedirect(form, pk)
    else:
        initial = {
            'name': feedbackProvider.name,
            'last_name': feedbackProvider.last_name,
            'email': feedbackProvider.email,
            'relation_type': feedbackProvider.relation_type
            }
        form = FeedbackProviderForm(initial)

    return _renderFeedbackProviderTemplate(request, form, pk)

class SurveyIndexView(ListView):
    template_name = 'Degree360/SurveyIndex.html'
    context_object_name = 'surveys_list'

    def get_queryset(self):
        return Survey.objects.all()

<|endoftext|>


<commit_before>#!/usr/bin/env python
# tuned-resonator
# experiments with google physical-web mdns broadcast

import logging
import tornado
from handlers.BaseHandler import BaseHandler

class BrowserHandler(BaseHandler):
    """HTML display of Keywords browsed in the last day"""

    def get(self):
        loader = tornado.template.Loader('../server/templates')
        n = self.get_argument('n', 3)
        db = self.settings['db']
        logging.debug('hit the BrowserHandler endpoint with n=', n)
        keywords = []
        found = 0
        while found < int(n):
            k = db.randomkey()
            if k not in keywords:
                keywords.append(k)
                found += 1
        self.write(loader.load("zen.html").generate(keywords=keywords))
        self.finish()
<commit_msg>cleanup<commit_after>#!/usr/bin/env python
# tuned-resonator
# experiments with google physical-web mdns broadcast

import logging
import tornado
from handlers.BaseHandler import BaseHandler

class BrowserHandler(BaseHandler):
    """HTML display of Keywords browsed in the last day"""

    def get(self):
        loader = tornado.template.Loader('../server/templates')
        n = self.get_argument('n', 3)
        db = self.settings['db']
        logging.debug('hit the BrowserHandler endpoint with n=', n)
        keywords = []
        found = 0
        while found < int(n):
            k = db.randomkey()
            if k is not None:
                if k not in keywords:
                    keywords.append(k)
                    found += 1
            else:
                keywords.append('no results yet')
        self.write(loader.load("zen.html").generate(keywords=keywords))
        self.finish()
<|endoftext|>