<commit_before># Copyright 2017 FiberHome Telecommunication Technologies CO.,LTD
# 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.

from oslo_serialization import jsonutils as json

from tempest.lib.services.volume.v3 import volumes_client
from tempest.tests.lib import fake_auth_provider
from tempest.tests.lib.services import base

class TestVolumesClient(base.BaseServiceTest):

    FAKE_VOLUME_SUMMARY = {
        "volume-summary": {
            "total_size": 20,
            "total_count": 5
        }
    }

    FAKE_VOLUME_METADATA_ITEM = {
        "meta": {
            "key1": "value1"
        }
    }

    FAKE_VOLUME_IMAGE_METADATA = {
        "metadata": {
            "container_format": "bare",
            "min_ram": "0",
            "disk_format": "raw",
            "image_name": "xly-ubuntu16-server",
            "image_id": "3e087b0c-10c5-4255-b147-6e8e9dbad6fc",
            "checksum": "008f5d22fe3cb825d714da79607a90f9",
            "min_disk": "0",
            "size": "8589934592"
        }
    }

    def setUp(self):
        super(TestVolumesClient, self).setUp()
        fake_auth = fake_auth_provider.FakeAuthProvider()
        self.client = volumes_client.VolumesClient(fake_auth,
                                                   'volume',
                                                   'regionOne')

    def _test_retype_volume(self, bytes_body=False):
        kwargs = {
            "new_type": "dedup-tier-replication",
            "migration_policy": "never"
        }

        self.check_service_client_function(
            self.client.retype_volume,
            'tempest.lib.common.rest_client.RestClient.post',
            {},
            to_utf=bytes_body,
            status=202,
            volume_id="a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8",
            **kwargs
        )

    def _test_force_detach_volume(self, bytes_body=False):
        kwargs = {
            'attachment_id': '6980e295-920f-412e-b189-05c50d605acd',
            'connector': {
                'initiator': 'iqn.2017-04.org.fake:01'
            }
        }

        self.check_service_client_function(
            self.client.force_detach_volume,
            'tempest.lib.common.rest_client.RestClient.post',
            {},
            to_utf=bytes_body,
            status=202,
            volume_id="a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8",
            **kwargs
        )

    def _test_show_volume_metadata_item(self, bytes_body=False):
        self.check_service_client_function(
            self.client.show_volume_metadata_item,
            'tempest.lib.common.rest_client.RestClient.get',
            self.FAKE_VOLUME_METADATA_ITEM,
            to_utf=bytes_body,
            volume_id="a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8",
            id="key1")

    def _test_show_volume_image_metadata(self, bytes_body=False):
        fake_volume_id = "a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8"
        self.check_service_client_function(
            self.client.show_volume_image_metadata,
            'tempest.lib.common.rest_client.RestClient.post',
            self.FAKE_VOLUME_IMAGE_METADATA,
            to_utf=bytes_body,
            mock_args=['volumes/%s/action' % fake_volume_id,
                       json.dumps({"os-show_image_metadata": {}})],
            volume_id=fake_volume_id)

    def _test_show_volume_summary(self, bytes_body=False):
        self.check_service_client_function(
            self.client.show_volume_summary,
            'tempest.lib.common.rest_client.RestClient.get',
            self.FAKE_VOLUME_SUMMARY,
            bytes_body)

    def test_force_detach_volume_with_str_body(self):
        self._test_force_detach_volume()

    def test_force_detach_volume_with_bytes_body(self):
        self._test_force_detach_volume(bytes_body=True)

    def test_show_volume_metadata_item_with_str_body(self):
        self._test_show_volume_metadata_item()

    def test_show_volume_metadata_item_with_bytes_body(self):
        self._test_show_volume_metadata_item(bytes_body=True)

    def test_show_volume_image_metadata_with_str_body(self):
        self._test_show_volume_image_metadata()

    def test_show_volume_image_metadata_with_bytes_body(self):
        self._test_show_volume_image_metadata(bytes_body=True)

    def test_retype_volume_with_str_body(self):
        self._test_retype_volume()

    def test_retype_volume_with_bytes_body(self):
        self._test_retype_volume(bytes_body=True)

    def test_show_volume_summary_with_str_body(self):
        self._test_show_volume_summary()

    def test_show_volume_summary_with_bytes_body(self):
        self._test_show_volume_summary(bytes_body=True)
<commit_msg>Modify the fake date<commit_after># Copyright 2017 FiberHome Telecommunication Technologies CO.,LTD
# 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.

from oslo_serialization import jsonutils as json

from tempest.lib.services.volume.v3 import volumes_client
from tempest.tests.lib import fake_auth_provider
from tempest.tests.lib.services import base

class TestVolumesClient(base.BaseServiceTest):

    FAKE_VOLUME_SUMMARY = {
        "volume-summary": {
            "total_size": 4,
            "total_count": 4,
            "metadata": {
                "key1": ["value1", "value2"],
                "key2": ["value2"]
            }
        }
    }

    FAKE_VOLUME_METADATA_ITEM = {
        "meta": {
            "name": "metadata1"
        }
    }

    FAKE_VOLUME_IMAGE_METADATA = {
        "metadata": {
            "key1": "value1",
            "key2": "value2"
        }
    }

    def setUp(self):
        super(TestVolumesClient, self).setUp()
        fake_auth = fake_auth_provider.FakeAuthProvider()
        self.client = volumes_client.VolumesClient(fake_auth,
                                                   'volume',
                                                   'regionOne')

    def _test_retype_volume(self, bytes_body=False):
        kwargs = {
            "new_type": "dedup-tier-replication",
            "migration_policy": "never"
        }

        self.check_service_client_function(
            self.client.retype_volume,
            'tempest.lib.common.rest_client.RestClient.post',
            {},
            to_utf=bytes_body,
            status=202,
            volume_id="a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8",
            **kwargs
        )

    def _test_force_detach_volume(self, bytes_body=False):
        kwargs = {
            'attachment_id': '6980e295-920f-412e-b189-05c50d605acd',
            'connector': {
                'initiator': 'iqn.2017-04.org.fake:01'
            }
        }

        self.check_service_client_function(
            self.client.force_detach_volume,
            'tempest.lib.common.rest_client.RestClient.post',
            {},
            to_utf=bytes_body,
            status=202,
            volume_id="a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8",
            **kwargs
        )

    def _test_show_volume_metadata_item(self, bytes_body=False):
        self.check_service_client_function(
            self.client.show_volume_metadata_item,
            'tempest.lib.common.rest_client.RestClient.get',
            self.FAKE_VOLUME_METADATA_ITEM,
            to_utf=bytes_body,
            volume_id="a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8",
            id="key1")

    def _test_show_volume_image_metadata(self, bytes_body=False):
        fake_volume_id = "a3be971b-8de5-4bdf-bdb8-3d8eb0fb69f8"
        self.check_service_client_function(
            self.client.show_volume_image_metadata,
            'tempest.lib.common.rest_client.RestClient.post',
            self.FAKE_VOLUME_IMAGE_METADATA,
            to_utf=bytes_body,
            mock_args=['volumes/%s/action' % fake_volume_id,
                       json.dumps({"os-show_image_metadata": {}})],
            volume_id=fake_volume_id)

    def _test_show_volume_summary(self, bytes_body=False):
        self.check_service_client_function(
            self.client.show_volume_summary,
            'tempest.lib.common.rest_client.RestClient.get',
            self.FAKE_VOLUME_SUMMARY,
            bytes_body)

    def test_force_detach_volume_with_str_body(self):
        self._test_force_detach_volume()

    def test_force_detach_volume_with_bytes_body(self):
        self._test_force_detach_volume(bytes_body=True)

    def test_show_volume_metadata_item_with_str_body(self):
        self._test_show_volume_metadata_item()

    def test_show_volume_metadata_item_with_bytes_body(self):
        self._test_show_volume_metadata_item(bytes_body=True)

    def test_show_volume_image_metadata_with_str_body(self):
        self._test_show_volume_image_metadata()

    def test_show_volume_image_metadata_with_bytes_body(self):
        self._test_show_volume_image_metadata(bytes_body=True)

    def test_retype_volume_with_str_body(self):
        self._test_retype_volume()

    def test_retype_volume_with_bytes_body(self):
        self._test_retype_volume(bytes_body=True)

    def test_show_volume_summary_with_str_body(self):
        self._test_show_volume_summary()

    def test_show_volume_summary_with_bytes_body(self):
        self._test_show_volume_summary(bytes_body=True)
<|endoftext|>


<commit_before># Copyright 2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import collections
from concurrent import futures
import contextlib
import distutils.spawn
import errno
import importlib
import os
import os.path
import pkgutil
import shutil
import subprocess
import sys
import tempfile
import threading
import unittest

import grpc
from grpc_tools import protoc
from tests.unit.framework.common import test_constants

_MESSAGES_IMPORT = b'import "messages.proto";'
_SPLIT_NAMESPACE = b'package grpc_protoc_plugin.invocation_testing.split;'
_COMMON_NAMESPACE = b'package grpc_protoc_plugin.invocation_testing;'

@contextlib.contextmanager
def _system_path(path):
    old_system_path = sys.path[:]
    sys.path = sys.path[0:1] + path + sys.path[1:]
    yield
    sys.path = old_system_path

class DummySplitServicer(object):

    def __init__(self, request_class, response_class):
        self.request_class = request_class
        self.response_class = response_class

    def Call(self, request, context):
        return self.response_class()

class SeparateTestMixin(object):

    def testImportAttributes(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        pb2.Request
        pb2.Response
        if self.should_find_services_in_pb2:
            pb2.TestServiceServicer
        else:
            with self.assertRaises(AttributeError):
                pb2.TestServiceServicer

        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        pb2_grpc.TestServiceServicer
        with self.assertRaises(AttributeError):
            pb2_grpc.Request
        with self.assertRaises(AttributeError):
            pb2_grpc.Response

    def testCall(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
        pb2_grpc.add_TestServiceServicer_to_server(
            DummySplitServicer(pb2.Request, pb2.Response), server)
        port = server.add_insecure_port('[::]:0')
        server.start()
        channel = grpc.insecure_channel('localhost:{}'.format(port))
        stub = pb2_grpc.TestServiceStub(channel)
        request = pb2.Request()
        expected_response = pb2.Response()
        response = stub.Call(request)
        self.assertEqual(expected_response, response)

class CommonTestMixin(object):

    def testImportAttributes(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        pb2.Request
        pb2.Response
        if self.should_find_services_in_pb2:
            pb2.TestServiceServicer
        else:
            with self.assertRaises(AttributeError):
                pb2.TestServiceServicer

        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        pb2_grpc.TestServiceServicer
        with self.assertRaises(AttributeError):
            pb2_grpc.Request
        with self.assertRaises(AttributeError):
            pb2_grpc.Response

    def testCall(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
        pb2_grpc.add_TestServiceServicer_to_server(
            DummySplitServicer(pb2.Request, pb2.Response), server)
        port = server.add_insecure_port('[::]:0')
        server.start()
        channel = grpc.insecure_channel('localhost:{}'.format(port))
        stub = pb2_grpc.TestServiceStub(channel)
        request = pb2.Request()
        expected_response = pb2.Response()
        response = stub.Call(request)
        self.assertEqual(expected_response, response)

class SameSeparateTest(unittest.TestCase, SeparateTestMixin):

    def setUp(self):
        same_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing', 'same.proto')
        self.directory = tempfile.mkdtemp(suffix='same_separate', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = os.path.join(self.directory,
                                                      'grpc_python_out')
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        os.makedirs(self.grpc_python_out_directory)
        same_proto_file = os.path.join(self.proto_directory,
                                       'same_separate.proto')
        open(same_proto_file, 'wb').write(
            same_proto_contents.replace(
                _COMMON_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.same_separate;'))
        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out=grpc_2_0:{}'.format(
                self.grpc_python_out_directory),
            same_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.grpc_python_out_directory, '__init__.py'),
             'w').write('')
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'same_separate_pb2'
        self.pb2_grpc_import = 'same_separate_pb2_grpc'
        self.should_find_services_in_pb2 = False

    def tearDown(self):
        shutil.rmtree(self.directory)

class SameCommonTest(unittest.TestCase, CommonTestMixin):

    def setUp(self):
        same_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing', 'same.proto')
        self.directory = tempfile.mkdtemp(suffix='same_common', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = self.python_out_directory
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        same_proto_file = os.path.join(self.proto_directory,
                                       'same_common.proto')
        open(same_proto_file, 'wb').write(
            same_proto_contents.replace(
                _COMMON_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.same_common;'))

        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out={}'.format(self.grpc_python_out_directory),
            same_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'same_common_pb2'
        self.pb2_grpc_import = 'same_common_pb2_grpc'
        self.should_find_services_in_pb2 = True

    def tearDown(self):
        shutil.rmtree(self.directory)

class SplitCommonTest(unittest.TestCase, CommonTestMixin):

    def setUp(self):
        services_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_services',
            'services.proto')
        messages_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_messages',
            'messages.proto')
        self.directory = tempfile.mkdtemp(suffix='split_common', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = self.python_out_directory
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        services_proto_file = os.path.join(self.proto_directory,
                                           'split_common_services.proto')
        messages_proto_file = os.path.join(self.proto_directory,
                                           'split_common_messages.proto')
        open(services_proto_file, 'wb').write(
            services_proto_contents.replace(
                _MESSAGES_IMPORT, b'import "split_common_messages.proto";')
            .replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_common;'))
        open(messages_proto_file, 'wb').write(
            messages_proto_contents.replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_common;'))
        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out={}'.format(self.grpc_python_out_directory),
            services_proto_file,
            messages_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'split_common_messages_pb2'
        self.pb2_grpc_import = 'split_common_services_pb2_grpc'
        self.should_find_services_in_pb2 = False

    def tearDown(self):
        shutil.rmtree(self.directory)

class SplitSeparateTest(unittest.TestCase, SeparateTestMixin):

    def setUp(self):
        services_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_services',
            'services.proto')
        messages_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_messages',
            'messages.proto')
        self.directory = tempfile.mkdtemp(suffix='split_separate', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = os.path.join(self.directory,
                                                      'grpc_python_out')
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        os.makedirs(self.grpc_python_out_directory)
        services_proto_file = os.path.join(self.proto_directory,
                                           'split_separate_services.proto')
        messages_proto_file = os.path.join(self.proto_directory,
                                           'split_separate_messages.proto')
        open(services_proto_file, 'wb').write(
            services_proto_contents.replace(
                _MESSAGES_IMPORT, b'import "split_separate_messages.proto";')
            .replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_separate;'
            ))
        open(messages_proto_file, 'wb').write(
            messages_proto_contents.replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_separate;'
            ))
        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out=grpc_2_0:{}'.format(
                self.grpc_python_out_directory),
            services_proto_file,
            messages_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'split_separate_messages_pb2'
        self.pb2_grpc_import = 'split_separate_services_pb2_grpc'
        self.should_find_services_in_pb2 = False

    def tearDown(self):
        shutil.rmtree(self.directory)

if __name__ == '__main__':
    unittest.main(verbosity=2)
<commit_msg>Skip split_definitions_test if PyPy<commit_after># Copyright 2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

import collections
from concurrent import futures
import contextlib
import distutils.spawn
import errno
import importlib
import os
import os.path
import pkgutil
import shutil
import subprocess
import sys
import tempfile
import threading
import unittest
import platform

import grpc
from grpc_tools import protoc
from tests.unit.framework.common import test_constants

_MESSAGES_IMPORT = b'import "messages.proto";'
_SPLIT_NAMESPACE = b'package grpc_protoc_plugin.invocation_testing.split;'
_COMMON_NAMESPACE = b'package grpc_protoc_plugin.invocation_testing;'

@contextlib.contextmanager
def _system_path(path):
    old_system_path = sys.path[:]
    sys.path = sys.path[0:1] + path + sys.path[1:]
    yield
    sys.path = old_system_path

class DummySplitServicer(object):

    def __init__(self, request_class, response_class):
        self.request_class = request_class
        self.response_class = response_class

    def Call(self, request, context):
        return self.response_class()

class SeparateTestMixin(object):

    def testImportAttributes(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        pb2.Request
        pb2.Response
        if self.should_find_services_in_pb2:
            pb2.TestServiceServicer
        else:
            with self.assertRaises(AttributeError):
                pb2.TestServiceServicer

        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        pb2_grpc.TestServiceServicer
        with self.assertRaises(AttributeError):
            pb2_grpc.Request
        with self.assertRaises(AttributeError):
            pb2_grpc.Response

    def testCall(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
        pb2_grpc.add_TestServiceServicer_to_server(
            DummySplitServicer(pb2.Request, pb2.Response), server)
        port = server.add_insecure_port('[::]:0')
        server.start()
        channel = grpc.insecure_channel('localhost:{}'.format(port))
        stub = pb2_grpc.TestServiceStub(channel)
        request = pb2.Request()
        expected_response = pb2.Response()
        response = stub.Call(request)
        self.assertEqual(expected_response, response)

class CommonTestMixin(object):

    def testImportAttributes(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        pb2.Request
        pb2.Response
        if self.should_find_services_in_pb2:
            pb2.TestServiceServicer
        else:
            with self.assertRaises(AttributeError):
                pb2.TestServiceServicer

        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        pb2_grpc.TestServiceServicer
        with self.assertRaises(AttributeError):
            pb2_grpc.Request
        with self.assertRaises(AttributeError):
            pb2_grpc.Response

    def testCall(self):
        with _system_path([self.python_out_directory]):
            pb2 = importlib.import_module(self.pb2_import)
        with _system_path([self.grpc_python_out_directory]):
            pb2_grpc = importlib.import_module(self.pb2_grpc_import)
        server = grpc.server(
            futures.ThreadPoolExecutor(max_workers=test_constants.POOL_SIZE))
        pb2_grpc.add_TestServiceServicer_to_server(
            DummySplitServicer(pb2.Request, pb2.Response), server)
        port = server.add_insecure_port('[::]:0')
        server.start()
        channel = grpc.insecure_channel('localhost:{}'.format(port))
        stub = pb2_grpc.TestServiceStub(channel)
        request = pb2.Request()
        expected_response = pb2.Response()
        response = stub.Call(request)
        self.assertEqual(expected_response, response)

@unittest.skipIf(platform.python_implementation() == "PyPy",
                 "Skip test if run with PyPy")
class SameSeparateTest(unittest.TestCase, SeparateTestMixin):

    def setUp(self):
        same_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing', 'same.proto')
        self.directory = tempfile.mkdtemp(suffix='same_separate', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = os.path.join(self.directory,
                                                      'grpc_python_out')
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        os.makedirs(self.grpc_python_out_directory)
        same_proto_file = os.path.join(self.proto_directory,
                                       'same_separate.proto')
        open(same_proto_file, 'wb').write(
            same_proto_contents.replace(
                _COMMON_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.same_separate;'))
        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out=grpc_2_0:{}'.format(
                self.grpc_python_out_directory),
            same_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.grpc_python_out_directory, '__init__.py'),
             'w').write('')
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'same_separate_pb2'
        self.pb2_grpc_import = 'same_separate_pb2_grpc'
        self.should_find_services_in_pb2 = False

    def tearDown(self):
        shutil.rmtree(self.directory)

@unittest.skipIf(platform.python_implementation() == "PyPy",
                 "Skip test if run with PyPy")
class SameCommonTest(unittest.TestCase, CommonTestMixin):

    def setUp(self):
        same_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing', 'same.proto')
        self.directory = tempfile.mkdtemp(suffix='same_common', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = self.python_out_directory
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        same_proto_file = os.path.join(self.proto_directory,
                                       'same_common.proto')
        open(same_proto_file, 'wb').write(
            same_proto_contents.replace(
                _COMMON_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.same_common;'))

        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out={}'.format(self.grpc_python_out_directory),
            same_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'same_common_pb2'
        self.pb2_grpc_import = 'same_common_pb2_grpc'
        self.should_find_services_in_pb2 = True

    def tearDown(self):
        shutil.rmtree(self.directory)

@unittest.skipIf(platform.python_implementation() == "PyPy",
                 "Skip test if run with PyPy")
class SplitCommonTest(unittest.TestCase, CommonTestMixin):

    def setUp(self):
        services_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_services',
            'services.proto')
        messages_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_messages',
            'messages.proto')
        self.directory = tempfile.mkdtemp(suffix='split_common', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = self.python_out_directory
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        services_proto_file = os.path.join(self.proto_directory,
                                           'split_common_services.proto')
        messages_proto_file = os.path.join(self.proto_directory,
                                           'split_common_messages.proto')
        open(services_proto_file, 'wb').write(
            services_proto_contents.replace(
                _MESSAGES_IMPORT, b'import "split_common_messages.proto";')
            .replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_common;'))
        open(messages_proto_file, 'wb').write(
            messages_proto_contents.replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_common;'))
        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out={}'.format(self.grpc_python_out_directory),
            services_proto_file,
            messages_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'split_common_messages_pb2'
        self.pb2_grpc_import = 'split_common_services_pb2_grpc'
        self.should_find_services_in_pb2 = False

    def tearDown(self):
        shutil.rmtree(self.directory)

@unittest.skipIf(platform.python_implementation() == "PyPy",
                 "Skip test if run with PyPy")
class SplitSeparateTest(unittest.TestCase, SeparateTestMixin):

    def setUp(self):
        services_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_services',
            'services.proto')
        messages_proto_contents = pkgutil.get_data(
            'tests.protoc_plugin.protos.invocation_testing.split_messages',
            'messages.proto')
        self.directory = tempfile.mkdtemp(suffix='split_separate', dir='.')
        self.proto_directory = os.path.join(self.directory, 'proto_path')
        self.python_out_directory = os.path.join(self.directory, 'python_out')
        self.grpc_python_out_directory = os.path.join(self.directory,
                                                      'grpc_python_out')
        os.makedirs(self.proto_directory)
        os.makedirs(self.python_out_directory)
        os.makedirs(self.grpc_python_out_directory)
        services_proto_file = os.path.join(self.proto_directory,
                                           'split_separate_services.proto')
        messages_proto_file = os.path.join(self.proto_directory,
                                           'split_separate_messages.proto')
        open(services_proto_file, 'wb').write(
            services_proto_contents.replace(
                _MESSAGES_IMPORT, b'import "split_separate_messages.proto";')
            .replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_separate;'
            ))
        open(messages_proto_file, 'wb').write(
            messages_proto_contents.replace(
                _SPLIT_NAMESPACE,
                b'package grpc_protoc_plugin.invocation_testing.split_separate;'
            ))
        protoc_result = protoc.main([
            '',
            '--proto_path={}'.format(self.proto_directory),
            '--python_out={}'.format(self.python_out_directory),
            '--grpc_python_out=grpc_2_0:{}'.format(
                self.grpc_python_out_directory),
            services_proto_file,
            messages_proto_file,
        ])
        if protoc_result != 0:
            raise Exception("unexpected protoc error")
        open(os.path.join(self.python_out_directory, '__init__.py'),
             'w').write('')
        self.pb2_import = 'split_separate_messages_pb2'
        self.pb2_grpc_import = 'split_separate_services_pb2_grpc'
        self.should_find_services_in_pb2 = False

    def tearDown(self):
        shutil.rmtree(self.directory)

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


<commit_before>import sys
import unittest
import numpy as np

from unittest.mock import MagicMock

from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtTest import QTest

# set up import paths
import path_prepare

import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary
import sas.qtgui.Utilities.GuiUtils as GuiUtils
from sas.qtgui.Plotting.PlotterData import Data1D

# Local
from sas.qtgui.Perspectives.Fitting.ConstraintWidget import ConstraintWidget
from sas.qtgui.Perspectives.Fitting.Constraint import Constraint
from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow
from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget

if not QtWidgets.QApplication.instance():
    app = QtWidgets.QApplication(sys.argv)

class ConstraintWidgetTest(unittest.TestCase):
    '''Test the ConstraintWidget dialog'''
    def setUp(self):
        '''Create ConstraintWidget dialog'''
        class dummy_manager(object):
            def communicator(self):
                return GuiUtils.Communicate()
            communicate = GuiUtils.Communicate()

        '''Create the perspective'''
        self.perspective = FittingWindow(dummy_manager())
        ConstraintWidget.updateSignalsFromTab = MagicMock()

        self.widget = ConstraintWidget(parent=self.perspective)

        # Example constraint object
        self.constraint1 = Constraint(parent=None, param="test", value="7.0", min="0.0", max="inf", func="M1:sld")
        self.constraint2 = Constraint(parent=None, param="poop", value="7.0", min="0.0", max="inf", func="7.0")

    def tearDown(self):
        '''Destroy the GUI'''
        self.widget.close()
        self.widget = None

    def testDefaults(self):
        '''Test the GUI in its default state'''
        self.assertIsInstance(self.widget, QtWidgets.QWidget)
        # Default title
        self.assertEqual(self.widget.windowTitle(), "Constrained and Simultaneous Fit")
        # Dicts
        self.assertIsInstance(self.widget.available_constraints, dict)
        self.assertIsInstance(self.widget.available_tabs, dict)
        # TableWidgets
        self.assertEqual(self.widget.tblTabList.columnCount(), 4)
        self.assertEqual(self.widget.tblConstraints.columnCount(), 1)
        # Data accept
        self.assertFalse(self.widget.acceptsData())
        # By default, the constraint table is disabled
        self.assertFalse(self.widget.tblConstraints.isEnabled())

    def testOnFitTypeChange(self):
        ''' test the single/batch fit switch '''
        self.widget.initializeFitList = MagicMock()
        # Assure current type is Single
        self.assertEqual(self.widget.currentType, "FitPage")
        # click on "batch"
        QTest.mouseClick(self.widget.btnBatch, QtCore.Qt.LeftButton)
        QtWidgets.QApplication.processEvents()
        # See what the current type is now
        self.assertEqual(self.widget.currentType, "BatchPage")
        # See if the list is getting initialized
        self.assertTrue(self.widget.initializeFitList.called)
        # Go back to single fit
        QTest.mouseClick(self.widget.btnSingle, QtCore.Qt.LeftButton)
        QtWidgets.QApplication.processEvents()
        # See what the current type is now
        self.assertEqual(self.widget.currentType, "FitPage")

    def testGetTabsForFit(self):
        ''' Test the fitting tab list '''
        self.assertEqual(self.widget.getTabsForFit(), [])
        # add one tab
        self.widget.tabs_for_fitting = {"foo": True}
        self.assertEqual(self.widget.getTabsForFit(), ['foo'])
        # add two tabs
        self.widget.tabs_for_fitting = {"foo": True, "bar": True}
        self.assertEqual(self.widget.getTabsForFit(), ['foo', 'bar'])
        # disable one tab
        self.widget.tabs_for_fitting = {"foo": False, "bar": True}
        self.assertEqual(self.widget.getTabsForFit(), ['bar'])

    def testIsTabImportable(self):
        ''' tab checks for consistency '''
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = None
        test_tab.data = self.constraint1
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)

        self.assertFalse(self.widget.isTabImportable(None))
        self.assertFalse(self.widget.isTabImportable("BatchTab1"))
        self.widget.currentType = "Batch"
        self.assertFalse(self.widget.isTabImportable("BatchTab"))
        self.widget.currentType = "test"
        self.assertFalse(self.widget.isTabImportable("test_tab"))
        test_tab.data_is_loaded = True
        self.assertTrue(self.widget.isTabImportable("test_tab"))

    def testOnTabCellEdit(self):
        ''' test what happens on monicker edit '''
        # Mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)
        self.widget.updateFitLine("test_tab")

        # disable the tab
        self.widget.tblTabList.item(0, 0).setCheckState(0)
        self.assertEqual(self.widget.tabs_for_fitting["test_tab"], False)
        self.assertFalse(self.widget.cmdFit.isEnabled())
        # enable the tab
        self.widget.tblTabList.item(0, 0).setCheckState(2)
        self.assertEqual(self.widget.tabs_for_fitting["test_tab"], True)
        self.assertTrue(self.widget.cmdFit.isEnabled())

    def testUpdateFitLine(self):
        ''' See if the fit table row can be updated '''
        # mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)

        # Add a tab without an constraint
        self.widget.updateFitLine("test_tab")
        self.assertEqual(self.widget.tblTabList.rowCount(), 1)
        # Constraint tab should be empty
        self.assertEqual(self.widget.tblConstraints.rowCount(), 0)

        # Add a second tab with an active constraint
        test_tab.getComplexConstraintsForModel = MagicMock(return_value=[('scale', self.constraint1.func)])
        test_tab.getFullConstraintNameListForModel = MagicMock(return_value=[('scale', self.constraint1.func)])
        test_tab.getConstraintObjectsForModel = MagicMock(return_value=[self.constraint1])
        self.widget.updateFitLine("test_tab")
        # We should have 2 tabs in the model tab
        self.assertEqual(self.widget.tblTabList.rowCount(), 2)
        # One constraint in the constraint tab
        self.assertEqual(self.widget.tblConstraints.rowCount(), 1)
        # Constraint should be active
        self.assertEqual(self.widget.tblConstraints.item(0, 0).checkState(), 2)
        # Check the text
        self.assertEqual(self.widget.tblConstraints.item(0, 0).text(),
                         test_tab.kernel_module.name +
                         ":" +
                         "scale" +
                         " = " +
                         self.constraint1.func)
        # Add a tab with a non active constraint
        test_tab.getComplexConstraintsForModel = MagicMock(return_value=[])
        self.widget.updateFitLine("test_tab")
        # There should be two constraints now
        self.assertEqual(self.widget.tblConstraints.rowCount(), 2)
        # Added constraint should not be checked since it isn't active
        self.assertEqual(self.widget.tblConstraints.item(1, 0).checkState(), 0)

    def testUpdateFitList(self):
        ''' see if the fit table can be updated '''
        # mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)

        # Fit button should be disabled if no tabs are present
        ObjectLibrary.listObjects =MagicMock(return_value=False)
        self.widget.initializeFitList()
        self.assertEqual(self.widget.available_tabs, {})
        self.assertEqual(self.widget.available_constraints, {})
        self.assertEqual(self.widget.tblConstraints.rowCount(), 0)
        self.assertEqual(self.widget.tblTabList.rowCount(), 0)
        self.assertFalse(self.widget.cmdFit.isEnabled())

        # Add a tab
        self.widget.isTabImportable = MagicMock(return_value=True)
        ObjectLibrary.listObjects = MagicMock(return_value=[test_tab])
        self.widget.updateFitLine = MagicMock()
        self.widget.updateSignalsFromTab = MagicMock()
        self.widget.initializeFitList()
        self.assertTrue(self.widget.updateFitLine.called_once)
        self.assertTrue(self.widget.updateSignalsFromTab.called_once)
        self.assertTrue(self.widget.cmdFit.isEnabled())

        # Check if the tab list gets ordered
        self.widget.isTabImportable = MagicMock(return_value=True)
        ObjectLibrary.listObjects = MagicMock(return_value=[test_tab])
        self.widget.updateFitLine = MagicMock()
        self.widget.updateSignalsFromTab = MagicMock()
        self.widget._row_order = [test_tab]
        self.widget.orderedSubList = MagicMock()
        self.widget.initializeFitList()
        self.assertTrue(self.widget.orderedSubList.called_once)

    def testUpdateConstraintList(self):
        ''' see if the constraint table can be updated '''
        pass

    def testFindNameErrorInConstraint(self):
        ''' test if we get get a faulty constraint'''
        constraint = "M1.scale = M2.scal + M2.background"
        name_error = "M2"
        expression = "P1.scale = M2.scal + M2.background"
        name = self.widget.findNameErrorInConstraint(constraint, expression, name_error)
        self.assertEqual("M2.scal", name)
<commit_msg>unit test for adding constraints<commit_after>import sys
import unittest
import numpy as np

from unittest.mock import MagicMock

from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5.QtTest import QTest

# set up import paths
import path_prepare

import sas.qtgui.Utilities.ObjectLibrary as ObjectLibrary
import sas.qtgui.Utilities.GuiUtils as GuiUtils
from sas.qtgui.Plotting.PlotterData import Data1D

# Local
from sas.qtgui.Perspectives.Fitting.ConstraintWidget import ConstraintWidget
from sas.qtgui.Perspectives.Fitting.Constraint import Constraint
from sas.qtgui.Perspectives.Fitting.FittingPerspective import FittingWindow
from sas.qtgui.Perspectives.Fitting.FittingWidget import FittingWidget

if not QtWidgets.QApplication.instance():
    app = QtWidgets.QApplication(sys.argv)

class ConstraintWidgetTest(unittest.TestCase):
    '''Test the ConstraintWidget dialog'''
    def setUp(self):
        '''Create ConstraintWidget dialog'''
        class dummy_manager(object):
            def communicator(self):
                return GuiUtils.Communicate()
            communicate = GuiUtils.Communicate()

        '''Create the perspective'''
        self.perspective = FittingWindow(dummy_manager())
        ConstraintWidget.updateSignalsFromTab = MagicMock()

        self.widget = ConstraintWidget(parent=self.perspective)

        # Example constraint object
        self.constraint1 = Constraint(parent=None, param="test", value="7.0", min="0.0", max="inf", func="M1:sld")
        self.constraint2 = Constraint(parent=None, param="poop", value="7.0", min="0.0", max="inf", func="7.0")

    def tearDown(self):
        '''Destroy the GUI'''
        self.widget.close()
        self.widget = None

    def testDefaults(self):
        '''Test the GUI in its default state'''
        self.assertIsInstance(self.widget, QtWidgets.QWidget)
        # Default title
        self.assertEqual(self.widget.windowTitle(), "Constrained and Simultaneous Fit")
        # Dicts
        self.assertIsInstance(self.widget.available_constraints, dict)
        self.assertIsInstance(self.widget.available_tabs, dict)
        # TableWidgets
        self.assertEqual(self.widget.tblTabList.columnCount(), 4)
        self.assertEqual(self.widget.tblConstraints.columnCount(), 1)
        # Data accept
        self.assertFalse(self.widget.acceptsData())
        # By default, the constraint table is disabled
        self.assertFalse(self.widget.tblConstraints.isEnabled())

    def testOnFitTypeChange(self):
        ''' test the single/batch fit switch '''
        self.widget.initializeFitList = MagicMock()
        # Assure current type is Single
        self.assertEqual(self.widget.currentType, "FitPage")
        # click on "batch"
        QTest.mouseClick(self.widget.btnBatch, QtCore.Qt.LeftButton)
        QtWidgets.QApplication.processEvents()
        # See what the current type is now
        self.assertEqual(self.widget.currentType, "BatchPage")
        # See if the list is getting initialized
        self.assertTrue(self.widget.initializeFitList.called)
        # Go back to single fit
        QTest.mouseClick(self.widget.btnSingle, QtCore.Qt.LeftButton)
        QtWidgets.QApplication.processEvents()
        # See what the current type is now
        self.assertEqual(self.widget.currentType, "FitPage")

    def testGetTabsForFit(self):
        ''' Test the fitting tab list '''
        self.assertEqual(self.widget.getTabsForFit(), [])
        # add one tab
        self.widget.tabs_for_fitting = {"foo": True}
        self.assertEqual(self.widget.getTabsForFit(), ['foo'])
        # add two tabs
        self.widget.tabs_for_fitting = {"foo": True, "bar": True}
        self.assertEqual(self.widget.getTabsForFit(), ['foo', 'bar'])
        # disable one tab
        self.widget.tabs_for_fitting = {"foo": False, "bar": True}
        self.assertEqual(self.widget.getTabsForFit(), ['bar'])

    def testIsTabImportable(self):
        ''' tab checks for consistency '''
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = None
        test_tab.data = self.constraint1
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)

        self.assertFalse(self.widget.isTabImportable(None))
        self.assertFalse(self.widget.isTabImportable("BatchTab1"))
        self.widget.currentType = "Batch"
        self.assertFalse(self.widget.isTabImportable("BatchTab"))
        self.widget.currentType = "test"
        self.assertFalse(self.widget.isTabImportable("test_tab"))
        test_tab.data_is_loaded = True
        self.assertTrue(self.widget.isTabImportable("test_tab"))

    def testOnTabCellEdit(self):
        ''' test what happens on monicker edit '''
        # Mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)
        self.widget.updateFitLine("test_tab")

        # disable the tab
        self.widget.tblTabList.item(0, 0).setCheckState(0)
        self.assertEqual(self.widget.tabs_for_fitting["test_tab"], False)
        self.assertFalse(self.widget.cmdFit.isEnabled())
        # enable the tab
        self.widget.tblTabList.item(0, 0).setCheckState(2)
        self.assertEqual(self.widget.tabs_for_fitting["test_tab"], True)
        self.assertTrue(self.widget.cmdFit.isEnabled())

    def testUpdateFitLine(self):
        ''' See if the fit table row can be updated '''
        # mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)

        # Add a tab without an constraint
        self.widget.updateFitLine("test_tab")
        self.assertEqual(self.widget.tblTabList.rowCount(), 1)
        # Constraint tab should be empty
        self.assertEqual(self.widget.tblConstraints.rowCount(), 0)

        # Add a second tab with an active constraint
        test_tab.getComplexConstraintsForModel = MagicMock(return_value=[('scale', self.constraint1.func)])
        test_tab.getFullConstraintNameListForModel = MagicMock(return_value=[('scale', self.constraint1.func)])
        test_tab.getConstraintObjectsForModel = MagicMock(return_value=[self.constraint1])
        self.widget.updateFitLine("test_tab")
        # We should have 2 tabs in the model tab
        self.assertEqual(self.widget.tblTabList.rowCount(), 2)
        # One constraint in the constraint tab
        self.assertEqual(self.widget.tblConstraints.rowCount(), 1)
        # Constraint should be active
        self.assertEqual(self.widget.tblConstraints.item(0, 0).checkState(), 2)
        # Check the text
        self.assertEqual(self.widget.tblConstraints.item(0, 0).text(),
                         test_tab.kernel_module.name +
                         ":" +
                         "scale" +
                         " = " +
                         self.constraint1.func)
        # Add a tab with a non active constraint
        test_tab.getComplexConstraintsForModel = MagicMock(return_value=[])
        self.widget.updateFitLine("test_tab")
        # There should be two constraints now
        self.assertEqual(self.widget.tblConstraints.rowCount(), 2)
        # Added constraint should not be checked since it isn't active
        self.assertEqual(self.widget.tblConstraints.item(1, 0).checkState(), 0)

    def testUpdateFitList(self):
        ''' see if the fit table can be updated '''
        # mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        ObjectLibrary.getObject = MagicMock(return_value=test_tab)

        # Fit button should be disabled if no tabs are present
        ObjectLibrary.listObjects =MagicMock(return_value=False)
        self.widget.initializeFitList()
        self.assertEqual(self.widget.available_tabs, {})
        self.assertEqual(self.widget.available_constraints, {})
        self.assertEqual(self.widget.tblConstraints.rowCount(), 0)
        self.assertEqual(self.widget.tblTabList.rowCount(), 0)
        self.assertFalse(self.widget.cmdFit.isEnabled())

        # Add a tab
        self.widget.isTabImportable = MagicMock(return_value=True)
        ObjectLibrary.listObjects = MagicMock(return_value=[test_tab])
        self.widget.updateFitLine = MagicMock()
        self.widget.updateSignalsFromTab = MagicMock()
        self.widget.initializeFitList()
        self.widget.updateFitLine.assert_called_once()
        self.widget.updateSignalsFromTab.assert_called_once()
        self.assertTrue(self.widget.cmdFit.isEnabled())

        # Check if the tab list gets ordered
        self.widget.isTabImportable = MagicMock(return_value=True)
        ObjectLibrary.listObjects = MagicMock(return_value=[test_tab])
        self.widget.updateFitLine = MagicMock()
        self.widget.updateSignalsFromTab = MagicMock()
        self.widget._row_order = [test_tab]
        self.widget.orderedSublist = MagicMock()
        self.widget.initializeFitList()
        self.widget.orderedSublist.assert_called_with([test_tab], [test_tab])

    def testUpdateConstraintList(self):
        ''' see if the constraint table can be updated '''
        # mock a tab
        test_tab = MagicMock(spec=FittingWidget)
        test_tab.data_is_loaded = False
        test_tab.kernel_module = MagicMock()
        test_tab.kernel_module.id = "foo"
        test_tab.kernel_module.name = "bar"
        test_tab.data.filename = "baz"
        self.widget.getObjectByName = MagicMock(return_value=test_tab)
        test_tab.addConstraintToRow = MagicMock()
        test_tab.changeCheckboxStatus = MagicMock()
        test_tab.getRowFromName = MagicMock(return_value=1)
        test_tab.changeCheckboxStatus = MagicMock()

        # add a constraint
        self.widget.onAcceptConstraint((test_tab, self.constraint1))
        test_tab.addConstraintToRow.assert_called_with(self.constraint1, 1)
        test_tab.changeCheckboxStatus.assert_called_with(1, True)

    def testFindNameErrorInConstraint(self):
        ''' test if we get get a faulty constraint'''
        constraint = "M1.scale = M2.scal + M2.background"
        name_error = "M2"
        expression = "P1.scale = M2.scal + M2.background"
        name = self.widget.findNameErrorInConstraint(constraint, expression, name_error)
        self.assertEqual("M2.scal", name)
<|endoftext|>


<commit_before>import time
import unittest
from unittest.mock import Mock

import stomp
from stomp import exception
from stomp.listener import TestListener
from stomp.test.testutils import *

class Test12Connect(unittest.TestCase):

    def setUp(self):
        conn = stomp.Connection12(get_default_host(), vhost=get_default_vhost())
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.start()
        conn.connect(get_default_user(), get_default_password(), wait=True)
        self.conn = conn
        self.listener = listener
        self.timestamp = time.strftime('%Y%m%d%H%M%S')

    def tearDown(self):
        if self.conn:
            self.conn.disconnect(receipt=None)

    def test_send(self):
        queuename = '/queue/testsend12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='auto')

        self.conn.send(body='this is a test using protocol 1.2', destination=queuename, receipt='123')

        self.listener.wait_on_receipt()

        self.assertTrue(self.listener.connections == 1, 'should have received 1 connection acknowledgement')
        self.assertTrue(self.listener.messages == 1, 'should have received 1 message')
        self.assertTrue(self.listener.errors == 0, 'should not have received any errors')

    def test_clientack(self):
        queuename = '/queue/testclientack12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='client-individual')

        self.conn.send(body='this is a test', destination=queuename, receipt='123')

        self.listener.wait_for_message()

        (headers, _) = self.listener.get_latest_message()

        ack_id = headers['ack']

        self.conn.ack(ack_id)

    def test_clientnack(self):
        queuename = '/queue/testclientnack12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='client-individual')

        self.conn.send(body='this is a test', destination=queuename, receipt='123')

        self.listener.wait_for_message()

        (headers, _) = self.listener.get_latest_message()

        ack_id = headers['ack']

        self.conn.nack(ack_id)

    def test_timeout(self):
        server = TestStompServer('127.0.0.1', 60000)
        try:
            server.start()

            server.add_frame('''ERROR
message: connection failed\x00''')

            conn = stomp.Connection12([('127.0.0.1', 60000)])
            listener = TestListener()
            conn.set_listener('', listener)
            conn.start()
            try:
                conn.connect(wait=True)
                self.fail("shouldn't happen")
            except exception.ConnectFailedException:
                pass
        finally:
            server.stop()

    def test_specialchars(self):
        queuename = '/queue/testspecialchars12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='client')

        hdrs = {
            'special-1': 'test with colon : test',
            'special-2': 'test with backslash \\ test',
            'special-3': 'test with newlines \n \n',
            'special-4': 'test with carriage return \r'
        }

        self.conn.send(body='this is a test', headers=hdrs, destination=queuename, receipt='123')

        self.listener.wait_on_receipt()

        (headers, _) = self.listener.get_latest_message()

        _ = headers['message-id']
        _ = headers['subscription']
        self.assertTrue('special-1' in headers)
        self.assertEqual('test with colon : test', headers['special-1'])
        self.assertTrue('special-2' in headers)
        self.assertEqual('test with backslash \\ test', headers['special-2'])
        self.assertTrue('special-3' in headers)
        self.assertEqual('test with newlines \n \n', headers['special-3'])
        self.assertTrue('special-4' in headers)
        self.assertEqual('test with carriage return \r', headers['special-4'])

    def test_suppress_content_length(self):
        queuename = '/queue/testspecialchars12-%s' % self.timestamp
        self.conn = stomp.Connection12(get_default_host(), vhost=get_default_vhost(), auto_content_length=False)
        self.conn.transport = Mock()

        self.conn.send(body='test', destination=queuename, receipt='123')

        args, kwargs = self.conn.transport.transmit.call_args
        frame = args[0]
        self.assertTrue('content-length' not in frame.headers)
<commit_msg>tests: Support mock in addition to unittest.mock in s12_test<commit_after>import time
import unittest
try:
    from unittest.mock import Mock
except ImportError:
    from mock import Mock

import stomp
from stomp import exception
from stomp.listener import TestListener
from stomp.test.testutils import *

class Test12Connect(unittest.TestCase):

    def setUp(self):
        conn = stomp.Connection12(get_default_host(), vhost=get_default_vhost())
        listener = TestListener('123')
        conn.set_listener('', listener)
        conn.start()
        conn.connect(get_default_user(), get_default_password(), wait=True)
        self.conn = conn
        self.listener = listener
        self.timestamp = time.strftime('%Y%m%d%H%M%S')

    def tearDown(self):
        if self.conn:
            self.conn.disconnect(receipt=None)

    def test_send(self):
        queuename = '/queue/testsend12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='auto')

        self.conn.send(body='this is a test using protocol 1.2', destination=queuename, receipt='123')

        self.listener.wait_on_receipt()

        self.assertTrue(self.listener.connections == 1, 'should have received 1 connection acknowledgement')
        self.assertTrue(self.listener.messages == 1, 'should have received 1 message')
        self.assertTrue(self.listener.errors == 0, 'should not have received any errors')

    def test_clientack(self):
        queuename = '/queue/testclientack12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='client-individual')

        self.conn.send(body='this is a test', destination=queuename, receipt='123')

        self.listener.wait_for_message()

        (headers, _) = self.listener.get_latest_message()

        ack_id = headers['ack']

        self.conn.ack(ack_id)

    def test_clientnack(self):
        queuename = '/queue/testclientnack12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='client-individual')

        self.conn.send(body='this is a test', destination=queuename, receipt='123')

        self.listener.wait_for_message()

        (headers, _) = self.listener.get_latest_message()

        ack_id = headers['ack']

        self.conn.nack(ack_id)

    def test_timeout(self):
        server = TestStompServer('127.0.0.1', 60000)
        try:
            server.start()

            server.add_frame('''ERROR
message: connection failed\x00''')

            conn = stomp.Connection12([('127.0.0.1', 60000)])
            listener = TestListener()
            conn.set_listener('', listener)
            conn.start()
            try:
                conn.connect(wait=True)
                self.fail("shouldn't happen")
            except exception.ConnectFailedException:
                pass
        finally:
            server.stop()

    def test_specialchars(self):
        queuename = '/queue/testspecialchars12-%s' % self.timestamp
        self.conn.subscribe(destination=queuename, id=1, ack='client')

        hdrs = {
            'special-1': 'test with colon : test',
            'special-2': 'test with backslash \\ test',
            'special-3': 'test with newlines \n \n',
            'special-4': 'test with carriage return \r'
        }

        self.conn.send(body='this is a test', headers=hdrs, destination=queuename, receipt='123')

        self.listener.wait_on_receipt()

        (headers, _) = self.listener.get_latest_message()

        _ = headers['message-id']
        _ = headers['subscription']
        self.assertTrue('special-1' in headers)
        self.assertEqual('test with colon : test', headers['special-1'])
        self.assertTrue('special-2' in headers)
        self.assertEqual('test with backslash \\ test', headers['special-2'])
        self.assertTrue('special-3' in headers)
        self.assertEqual('test with newlines \n \n', headers['special-3'])
        self.assertTrue('special-4' in headers)
        self.assertEqual('test with carriage return \r', headers['special-4'])

    def test_suppress_content_length(self):
        queuename = '/queue/testspecialchars12-%s' % self.timestamp
        self.conn = stomp.Connection12(get_default_host(), vhost=get_default_vhost(), auto_content_length=False)
        self.conn.transport = Mock()

        self.conn.send(body='test', destination=queuename, receipt='123')

        args, kwargs = self.conn.transport.transmit.call_args
        frame = args[0]
        self.assertTrue('content-length' not in frame.headers)
<|endoftext|>


<commit_before>import chainer
from chainer import functions as F
import numpy

import chainerx

from chainerx_tests import dtype_utils
from chainerx_tests import op_utils

_in_out_loss_dtypes = dtype_utils._permutate_dtype_mapping([
    (('float16', 'float16'), 'float16'),
    (('float32', 'float32'), 'float32'),
    (('float64', 'float64'), 'float64'),
    (('float32', 'float16'), 'float32'),
    (('float64', 'float16'), 'float64'),
    (('float64', 'float32'), 'float64'),
])

class LossBase(op_utils.ChainerOpTest):

    def generate_inputs(self):
        y = numpy.random.normal(loc=0, scale=1.0, size=self.shape)
        targ = numpy.random.normal(loc=0, scale=1.0, size=self.shape) + \
            numpy.random.normal(loc=0, scale=0.5, size=self.shape)
        return y, targ

    def forward_chainerx(self, inputs):
        return self.forward_xp(inputs, chainerx)

    def forward_chainer(self, inputs):
        return self.forward_xp(inputs, F)

    def forward_xp(self, inputs, xp):
        raise NotImplementedError(
            'Op test implementation must override `forward_xp`.')

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes)
    ])
))
class TestSquaredError(LossBase):

    def forward_xp(self, inputs, xp):
        x1, x2 = inputs
        return xp.squared_error(x1, x2),

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes)
    ])
))
class TestAbsoluteError(LossBase):

    # Absolute is non-differentiable at zero.
    dodge_nondifferentiable = True

    def forward_xp(self, inputs, xp):
        x1, x2 = inputs
        return xp.absolute_error(x1, x2),

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes)
    ])
))
class TestGaussianKLDivergence(LossBase):

    def forward_xp(self, inputs, xp):
        mean, ln_var = inputs
        if xp is chainerx:
            out = xp.gaussian_kl_divergence(mean, ln_var)
        else:
            out = xp.gaussian_kl_divergence(mean, ln_var, reduce='no')
        return out,

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes),
        chainer.testing.from_pytest_parameterize(
            'delta', [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5])
    ])
))
class TestHuberLoss(LossBase):

    # Absolute is non-differentiable at zero.
    dodge_nondifferentiable = True

    def forward_xp(self, inputs, xp):
        x, t = inputs
        if xp is chainerx:
            out = xp.huber_loss(x, t, self.delta)
        else:
            out = xp.huber_loss(x, t, self.delta, reduce='no')
        return out,
<commit_msg>Fix flaky chainerx.huber_loss<commit_after>import chainer
from chainer import functions as F
import numpy

import chainerx

from chainerx_tests import dtype_utils
from chainerx_tests import op_utils

_in_out_loss_dtypes = dtype_utils._permutate_dtype_mapping([
    (('float16', 'float16'), 'float16'),
    (('float32', 'float32'), 'float32'),
    (('float64', 'float64'), 'float64'),
    (('float32', 'float16'), 'float32'),
    (('float64', 'float16'), 'float64'),
    (('float64', 'float32'), 'float64'),
])

class LossBase(op_utils.ChainerOpTest):

    def generate_inputs(self):
        y = numpy.random.normal(loc=0, scale=1.0, size=self.shape)
        targ = numpy.random.normal(loc=0, scale=1.0, size=self.shape) + \
            numpy.random.normal(loc=0, scale=0.5, size=self.shape)
        return y, targ

    def forward_chainerx(self, inputs):
        return self.forward_xp(inputs, chainerx)

    def forward_chainer(self, inputs):
        return self.forward_xp(inputs, F)

    def forward_xp(self, inputs, xp):
        raise NotImplementedError(
            'Op test implementation must override `forward_xp`.')

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes)
    ])
))
class TestSquaredError(LossBase):

    def forward_xp(self, inputs, xp):
        x1, x2 = inputs
        return xp.squared_error(x1, x2),

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes)
    ])
))
class TestAbsoluteError(LossBase):

    # Absolute is non-differentiable at zero.
    dodge_nondifferentiable = True

    def forward_xp(self, inputs, xp):
        x1, x2 = inputs
        return xp.absolute_error(x1, x2),

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes)
    ])
))
class TestGaussianKLDivergence(LossBase):

    def forward_xp(self, inputs, xp):
        mean, ln_var = inputs
        if xp is chainerx:
            out = xp.gaussian_kl_divergence(mean, ln_var)
        else:
            out = xp.gaussian_kl_divergence(mean, ln_var, reduce='no')
        return out,

@op_utils.op_test(['native:0', 'cuda:0'])
@chainer.testing.parameterize(*(
    chainer.testing.product([
        chainer.testing.from_pytest_parameterize(
            'shape', [
                (2, 2),
                (3, 3, 3),
                (5, 5, 5),
                (4, 1, 2, 4)
            ]),
        chainer.testing.from_pytest_parameterize(
            'in_dtypes,out_dtype', _in_out_loss_dtypes),
        chainer.testing.from_pytest_parameterize(
            'delta', [0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5])
    ])
))
class TestHuberLoss(LossBase):

    # Absolute is non-differentiable at zero.
    dodge_nondifferentiable = True

    def generate_inputs(self):
        x, t = super().generate_inputs()
        mask = numpy.abs(numpy.abs(x - t) - self.delta) > 1e-3
        return x * mask, t * mask

    def forward_xp(self, inputs, xp):
        x, t = inputs
        if xp is chainerx:
            out = xp.huber_loss(x, t, self.delta)
        else:
            out = xp.huber_loss(x, t, self.delta, reduce='no')
        return out,
<|endoftext|>


<commit_before>from totalimpact.providers import provider
from totalimpact.providers.provider import Provider, ProviderContentMalformedError, ProviderServerError

import simplejson, re, os, random, string, urllib

import logging
logger = logging.getLogger('ti.providers.scopus')

class Scopus(Provider):

    example_id = ("doi", "10.1371/journal.pone.0000308")

    url = "http://www.info.sciverse.com/scopus/about"
    descr = "The world's largest abstract and citation database of peer-reviewed literature."
    # template urls below because they need a freshly-minted random string
    metrics_url_template = None
    provenance_url_template = "http://www.scopus.com/inward/record.url?partnerID=HzOxMe3b&scp=%s"

    static_meta_dict =  {
        "citations": {
            "display_name": "citations",
            "provider": "Scopus",
            "provider_url": "http://www.info.sciverse.com/scopus/about",
            "description": "Number of times the item has been cited",
            "icon": "http://www.info.sciverse.com/sites/all/themes/sciverse/favicon.ico" ,
        }
    }

    def __init__(self):
        super(Scopus, self).__init__()

    def is_relevant_alias(self, alias):
        (namespace, nid) = alias
        return (namespace in ["doi", "biblio"])

    def _get_json(self, fullpage):
        try:
            # extract json from inside the first and last parens
            # from http://codereview.stackexchange.com/questions/2561/converting-jsonp-to-json-is-this-regex-correct
            page = fullpage[ fullpage.index("(")+1 : fullpage.rindex(")") ]
        except (AttributeError, ValueError):
            raise ProviderContentMalformedError()

        data = provider._load_json(page)
        return(data)

    def _extract_metrics(self, record, status_code=200, id=None):
        try:
            citations = int(record["citedby-count"])
        except (KeyError, TypeError, ValueError):
            return {}

        if citations:
            metrics_dict = {"scopus:citations": citations}
        else:
            metrics_dict = {}
        return metrics_dict

    def _extract_provenance_url(self, record, status_code=200, id=None):
        try:
            api_url = record["prism:url"]
            match = re.findall("scopus_id:([\dA-Z]+)", api_url)
            scopus_id = match[0]
            provenance_url = self._get_templated_url(self.provenance_url_template, scopus_id)
        except (KeyError, TypeError):
            provenance_url = ""
        return provenance_url

    def _get_page(self, url, headers={}):
        response = self.http_get(url, headers=headers, timeout=30)
        if response.status_code != 200:
            if response.status_code == 404:
                return None
            else:
                raise(self._get_error(response.status_code, response))
        page = response.text
        if not page:
            raise ProviderContentMalformedError()
        return page

    def _extract_relevant_record(self, fullpage, id):
        data = provider._load_json(fullpage)
        response = None
        try:
            response = data["search-results"]["entry"][0]
        except (KeyError, ValueError):
            # not in Scopus database
            return None
        return response

    def _get_scopus_page(self, url):
        headers = {}
        headers["accept"] = "application/json"

        try:
            page = self._get_page(url, headers)
        except ProviderServerError:
            logger.info(u"error getting page with id {url}".format(url=url))
            return None

        if not page:
            logger.info(u"empty page with id {url}".format(url=url))
            return None
        if "Result set was empty" in page:
            #logger.warning(u"empty result set with doi {url}".format(url=url))
            return None
        return page

    def _get_relevant_record_with_doi(self, doi):
        # pick a new random string so don't time out.  Unfort, url now can't cache.
        random_string = "".join(random.sample(string.letters, 10))

        # this is how scopus wants us to escape special characters
        # http://help.scopus.com/Content/h_specialchars.htm
        # what scopus considers a special character and can appear in a DOI is a little unknown
        # but definitely includes parens.
        doi = doi.replace("(", "{(}")
        doi = doi.replace(")", "{)}")
        url_template = "https://api.elsevier.com/content/search/index:SCOPUS?query=DOI({doi})&field=citedby-count&apiKey="+os.environ["SCOPUS_KEY"]+"&insttoken="+os.environ["SCOPUS_INSTTOKEN"]
        url = url_template.format(doi=doi)

        page = self._get_scopus_page(url)

        if not page:
            return None  # empty result set

        relevant_record = self._extract_relevant_record(page, id)
        return relevant_record

    def _get_scopus_url(self, biblio_dict):
        url_template_one_journal = "https://api.elsevier.com/content/search/index:SCOPUS?query=AUTHOR-NAME({first_author})%20AND%20TITLE({title})%20AND%20SRCTITLE({journal})&field=citedby-count&apiKey="+os.environ["SCOPUS_KEY"]+"&insttoken="+os.environ["SCOPUS_INSTTOKEN"]
        url_template_two_journals = "https://api.elsevier.com/content/search/index:SCOPUS?query=AUTHOR-NAME({first_author})%20AND%20TITLE({title})%20AND%20(SRCTITLE({journal1})%20OR%20SRCTITLE({journal2}))&field=citedby-count&apiKey="+os.environ["SCOPUS_KEY"]+"&insttoken="+os.environ["SCOPUS_INSTTOKEN"]
        alt_journal_names = {"BMJ": "British Medical Journal"}

        title = biblio_dict["title"].replace("(", "{(}").replace(")", "{)}")
        journal = biblio_dict["journal"].replace("(", "{(}").replace(")", "{)}")

        if journal in alt_journal_names.keys():
            journal1 = journal
            journal2 = alt_journal_names[journal]
            url = url_template_two_journals.format(
                    first_author=urllib.quote(biblio_dict["first_author"]),
                    title=urllib.quote(title),
                    journal1=urllib.quote(journal1),
                    journal2=urllib.quote(journal2))
        else:
            url = url_template_one_journal.format(
                    first_author=urllib.quote(biblio_dict["first_author"]),
                    title=urllib.quote(title),
                    journal=urllib.quote(journal))
        return url

    def _get_relevant_record_with_biblio(self, biblio_dict):
        try:
            if not "first_author" in biblio_dict:
                biblio_dict["first_author"] = biblio_dict["authors"].split(" ")[0]
            if not biblio_dict["first_author"]:
                return None
            if not biblio_dict["title"] and biblio_dict["journal"] and biblio_dict["first_author"]:
                logger.debug("not enough info in _get_relevant_record_with_biblio to look up citations")
            url = self._get_scopus_url(biblio_dict)

        except KeyError:
            logger.debug("tried _get_relevant_record_with_biblio but leaving because KeyError")
            return None

        page = self._get_scopus_page(url)

        if not page:
            return None  # empty result set

        relevant_record = self._extract_relevant_record(page, biblio_dict)
        return relevant_record

    def _get_metrics_and_drilldown_from_metrics_page(self, provider_url_template, namespace, id):
        relevant_record = None
        if namespace=="doi":
            relevant_record = self._get_relevant_record_with_doi(id)
        elif namespace=="biblio":
            relevant_record = self._get_relevant_record_with_biblio(id)

        if not relevant_record:
            logger.info(u"no scopus page with id {id}".format(id=id))
            return {}

        metrics_dict = self._extract_metrics(relevant_record)

        metrics_and_drilldown = {}
        for metric_name in metrics_dict:
            drilldown_url = self._extract_provenance_url(relevant_record)
            metrics_and_drilldown[metric_name] = (metrics_dict[metric_name], drilldown_url)
        return metrics_and_drilldown

    def get_best_alias(self, aliases_dict):
        for namespace in ["doi", "biblio"]:
            if namespace in aliases_dict:
                return (namespace, aliases_dict[namespace][0])
        return (None, None)

    # custom, because uses doi if available, else biblio
    def metrics(self,
            aliases,
            provider_url_template=None,
            cache_enabled=True):

        aliases_dict = provider.alias_dict_from_tuples(aliases)

        metrics_and_drilldown = {}
        if "doi" in aliases_dict:
            nid = aliases_dict["doi"][0]
            metrics_and_drilldown = self._get_metrics_and_drilldown_from_metrics_page(provider_url_template,
                    namespace="doi",
                    id=nid)
        if not metrics_and_drilldown and "biblio" in aliases_dict:
            nid = aliases_dict["biblio"][0]
            metrics_and_drilldown = self._get_metrics_and_drilldown_from_metrics_page(provider_url_template,
                    namespace="biblio",
                    id=nid)

        return metrics_and_drilldown
<commit_msg>use AUTHLASTNAME instead of AUTHOR-NAME to look up citations<commit_after>from totalimpact.providers import provider
from totalimpact.providers.provider import Provider, ProviderContentMalformedError, ProviderServerError

import simplejson, re, os, random, string, urllib

import logging
logger = logging.getLogger('ti.providers.scopus')

class Scopus(Provider):

    example_id = ("doi", "10.1371/journal.pone.0000308")

    url = "http://www.info.sciverse.com/scopus/about"
    descr = "The world's largest abstract and citation database of peer-reviewed literature."
    # template urls below because they need a freshly-minted random string
    metrics_url_template = None
    provenance_url_template = "http://www.scopus.com/inward/record.url?partnerID=HzOxMe3b&scp=%s"

    static_meta_dict =  {
        "citations": {
            "display_name": "citations",
            "provider": "Scopus",
            "provider_url": "http://www.info.sciverse.com/scopus/about",
            "description": "Number of times the item has been cited",
            "icon": "http://www.info.sciverse.com/sites/all/themes/sciverse/favicon.ico" ,
        }
    }

    def __init__(self):
        super(Scopus, self).__init__()

    def is_relevant_alias(self, alias):
        (namespace, nid) = alias
        return (namespace in ["doi", "biblio"])

    def _get_json(self, fullpage):
        try:
            # extract json from inside the first and last parens
            # from http://codereview.stackexchange.com/questions/2561/converting-jsonp-to-json-is-this-regex-correct
            page = fullpage[ fullpage.index("(")+1 : fullpage.rindex(")") ]
        except (AttributeError, ValueError):
            raise ProviderContentMalformedError()

        data = provider._load_json(page)
        return(data)

    def _extract_metrics(self, record, status_code=200, id=None):
        try:
            citations = int(record["citedby-count"])
        except (KeyError, TypeError, ValueError):
            return {}

        if citations:
            metrics_dict = {"scopus:citations": citations}
        else:
            metrics_dict = {}
        return metrics_dict

    def _extract_provenance_url(self, record, status_code=200, id=None):
        try:
            api_url = record["prism:url"]
            match = re.findall("scopus_id:([\dA-Z]+)", api_url)
            scopus_id = match[0]
            provenance_url = self._get_templated_url(self.provenance_url_template, scopus_id)
        except (KeyError, TypeError):
            provenance_url = ""
        return provenance_url

    def _get_page(self, url, headers={}):
        response = self.http_get(url, headers=headers, timeout=30)
        if response.status_code != 200:
            if response.status_code == 404:
                return None
            else:
                raise(self._get_error(response.status_code, response))
        page = response.text
        if not page:
            raise ProviderContentMalformedError()
        return page

    def _extract_relevant_record(self, fullpage, id):
        data = provider._load_json(fullpage)
        response = None
        try:
            response = data["search-results"]["entry"][0]
        except (KeyError, ValueError):
            # not in Scopus database
            return None
        return response

    def _get_scopus_page(self, url):
        headers = {}
        headers["accept"] = "application/json"

        try:
            page = self._get_page(url, headers)
        except ProviderServerError:
            logger.info(u"error getting page with id {url}".format(url=url))
            return None

        if not page:
            logger.info(u"empty page with id {url}".format(url=url))
            return None
        if "Result set was empty" in page:
            #logger.warning(u"empty result set with doi {url}".format(url=url))
            return None
        return page

    def _get_relevant_record_with_doi(self, doi):
        # pick a new random string so don't time out.  Unfort, url now can't cache.
        random_string = "".join(random.sample(string.letters, 10))

        # this is how scopus wants us to escape special characters
        # http://help.scopus.com/Content/h_specialchars.htm
        # what scopus considers a special character and can appear in a DOI is a little unknown
        # but definitely includes parens.
        doi = doi.replace("(", "{(}")
        doi = doi.replace(")", "{)}")
        url_template = "https://api.elsevier.com/content/search/index:SCOPUS?query=DOI({doi})&field=citedby-count&apiKey="+os.environ["SCOPUS_KEY"]+"&insttoken="+os.environ["SCOPUS_INSTTOKEN"]
        url = url_template.format(doi=doi)

        page = self._get_scopus_page(url)

        if not page:
            return None  # empty result set

        relevant_record = self._extract_relevant_record(page, id)
        return relevant_record

    def _get_scopus_url(self, biblio_dict):
        url_template_one_journal = "https://api.elsevier.com/content/search/index:SCOPUS?query=AUTHLASTNAME({first_author})%20AND%20TITLE({title})%20AND%20SRCTITLE({journal})&field=citedby-count&apiKey="+os.environ["SCOPUS_KEY"]+"&insttoken="+os.environ["SCOPUS_INSTTOKEN"]
        url_template_two_journals = "https://api.elsevier.com/content/search/index:SCOPUS?query=AUTHLASTNAME({first_author})%20AND%20TITLE({title})%20AND%20(SRCTITLE({journal1})%20OR%20SRCTITLE({journal2}))&field=citedby-count&apiKey="+os.environ["SCOPUS_KEY"]+"&insttoken="+os.environ["SCOPUS_INSTTOKEN"]
        alt_journal_names = {"BMJ": "British Medical Journal"}

        title = biblio_dict["title"].replace("(", "{(}").replace(")", "{)}")
        journal = biblio_dict["journal"].replace("(", "{(}").replace(")", "{)}")

        if journal in alt_journal_names.keys():
            journal1 = journal
            journal2 = alt_journal_names[journal]
            url = url_template_two_journals.format(
                    first_author=urllib.quote(biblio_dict["first_author"]),
                    title=urllib.quote(title),
                    journal1=urllib.quote(journal1),
                    journal2=urllib.quote(journal2))
        else:
            url = url_template_one_journal.format(
                    first_author=urllib.quote(biblio_dict["first_author"]),
                    title=urllib.quote(title),
                    journal=urllib.quote(journal))
        return url

    def _get_relevant_record_with_biblio(self, biblio_dict):
        try:
            if not "first_author" in biblio_dict:
                biblio_dict["first_author"] = biblio_dict["authors"].split(" ")[0]
            if not biblio_dict["first_author"]:
                return None
            if not biblio_dict["title"] and biblio_dict["journal"] and biblio_dict["first_author"]:
                logger.debug("not enough info in _get_relevant_record_with_biblio to look up citations")
            url = self._get_scopus_url(biblio_dict)

        except KeyError:
            logger.debug("tried _get_relevant_record_with_biblio but leaving because KeyError")
            return None

        page = self._get_scopus_page(url)

        if not page:
            return None  # empty result set

        relevant_record = self._extract_relevant_record(page, biblio_dict)
        return relevant_record

    def _get_metrics_and_drilldown_from_metrics_page(self, provider_url_template, namespace, id):
        relevant_record = None
        if namespace=="doi":
            relevant_record = self._get_relevant_record_with_doi(id)
        elif namespace=="biblio":
            relevant_record = self._get_relevant_record_with_biblio(id)

        if not relevant_record:
            logger.info(u"no scopus page with id {id}".format(id=id))
            return {}

        metrics_dict = self._extract_metrics(relevant_record)

        metrics_and_drilldown = {}
        for metric_name in metrics_dict:
            drilldown_url = self._extract_provenance_url(relevant_record)
            metrics_and_drilldown[metric_name] = (metrics_dict[metric_name], drilldown_url)
        return metrics_and_drilldown

    def get_best_alias(self, aliases_dict):
        for namespace in ["doi", "biblio"]:
            if namespace in aliases_dict:
                return (namespace, aliases_dict[namespace][0])
        return (None, None)

    # custom, because uses doi if available, else biblio
    def metrics(self,
            aliases,
            provider_url_template=None,
            cache_enabled=True):

        aliases_dict = provider.alias_dict_from_tuples(aliases)

        metrics_and_drilldown = {}
        if "doi" in aliases_dict:
            nid = aliases_dict["doi"][0]
            metrics_and_drilldown = self._get_metrics_and_drilldown_from_metrics_page(provider_url_template,
                    namespace="doi",
                    id=nid)
        if not metrics_and_drilldown and "biblio" in aliases_dict:
            nid = aliases_dict["biblio"][0]
            metrics_and_drilldown = self._get_metrics_and_drilldown_from_metrics_page(provider_url_template,
                    namespace="biblio",
                    id=nid)

        return metrics_and_drilldown
<|endoftext|>


<commit_before>from future.moves.urllib.parse import urlencode
from future.moves.urllib.request import urlopen

class SlackRequest(object):
    def __init__(self):
        pass

    def do(self, token, request="?", post_data=None, domain="slack.com"):
        if post_data is None:
            post_data = {}
        post_data["token"] = token
        post_data = urlencode(post_data)
        url = 'https://{}/api/{}'.format(domain, request)
        return urlopen(url, post_data.encode('utf-8'))

<commit_msg>Make SlackRequest.do method static<commit_after>from future.moves.urllib.parse import urlencode
from future.moves.urllib.request import urlopen

class SlackRequest(object):
    def __init__(self):
        pass

    @staticmethod
    def do(token, request="?", post_data=None, domain="slack.com"):
        if post_data is None:
            post_data = {}
        post_data["token"] = token
        post_data = urlencode(post_data)
        url = 'https://{}/api/{}'.format(domain, request)
        return urlopen(url, post_data.encode('utf-8'))

<|endoftext|>