mock.Mock

Here are the examples of the python api mock.Mock taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

200 Examples 7

Example 1

Project: pypowervm
Source File: test_slot_map.py
View license
    def test_drop_vscsi_mappings(self):
        """Test drop_vscsi_mappings."""
        # Init objects to test with
        bstor = mock.Mock(stor.LU,
                          udid='274d7bb790666211e3bc1a00006cae8b01c96f59091'
                          '4bccbc8b7b88c37165c0485')
        mock_server_adapter = mock.Mock(lpar_slot_num=2)
        vscsimap = mock.Mock(backing_storage=bstor,
                             server_adapter=mock_server_adapter)
        smt = self.smt_impl('foo')
        smt._slot_topo = {
            2: {'LU': {'274d7bb790666211e3bc1a00006cae8b013842794fa0b8e9dd771'
                       'd6a32accde003': None,
                       '274d7bb790666211e3bc1a00006cae8b0148326cf1e5542c583ec'
                       '14327771522b0': None,
                       '274d7bb790666211e3bc1a00006cae8b01ac18997ab9bc23fb247'
                       '56e9713a93f90': None,
                       '274d7bb790666211e3bc1a00006cae8b01c96f590914bccbc8b7b'
                       '88c37165c0485': None},
                'PV': {'01M0lCTTIxNDUzMTI2MDA1MDc2ODAyODIwQTlEQTgwMDAwMDAwMDA'
                       'wNTJBOQ==': None},
                'VDisk': {'0300004c7a00007a00000001466c54110f.16': 0.125},
                'VOptMedia': {
                    '0evopt_19bbb46ad15747d79fe08f8464466144':
                        'vopt_19bbb46ad15747d79fe08f8464466144',
                    '0evopt_2c7aa01349714368a3d040bb0d613a67':
                        'vopt_2c7aa01349714368a3d040bb0d613a67',
                    '0evopt_2e51e8b4b9f04b159700e654b2436a01':
                        'vopt_2e51e8b4b9f04b159700e654b2436a01',
                    '0evopt_84d7bfcf44964f398e60254776b94d41':
                        'vopt_84d7bfcf44964f398e60254776b94d41',
                    '0evopt_de86c46e07004993b412c948bd5047c2':
                        'vopt_de86c46e07004993b412c948bd5047c2'}},
            3: {'VDisk': {'0300025d4a00007a000000014b36d9deaf.1': 60.0}}
        }

        # Remove a single LU entry and verify it was removed
        smt.drop_vscsi_mapping(vscsimap)
        self.assertEqual(
            {2: {'LU': {'274d7bb790666211e3bc1a00006cae8b013842794fa0b8e9dd771'
                        'd6a32accde003': None,
                        '274d7bb790666211e3bc1a00006cae8b0148326cf1e5542c583ec'
                        '14327771522b0': None,
                        '274d7bb790666211e3bc1a00006cae8b01ac18997ab9bc23fb247'
                        '56e9713a93f90': None},
                 'PV': {'01M0lCTTIxNDUzMTI2MDA1MDc2ODAyODIwQTlEQTgwMDAwMDAwMDA'
                        'wNTJBOQ==': None},
                 'VDisk': {'0300004c7a00007a00000001466c54110f.16': 0.125},
                 'VOptMedia': {
                     '0evopt_19bbb46ad15747d79fe08f8464466144':
                         'vopt_19bbb46ad15747d79fe08f8464466144',
                     '0evopt_2c7aa01349714368a3d040bb0d613a67':
                         'vopt_2c7aa01349714368a3d040bb0d613a67',
                     '0evopt_2e51e8b4b9f04b159700e654b2436a01':
                         'vopt_2e51e8b4b9f04b159700e654b2436a01',
                     '0evopt_84d7bfcf44964f398e60254776b94d41':
                         'vopt_84d7bfcf44964f398e60254776b94d41',
                     '0evopt_de86c46e07004993b412c948bd5047c2':
                         'vopt_de86c46e07004993b412c948bd5047c2'}},
             3: {'VDisk': {'0300025d4a00007a000000014b36d9deaf.1': 60.0}}},
            smt.topology)

        # Remove all other LPAR 2 LU entries and verify they are removed
        udids = ['274d7bb790666211e3bc1a00006cae8b013842794fa0b8e9dd771'
                 'd6a32accde003',
                 '274d7bb790666211e3bc1a00006cae8b0148326cf1e5542c583ec'
                 '14327771522b0',
                 '274d7bb790666211e3bc1a00006cae8b01ac18997ab9bc23fb247'
                 '56e9713a93f90']
        for udid in udids:
            bstor.udid = udid
            smt.drop_vscsi_mapping(vscsimap)
        self.assertEqual(
            {2: {'PV': {'01M0lCTTIxNDUzMTI2MDA1MDc2ODAyODIwQTlEQTgwMDAwMDAwMDA'
                        'wNTJBOQ==': None},
                 'VDisk': {'0300004c7a00007a00000001466c54110f.16': 0.125},
                 'VOptMedia': {
                     '0evopt_19bbb46ad15747d79fe08f8464466144':
                         'vopt_19bbb46ad15747d79fe08f8464466144',
                     '0evopt_2c7aa01349714368a3d040bb0d613a67':
                         'vopt_2c7aa01349714368a3d040bb0d613a67',
                     '0evopt_2e51e8b4b9f04b159700e654b2436a01':
                         'vopt_2e51e8b4b9f04b159700e654b2436a01',
                     '0evopt_84d7bfcf44964f398e60254776b94d41':
                         'vopt_84d7bfcf44964f398e60254776b94d41',
                     '0evopt_de86c46e07004993b412c948bd5047c2':
                         'vopt_de86c46e07004993b412c948bd5047c2'}},
             3: {'VDisk': {'0300025d4a00007a000000014b36d9deaf.1': 60.0}}},
            smt.topology)

Example 2

Project: pulp
Source File: mock_plugins.py
View license
def install():
    """
    Called during test setup to monkey patch the plugin loader for testing.
    """
    plugin_api._create_manager()

    plugin_api._MANAGER.importers.add_plugin('mock-importer', MockImporter, {})
    plugin_api._MANAGER.group_importers.add_plugin('mock-group-importer', MockGroupImporter, {})
    plugin_api._MANAGER.distributors.add_plugin('mock-distributor', MockDistributor, {})
    plugin_api._MANAGER.distributors.add_plugin('mock-distributor-2', MockDistributor, {})
    plugin_api._MANAGER.group_distributors.add_plugin('mock-group-distributor',
                                                      MockGroupDistributor, {})
    plugin_api._MANAGER.group_distributors.add_plugin('mock-group-distributor-2',
                                                      MockGroupDistributor, {})
    plugin_api._MANAGER.profilers.add_plugin('mock-profiler', MockProfiler, {})
    plugin_api._MANAGER.profilers.add_plugin('mock-rpm-profiler', MockRpmProfiler, {})

    # return mock instances instead of ephemeral ones
    # Save the state of the original plugin loader so it can be reverted
    global _ORIG_GET_DISTRIBUTOR_BY_ID
    global _ORIG_GET_GROUP_DISTRIBUTOR_BY_ID
    global _ORIG_GET_IMPORTER_BY_ID
    global _ORIG_GET_GROUP_IMPORTER_BY_ID
    global _ORIG_GET_PROFILER_BY_TYPE

    _ORIG_GET_DISTRIBUTOR_BY_ID = plugin_api.get_distributor_by_id
    _ORIG_GET_GROUP_DISTRIBUTOR_BY_ID = plugin_api.get_group_distributor_by_id
    _ORIG_GET_IMPORTER_BY_ID = plugin_api.get_importer_by_id
    _ORIG_GET_GROUP_IMPORTER_BY_ID = plugin_api.get_group_importer_by_id
    _ORIG_GET_PROFILER_BY_TYPE = plugin_api.get_profiler_by_type

    # Setup the importer/distributor mappings that return the mock instances
    global DISTRIBUTOR_MAPPINGS
    DISTRIBUTOR_MAPPINGS = {
        'mock-distributor': MOCK_DISTRIBUTOR,
        'mock-distributor-2': MOCK_DISTRIBUTOR_2,
    }

    global GROUP_DISTRIBUTOR_MAPPINGS
    GROUP_DISTRIBUTOR_MAPPINGS = {
        'mock-group-distributor': MOCK_GROUP_DISTRIBUTOR,
        'mock-group-distributor-2': MOCK_GROUP_DISTRIBUTOR_2,
    }

    global IMPORTER_MAPPINGS
    IMPORTER_MAPPINGS = {
        'mock-importer': MOCK_IMPORTER
    }

    global GROUP_IMPORTER_MAPPINGS
    GROUP_IMPORTER_MAPPINGS = {
        'mock-group-importer': MOCK_GROUP_IMPORTER
    }

    global PROFILER_MAPPINGS
    PROFILER_MAPPINGS = {}
    for profiler in MOCK_PROFILERS:
        for t in profiler.metadata()['types']:
            PROFILER_MAPPINGS[t] = profiler

    # Return the mock instance; eventually can enhance this to support
    # multiple IDs and instances
    def mock_get_distributor_by_id(id):
        if id not in DISTRIBUTOR_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return DISTRIBUTOR_MAPPINGS[id], {}

    def mock_get_group_distributor_by_id(id):
        if id not in GROUP_DISTRIBUTOR_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return GROUP_DISTRIBUTOR_MAPPINGS[id], {}

    def mock_get_importer_by_id(id):
        if id not in IMPORTER_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return IMPORTER_MAPPINGS[id], {}

    def mock_get_group_importer_by_id(id):
        if id not in GROUP_IMPORTER_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return GROUP_IMPORTER_MAPPINGS[id], {}

    def mock_get_profiler_by_type(type):
        if type not in PROFILER_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return PROFILER_MAPPINGS[type], {}

    # Monkey patch in the mock methods
    plugin_api.get_distributor_by_id = mock_get_distributor_by_id
    plugin_api.get_group_distributor_by_id = mock_get_group_distributor_by_id
    plugin_api.get_importer_by_id = mock_get_importer_by_id
    plugin_api.get_group_importer_by_id = mock_get_group_importer_by_id
    plugin_api.get_profiler_by_type = mock_get_profiler_by_type

    # configure the mock instances. By default, have the plugins indicate configurations are valid
    MOCK_IMPORTER.validate_config.return_value = True, None
    MOCK_IMPORTER.sync_repo.return_value = SyncReport(True, 10, 5, 1, 'Summary of the sync',
                                                      'Details of the sync')

    MOCK_GROUP_IMPORTER.validate_config.return_value = True, None

    MOCK_DISTRIBUTOR.validate_config.return_value = True, None
    MOCK_DISTRIBUTOR.publish_repo.return_value = PublishReport(True, 'Summary of the publish',
                                                               'Details of the publish')

    MOCK_DISTRIBUTOR_2.validate_config.return_value = True, None
    MOCK_DISTRIBUTOR_2.publish_repo.return_value = PublishReport(True, 'Summary of the publish',
                                                                 'Details of the publish')

    MOCK_GROUP_DISTRIBUTOR.validate_config.return_value = True, None
    MOCK_GROUP_DISTRIBUTOR_2.validate_config.return_value = True, None

    for profiler in MOCK_PROFILERS:
        profiler.update_profile = \
            mock.Mock(side_effect=lambda consumer, content_type, profile, config: profile)
        profiler.install_units = \
            mock.Mock(side_effect=lambda i, u, o, c, x: sorted(u))
        profiler.update_units = \
            mock.Mock(side_effect=lambda i, u, o, c, x: sorted(u))
        profiler.uninstall_units = \
            mock.Mock(side_effect=lambda i, u, o, c, x: sorted(u))
        profiler.calculate_applicable_units = \
            mock.Mock(side_effect=lambda t, p, r, c, x: ['mocked-unit1', 'mocked-unit2'])

Example 3

Project: pulp
Source File: mock_plugins.py
View license
def install():
    """
    Called during test setup to monkey patch the plugin loader for testing.
    """

    # update plugin loader inventory

    plugin_api._create_manager()

    plugin_api._MANAGER.importers.add_plugin('mock-importer', MockImporter, {})
    plugin_api._MANAGER.group_importers.add_plugin('mock-group-importer', MockGroupImporter, {})
    plugin_api._MANAGER.distributors.add_plugin('mock-distributor', MockDistributor, {})
    plugin_api._MANAGER.distributors.add_plugin('mock-distributor-2', MockDistributor, {})
    plugin_api._MANAGER.group_distributors.add_plugin('mock-group-distributor',
                                                      MockGroupDistributor, {})
    plugin_api._MANAGER.group_distributors.add_plugin('mock-group-distributor-2',
                                                      MockGroupDistributor, {})
    plugin_api._MANAGER.profilers.add_plugin('mock-profiler', MockProfiler, {})
    plugin_api._MANAGER.profilers.add_plugin('mock-rpm-profiler', MockRpmProfiler, {})

    # return mock instances instead of ephemeral ones

    # Save the state of the original plugin loader so it can be reverted
    global _ORIG_GET_DISTRIBUTOR_BY_ID
    global _ORIG_GET_GROUP_DISTRIBUTOR_BY_ID
    global _ORIG_GET_IMPORTER_BY_ID
    global _ORIG_GET_GROUP_IMPORTER_BY_ID
    global _ORIG_GET_PROFILER_BY_TYPE

    _ORIG_GET_DISTRIBUTOR_BY_ID = plugin_api.get_distributor_by_id
    _ORIG_GET_GROUP_DISTRIBUTOR_BY_ID = plugin_api.get_group_distributor_by_id
    _ORIG_GET_IMPORTER_BY_ID = plugin_api.get_importer_by_id
    _ORIG_GET_GROUP_IMPORTER_BY_ID = plugin_api.get_group_importer_by_id
    _ORIG_GET_PROFILER_BY_TYPE = plugin_api.get_profiler_by_type

    # Setup the importer/distributor mappings that return the mock instances
    global DISTRIBUTOR_MAPPINGS
    DISTRIBUTOR_MAPPINGS = {
        'mock-distributor': MOCK_DISTRIBUTOR,
        'mock-distributor-2': MOCK_DISTRIBUTOR_2,
    }

    global GROUP_DISTRIBUTOR_MAPPINGS
    GROUP_DISTRIBUTOR_MAPPINGS = {
        'mock-group-distributor': MOCK_GROUP_DISTRIBUTOR,
        'mock-group-distributor-2': MOCK_GROUP_DISTRIBUTOR_2,
    }

    global IMPORTER_MAPPINGS
    IMPORTER_MAPPINGS = {
        'mock-importer': MOCK_IMPORTER
    }

    global GROUP_IMPORTER_MAPPINGS
    GROUP_IMPORTER_MAPPINGS = {
        'mock-group-importer': MOCK_GROUP_IMPORTER
    }

    global PROFILER_MAPPINGS
    PROFILER_MAPPINGS = {}
    for profiler in MOCK_PROFILERS:
        for t in profiler.metadata()['types']:
            PROFILER_MAPPINGS[t] = profiler

    # Return the mock instance; eventually can enhance this to support
    # multiple IDs and instances
    def mock_get_distributor_by_id(id):
        if id not in DISTRIBUTOR_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return DISTRIBUTOR_MAPPINGS[id], {}

    def mock_get_group_distributor_by_id(id):
        if id not in GROUP_DISTRIBUTOR_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return GROUP_DISTRIBUTOR_MAPPINGS[id], {}

    def mock_get_importer_by_id(id):
        if id not in IMPORTER_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return IMPORTER_MAPPINGS[id], {}

    def mock_get_group_importer_by_id(id):
        if id not in GROUP_IMPORTER_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return GROUP_IMPORTER_MAPPINGS[id], {}

    def mock_get_profiler_by_type(type):
        if type not in PROFILER_MAPPINGS:
            raise plugin_exceptions.PluginNotFound()

        return PROFILER_MAPPINGS[type], {}

    # Monkey patch in the mock methods
    plugin_api.get_distributor_by_id = mock_get_distributor_by_id
    plugin_api.get_group_distributor_by_id = mock_get_group_distributor_by_id
    plugin_api.get_importer_by_id = mock_get_importer_by_id
    plugin_api.get_group_importer_by_id = mock_get_group_importer_by_id
    plugin_api.get_profiler_by_type = mock_get_profiler_by_type

    # configure the mock instances

    # By default, have the plugins indicate configurations are valid
    MOCK_IMPORTER.validate_config.return_value = True, None
    MOCK_IMPORTER.sync_repo.return_value = SyncReport(True, 10, 5, 1, 'Summary of the sync',
                                                      'Details of the sync')

    MOCK_GROUP_IMPORTER.validate_config.return_value = True, None

    MOCK_DISTRIBUTOR.validate_config.return_value = True, None
    MOCK_DISTRIBUTOR.publish_repo.return_value = PublishReport(True, 'Summary of the publish',
                                                               'Details of the publish')

    MOCK_DISTRIBUTOR_2.validate_config.return_value = True, None
    MOCK_DISTRIBUTOR_2.publish_repo.return_value = PublishReport(True, 'Summary of the publish',
                                                                 'Details of the publish')

    MOCK_GROUP_DISTRIBUTOR.validate_config.return_value = True, None
    MOCK_GROUP_DISTRIBUTOR_2.validate_config.return_value = True, None

    for profiler in MOCK_PROFILERS:
        profiler.update_profile = \
            mock.Mock(side_effect=lambda i, p, c, x: p)
        profiler.install_units = \
            mock.Mock(side_effect=lambda i, u, o, c, x: sorted(u))
        profiler.update_units = \
            mock.Mock(side_effect=lambda i, u, o, c, x: sorted(u))
        profiler.uninstall_units = \
            mock.Mock(side_effect=lambda i, u, o, c, x: sorted(u))
        profiler.unit_applicable = \
            mock.Mock(side_effect=lambda i, u, c, x: ApplicabilityReport(u, False, 'mocked'))

Example 4

Project: flake8
Source File: test_checker.py
View license
@pytest.mark.parametrize('results, expected_order', [
    # No entries should be added
    ([], []),
    # Results are correctly ordered
    ([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 1]),
    # Reversed order of lines
    ([('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE)], [1, 0]),
    # Columns are not ordered correctly (when reports are ordered correctly)
    ([('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [1, 0, 2]),
    ([('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE)], [1, 2, 0]),
    ([('A101', 1, 2, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 2, 1]),
    ([('A101', 1, 3, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 3, 1, 'placeholder error', PLACEHOLDER_CODE)], [0, 1, 2]),
    ([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 1, 3, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 2, 'placeholder error', PLACEHOLDER_CODE)], [0, 1, 2]),
    # Previously sort column and message (so reversed) (see bug 196)
    ([('A101', 1, 1, 'placeholder error', PLACEHOLDER_CODE),
      ('A101', 2, 1, 'charlie error', PLACEHOLDER_CODE)], [0, 1]),
])
def test_report_order(results, expected_order):
    """
    Test in which order the results will be reported.

    It gets a list of reports from the file checkers and verifies that the
    result will be ordered independent from the original report.
    """
    def count_side_effect(name, sorted_results):
        """Side effect for the result handler to tell all are reported."""
        return len(sorted_results)

    # To simplify the parameters (and prevent copy & pasting) reuse report
    # tuples to create the expected result lists from the indexes
    expected_results = [results[index] for index in expected_order]

    file_checker = mock.Mock(spec=['results', 'display_name'])
    file_checker.results = results
    file_checker.display_name = 'placeholder'

    style_guide = mock.Mock(spec=['options'])

    # Create a placeholder manager without arguments or plugins
    # Just add one custom file checker which just provides the results
    manager = checker.Manager(style_guide, [], [])
    manager.checkers = [file_checker]

    # _handle_results is the first place which gets the sorted result
    # Should something non-private be mocked instead?
    handler = mock.Mock()
    handler.side_effect = count_side_effect
    manager._handle_results = handler

    assert manager.report() == (len(results), len(results))
    handler.assert_called_once_with('placeholder', expected_results)

Example 5

Project: django-pushy
Source File: test_tasks.py
View license
    def test_send_notification_groups(self):
        notification = PushNotification.objects.create(
            title='test',
            payload=self.payload,
            active=PushNotification.PUSH_ACTIVE,
            sent=PushNotification.PUSH_NOT_SENT
        )

        # Assert return when the notification was not found
        self.assertFalse(send_push_notification_group(13, 0, 1))

        # Create a test device key
        device = Device.objects.create(key='TEST_DEVICE_KEY_ANDROID', type=Device.DEVICE_TYPE_ANDROID)

        # Make sure canonical ID is saved
        gcm = mock.Mock()
        gcm.return_value = 123123
        with mock.patch('gcm.GCM.plaintext_request', new=gcm):
            send_push_notification_group(notification.id, 0, 1)

            device = Device.objects.get(pk=device.id)
            self.assertEqual(device.key, '123123')

        # Make sure the key is deleted when not registered exception is fired
        gcm = mock.Mock()
        gcm.side_effect = GCMNotRegisteredException
        with mock.patch('gcm.GCM.plaintext_request', new=gcm):
            send_push_notification_group(notification.id, 0, 1)

            self.assertRaises(Device.DoesNotExist, Device.objects.get, pk=device.id)

        # Create an another test device key
        device = Device.objects.create(key='TEST_DEVICE_KEY_ANDROID2', type=Device.DEVICE_TYPE_ANDROID)

        # No canonical ID wasn't returned
        gcm = mock.Mock()
        gcm.return_value = False
        with mock.patch('gcm.GCM.plaintext_request', new=gcm):
            send_push_notification_group(notification.id, 0, 1)

            device = Device.objects.get(pk=device.id)
            self.assertEqual(device.key, 'TEST_DEVICE_KEY_ANDROID2')

Example 6

Project: blivet
Source File: clearpart_test.py
View license
    def test_should_clear(self):
        """ Test the Blivet.should_clear method. """
        b = blivet.Blivet()

        DiskDevice = blivet.devices.DiskDevice
        PartitionDevice = blivet.devices.PartitionDevice

        # sda is a disk with an existing disklabel containing two partitions
        sda = DiskDevice("sda", size=100000, exists=True)
        sda.format = blivet.formats.get_format("disklabel", device=sda.path,
                                               exists=True)
        sda.format._parted_disk = mock.Mock()
        sda.format._parted_device = mock.Mock()
        sda.format._parted_disk.configure_mock(partitions=[])
        b.devicetree._add_device(sda)

        # sda1 is a partition containing an existing ext4 filesystem
        sda1 = PartitionDevice("sda1", size=500, exists=True,
                               parents=[sda])
        sda1._parted_partition = mock.Mock(**{'type': PARTITION_NORMAL,
                                              'getFlag.return_value': 0})
        sda1.format = blivet.formats.get_format("ext4", mountpoint="/boot",
                                                device=sda1.path,
                                                exists=True)
        b.devicetree._add_device(sda1)

        # sda2 is a partition containing an existing vfat filesystem
        sda2 = PartitionDevice("sda2", size=10000, exists=True,
                               parents=[sda])
        sda2._parted_partition = mock.Mock(**{'type': PARTITION_NORMAL,
                                              'getFlag.return_value': 0})
        sda2.format = blivet.formats.get_format("vfat", mountpoint="/foo",
                                                device=sda2.path,
                                                exists=True)
        b.devicetree._add_device(sda2)

        # sdb is an unpartitioned disk containing an xfs filesystem
        sdb = DiskDevice("sdb", size=100000, exists=True)
        sdb.format = blivet.formats.get_format("xfs", device=sdb.path,
                                               exists=True)
        b.devicetree._add_device(sdb)

        # sdc is an unformatted/uninitialized/empty disk
        sdc = DiskDevice("sdc", size=100000, exists=True)
        b.devicetree._add_device(sdc)

        # sdd is a disk containing an existing disklabel with no partitions
        sdd = DiskDevice("sdd", size=100000, exists=True)
        sdd.format = blivet.formats.get_format("disklabel", device=sdd.path,
                                               exists=True)
        b.devicetree._add_device(sdd)

        #
        # clearpart type none
        #
        b.config.clear_part_type = CLEARPART_TYPE_NONE
        self.assertFalse(b.should_clear(sda1),
                         msg="type none should not clear any partitions")
        self.assertFalse(b.should_clear(sda2),
                         msg="type none should not clear any partitions")

        b.config.initialize_disks = False
        self.assertFalse(b.should_clear(sda),
                         msg="type none should not clear non-empty disks")
        self.assertFalse(b.should_clear(sdb),
                         msg="type none should not clear formatting from "
                             "unpartitioned disks")

        self.assertFalse(b.should_clear(sdc),
                         msg="type none should not clear empty disk without "
                             "initlabel")
        self.assertFalse(b.should_clear(sdd),
                         msg="type none should not clear empty partition table "
                             "without initlabel")

        b.config.initialize_disks = True
        self.assertFalse(b.should_clear(sda),
                         msg="type none should not clear non-empty disks even "
                             "with initlabel")
        self.assertFalse(b.should_clear(sdb),
                         msg="type non should not clear formatting from "
                             "unpartitioned disks even with initlabel")
        self.assertTrue(b.should_clear(sdc),
                        msg="type none should clear empty disks when initlabel "
                            "is set")
        self.assertTrue(b.should_clear(sdd),
                        msg="type none should clear empty partition table when "
                            "initlabel is set")

        #
        # clearpart type linux
        #
        b.config.clear_part_type = CLEARPART_TYPE_LINUX
        self.assertTrue(b.should_clear(sda1),
                        msg="type linux should clear partitions containing "
                            "ext4 filesystems")
        self.assertFalse(b.should_clear(sda2),
                         msg="type linux should not clear partitions "
                             "containing vfat filesystems")

        b.config.initialize_disks = False
        self.assertFalse(b.should_clear(sda),
                         msg="type linux should not clear non-empty disklabels")
        self.assertTrue(b.should_clear(sdb),
                        msg="type linux should clear linux-native whole-disk "
                            "formatting regardless of initlabel setting")
        self.assertFalse(b.should_clear(sdc),
                         msg="type linux should not clear unformatted disks "
                             "unless initlabel is set")
        self.assertFalse(b.should_clear(sdd),
                         msg="type linux should not clear disks with empty "
                             "partition tables unless initlabel is set")

        b.config.initialize_disks = True
        self.assertFalse(b.should_clear(sda),
                         msg="type linux should not clear non-empty disklabels")
        self.assertTrue(b.should_clear(sdb),
                        msg="type linux should clear linux-native whole-disk "
                            "formatting regardless of initlabel setting")
        self.assertTrue(b.should_clear(sdc),
                        msg="type linux should clear unformatted disks when "
                        "initlabel is set")
        self.assertTrue(b.should_clear(sdd),
                        msg="type linux should clear disks with empty "
                        "partition tables when initlabel is set")

        sda1.protected = True
        self.assertFalse(b.should_clear(sda1),
                         msg="protected devices should never be cleared")
        self.assertFalse(b.should_clear(sda),
                         msg="disks containing protected devices should never "
                             "be cleared")
        sda1.protected = False

        #
        # clearpart type all
        #
        b.config.clear_part_type = CLEARPART_TYPE_ALL
        self.assertTrue(b.should_clear(sda1),
                        msg="type all should clear all partitions")
        self.assertTrue(b.should_clear(sda2),
                        msg="type all should clear all partitions")

        b.config.initialize_disks = False
        self.assertTrue(b.should_clear(sda),
                        msg="type all should initialize all disks")
        self.assertTrue(b.should_clear(sdb),
                        msg="type all should initialize all disks")
        self.assertTrue(b.should_clear(sdc),
                        msg="type all should initialize all disks")
        self.assertTrue(b.should_clear(sdd),
                        msg="type all should initialize all disks")

        b.config.initialize_disks = True
        self.assertTrue(b.should_clear(sda),
                        msg="type all should initialize all disks")
        self.assertTrue(b.should_clear(sdb),
                        msg="type all should initialize all disks")
        self.assertTrue(b.should_clear(sdc),
                        msg="type all should initialize all disks")
        self.assertTrue(b.should_clear(sdd),
                        msg="type all should initialize all disks")

        sda1.protected = True
        self.assertFalse(b.should_clear(sda1),
                         msg="protected devices should never be cleared")
        self.assertFalse(b.should_clear(sda),
                         msg="disks containing protected devices should never "
                             "be cleared")
        sda1.protected = False

Example 7

Project: flask-dance
Source File: test_oauth2.py
View license
@requires_blinker
def test_signal_sender_oauth_authorized(request):
    app, bp = make_app()
    bp2 = OAuth2ConsumerBlueprint("test2", __name__,
        client_id="client_id",
        client_secret="client_secret",
        scope="admin",
        state="random-string",
        base_url="https://example.com",
        authorization_url="https://example.com/oauth/authorize",
        token_url="https://example.com/oauth/access_token",
        redirect_to="index",
    )
    app.register_blueprint(bp2, url_prefix="/login")

    calls = []
    def callback(*args, **kwargs):
        calls.append((args, kwargs))

    oauth_authorized.connect(callback, sender=bp)
    request.addfinalizer(lambda: oauth_authorized.disconnect(callback, sender=bp))
    fake_token = {"access_token": "test-token"}
    fake_token2 = {"access_token": "test-token2"}

    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess["test-service_oauth_state"] = "random-string"

        bp.session.fetch_token = mock.Mock(return_value=fake_token)
        bp2.session.fetch_token = mock.Mock(return_value=fake_token2)

        resp = client.get(
            "/login/test2/authorized?code=secret-code&state=random-string",
        )

    assert len(calls) == 0

    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess["test-service_oauth_state"] = "random-string"

        bp.session.fetch_token = mock.Mock(return_value="test-token")
        bp2.session.fetch_token = mock.Mock(return_value="test2-token")

        resp = client.get(
            "/login/test-service/authorized?code=secret-code&state=random-string",
        )

    assert len(calls) == 1
    assert calls[0][0] == (bp,)
    assert calls[0][1] == {"token": "test-token"}

    with app.test_client() as client:
        with client.session_transaction() as sess:
            sess["test-service_oauth_state"] = "random-string"

        bp.session.fetch_token = mock.Mock(return_value=fake_token)
        bp2.session.fetch_token = mock.Mock(return_value=fake_token2)

        resp = client.get(
            "/login/test2/authorized?code=secret-code&state=random-string",
        )

    assert len(calls) == 1  # unchanged

Example 8

Project: st2
Source File: test_sensor_wrapper.py
View license
    def test_trigger_cud_event_handlers(self):
        trigger_id = '57861fcb0640fd1524e577c0'
        file_path = os.path.join(RESOURCES_DIR, 'test_sensor.py')
        trigger_types = ['trigger1', 'trigger2']
        parent_args = ['--config-file', TESTS_CONFIG_PATH]

        wrapper = SensorWrapper(pack='core', file_path=file_path,
                                class_name='TestSensor',
                                trigger_types=trigger_types,
                                parent_args=parent_args)

        self.assertEqual(wrapper._trigger_names, {})

        wrapper._sensor_instance.add_trigger = mock.Mock()
        wrapper._sensor_instance.update_trigger = mock.Mock()
        wrapper._sensor_instance.remove_trigger = mock.Mock()

        # Call create handler with a trigger which refers to this sensor
        self.assertEqual(wrapper._sensor_instance.add_trigger.call_count, 0)

        trigger = TriggerDB(id=trigger_id, name='test', pack='dummy', type=trigger_types[0])
        wrapper._handle_create_trigger(trigger=trigger)
        self.assertEqual(wrapper._trigger_names, {trigger_id: trigger})
        self.assertEqual(wrapper._sensor_instance.add_trigger.call_count, 1)

        # Validate that update handler updates the trigger_names
        self.assertEqual(wrapper._sensor_instance.update_trigger.call_count, 0)

        trigger = TriggerDB(id=trigger_id, name='test', pack='dummy', type=trigger_types[0])
        wrapper._handle_update_trigger(trigger=trigger)
        self.assertEqual(wrapper._trigger_names, {trigger_id: trigger})
        self.assertEqual(wrapper._sensor_instance.update_trigger.call_count, 1)

        # Validate that delete handler deletes the trigger from trigger_names
        self.assertEqual(wrapper._sensor_instance.remove_trigger.call_count, 0)

        trigger = TriggerDB(id=trigger_id, name='test', pack='dummy', type=trigger_types[0])
        wrapper._handle_delete_trigger(trigger=trigger)
        self.assertEqual(wrapper._trigger_names, {})
        self.assertEqual(wrapper._sensor_instance.remove_trigger.call_count, 1)

Example 9

Project: tahoe-lafs
Source File: test_tor_provider.py
View license
    def _do_test_launch(self, executable):
        basedir = self.mktemp()
        os.mkdir(basedir)
        private_dir = os.path.join(basedir, "private")
        os.mkdir(private_dir)
        reactor = object()
        args = ["--listen=tor", "--tor-launch"]
        if executable:
            args.append("--tor-executable=%s" % executable)
        cli_config = make_cli_config(basedir, *args)
        protocol = object()
        launch_tor = mock.Mock(return_value=defer.succeed(("control_endpoint",
                                                           protocol)))
        txtorcon = mock.Mock()
        ehs = mock.Mock()
        ehs.private_key = "privkey"
        ehs.hostname = "ONION.onion"
        txtorcon.EphemeralHiddenService = mock.Mock(return_value=ehs)
        ehs.add_to_tor = mock.Mock(return_value=defer.succeed(None))
        ehs.remove_from_tor = mock.Mock(return_value=defer.succeed(None))

        with mock_txtorcon(txtorcon):
            with mock.patch("allmydata.util.tor_provider._launch_tor",
                            launch_tor):
                with mock.patch("allmydata.util.tor_provider.allocate_tcp_port",
                                return_value=999999):
                    d = tor_provider.create_onion(reactor, cli_config)
        tahoe_config_tor, tor_port, tor_location = self.successResultOf(d)

        launch_tor.assert_called_with(reactor, executable,
                                      os.path.abspath(private_dir), txtorcon)
        txtorcon.EphemeralHiddenService.assert_called_with("3457 127.0.0.1:999999")
        ehs.add_to_tor.assert_called_with(protocol)
        ehs.remove_from_tor.assert_called_with(protocol)

        expected = {"launch": "true",
                    "onion": "true",
                    "onion.local_port": "999999",
                    "onion.external_port": "3457",
                    "onion.private_key_file": os.path.join("private",
                                                           "tor_onion.privkey"),
                    }
        if executable:
            expected["tor.executable"] = executable
        self.assertEqual(tahoe_config_tor, expected)
        self.assertEqual(tor_port, "tcp:999999:interface=127.0.0.1")
        self.assertEqual(tor_location, "tor:ONION.onion:3457")
        fn = os.path.join(basedir, tahoe_config_tor["onion.private_key_file"])
        with open(fn, "rb") as f:
            privkey = f.read()
        self.assertEqual(privkey, "privkey")

Example 10

Project: thumbor
Source File: test_crypto.py
View license
    def test_get_options_from_storage(self):
        image_url = "/some/image.jpg"
        custom_security_key = "custom-sec"
        cryptor = Cryptor(security_key=custom_security_key)
        decryptor = Cryptor(security_key="something")

        expected_options = dict(
            width=300,
            height=300,
            smart=True,
            adaptive=False,
            full=False,
            fit_in=False,
            flip_horizontal=True,
            flip_vertical=True,
            halign="center",
            valign="middle",
            trim=True,
            crop_left=10,
            crop_top=11,
            crop_right=12,
            crop_bottom=13,
            filters='some_filter()',
            image=image_url,
        )

        encrypted_str = cryptor.encrypt(**expected_options)

        mock_storage = mock.Mock()
        decryptor.context = mock.Mock(
            config=mock.Mock(
                STORES_CRYPTO_KEY_FOR_EACH_IMAGE=True
            ),
            modules=mock.Mock(
                storage=mock_storage
            ),
        )

        mock_storage.get_crypto.return_value = custom_security_key

        options = decryptor.get_options(encrypted_str, image_url)
        expect(options).not_to_be_null()

        expected_options = {
            'trim': 'trim', 'full': False, 'halign': 'center', 'fit_in': False,
            'vertical_flip': True, 'image': '/some/image.jpg',
            'crop': {'top': 11, 'right': 12, 'bottom': 13, 'left': 10},
            'height': 300, 'width': 300, 'meta': False, 'horizontal_flip': True,
            'filters': 'some_filter()', 'valign': 'middle', 'debug': False,
            'hash': 'e2baf424fa420b73a97476956dfb858f', 'adaptive': False, 'smart': True
        }

        expect(options).to_be_like(expected_options)

Example 11

Project: magic-wormhole
Source File: test_scripts.py
View license
    def test_filenames(self):
        args = mock.Mock()
        args.relay_url = ""
        ef = cmd_receive.TwistedReceiver(args)._extract_file
        extract_dir = os.path.abspath(self.mktemp())

        zf = mock.Mock()
        zi = mock.Mock()
        zi.filename = "ok"
        zi.external_attr = 5 << 16
        expected = os.path.join(extract_dir, "ok")
        with mock.patch.object(cmd_receive.os, "chmod") as chmod:
            ef(zf, zi, extract_dir)
            self.assertEqual(zf.extract.mock_calls,
                             [mock.call(zi.filename, path=extract_dir)])
            self.assertEqual(chmod.mock_calls, [mock.call(expected, 5)])

        zf = mock.Mock()
        zi = mock.Mock()
        zi.filename = "../haha"
        e = self.assertRaises(ValueError, ef, zf, zi, extract_dir)
        self.assertIn("malicious zipfile", str(e))

        zf = mock.Mock()
        zi = mock.Mock()
        zi.filename = "haha//root" # abspath squashes this, hopefully zipfile
                                   # does too
        zi.external_attr = 5 << 16
        expected = os.path.join(extract_dir, "haha", "root")
        with mock.patch.object(cmd_receive.os, "chmod") as chmod:
            ef(zf, zi, extract_dir)
            self.assertEqual(zf.extract.mock_calls,
                             [mock.call(zi.filename, path=extract_dir)])
            self.assertEqual(chmod.mock_calls, [mock.call(expected, 5)])

        zf = mock.Mock()
        zi = mock.Mock()
        zi.filename = "/etc/passwd"
        e = self.assertRaises(ValueError, ef, zf, zi, extract_dir)
        self.assertIn("malicious zipfile", str(e))

Example 12

Project: elastalert
Source File: alerts_test.py
View license
def test_jira():
    description_txt = "Description stuff goes here like a runbook link."
    rule = {
        'name': 'test alert',
        'jira_account_file': 'jirafile',
        'type': mock_rule(),
        'jira_project': 'testproject',
        'jira_priority': 0,
        'jira_issuetype': 'testtype',
        'jira_server': 'jiraserver',
        'jira_label': 'testlabel',
        'jira_component': 'testcomponent',
        'jira_description': description_txt,
        'jira_watchers': ['testwatcher1', 'testwatcher2'],
        'timestamp_field': '@timestamp',
        'alert_subject': 'Issue {0} occurred at {1}',
        'alert_subject_args': ['test_term', '@timestamp']
    }

    mock_priority = mock.Mock(id='5')

    with nested(
        mock.patch('elastalert.alerts.JIRA'),
        mock.patch('elastalert.alerts.yaml_loader')
    ) as (mock_jira, mock_open):
        mock_open.return_value = {'user': 'jirauser', 'password': 'jirapassword'}
        mock_jira.return_value.priorities.return_value = [mock_priority]
        mock_jira.return_value.fields.return_value = []
        alert = JiraAlerter(rule)
        alert.alert([{'test_term': 'test_value', '@timestamp': '2014-10-31T00:00:00'}])

    expected = [
        mock.call('jiraserver', basic_auth=('jirauser', 'jirapassword')),
        mock.call().priorities(),
        mock.call().fields(),
        mock.call().create_issue(
            issuetype={'name': 'testtype'},
            priority={'id': '5'},
            project={'key': 'testproject'},
            labels=['testlabel'],
            components=[{'name': 'testcomponent'}],
            description=mock.ANY,
            summary='Issue test_value occurred at 2014-10-31T00:00:00',
        ),
        mock.call().add_watcher(mock.ANY, 'testwatcher1'),
        mock.call().add_watcher(mock.ANY, 'testwatcher2'),
    ]

    # We don't care about additional calls to mock_jira, such as __str__
    assert mock_jira.mock_calls[:6] == expected
    assert mock_jira.mock_calls[3][2]['description'].startswith(description_txt)

    # Search called if jira_bump_tickets
    rule['jira_bump_tickets'] = True
    with nested(
        mock.patch('elastalert.alerts.JIRA'),
        mock.patch('elastalert.alerts.yaml_loader')
    ) as (mock_jira, mock_open):
        mock_open.return_value = {'user': 'jirauser', 'password': 'jirapassword'}
        mock_jira.return_value = mock.Mock()
        mock_jira.return_value.search_issues.return_value = []
        mock_jira.return_value.priorities.return_value = [mock_priority]
        mock_jira.return_value.fields.return_value = []

        alert = JiraAlerter(rule)
        alert.alert([{'test_term': 'test_value', '@timestamp': '2014-10-31T00:00:00'}])

    expected.insert(3, mock.call().search_issues(mock.ANY))
    assert mock_jira.mock_calls == expected

    # Remove a field if jira_ignore_in_title set
    rule['jira_ignore_in_title'] = 'test_term'
    with nested(
        mock.patch('elastalert.alerts.JIRA'),
        mock.patch('elastalert.alerts.yaml_loader')
    ) as (mock_jira, mock_open):
        mock_open.return_value = {'user': 'jirauser', 'password': 'jirapassword'}
        mock_jira.return_value = mock.Mock()
        mock_jira.return_value.search_issues.return_value = []
        mock_jira.return_value.priorities.return_value = [mock_priority]
        mock_jira.return_value.fields.return_value = []

        alert = JiraAlerter(rule)
        alert.alert([{'test_term': 'test_value', '@timestamp': '2014-10-31T00:00:00'}])

    assert 'test_value' not in mock_jira.mock_calls[3][1][0]

    # Issue is still created if search_issues throws an exception
    with nested(
        mock.patch('elastalert.alerts.JIRA'),
        mock.patch('elastalert.alerts.yaml_loader')
    ) as (mock_jira, mock_open):
        mock_open.return_value = {'user': 'jirauser', 'password': 'jirapassword'}
        mock_jira.return_value = mock.Mock()
        mock_jira.return_value.search_issues.side_effect = JIRAError
        mock_jira.return_value.priorities.return_value = [mock_priority]
        mock_jira.return_value.fields.return_value = []

        alert = JiraAlerter(rule)
        alert.alert([{'test_term': 'test_value', '@timestamp': '2014-10-31T00:00:00'}])

    assert mock_jira.mock_calls == expected

Example 13

Project: paasta
Source File: test_marathon_serviceinit.py
View license
def test_status_smartstack_backends_different_nerve_ns():
    service = 'my_service'
    instance = 'my_instance'
    different_ns = 'different_ns'
    service_instance = compose_job_id(service, different_ns)

    cluster = 'fake_cluster'
    good_task = mock.Mock()
    bad_task = mock.Mock()
    other_task = mock.Mock()
    haproxy_backends_by_task = {
        good_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                    'check_code': '200', 'svname': 'ipaddress1:1001_hostname1',
                    'check_status': 'L7OK', 'check_duration': 1},
        bad_task: {'status': 'UP', 'lastchg': '1', 'last_chk': 'OK',
                   'check_code': '200', 'svname': 'ipaddress2:1002_hostname2',
                   'check_status': 'L7OK', 'check_duration': 1},
    }

    with contextlib.nested(
        mock.patch('paasta_tools.marathon_tools.load_service_namespace_config', autospec=True),
        mock.patch('paasta_tools.marathon_tools.read_registration_for_service_instance', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_all_slaves_for_blacklist_whitelist', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.get_backends', autospec=True),
        mock.patch('paasta_tools.marathon_serviceinit.match_backends_and_tasks', autospec=True),
    ) as (
        mock_load_service_namespace_config,
        mock_read_reg,
        mock_get_all_slaves_for_blacklist_whitelist,
        mock_get_backends,
        mock_match_backends_and_tasks,
    ):
        mock_load_service_namespace_config.return_value.get_discover.return_value = 'fake_discover'
        mock_get_all_slaves_for_blacklist_whitelist.return_value = [{
            'hostname': 'fakehost',
            'attributes': {
                'fake_discover': 'fakelocation'
            }
        }]

        mock_read_reg.return_value = service_instance
        mock_get_backends.return_value = haproxy_backends_by_task.values()
        mock_match_backends_and_tasks.return_value = [
            (haproxy_backends_by_task[good_task], good_task),
            (haproxy_backends_by_task[bad_task], None),
            (None, other_task),
        ]
        mock_read_reg.return_value = compose_job_id(service, different_ns)
        tasks = [good_task, other_task]
        actual = marathon_serviceinit.status_smartstack_backends(
            service=service,
            instance=instance,
            cluster=cluster,
            job_config=fake_marathon_job_config,
            tasks=tasks,
            expected_count=len(haproxy_backends_by_task),
            soa_dir=None,
            verbose=False,
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        mock_get_backends.assert_called_once_with(
            service_instance,
            synapse_host='fakehost',
            synapse_port=123456,
            synapse_haproxy_url_format=DEFAULT_SYNAPSE_HAPROXY_URL_FORMAT,
        )
        assert "fakelocation" in actual
        assert "Healthy" in actual

Example 14

View license
    def test_start_upgrade_rollback_scaledown(self, system_paasta_config):
        service_name = "service_name"
        instance_name = "instance_name"
        cluster = "cluster"

        service_configs = []
        for force_bounce in xrange(2):
            service_configs.append(native_mesos_scheduler.PaastaNativeServiceConfig(
                service=service_name,
                instance=instance_name,
                cluster=cluster,
                config_dict={
                    "cpus": 0.1,
                    "mem": 50,
                    "instances": 3,
                    "cmd": 'sleep 50',
                    "drain_method": "test"
                },
                branch_dict={
                    'docker_image': 'busybox',
                    'desired_state': 'start',
                    'force_bounce': str(force_bounce),
                },
            ))

        scheduler = native_mesos_scheduler.PaastaScheduler(
            service_name=service_name,
            instance_name=instance_name,
            cluster=cluster,
            system_paasta_config=system_paasta_config,
            service_config=service_configs[0],
        )
        fake_driver = mock.Mock()

        # First, start up 3 old tasks
        old_tasks = scheduler.start_task(fake_driver, fake_offer())
        assert len(scheduler.tasks_with_flags) == 3
        # and mark the old tasks as up
        for task in old_tasks:
            scheduler.statusUpdate(fake_driver, mock.Mock(task_id=task.task_id, state=TASK_RUNNING))
        assert len(scheduler.drain_method.downed_task_ids) == 0

        # Now, change force_bounce
        scheduler.service_config = service_configs[1]

        # and start 3 more tasks
        new_tasks = scheduler.start_task(fake_driver, fake_offer())
        assert len(scheduler.tasks_with_flags) == 6
        # It should not drain anything yet, since the new tasks aren't up.
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert len(scheduler.tasks_with_flags) == 6
        assert len(scheduler.drain_method.downed_task_ids) == 0

        # Now we mark the new tasks as up.
        for i, task in enumerate(new_tasks):
            scheduler.statusUpdate(fake_driver, mock.Mock(task_id=task.task_id, state=TASK_RUNNING))
            # As each of these new tasks come up, we should drain an old one.
            assert len(scheduler.drain_method.downed_task_ids) == i + 1

        # Now let's roll back and make sure it undrains the old ones and drains new.
        scheduler.service_config = service_configs[0]
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert scheduler.drain_method.downed_task_ids == set([])
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert scheduler.drain_method.downed_task_ids == set([t.task_id.value for t in new_tasks])

        # Once we drain the new tasks, it should kill them.
        assert fake_driver.killTask.call_count == 0

        # we issue duplicate kills for tasks until we get notified about TASK_KILLED, so we keep track of
        # the unique IDs of tasks being killed.
        killed_tasks = set()

        def killTask_side_effect(task_id):
            killed_tasks.add(task_id.value)

        fake_driver.killTask.side_effect = killTask_side_effect

        scheduler.drain_method.mark_arbitrary_task_as_safe_to_kill()
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert len(killed_tasks) == 1
        scheduler.drain_method.mark_arbitrary_task_as_safe_to_kill()
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert len(killed_tasks) == 2
        scheduler.drain_method.mark_arbitrary_task_as_safe_to_kill()
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert scheduler.drain_method.safe_to_kill_task_ids == set([t.task_id.value for t in new_tasks])
        assert len(killed_tasks) == 3

        for task in new_tasks:
            fake_driver.killTask.assert_any_call(task.task_id)

        # Now tell the scheduler those tasks have died.
        for task in new_tasks:
            scheduler.statusUpdate(fake_driver, mock.Mock(task_id=task.task_id, state=TASK_KILLED))

        # Clean up the TestDrainMethod for the rest of this test.
        for task in list(scheduler.drain_method.downed_task_ids):
            scheduler.drain_method.stop_draining(mock.Mock(id=task))
        assert len(scheduler.drain_method.downed_task_ids) == 0

        # Now scale down old app
        scheduler.service_config.config_dict['instances'] = 2
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert len(scheduler.drain_method.downed_task_ids) == 1

        # mark it as drained and let the scheduler kill it.
        scheduler.drain_method.mark_arbitrary_task_as_safe_to_kill()
        killed_tasks.clear()
        scheduler.kill_tasks_if_necessary(fake_driver)
        assert len(killed_tasks) == 1

Example 15

Project: 8-bits
Source File: test_cssutils.py
View license
    def test_resolveImports(self):
        "cssutils.resolveImports(sheet)"
        if mock:
            self._tempSer()
            cssutils.ser.prefs.useMinified()

            a = u'@charset "iso-8859-1";@import"b.css";\xe4{color:green}'.encode('iso-8859-1')
            b = u'@charset "ascii";\\E4 {color:red}'.encode('ascii')

            # normal
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, b)
                s = cssutils.parseString(a)

                # py3 TODO
                self.assertEqual(a, s.cssText)
                self.assertEqual(b, s.cssRules[1].styleSheet.cssText)

                c = cssutils.resolveImports(s)

                # py3 TODO
                self.assertEqual(u'\xc3\xa4{color:red}\xc3\xa4{color:green}'.encode('iso-8859-1'),
                                 c.cssText)

                c.encoding = 'ascii'
                self.assertEqual(ur'@charset "ascii";\E4 {color:red}\E4 {color:green}'.encode(),
                                 c.cssText)

            # b cannot be found
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, None)
                s = cssutils.parseString(a)

                # py3 TODO
                self.assertEqual(a, s.cssText)
                self.assertEqual(cssutils.css.CSSStyleSheet,
                                 type(s.cssRules[1].styleSheet))
                c = cssutils.resolveImports(s)
                # py3 TODO
                self.assertEqual(u'@import"b.css";\xc3\xa4{color:green}'.encode('iso-8859-1'),
                                 c.cssText)

            # @import with media
            a = u'@import"b.css";@import"b.css" print, tv ;@import"b.css" all;'
            b = u'a {color: red}'
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, b)
                s = cssutils.parseString(a)

                c = cssutils.resolveImports(s)

                self.assertEqual('a{color:red}@media print,tv{a{color:red}}a{color:red}'.encode(),
                                 c.cssText)

            # cannot resolve with media => keep original
            a = u'@import"b.css"print;'
            b = u'@namespace "http://example.com";'
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, b)
                s = cssutils.parseString(a)
                c = cssutils.resolveImports(s)
                self.assertEqual(a.encode(), c.cssText)

            # urls are adjusted too, layout:
            # a.css
            # c.css
            # img/img.gif
            # b/
            #     b.css
            #     subimg/subimg.gif
            a = u'''
                 @import"b/b.css";
                 a {
                     x: url(/img/abs.gif);
                     y: url(img/img.gif);
                     z: url(b/subimg/subimg.gif);
                     }'''
            def fetcher(url):
                c = {
                     'b.css': u'''
                         @import"../c.css";
                         b {
                             x: url(/img/abs.gif);
                             y: url(../img/img.gif);
                             z: url(subimg/subimg.gif);
                             }''',
                     'c.css': u'''
                         c {
                             x: url(/img/abs.gif);
                             y: url(./img/img.gif);
                             z: url(./b/subimg/subimg.gif);
                             }'''
                     }
                return 'utf-8', c[os.path.split(url)[1]]

            @mock.patch.object(cssutils.util, '_defaultFetcher',
                               new=fetcher)
            def do():
                s = cssutils.parseString(a)
                r = cssutils.resolveImports(s)
                return s, r

            s, r = do()

            cssutils.ser.prefs.useDefaults()
            cssutils.ser.prefs.keepComments = False
            self.assertEqual(u'''c {
    x: url(/img/abs.gif);
    y: url(img/img.gif);
    z: url(b/subimg/subimg.gif)
    }
b {
    x: url(/img/abs.gif);
    y: url(img/img.gif);
    z: url(b/subimg/subimg.gif)
    }
a {
    x: url(/img/abs.gif);
    y: url(img/img.gif);
    z: url(b/subimg/subimg.gif)
    }'''.encode(), r.cssText)

            cssutils.ser.prefs.useDefaults()
        else:
            self.assertEqual(False, u'Mock needed for this test')

Example 16

View license
    def test_attach(self):
        volume_id = '00000000-0000-0000-0000-000000000000'
        server_id = '11111111-1111-1111-1111-111111111111'
        device_name = '/dev/fake'

        volume_ctx = cfy_mocks.MockContext({
            'node': cfy_mocks.MockContext({
                'properties': {volume.DEVICE_NAME_PROPERTY: device_name}
            }),
            'instance': cfy_mocks.MockContext({
                'runtime_properties': {
                    OPENSTACK_ID_PROPERTY: volume_id,
                }
            })
        })
        server_ctx = cfy_mocks.MockContext({
            'node': cfy_mocks.MockContext({
                'properties': {}
            }),
            'instance': cfy_mocks.MockContext({
                'runtime_properties': {
                    server.OPENSTACK_ID_PROPERTY: server_id
                }
            })
        })

        ctx_m = self._mock(node_id='a',
                           target=server_ctx,
                           source=volume_ctx)

        cinderclient_m = mock.Mock()
        novaclient_m = mock.Mock()
        novaclient_m.volumes = mock.Mock()
        novaclient_m.volumes.create_server_volume = mock.Mock()

        with contextlib.nested(
                mock.patch.object(NovaClient, 'get',
                                  mock.Mock(return_value=novaclient_m)),
                mock.patch.object(CinderClient, 'get',
                                  mock.Mock(return_value=cinderclient_m)),
                mock.patch.object(volume,
                                  'wait_until_status',
                                  mock.Mock(return_value=(None, True)))):

            server.attach_volume(ctx=ctx_m)

            novaclient_m.volumes.create_server_volume.assert_called_once_with(
                server_id, volume_id, device_name)
            volume.wait_until_status.assert_called_once_with(
                cinder_client=cinderclient_m,
                volume_id=volume_id,
                status=volume.VOLUME_STATUS_IN_USE)

Example 17

View license
    def _test_cleanup__after_attach_fails(
            self, volume_ctx_mgr, expected_err_cls, expect_cleanup=True):
        volume_id = '00000000-0000-0000-0000-000000000000'
        server_id = '11111111-1111-1111-1111-111111111111'
        attachment_id = '22222222-2222-2222-2222-222222222222'
        device_name = '/dev/fake'

        attachment = {'id': attachment_id,
                      'server_id': server_id,
                      'volume_id': volume_id}

        volume_ctx = cfy_mocks.MockContext({
            'node': cfy_mocks.MockContext({
                'properties': {volume.DEVICE_NAME_PROPERTY: device_name}
            }),
            'instance': cfy_mocks.MockContext({
                'runtime_properties': {
                    OPENSTACK_ID_PROPERTY: volume_id,
                }
            })
        })
        server_ctx = cfy_mocks.MockContext({
            'node': cfy_mocks.MockContext({
                'properties': {}
            }),
            'instance': cfy_mocks.MockContext({
                'runtime_properties': {
                    server.OPENSTACK_ID_PROPERTY: server_id
                }
            })
        })

        ctx_m = self._mock(node_id='a',
                           target=server_ctx,
                           source=volume_ctx)

        attached_volume_m = mock.Mock()
        attached_volume_m.id = volume_id
        attached_volume_m.status = volume.VOLUME_STATUS_IN_USE
        attached_volume_m.attachments = [attachment]
        cinderclient_m = mock.Mock()
        cinderclient_m.volumes = mock.Mock()
        cinderclient_m.volumes.get = mock.Mock(
            return_value=attached_volume_m)
        novaclient_m = mock.Mock()
        novacl_vols_m = novaclient_m.volumes = mock.Mock()
        novacl_vols_m.create_server_volume = mock.Mock()

        with contextlib.nested(
                mock.patch.object(NovaClient, 'get',
                                  mock.Mock(return_value=novaclient_m)),
                mock.patch.object(CinderClient, 'get',
                                  mock.Mock(return_value=cinderclient_m)),
                volume_ctx_mgr):
            with self.assertRaises(expected_err_cls):
                server.attach_volume(ctx=ctx_m)

            novacl_vols_m.create_server_volume.assert_called_once_with(
                server_id, volume_id, device_name)
            volume.wait_until_status.assert_any_call(
                cinder_client=cinderclient_m,
                volume_id=volume_id,
                status=volume.VOLUME_STATUS_IN_USE)
            if expect_cleanup:
                novacl_vols_m.delete_server_volume.assert_called_once_with(
                    server_id, attachment_id)
                self.assertEqual(2, volume.wait_until_status.call_count)
                volume.wait_until_status.assert_called_with(
                    cinder_client=cinderclient_m,
                    volume_id=volume_id,
                    status=volume.VOLUME_STATUS_AVAILABLE)

Example 18

View license
    def test04__integration_1(self):
        core = mock.Mock()
        playlist = 'Playlist URI'
        core.playlists.lookup('Playlist URI').get().tracks = 'Tracks 811, 821, 823, 827, 829, 839'
        self.assertEqual(core.playlists.lookup.call_count, 1)  # First call when setting up the Mock
        threadcount = threading.active_count()

        am = AlarmManager()

        # Test get_core()
        self.assertTrue(am is am.get_core(core))

        # Test is_set() and threading when NOT set
        self.assertFalse(am.is_set())
        self.assertEqual(threading.active_count(), threadcount)

        # Set alarm to FAR future
        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        # Test when set
        self.assertTrue(am.is_set())
        self.assertEqual(threading.active_count(), threadcount + 1)
        self.assertEqual(am.get_ring_time(), b'07:59')
        self.assertFalse(am.random_mode)
        self.assertEqual(am.volume, 41)
        self.assertEqual(am.volume_increase_seconds, 83)

        # Cancel alarm
        am.cancel()

        # Test is_set() and threading when NOT set
        self.assertFalse(am.is_set())
        self.assertEqual(threading.active_count(), threadcount)

        # Set alarm to NEAR future
        am.set_alarm(datetime.datetime.now() + datetime.timedelta(seconds=29), playlist, False, 23, 127)

        # Tests a few seconds BEFORE alarm
        time.sleep(27)
        self.assertTrue(am.is_set())
        self.assertEqual(threading.active_count(), threadcount + 1)
        self.assertFalse(am.random_mode)
        self.assertEqual(am.volume, 23)
        self.assertEqual(am.volume_increase_seconds, 127)
        self.assertEqual(core.playlists.lookup.call_count, 1)  # First call when setting up the Mock

        # Cancel alarm
        am.cancel()

        # Test is_set() and threading when NOT set
        self.assertFalse(am.is_set())
        self.assertEqual(threading.active_count(), threadcount)

        # Sleep 20 seconds more to ensure that alarm will start if not cancelled
        time.sleep(20)

        # Set alarm to NEAR future
        am.set_alarm(datetime.datetime.now() + datetime.timedelta(seconds=31), playlist, True, 3, 17)

        # Test when set
        self.assertTrue(am.is_set())
        self.assertEqual(threading.active_count(), threadcount + 1)
        self.assertTrue(am.random_mode)
        self.assertEqual(am.volume, 3)
        self.assertEqual(am.volume_increase_seconds, 17)

        # Tests a few seconds BEFORE alarm
        time.sleep(29)
        self.assertTrue(am.is_set())
        self.assertEqual(threading.active_count(), threadcount + 1)
        self.assertIsInstance(core.tracklist.consume, mock.Mock)
        self.assertIsInstance(core.tracklist.single, mock.Mock)
        self.assertIsInstance(core.tracklist.repeat, mock.Mock)
        self.assertIsInstance(core.tracklist.random, mock.Mock)
        self.assertIsInstance(core.playback.mute, mock.Mock)
        self.assertIsInstance(core.playback.volume, mock.Mock)
        self.assertEqual(core.playback.stop.call_count, 0)
        self.assertEqual(core.tracklist.clear.call_count, 0)
        self.assertEqual(core.tracklist.add.call_count, 0)
        self.assertEqual(core.playback.next.call_count, 0)
        self.assertEqual(core.playback.play.call_count, 0)
        self.assertEqual(core.playlists.lookup.call_count, 1)  # First call when setting up the Mock

        # Tests a few seconds AFTER alarm START
        time.sleep(8)
        self.assertFalse(am.is_set())
        self.assertEqual(threading.active_count(), threadcount + 1)  # Additional thread is created by adjust_volume()
        self.assertEqual(core.tracklist.consume, False)
        self.assertEqual(core.tracklist.single, False)
        self.assertEqual(core.tracklist.repeat, True)
        self.assertEqual(core.tracklist.random, True)
        self.assertEqual(core.playback.mute, False)
        self.assertEqual(core.playback.volume, 1)  # First step of gradual volume increasing
        core.playback.stop.assert_called_once_with()
        core.tracklist.clear.assert_called_once_with()
        core.tracklist.add.assert_called_once_with('Tracks 811, 821, 823, 827, 829, 839')
        core.playback.next.assert_called_once_with()
        core.playback.play.assert_called_once_with()
        self.assertEqual(core.playlists.lookup.call_count, 2)

        # Further tests of gradual volume increasing
        time.sleep(5.67)  # Race conditions already prevented by previous sleep()
        self.assertEqual(core.playback.volume, 2)
        self.assertEqual(threading.active_count(), threadcount + 1)
        time.sleep(5.67)
        self.assertEqual(core.playback.volume, 2)
        self.assertEqual(threading.active_count(), threadcount + 1)
        time.sleep(5.67)
        self.assertEqual(core.playback.volume, 3)
        self.assertEqual(threading.active_count(), threadcount)
        time.sleep(20)  # More than 3x increase step time
        self.assertEqual(core.playback.volume, 3)
        self.assertEqual(threading.active_count(), threadcount)

        # Test alarm cancellation after alarm has been started
        self.assertFalse(am.is_set())
        am.cancel()
        self.assertFalse(am.is_set())
        self.assertEqual(threading.active_count(), threadcount)

        # Set alarm to FAR future
        am.set_alarm(datetime.datetime(2055, 4, 28, 7, 59, 15, 324341), playlist, False, 41, 83)

        # Test when set
        self.assertTrue(am.is_set())
        self.assertEqual(threading.active_count(), threadcount + 1)
        self.assertEqual(am.get_ring_time(), b'07:59')
        self.assertFalse(am.random_mode)
        self.assertEqual(am.volume, 41)
        self.assertEqual(am.volume_increase_seconds, 83)

        # Cancel alarm
        am.cancel()

        # Test is_set() and threading when NOT set
        self.assertFalse(am.is_set())
        self.assertEqual(threading.active_count(), threadcount)

Example 19

Project: mopidy-alarmclock
Source File: test_http.py
View license
    @freeze_time("2015-05-03 07:17:53")
    def test_SetAlarmRequestHandler(self):
        config = mock.Mock()
        core = mock.Mock()
        alarm_manager = mock.Mock()
        msg_store = http.MessageStore()

        patcher = mock.patch.object(http.SetAlarmRequestHandler, '__bases__', (mock.Mock,))
        with patcher:
            patcher.is_local = True
            handler = http.SetAlarmRequestHandler()

        handler.initialize(config, core, alarm_manager, msg_store)
        handler.redirect = mock.Mock()
        handler.get_argument = mock.Mock()

        # Test 1
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '8:00', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        alarm_manager.set_alarm.assert_called_once_with(datetime.datetime(2015, 05, 03, 8, 0), 'Playlist URI', True, 81, 23)
        self.assertEqual(msg_store.msg_code, 'ok')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 2 - defaults, time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '05:7', 'random': d, 'volume': d, 'incsec': d}[v]

        handler.post()

        # WARNING! Default configuration must be also updated in README.rst and ext.conf
        # WARNING! Internal defaults of volume and volume increase seconds are in SetAlarmRequestHandler of http.py
        alarm_manager.set_alarm.assert_called_once_with(datetime.datetime(2015, 05, 04, 5, 7), 'Playlist URI', False, 100, 30)
        self.assertEqual(msg_store.msg_code, 'ok')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 3 - ranges, time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '23:59', 'random': '1', 'volume': '0', 'incsec': '-1'}[v]

        handler.post()

        # WARNING! Default configuration (AND RANGES) must be also updated in README.rst and ext.conf
        # WARNING! Internal defaults of volume and volume increase seconds are in SetAlarmRequestHandler of http.py
        # WARNING! Ranges of volume and volume increase seconds are in SetAlarmRequestHandler of http.py AND HTML form of index.html
        alarm_manager.set_alarm.assert_called_once_with(datetime.datetime(2015, 05, 03, 23, 59), 'Playlist URI', True, 100, 30)
        self.assertEqual(msg_store.msg_code, 'ok')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 4 - ranges, time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '0:0', 'random': '1', 'volume': '101', 'incsec': '301'}[v]

        handler.post()

        # WARNING! Default configuration (AND RANGES) must be also updated in README.rst and ext.conf
        # WARNING! Internal defaults of volume and volume increase seconds are in SetAlarmRequestHandler of http.py
        # WARNING! Ranges of volume and volume increase seconds are in SetAlarmRequestHandler of http.py AND HTML form of index.html
        alarm_manager.set_alarm.assert_called_once_with(datetime.datetime(2015, 05, 04, 0, 0), 'Playlist URI', True, 100, 30)
        self.assertEqual(msg_store.msg_code, 'ok')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 5 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': 'a8:00', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 6 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '8:00a', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 7 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '8:0a0', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 8 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '800', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 9 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '8_00', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 10 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 11 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': 'a', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 12 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '24:00', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 13 - invalid time format
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': '8:60', 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)
        self.assertEqual(msg_store.msg_code, 'format')
        handler.redirect.assert_called_once_with('/alarmclock/')

        # Cleanup
        alarm_manager.reset_mock()
        handler.redirect.reset_mock()
        msg_store.msg_code = None

        # Test 14 - missing time
        handler.get_argument.side_effect = lambda v, d: {'playlist': 'Playlist URI', 'time': d, 'random': '1', 'volume': '81', 'incsec': '23'}[v]

        with self.assertRaises(TypeError):
            handler.post()

        self.assertFalse(alarm_manager.set_alarm.called)

Example 20

Project: Piped
Source File: test_providers.py
View license
    @defer.inlineCallbacks
    def test_get_named_channels(self):
        connection = providers.AMQPConnection('test_name', servers=['tcp:host=server_1:port=5672'])
        connection.setServiceParent(self.service)

        with patch.object(providers.endpoints, 'clientFromString') as mocked_client_from_string:
            def connect(factory):
                protocol = factory.buildProtocol(None)
                protocol.transport = mock.Mock()
                # mark the protocol as ready
                protocol.connectionReady(protocol)
                return protocol

            mocked_client_from_string.return_value.connect.side_effect = connect
            connection.startService()

            # the connection should now be finished connecting:
            self.assertTrue(connection.ready)

            protocol = connection.protocol

            channel_requests = list()
            with patch.object(protocol, 'channel') as mocked_channel:
                def channel():
                    d = defer.Deferred()
                    channel_requests.append(d)
                    return d

                mocked_channel.side_effect = channel

                foo_1_d = protocol.get_named_channel('foo')
                foo_2_d = protocol.get_named_channel('foo')
                foo_3_d = protocol.get_named_channel('foo')

                bar_d = protocol.get_named_channel('bar')

                self.assertEquals(len(channel_requests), 2)

                mocked_foo = mock.Mock()
                mocked_foo_close = event.Event()
                mocked_foo.add_on_close_callback.side_effect = mocked_foo_close.handle
                mocked_bar = mock.Mock()

                channel_requests.pop(0).callback(mocked_foo)
                channel_requests.pop(0).callback(mocked_bar)

                # all the pending requests for the channel should receive the same instance
                foo = yield defer.DeferredList([foo_1_d, foo_2_d, foo_3_d])
                self.assertEquals(set(foo), set([(True, mocked_foo)]))

                bar = yield bar_d
                self.assertEquals(bar, mocked_bar)

                self.assertEquals(len(channel_requests), 0)
                another_foo = yield protocol.get_named_channel('foo')
                self.assertEquals(len(channel_requests), 0)

                # if the channel is closed, the next request to get the channel will recreate it
                mocked_foo_close()
                self.assertEquals(len(channel_requests), 0)
                new_foo = protocol.get_named_channel('foo')
                self.assertEquals(len(channel_requests), 1)

                # if the channel request errbacks, all pending requests for the named channel should errback
                new_foo_2 = protocol.get_named_channel('foo')
                test_exception = Exception()
                channel_requests.pop(0).callback(failure.Failure(test_exception))

                for pending_foo in (new_foo, new_foo_2):
                    try:
                        yield pending_foo
                        self.fail('Expected {0!r} to be raised.'.format(new_foo))
                    except Exception as e:
                        self.assertEquals(e, test_exception)

Example 21

Project: nikola
Source File: test_rss_feeds.py
View license
    def setUp(self):
        LocaleSupportInTesting.initialize_locales_for_testing('unilingual')
        self.blog_url = "http://some.blog"

        with mock.patch('nikola.post.get_meta',
                        mock.Mock(return_value=(
                            (defaultdict(str, {'title': 'post title',
                                               'slug': 'awesome_article',
                                               'date': '2012-10-01 22:41',
                                               'author': None,
                                               'tags': 'tags',
                                               'link': 'link',
                                               'description': 'description',
                                               'enclosure': 'http://www.example.org/foo.mp3',
                                               'enclosure_length': '5'}),
                             True)
                        ))):
            with mock.patch('nikola.nikola.utils.os.path.isdir',
                            mock.Mock(return_value=True)):
                with mock.patch('nikola.nikola.Post.text',
                                mock.Mock(return_value='some long text')):

                    example_post = nikola.nikola.Post('source.file',
                                                      fake_conf,
                                                      'blog_folder',
                                                      True,
                                                      {'en': ''},
                                                      'post.tmpl',
                                                      FakeCompiler())

                    opener_mock = mock.mock_open()

                    with mock.patch('nikola.nikola.io.open', opener_mock, create=True):
                        nikola.nikola.Nikola().generic_rss_renderer('en',
                                                                    "blog_title",
                                                                    self.blog_url,
                                                                    "blog_description",
                                                                    [example_post,
                                                                     ],
                                                                    'testfeed.rss',
                                                                    True,
                                                                    False)

                    opener_mock.assert_called_once_with(
                        'testfeed.rss', 'w+', encoding='utf-8')

                    # Python 3 / unicode strings workaround
                    # lxml will complain if the encoding is specified in the
                    # xml when running with unicode strings.
                    # We do not include this in our content.
                    file_content = [
                        call[1][0]
                        for call in opener_mock.mock_calls[2:-1]][0]
                    splitted_content = file_content.split('\n')
                    self.encoding_declaration = splitted_content[0]
                    content_without_encoding_declaration = splitted_content[1:]
                    self.file_content = '\n'.join(
                        content_without_encoding_declaration)

Example 22

Project: googleads-python-lib
Source File: oauth2_test.py
View license
  def setUp(self):
    self.scope = 'scope'
    self.service_account_email = '[email protected]'
    self.private_key_password = 'notasecret'
    https_proxy_host = 'myproxy.com'
    https_proxy_port = 443
    https_proxy = googleads.common.ProxyConfig.Proxy(https_proxy_host,
                                                     https_proxy_port)
    self.proxy_config = googleads.common.ProxyConfig(https_proxy=https_proxy)
    self.https_proxy = '%s:%s' % (https_proxy_host, https_proxy_port)
    self.access_token_unrefreshed = 'a'
    self.access_token_refreshed = 'b'

    # Mock out filesystem and file for testing.
    filesystem = fake_filesystem.FakeFilesystem()
    tempfile = fake_tempfile.FakeTempfileModule(filesystem)
    fake_open = fake_filesystem.FakeFileOpen(filesystem)
    key_file_path = tempfile.NamedTemporaryFile(delete=False).name

    with fake_open(key_file_path, 'w') as file_handle:
      file_handle.write('IT\'S A SECRET TO EVERYBODY.')

    # Mock out httplib2.Http for testing.
    self.http = mock.Mock(spec=httplib2.Http)
    self.opener = self.http.return_value = mock.Mock()
    self.opener.proxy_info = self.proxy_config.proxy_info
    self.opener.ca_certs = self.proxy_config.cafile
    self.opener.disable_ssl_certificate_valiation = (
        self.proxy_config.disable_certificate_validation)

    # Mock out oauth2client.client.OAuth2Credentials for testing
    self.oauth2_credentials = mock.Mock(spec=SignedJwtAssertionCredentials)
    self.mock_oauth2_credentials = self.oauth2_credentials.return_value = (
        mock.Mock())
    self.mock_oauth2_credentials.access_token = 'x'
    self.mock_oauth2_credentials.token_expiry = datetime.datetime(1980, 1, 1,
                                                                  12)

    def apply(headers):
      headers['Authorization'] = ('Bearer %s'
                                  % self.mock_oauth2_credentials.access_token)

    def refresh(mock_http):
      self.mock_oauth2_credentials.access_token = (
          self.access_token_unrefreshed if
          self.mock_oauth2_credentials.access_token is 'x'
          else self.access_token_refreshed)
      self.mock_oauth2_credentials.token_expiry = datetime.datetime.utcnow()

    self.mock_oauth2_credentials.apply = mock.Mock(side_effect=apply)
    self.mock_oauth2_credentials.refresh = mock.Mock(side_effect=refresh)
    with mock.patch('__builtin__.open', fake_open):
      with mock.patch('oauth2client.client'
                      '.SignedJwtAssertionCredentials',
                      self.oauth2_credentials):
        self.googleads_client = googleads.oauth2.GoogleServiceAccountClient(
            self.scope, self.service_account_email, key_file_path,
            self.private_key_password, proxy_config=self.proxy_config)
      # Undo the call count for the auto-refresh
      self.mock_oauth2_credentials.refresh.reset_mock()

Example 23

Project: django-testmigrate
Source File: tests.py
View license
    def test_scope(self):
        from django.conf import settings
        settings.configure()
        from django.contrib.auth.models import User
        from django_testmigrate.base import MigrateTestCase, StateScope
        
        MockUser = mock.Mock()
        MockUser.objects.get.side_effect = lambda *args, **kwargs: User(*args, **kwargs)
        
        test_case = MigrateTestCase('test')
        
        # try to get variable before there's a scope set.
        with self.assertRaises(AttributeError):
            test_case.baz
        
        self.assertTrue(isinstance(test_case._store, OrderedDict))
        self.assertTrue(isinstance(test_case._store[None], StateScope))
        
        state1 = mock.Mock()
        
        test_case.set_current_state(state1)
        test_case.foo = 'a string'
        state1model1 = test_case.model1 = User(id=1)
        
        self.assertTrue(isinstance(test_case._store[state1], StateScope))
        
        # models get scoped, but other values stored directly on MigrateTestCase
        self.assertIs(test_case.__dict__['foo'], test_case.foo)
        self.assertNotIn('model1', test_case.__dict__)
        self.assertEqual(test_case._store[state1].model1, state1model1)
        self.assertIs(test_case._store[state1].model1, state1model1)
        self.assertIs(test_case.model1, state1model1)
        
        with self.assertRaises(AttributeError):
            test_case.bar
        
        state2 = mock.Mock()
        
        test_case.set_current_state(state2)
        
        state3 = mock.Mock()
        state3.get_model.side_effect = lambda app_label, model_name: MockUser
        
        test_case.set_current_state(state3)
        
        self.assertIs(test_case.__dict__['foo'], test_case.foo)
        
        self.assertEqual(state3.get_model.call_count, 0)
        
        state3model1 = test_case.model1
        
        # models in different scopes should pass the equality test because they
        # share the same pk, but will actually be different objects.
        self.assertEqual(state1model1, state3model1)
        self.assertIsNot(state1model1, state3model1)
        
        self.assertIs(test_case._store[state1].model1, state1model1)
        self.assertIs(test_case._store[state3].model1, state3model1)
        
        self.assertIs(test_case.model1, state3model1)
        
        self.assertEqual(state3.get_model.call_count, 1)
        self.assertEqual(state3.get_model.call_args_list[0][0], (User._meta.app_label, User._meta.model_name))

Example 24

View license
    def test_resolveImports(self):
        "cssutils.resolveImports(sheet)"
        if mock:
            self._tempSer()
            cssutils.ser.prefs.useMinified()

            a = u'@charset "iso-8859-1";@import"b.css";\xe4{color:green}'.encode('iso-8859-1')
            b = u'@charset "ascii";\\E4 {color:red}'.encode('ascii')

            # normal
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, b)
                s = cssutils.parseString(a)

                # py3 TODO
                self.assertEqual(a, s.cssText)
                self.assertEqual(b, s.cssRules[1].styleSheet.cssText)

                c = cssutils.resolveImports(s)

                # py3 TODO
                self.assertEqual(u'\xc3\xa4{color:red}\xc3\xa4{color:green}'.encode('iso-8859-1'),
                                 c.cssText)

                c.encoding = 'ascii'
                self.assertEqual(ur'@charset "ascii";\E4 {color:red}\E4 {color:green}'.encode(),
                                 c.cssText)

            # b cannot be found
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, None)
                s = cssutils.parseString(a)

                # py3 TODO
                self.assertEqual(a, s.cssText)
                self.assertEqual(cssutils.css.CSSStyleSheet,
                                 type(s.cssRules[1].styleSheet))
                c = cssutils.resolveImports(s)
                # py3 TODO
                self.assertEqual(u'@import"b.css";\xc3\xa4{color:green}'.encode('iso-8859-1'),
                                 c.cssText)

            # @import with media
            a = u'@import"b.css";@import"b.css" print, tv ;@import"b.css" all;'
            b = u'a {color: red}'
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, b)
                s = cssutils.parseString(a)

                c = cssutils.resolveImports(s)

                self.assertEqual('a{color:red}@media print,tv{a{color:red}}a{color:red}'.encode(),
                                 c.cssText)

            # cannot resolve with media => keep original
            a = u'@import"b.css"print;'
            b = u'@namespace "http://example.com";'
            m = mock.Mock()
            with mock.patch('cssutils.util._defaultFetcher', m):
                m.return_value = (None, b)
                s = cssutils.parseString(a)
                c = cssutils.resolveImports(s)
                self.assertEqual(a.encode(), c.cssText)

            # urls are adjusted too, layout:
            # a.css
            # c.css
            # img/img.gif
            # b/
            #     b.css
            #     subimg/subimg.gif
            a = u'''
                 @import"b/b.css";
                 a {
                     x: url(/img/abs.gif);
                     y: url(img/img.gif);
                     z: url(b/subimg/subimg.gif);
                     }'''
            def fetcher(url):
                c = {
                     'b.css': u'''
                         @import"../c.css";
                         b {
                             x: url(/img/abs.gif);
                             y: url(../img/img.gif);
                             z: url(subimg/subimg.gif);
                             }''',
                     'c.css': u'''
                         c {
                             x: url(/img/abs.gif);
                             y: url(./img/img.gif);
                             z: url(./b/subimg/subimg.gif);
                             }'''
                     }
                return 'utf-8', c[os.path.split(url)[1]]

            @mock.patch.object(cssutils.util, '_defaultFetcher',
                               new=fetcher)
            def do():
                s = cssutils.parseString(a)
                r = cssutils.resolveImports(s)
                return s, r

            s, r = do()

            cssutils.ser.prefs.useDefaults()
            cssutils.ser.prefs.keepComments = False
            self.assertEqual(u'''c {
    x: url(/img/abs.gif);
    y: url(img/img.gif);
    z: url(b/subimg/subimg.gif)
    }
b {
    x: url(/img/abs.gif);
    y: url(img/img.gif);
    z: url(b/subimg/subimg.gif)
    }
a {
    x: url(/img/abs.gif);
    y: url(img/img.gif);
    z: url(b/subimg/subimg.gif)
    }'''.encode(), r.cssText)

            cssutils.ser.prefs.useDefaults()
        else:
            self.assertEqual(False, u'Mock needed for this test')

Example 25

Project: holocron
Source File: test_feed.py
View license
    def setUp(self):
        self.app = Holocron(conf={
            'site': {
                'title': 'MyTestSite',
                'author': 'Tester',
                'url': 'http://www.mytest.com/',
            },

            'encoding': {
                'output': 'my-enc',
            },

            'paths': {
                'output': 'path/to/output',
            },

            'ext': {
                'enabled': [],
                'feed': {
                    'save_as': 'myfeed.xml',
                    'posts_number': 3,
                },
            },
        })
        self.feed = Feed(self.app)

        self.date_early = datetime(2012, 2, 2)
        self.date_moderate = datetime(2013, 4, 1)
        self.date_late = datetime(2014, 6, 12)

        self.date_early_updated = datetime(2012, 12, 6)
        self.date_moderate_updated = datetime(2013, 12, 6)
        self.date_late_updated = datetime(2014, 12, 6)

        self.post_early = mock.Mock(
            spec=Post,
            published=self.date_early,
            updated_local=self.date_early_updated,
            abs_url='http://www.post_early.com',
            title='MyEarlyPost')

        self.post_moderate = mock.Mock(
            spec=Post,
            published=self.date_moderate,
            updated_local=self.date_moderate_updated,
            abs_url='http://www.post_moderate.com')

        self.post_late = mock.Mock(
            spec=Post,
            published=self.date_late,
            updated_local=self.date_late_updated,
            url='www.post_late.com',
            abs_url='http://www.post_late.com',
            title='MyTestPost')

        self.late_id = '<id>http://www.post_late.com</id>'
        self.moderate_id = '<id>http://www.post_moderate.com</id>'
        self.early_id = '<id>http://www.post_early.com</id>'

        self.page = mock.Mock(spec=Page, url='www.page.com')
        self.static = mock.Mock(spec=Static, url='www.image.com')

        self.open_fn = 'holocron.ext.feed.open'

Example 26

Project: okcupyd
Source File: conftest.py
View license
@pytest.fixture
def T(mock_profile_builder, mock_message_thread_builder, mock_message_builder):

    class testing(object):
        pass

    testing.ensure = mock.Mock()
    testing.build_mock = mock.Mock()
    testing.factory = mock.Mock()

    def ensure_thread_model_resembles_okcupyd_thread(
        thread_model, okcupyd_thread
    ):
        assert thread_model.okc_id == okcupyd_thread.id
        ensure_user_model_resembles_okcupyd_profile(
            thread_model.initiator, okcupyd_thread.initiator
        )
        ensure_user_model_resembles_okcupyd_profile(
            thread_model.respondent, okcupyd_thread.respondent
        )
        for pair in zip(thread_model.messages, okcupyd_thread.messages):
            ensure_message_model_resembles_okcupyd_message(*pair)
            assert len(thread_model.messages) == len(okcupyd_thread.messages)

    def ensure_message_model_resembles_okcupyd_message(
        message_model, okcupyd_message
    ):
        assert message_model.okc_id == okcupyd_message.id
        assert message_model.sender.handle == okcupyd_message.sender.username
        assert message_model.recipient.handle == okcupyd_message.recipient.username
        assert message_model.text == okcupyd_message.content

    def ensure_user_model_resembles_okcupyd_profile(user_model,
                                                    okcupyd_profile):
        assert user_model.handle == okcupyd_profile.username

    testing.ensure.user_model_resembles_okcupyd_profile = \
        ensure_user_model_resembles_okcupyd_profile
    testing.ensure.message_model_resembles_okcupyd_message = \
        ensure_message_model_resembles_okcupyd_message
    testing.ensure.thread_model_resembles_okcupyd_thread = \
        ensure_thread_model_resembles_okcupyd_thread

    testing.build_mock.thread = mock_message_thread_builder
    testing.build_mock.message = mock_message_builder
    testing.build_mock.profile = mock_profile_builder

    def build_message_thread():
        message_thread = testing.build_mock.thread()
        return adapters.ThreadAdapter(message_thread).get_thread()[0]

    def build_user(username):
        profile = testing.build_mock.profile(username)
        return adapters.UserAdapter(profile).get()

    def build_okcupyd_user(user):
        user_model = model.User.from_profile(user.profile)
        user_model.upsert_model(id_key='okc_id')
        okcupyd_user = model.OKCupydUser(user_id=user_model.id)
        okcupyd_user.upsert_model(id_key='user_id')
        return okcupyd_user

    testing.factory.message_thread = build_message_thread
    testing.factory.user = build_user
    testing.factory.okcupyd_user = build_okcupyd_user
    return testing

Example 27

Project: nox
Source File: test_main.py
View license
def test_run(monkeypatch, capsys):

    class MockSession(object):
        def __init__(self, return_value=True):
            self.name = 'session_name'
            self.signature = None
            self.execute = mock.Mock()
            self.execute.return_value = return_value

    global_config = Namespace(
        noxfile='somefile.py',
        sessions=None,
        list_sessions=False,
        stop_on_first_error=False)
    user_nox_module = mock.Mock()
    session_functions = mock.Mock()
    sessions = [
        MockSession(),
        MockSession()
    ]

    with contexter.ExitStack() as stack:
        mock_load_user_module = stack.enter_context(mock.patch(
            'nox.main.load_user_nox_module',
            side_effect=lambda _: user_nox_module))
        mock_discover_session_functions = stack.enter_context(mock.patch(
            'nox.main.discover_session_functions',
            side_effect=lambda _: session_functions))
        mock_make_sessions = stack.enter_context(mock.patch(
            'nox.main.make_sessions', side_effect=lambda _1, _2: sessions))

        # Default options
        result = nox.main.run(global_config)
        assert result

        mock_load_user_module.assert_called_with('somefile.py')
        mock_discover_session_functions.assert_called_with(user_nox_module)
        mock_make_sessions.assert_called_with(session_functions, global_config)

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # List sessions
        global_config.list_sessions = True
        result = nox.main.run(global_config)
        assert result

        out, _ = capsys.readouterr()
        assert '* session_name' in out

        global_config.list_sessions = False

        # One failing session at the beginning, should still execute all.
        failing_session = MockSession(return_value=False)
        sessions.insert(0, failing_session)

        result = nox.main.run(global_config)
        assert not result

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # Now it should stop after the first failed session.
        global_config.stop_on_first_error = True

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # This time it should only run a subset of sessions
        sessions[0].execute.return_value = True
        sessions[0].name = '1'
        sessions[1].name = '2'
        sessions[2].name = '3'

        global_config.sessions = ['1', '3']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is True

        for session in sessions:
            session.execute.reset_mock()

        # Try to run with a session that doesn't exist.
        global_config.sessions = ['1', 'doesntexist']

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Now we'll try with parametrized sessions. Calling the basename
        # should execute all parametrized versions.
        sessions[0].name = 'a'
        sessions[0].signature = 'a(1)'
        sessions[1].name = 'a'
        sessions[1].signature = 'a(2)'
        sessions[2].name = 'b'

        global_config.sessions = ['a']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling the signature of should only call one parametrized version.
        global_config.sessions = ['a(2)']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling a signature that does not exist should not call any version.
        global_config.sessions = ['a(1)', 'a(3)', 'b']

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

Example 28

Project: nox
Source File: test_main.py
View license
def test_run(monkeypatch, capsys):

    class MockSession(object):
        def __init__(self, return_value=True):
            self.name = 'session_name'
            self.signature = None
            self.execute = mock.Mock()
            self.execute.return_value = return_value

    global_config = Namespace(
        noxfile='somefile.py',
        sessions=None,
        list_sessions=False,
        stop_on_first_error=False)
    user_nox_module = mock.Mock()
    session_functions = mock.Mock()
    sessions = [
        MockSession(),
        MockSession()
    ]

    with contexter.ExitStack() as stack:
        mock_load_user_module = stack.enter_context(mock.patch(
            'nox.main.load_user_nox_module',
            side_effect=lambda _: user_nox_module))
        mock_discover_session_functions = stack.enter_context(mock.patch(
            'nox.main.discover_session_functions',
            side_effect=lambda _: session_functions))
        mock_make_sessions = stack.enter_context(mock.patch(
            'nox.main.make_sessions', side_effect=lambda _1, _2: sessions))

        # Default options
        result = nox.main.run(global_config)
        assert result

        mock_load_user_module.assert_called_with('somefile.py')
        mock_discover_session_functions.assert_called_with(user_nox_module)
        mock_make_sessions.assert_called_with(session_functions, global_config)

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # List sessions
        global_config.list_sessions = True
        result = nox.main.run(global_config)
        assert result

        out, _ = capsys.readouterr()
        assert '* session_name' in out

        global_config.list_sessions = False

        # One failing session at the beginning, should still execute all.
        failing_session = MockSession(return_value=False)
        sessions.insert(0, failing_session)

        result = nox.main.run(global_config)
        assert not result

        for session in sessions:
            assert session.execute.called
            session.execute.reset_mock()

        # Now it should stop after the first failed session.
        global_config.stop_on_first_error = True

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # This time it should only run a subset of sessions
        sessions[0].execute.return_value = True
        sessions[0].name = '1'
        sessions[1].name = '2'
        sessions[2].name = '3'

        global_config.sessions = ['1', '3']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is True

        for session in sessions:
            session.execute.reset_mock()

        # Try to run with a session that doesn't exist.
        global_config.sessions = ['1', 'doesntexist']

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Now we'll try with parametrized sessions. Calling the basename
        # should execute all parametrized versions.
        sessions[0].name = 'a'
        sessions[0].signature = 'a(1)'
        sessions[1].name = 'a'
        sessions[1].signature = 'a(2)'
        sessions[2].name = 'b'

        global_config.sessions = ['a']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is True
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling the signature of should only call one parametrized version.
        global_config.sessions = ['a(2)']

        result = nox.main.run(global_config)
        assert result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is True
        assert sessions[2].execute.called is False

        for session in sessions:
            session.execute.reset_mock()

        # Calling a signature that does not exist should not call any version.
        global_config.sessions = ['a(1)', 'a(3)', 'b']

        result = nox.main.run(global_config)
        assert not result

        assert sessions[0].execute.called is False
        assert sessions[1].execute.called is False
        assert sessions[2].execute.called is False

Example 29

Project: luci-py
Source File: notifications_test.py
View license
  def test_notify_gitiles_rejection(self):
    ctx = validation.Context()
    ctx.error('err')
    ctx.warning('warn')

    base = gitiles.Location.parse('https://example.com/x/+/infra/config')
    new_rev = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    new_loc = base._replace(treeish=new_rev)
    old_rev = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
    old_loc = base._replace(treeish=old_rev)

    self.mock(notifications, '_send', mock.Mock())

    john = gitiles.Contribution(
        'John', '[email protected]', datetime.datetime(2015, 1, 1))
    commit = gitiles.Commit(
        sha=new_rev,
        tree='badcoffee',
        parents=[],
        author=john,
        committer=john,
        message='New config',
        tree_diff=None)
    self.mock(gitiles, 'get_log_async', mock.Mock(return_value=ndb.Future()))
    gitiles.get_log_async.return_value.set_result(
        gitiles.Log(commits=[commit], next_cursor=None))

    self.mock(template, 'render', mock.Mock())

    self.mock(auth, 'list_group', mock.Mock())
    auth.list_group.return_value = [
      auth.Identity('user', '[email protected]'),
      auth.Identity('service', 'foo'),
    ]

    # Notify.

    notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())

    self.assertTrue(notifications._send.called)
    email = notifications._send.call_args[0][0]
    self.assertEqual(
        email.sender,
        'sample-app.appspot.com <[email protected]>')
    self.assertEqual(email.subject, 'Config revision aaaaaaa is rejected')
    self.assertEqual(email.to, ['John <[email protected]>'])
    self.assertEqual(email.cc, {'[email protected]'})

    template.render.assert_called_with(
        'templates/validation_notification.html',
        {
          'author': 'John',
          'messages': [
            {'severity': 'ERROR', 'text': 'err'},
            {'severity': 'WARNING', 'text': 'warn'}
          ],
          'rev_link': new_loc,
          'rev_hash': 'aaaaaaa',
          'rev_repo': 'x',
          'cur_rev_hash': None,
          'cur_rev_link': None,
        })

    # Do not send second time.
    notifications._send.reset_mock()
    notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
    self.assertFalse(notifications._send.called)

    # Now with config set.

    ndb.Key(notifications.Notification, str(new_loc)).delete()

    storage.ConfigSet(
      id='projects/x',
      latest_revision=old_rev,
      location=str(base)
    ).put()

    template.render.reset_mock()
    notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
    template.render.assert_called_with(
        'templates/validation_notification.html',
        {
          'author': 'John',
          'messages': [
            {'severity': 'ERROR', 'text': 'err'},
            {'severity': 'WARNING', 'text': 'warn'}
          ],
          'rev_link': new_loc,
          'rev_hash': 'aaaaaaa',
          'rev_repo': 'x',
          'cur_rev_hash': 'bbbbbbb',
          'cur_rev_link': old_loc,
        })

Example 30

Project: luci-py
Source File: notifications_test.py
View license
  def test_notify_gitiles_rejection(self):
    ctx = validation.Context()
    ctx.error('err')
    ctx.warning('warn')

    base = gitiles.Location.parse('https://example.com/x/+/infra/config')
    new_rev = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    new_loc = base._replace(treeish=new_rev)
    old_rev = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
    old_loc = base._replace(treeish=old_rev)

    self.mock(notifications, '_send', mock.Mock())

    john = gitiles.Contribution(
        'John', '[email protected]', datetime.datetime(2015, 1, 1))
    commit = gitiles.Commit(
        sha=new_rev,
        tree='badcoffee',
        parents=[],
        author=john,
        committer=john,
        message='New config',
        tree_diff=None)
    self.mock(gitiles, 'get_log_async', mock.Mock(return_value=ndb.Future()))
    gitiles.get_log_async.return_value.set_result(
        gitiles.Log(commits=[commit], next_cursor=None))

    self.mock(template, 'render', mock.Mock())

    self.mock(auth, 'list_group', mock.Mock())
    auth.list_group.return_value = [
      auth.Identity('user', '[email protected]'),
      auth.Identity('service', 'foo'),
    ]

    # Notify.

    notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())

    self.assertTrue(notifications._send.called)
    email = notifications._send.call_args[0][0]
    self.assertEqual(
        email.sender,
        'sample-app.appspot.com <[email protected]>')
    self.assertEqual(email.subject, 'Config revision aaaaaaa is rejected')
    self.assertEqual(email.to, ['John <[email protected]>'])
    self.assertEqual(email.cc, {'[email protected]'})

    template.render.assert_called_with(
        'templates/validation_notification.html',
        {
          'author': 'John',
          'messages': [
            {'severity': 'ERROR', 'text': 'err'},
            {'severity': 'WARNING', 'text': 'warn'}
          ],
          'rev_link': new_loc,
          'rev_hash': 'aaaaaaa',
          'rev_repo': 'x',
          'cur_rev_hash': None,
          'cur_rev_link': None,
        })

    # Do not send second time.
    notifications._send.reset_mock()
    notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
    self.assertFalse(notifications._send.called)

    # Now with config set.

    ndb.Key(notifications.Notification, str(new_loc)).delete()

    storage.ConfigSet(
      id='projects/x',
      latest_revision=old_rev,
      location=str(base)
    ).put()

    template.render.reset_mock()
    notifications.notify_gitiles_rejection('projects/x', new_loc, ctx.result())
    template.render.assert_called_with(
        'templates/validation_notification.html',
        {
          'author': 'John',
          'messages': [
            {'severity': 'ERROR', 'text': 'err'},
            {'severity': 'WARNING', 'text': 'warn'}
          ],
          'rev_link': new_loc,
          'rev_hash': 'aaaaaaa',
          'rev_repo': 'x',
          'cur_rev_hash': 'bbbbbbb',
          'cur_rev_link': old_loc,
        })

Example 31

Project: Melissa-Core
Source File: test_stt.py
View license
def test_run_sphinx_stt():
    """test run with sphinx stt.

    At the first run, it will run normally.
    On the next run an error will be raised to stop the loop.
    """
    profile_data = default_profile_data
    profile_data['stt'] = 'sphinx'
    mock_flag_modeldir = mock_flag_hmm = mock.Mock()
    mock_flag_lm = mock_flag_dic = mock.Mock()
    profile_data['pocketsphinx'] = {
        'modeldir': mock_flag_modeldir,
        'hmm': mock_flag_hmm,
        'lm': mock_flag_lm,
        'dic': mock_flag_dic,
    }
    mock_profile.data = profile_data

    mock_open = mock.mock_open()
    with mock.patch('__builtin__.__import__', side_effect=import_mock),\
            mock.patch('melissa.stt.sr') as mock_sr, \
            mock.patch('melissa.stt.Decoder') as mock_decoder, \
            mock.patch('melissa.stt.open', mock_open, create=True), \
            mock.patch('melissa.stt.tts'):
        from melissa.stt import stt
        raised_error = ValueError
        mock_audio = mock.Mock()
        mock_sr.Recognizer.return_value.listen.side_effect = [
            mock_audio, raised_error()]
        stt()
        mock_audio.get_wav_data.assert_called_once_with()
        assert len(mock_sr.mock_calls) == 5
        assert len(mock_open.mock_calls) == 7
        mock_open_data = [
            mock.call('recording.wav', 'wb'),
            mock.call().__enter__(),
            mock.call().write(mock_audio.get_wav_data()),
            mock.call().__exit__(None, None, None),
            mock.call('recording.wav', 'rb'),
            mock.call().seek(44),
            mock.call().read()
        ]
        for item in mock_open_data:
            assert item in mock_open.mock_calls

        mock_decoder_config = mock_decoder.default_profile_data()
        mock_decoder_data = [
            mock.call.default_config(),
            mock.call.default_config().set_string('-hmm', mock_flag_hmm),
            mock.call.default_config().set_string('-lm', mock_flag_lm),
            mock.call.default_config().set_string('-dict', mock_flag_dic),
            mock.call.default_config().set_string('-logfn', '/dev/null'),
            mock.call(mock_decoder_config()),
            mock.call().start_utt(),
            mock.call().process_raw('', False, True),
            mock.call().end_utt(),
            mock.call().hyp(),
            # mock.call().hyp().hypstr.__radd__().__add__("'"),
            # mock.call().hyp().hypstr.__radd__().__add__().__str__(),
            mock.call().hyp().hypstr.lower(),
            mock.call().hyp().hypstr.lower().replace("'", ''),
        ]
        for item in mock_decoder_data:
            assert item in mock_decoder.mock_calls

Example 32

Project: Melissa-Core
Source File: test_stt.py
View license
def test_run_sphinx_stt():
    """test run with sphinx stt.

    At the first run, it will run normally.
    On the next run an error will be raised to stop the loop.
    """
    profile_data = default_profile_data
    profile_data['stt'] = 'sphinx'
    mock_flag_modeldir = mock_flag_hmm = mock.Mock()
    mock_flag_lm = mock_flag_dic = mock.Mock()
    profile_data['pocketsphinx'] = {
        'modeldir': mock_flag_modeldir,
        'hmm': mock_flag_hmm,
        'lm': mock_flag_lm,
        'dic': mock_flag_dic,
    }
    mock_profile.data = profile_data

    mock_open = mock.mock_open()
    with mock.patch('__builtin__.__import__', side_effect=import_mock),\
            mock.patch('melissa.stt.sr') as mock_sr, \
            mock.patch('melissa.stt.Decoder') as mock_decoder, \
            mock.patch('melissa.stt.open', mock_open, create=True), \
            mock.patch('melissa.stt.tts'):
        from melissa.stt import stt
        raised_error = ValueError
        mock_audio = mock.Mock()
        mock_sr.Recognizer.return_value.listen.side_effect = [
            mock_audio, raised_error()]
        stt()
        mock_audio.get_wav_data.assert_called_once_with()
        assert len(mock_sr.mock_calls) == 5
        assert len(mock_open.mock_calls) == 7
        mock_open_data = [
            mock.call('recording.wav', 'wb'),
            mock.call().__enter__(),
            mock.call().write(mock_audio.get_wav_data()),
            mock.call().__exit__(None, None, None),
            mock.call('recording.wav', 'rb'),
            mock.call().seek(44),
            mock.call().read()
        ]
        for item in mock_open_data:
            assert item in mock_open.mock_calls

        mock_decoder_config = mock_decoder.default_profile_data()
        mock_decoder_data = [
            mock.call.default_config(),
            mock.call.default_config().set_string('-hmm', mock_flag_hmm),
            mock.call.default_config().set_string('-lm', mock_flag_lm),
            mock.call.default_config().set_string('-dict', mock_flag_dic),
            mock.call.default_config().set_string('-logfn', '/dev/null'),
            mock.call(mock_decoder_config()),
            mock.call().start_utt(),
            mock.call().process_raw('', False, True),
            mock.call().end_utt(),
            mock.call().hyp(),
            # mock.call().hyp().hypstr.__radd__().__add__("'"),
            # mock.call().hyp().hypstr.__radd__().__add__().__str__(),
            mock.call().hyp().hypstr.lower(),
            mock.call().hyp().hypstr.lower().replace("'", ''),
        ]
        for item in mock_decoder_data:
            assert item in mock_decoder.mock_calls

Example 33

View license
    def test_find_similar_instances(self):
        src_inst = {'id1': {'instance': {'id': 'id1',
                                         'tenant_id': 'tenant1',
                                         'interfaces': [
                                             {'ip_addresses': ['10.0.0.2']},
                                             {'ip_addresses': ['11.0.0.1']}],
                                         'name': 'Foo',
                                         'flav_details': 'flav1',
                                         'key_name': None,
                                         'volumes': None}},
                    'id2': {'instance': {'id': 'id2',
                                         'tenant_id': 'tenant1',
                                         'interfaces': [
                                             {'ip_addresses': ['10.0.0.5']}],
                                         'name': 'Bar',
                                         'flav_details': 'flav1',
                                         'key_name': None,
                                         'volumes': None}},
                    'id3': {'instance': {'id': 'id3',
                                         'tenant_id': 'tenant2',
                                         'interfaces': [
                                             {'ip_addresses': ['11.0.0.1']}],
                                         'name': 'Foo',
                                         'flav_details': 'flav1',
                                         'key_name': None,
                                         'volumes': None}},
                    }
        dst_inst = {'nid1': {'instance': {'id': 'nid1',
                                          'tenant_id': 'newTenant1',
                                          'interfaces': [
                                              {'ip_addresses': ['10.0.0.2']}],
                                          'name': 'Foo',
                                          'flav_details': 'flav1',
                                          'key_name': None,
                                          'volumes': None}},
                    'nid2': {'instance': {'id': 'nid2',
                                          'tenant_id': 'newTenant1',
                                          'interfaces': [
                                              {'ip_addresses': ['11.0.0.1']}],
                                          'name': 'Foo',
                                          'flav_details': 'flav1',
                                          'key_name': None,
                                          'volumes': None}},
                    'nid3': {'instance': {'id': 'nid3',
                                          'tenant_id': 'newTenant2',
                                          'interfaces': [
                                              {'ip_addresses': ['11.0.0.1']}],
                                          'name': 'Foo',
                                          'flav_details': 'flav1',
                                          'key_name': None,
                                          'volumes': None}},
                    }
        similar_inst = {'id3': {'nid3'}}
        conflict_inst = {'id1': {'nid1', 'nid2'}}
        fake_info = {'instances': dst_inst}
        fake_compute = mock.Mock()
        fake_compute.read_info.return_value = fake_info
        fake_dst_cloud = mock.Mock()
        fake_dst_cloud.resources = {'compute': fake_compute}

        fake_init = {
            'src_cloud': mock.Mock(),
            'dst_cloud': fake_dst_cloud,
            'cfg': mock.Mock()
        }
        fake_action = FilterSimilarVMsFromDST(fake_init)
        fake_action.tenant_id_to_new_id = {'tenant1': 'newTenant1',
                                           'tenant2': 'newTenant2'}
        fake_action.src_instances = src_inst
        fake_action.find_similar_instances()
        self.assertEqual(similar_inst, fake_action.similar_isntances)
        self.assertEqual(conflict_inst, fake_action.conflict_instances)

Example 34

Project: searx
Source File: test_btdigg.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, btdigg.response, None)
        self.assertRaises(AttributeError, btdigg.response, [])
        self.assertRaises(AttributeError, btdigg.response, '')
        self.assertRaises(AttributeError, btdigg.response, '[]')

        response = mock.Mock(content='<html></html>')
        self.assertEqual(btdigg.response(response), [])

        html = """
        <div id="search_res">
            <table>
                <tr>
                    <td class="idx">1</td>
                    <td>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="torrent_name">
                                    <a href="/url">Should be the title</a>
                                </td>
                            </tr>
                        </table>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="ttth">
                                    <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
                                    title="Télécharger des liens Magnet">[magnet]</a>
                                </td>
                                <td class="ttth">
                                    <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
                                    target="_blank" title="Ajouter à BTCloud">[cloud]</a>
                                </td>
                                <td>
                                    <span class="attr_name">Taille:</span>
                                    <span class="attr_val">8 B</span>
                                </td>
                                <td>
                                    <span class="attr_name">Fichiers:</span>
                                    <span class="attr_val">710</span>
                                </td>
                                <td>
                                    <span class="attr_name">Téléchargements:</span>
                                    <span class="attr_val">5</span>
                                </td>
                                <td>
                                    <span class="attr_name">Temps:</span>
                                    <span class="attr_val">417.8&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
                                    <span class="attr_val">5.3&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Faux:</span>
                                    <span class="attr_val">Aucun</span>
                                </td>
                            </tr>
                        </table>
                        <pre class="snippet">
                            Content
                        </pre>
                    </td>
                </tr>
            </table>
        </div>
        """
        response = mock.Mock(content=html)
        results = btdigg.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'Should be the title')
        self.assertEqual(results[0]['url'], 'https://btdigg.org/url')
        self.assertEqual(results[0]['content'], 'Content')
        self.assertEqual(results[0]['seed'], 5)
        self.assertEqual(results[0]['leech'], 0)
        self.assertEqual(results[0]['filesize'], 8)
        self.assertEqual(results[0]['files'], 710)
        self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:magnet&dn=Test')

        html = """
        <div id="search_res">
            <table>
            </table>
        </div>
        """
        response = mock.Mock(content=html)
        results = btdigg.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        html = """
        <div id="search_res">
            <table>
                <tr>
                    <td class="idx">1</td>
                    <td>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="torrent_name">
                                    <a href="/url">Should be the title</a>
                                </td>
                            </tr>
                        </table>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="ttth">
                                    <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
                                    title="Télécharger des liens Magnet">[magnet]</a>
                                </td>
                                <td class="ttth">
                                    <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
                                    target="_blank" title="Ajouter à BTCloud">[cloud]</a>
                                </td>
                                <td>
                                    <span class="attr_name">Taille:</span>
                                    <span class="attr_val">1 KB</span>
                                </td>
                                <td>
                                    <span class="attr_name">Fichiers:</span>
                                    <span class="attr_val">710</span>
                                </td>
                                <td>
                                    <span class="attr_name">Téléchargements:</span>
                                    <span class="attr_val">5</span>
                                </td>
                                <td>
                                    <span class="attr_name">Temps:</span>
                                    <span class="attr_val">417.8&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
                                    <span class="attr_val">5.3&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Faux:</span>
                                    <span class="attr_val">Aucun</span>
                                </td>
                            </tr>
                        </table>
                        <pre class="snippet">
                            Content
                        </pre>
                    </td>
                </tr>
                <tr>
                    <td class="idx">1</td>
                    <td>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="torrent_name">
                                    <a href="/url">Should be the title</a>
                                </td>
                            </tr>
                        </table>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="ttth">
                                    <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
                                    title="Télécharger des liens Magnet">[magnet]</a>
                                </td>
                                <td class="ttth">
                                    <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
                                    target="_blank" title="Ajouter à BTCloud">[cloud]</a>
                                </td>
                                <td>
                                    <span class="attr_name">Taille:</span>
                                    <span class="attr_val">1 MB</span>
                                </td>
                                <td>
                                    <span class="attr_name">Fichiers:</span>
                                    <span class="attr_val">a</span>
                                </td>
                                <td>
                                    <span class="attr_name">Téléchargements:</span>
                                    <span class="attr_val">4</span>
                                </td>
                                <td>
                                    <span class="attr_name">Temps:</span>
                                    <span class="attr_val">417.8&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
                                    <span class="attr_val">5.3&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Faux:</span>
                                    <span class="attr_val">Aucun</span>
                                </td>
                            </tr>
                        </table>
                        <pre class="snippet">
                            Content
                        </pre>
                    </td>
                </tr>
                <tr>
                    <td class="idx">1</td>
                    <td>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="torrent_name">
                                    <a href="/url">Should be the title</a>
                                </td>
                            </tr>
                        </table>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="ttth">
                                    <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
                                    title="Télécharger des liens Magnet">[magnet]</a>
                                </td>
                                <td class="ttth">
                                    <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
                                    target="_blank" title="Ajouter à BTCloud">[cloud]</a>
                                </td>
                                <td>
                                    <span class="attr_name">Taille:</span>
                                    <span class="attr_val">1 GB</span>
                                </td>
                                <td>
                                    <span class="attr_name">Fichiers:</span>
                                    <span class="attr_val">710</span>
                                </td>
                                <td>
                                    <span class="attr_name">Téléchargements:</span>
                                    <span class="attr_val">3</span>
                                </td>
                                <td>
                                    <span class="attr_name">Temps:</span>
                                    <span class="attr_val">417.8&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
                                    <span class="attr_val">5.3&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Faux:</span>
                                    <span class="attr_val">Aucun</span>
                                </td>
                            </tr>
                        </table>
                        <pre class="snippet">
                            Content
                        </pre>
                    </td>
                </tr>
                <tr>
                    <td class="idx">1</td>
                    <td>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="torrent_name">
                                    <a href="/url">Should be the title</a>
                                </td>
                            </tr>
                        </table>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="ttth">
                                    <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
                                    title="Télécharger des liens Magnet">[magnet]</a>
                                </td>
                                <td class="ttth">
                                    <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
                                    target="_blank" title="Ajouter à BTCloud">[cloud]</a>
                                </td>
                                <td>
                                    <span class="attr_name">Taille:</span>
                                    <span class="attr_val">1 TB</span>
                                </td>
                                <td>
                                    <span class="attr_name">Fichiers:</span>
                                    <span class="attr_val">710</span>
                                </td>
                                <td>
                                    <span class="attr_name">Téléchargements:</span>
                                    <span class="attr_val">2</span>
                                </td>
                                <td>
                                    <span class="attr_name">Temps:</span>
                                    <span class="attr_val">417.8&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
                                    <span class="attr_val">5.3&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Faux:</span>
                                    <span class="attr_val">Aucun</span>
                                </td>
                            </tr>
                        </table>
                        <pre class="snippet">
                            Content
                        </pre>
                    </td>
                </tr>
                <tr>
                    <td class="idx">1</td>
                    <td>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="torrent_name">
                                    <a href="/url">Should be the title</a>
                                </td>
                            </tr>
                        </table>
                        <table class="torrent_name_tbl">
                            <tr>
                                <td class="ttth">
                                    <a onclick="fclck(this.href)" href="magnet:?xt=urn:btih:magnet&amp;dn=Test"
                                    title="Télécharger des liens Magnet">[magnet]</a>
                                </td>
                                <td class="ttth">
                                    <a href="https://btcloud.io/manager?cmd=add&amp;info_hash=hash"
                                    target="_blank" title="Ajouter à BTCloud">[cloud]</a>
                                </td>
                                <td>
                                    <span class="attr_name">Taille:</span>
                                    <span class="attr_val">a TB</span>
                                </td>
                                <td>
                                    <span class="attr_name">Fichiers:</span>
                                    <span class="attr_val">710</span>
                                </td>
                                <td>
                                    <span class="attr_name">Téléchargements:</span>
                                    <span class="attr_val">z</span>
                                </td>
                                <td>
                                    <span class="attr_name">Temps:</span>
                                    <span class="attr_val">417.8&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Dernière&nbsp;mise&nbsp;à&nbsp;jour:</span>
                                    <span class="attr_val">5.3&nbsp;jours</span>
                                </td>
                                <td>
                                    <span class="attr_name">Faux:</span>
                                    <span class="attr_val">Aucun</span>
                                </td>
                            </tr>
                        </table>
                        <pre class="snippet">
                            Content
                        </pre>
                    </td>
                </tr>
            </table>
        </div>
        """
        response = mock.Mock(content=html)
        results = btdigg.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 5)
        self.assertEqual(results[0]['title'], 'Should be the title')
        self.assertEqual(results[0]['url'], 'https://btdigg.org/url')
        self.assertEqual(results[0]['content'], 'Content')
        self.assertEqual(results[0]['seed'], 5)
        self.assertEqual(results[0]['leech'], 0)
        self.assertEqual(results[0]['files'], 710)
        self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:magnet&dn=Test')
        self.assertEqual(results[0]['filesize'], 1024)
        self.assertEqual(results[1]['filesize'], 1048576)
        self.assertEqual(results[2]['filesize'], 1073741824)
        self.assertEqual(results[3]['filesize'], 1099511627776)

Example 35

View license
    def test_response(self):
        self.assertRaises(AttributeError, duckduckgo_definitions.response, None)
        self.assertRaises(AttributeError, duckduckgo_definitions.response, [])
        self.assertRaises(AttributeError, duckduckgo_definitions.response, '')
        self.assertRaises(AttributeError, duckduckgo_definitions.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(duckduckgo_definitions.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(duckduckgo_definitions.response(response), [])

        json = """
        {
          "DefinitionSource": "definition source",
          "Heading": "heading",
          "ImageWidth": 0,
          "RelatedTopics": [
            {
              "Result": "Top-level domains",
              "Icon": {
                "URL": "",
                "Height": "",
                "Width": ""
              },
              "FirstURL": "https://first.url",
              "Text": "text"
            },
            {
              "Topics": [
                {
                  "Result": "result topic",
                  "Icon": {
                    "URL": "",
                    "Height": "",
                    "Width": ""
                  },
                  "FirstURL": "https://duckduckgo.com/?q=2%2F2",
                  "Text": "result topic text"
                }
              ],
              "Name": "name"
            }
          ],
          "Entity": "Entity",
          "Type": "A",
          "Redirect": "",
          "DefinitionURL": "http://definition.url",
          "AbstractURL": "https://abstract.url",
          "Definition": "this is the definition",
          "AbstractSource": "abstract source",
          "Infobox": {
            "content": [
              {
                "data_type": "string",
                "value": "1999",
                "label": "Introduced",
                "wiki_order": 0
              }
            ],
            "meta": [
              {
                "data_type": "string",
                "value": ".test",
                "label": "article_title"
              }
            ]
          },
          "Image": "image.png",
          "ImageIsLogo": 0,
          "Abstract": "abstract",
          "AbstractText": "abstract text",
          "AnswerType": "",
          "ImageHeight": 0,
          "Results": [{
                 "Result" : "result title",
                 "Icon" : {
                    "URL" : "result url",
                    "Height" : 16,
                    "Width" : 16
                 },
                 "FirstURL" : "result first url",
                 "Text" : "result text"
              }
          ],
          "Answer": "answer"
        }
        """
        response = mock.Mock(text=json)
        results = duckduckgo_definitions.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 4)
        self.assertEqual(results[0]['answer'], 'answer')
        self.assertEqual(results[1]['title'], 'heading')
        self.assertEqual(results[1]['url'], 'result first url')
        self.assertEqual(results[2]['suggestion'], 'text')
        self.assertEqual(results[3]['infobox'], 'heading')
        self.assertEqual(results[3]['id'], 'https://definition.url')
        self.assertEqual(results[3]['entity'], 'Entity')
        self.assertIn('abstract', results[3]['content'])
        self.assertIn('this is the definition', results[3]['content'])
        self.assertEqual(results[3]['img_src'], 'image.png')
        self.assertIn('Introduced', results[3]['attributes'][0]['label'])
        self.assertIn('1999', results[3]['attributes'][0]['value'])
        self.assertIn({'url': 'https://abstract.url', 'title': 'abstract source'}, results[3]['urls'])
        self.assertIn({'url': 'http://definition.url', 'title': 'definition source'}, results[3]['urls'])
        self.assertIn({'name': 'name', 'suggestions': ['result topic text']}, results[3]['relatedTopics'])

        json = """
        {
          "DefinitionSource": "definition source",
          "Heading": "heading",
          "ImageWidth": 0,
          "RelatedTopics": [],
          "Entity": "Entity",
          "Type": "A",
          "Redirect": "",
          "DefinitionURL": "",
          "AbstractURL": "https://abstract.url",
          "Definition": "",
          "AbstractSource": "abstract source",
          "Image": "",
          "ImageIsLogo": 0,
          "Abstract": "",
          "AbstractText": "abstract text",
          "AnswerType": "",
          "ImageHeight": 0,
          "Results": [],
          "Answer": ""
        }
        """
        response = mock.Mock(text=json)
        results = duckduckgo_definitions.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['url'], 'https://abstract.url')
        self.assertEqual(results[0]['title'], 'heading')
        self.assertEqual(results[0]['content'], '')

        json = """
        {
          "DefinitionSource": "definition source",
          "Heading": "heading",
          "ImageWidth": 0,
          "RelatedTopics": [
            {
              "Result": "Top-level domains",
              "Icon": {
                "URL": "",
                "Height": "",
                "Width": ""
              },
              "FirstURL": "https://first.url",
              "Text": "heading"
            },
            {
              "Name": "name"
            },
            {
              "Topics": [
                {
                  "Result": "result topic",
                  "Icon": {
                    "URL": "",
                    "Height": "",
                    "Width": ""
                  },
                  "FirstURL": "https://duckduckgo.com/?q=2%2F2",
                  "Text": "heading"
                }
              ],
              "Name": "name"
            }
          ],
          "Entity": "Entity",
          "Type": "A",
          "Redirect": "",
          "DefinitionURL": "http://definition.url",
          "AbstractURL": "https://abstract.url",
          "Definition": "this is the definition",
          "AbstractSource": "abstract source",
          "Infobox": {
            "meta": [
              {
                "data_type": "string",
                "value": ".test",
                "label": "article_title"
              }
            ]
          },
          "Image": "image.png",
          "ImageIsLogo": 0,
          "Abstract": "abstract",
          "AbstractText": "abstract text",
          "AnswerType": "",
          "ImageHeight": 0,
          "Results": [{
                 "Result" : "result title",
                 "Icon" : {
                    "URL" : "result url",
                    "Height" : 16,
                    "Width" : 16
                 },
                 "Text" : "result text"
              }
          ],
          "Answer": ""
        }
        """
        response = mock.Mock(text=json)
        results = duckduckgo_definitions.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['infobox'], 'heading')
        self.assertEqual(results[0]['id'], 'https://definition.url')
        self.assertEqual(results[0]['entity'], 'Entity')
        self.assertIn('abstract', results[0]['content'])
        self.assertIn('this is the definition', results[0]['content'])
        self.assertEqual(results[0]['img_src'], 'image.png')
        self.assertIn({'url': 'https://abstract.url', 'title': 'abstract source'}, results[0]['urls'])
        self.assertIn({'url': 'http://definition.url', 'title': 'definition source'}, results[0]['urls'])
        self.assertIn({'name': 'name', 'suggestions': []}, results[0]['relatedTopics'])

Example 36

Project: searx
Source File: test_flickr_noapi.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, flickr_noapi.response, None)
        self.assertRaises(AttributeError, flickr_noapi.response, [])
        self.assertRaises(AttributeError, flickr_noapi.response, '')
        self.assertRaises(AttributeError, flickr_noapi.response, '[]')

        response = mock.Mock(text='"search-photos-lite-models","photos":{},"totalItems":')
        self.assertEqual(flickr_noapi.response(response), [])

        response = mock.Mock(text='search-photos-lite-models","photos":{"data": []},"totalItems":')
        self.assertEqual(flickr_noapi.response(response), [])

        # everthing is ok test
        json = """
        "search-photos-lite-models","photos":
        {
          "_data": [
            {
              "_flickrModelRegistry": "photo-lite-models",
              "title": "This is the title",
              "username": "Owner",
              "pathAlias": "klink692",
              "realname": "Owner",
              "license": 0,
              "ownerNsid": "[email protected]",
              "canComment": false,
              "commentCount": 14,
              "faveCount": 21,
              "id": "14001294434",
              "sizes": {
                "c": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_c.jpg",
                  "width": 541,
                  "height": 800,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_c.jpg",
                  "key": "c"
                },
                "h": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_761d32237a_h.jpg",
                  "width": 1081,
                  "height": 1600,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_761d32237a_h.jpg",
                  "key": "h"
                },
                "k": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_f145a2c11a_k.jpg",
                  "width": 1383,
                  "height": 2048,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_f145a2c11a_k.jpg",
                  "key": "k"
                },
                "l": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_b.jpg",
                  "width": 692,
                  "height": 1024,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_b.jpg",
                  "key": "l"
                },
                "m": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777.jpg",
                  "width": 338,
                  "height": 500,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777.jpg",
                  "key": "m"
                },
                "n": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_n.jpg",
                  "width": 216,
                  "height": 320,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_n.jpg",
                  "key": "n"
                },
                "q": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_q.jpg",
                  "width": 150,
                  "height": 150,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_q.jpg",
                  "key": "q"
                },
                "s": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_m.jpg",
                  "width": 162,
                  "height": 240,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_m.jpg",
                  "key": "s"
                },
                "sq": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_s.jpg",
                  "width": 75,
                  "height": 75,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_s.jpg",
                  "key": "sq"
                },
                "t": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_t.jpg",
                  "width": 68,
                  "height": 100,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_t.jpg",
                  "key": "t"
                },
                "z": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
                  "width": 433,
                  "height": 640,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
                  "key": "z"
                }
              }
            }
          ],
          "fetchedStart": true,
          "fetchedEnd": false,
          "totalItems": "4386039"
        },"totalItems":
        """
        json = json.replace('\r\n', '').replace('\n', '').replace('\r', '')
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/[email protected]/14001294434')
        self.assertIn('k.jpg', results[0]['img_src'])
        self.assertIn('n.jpg', results[0]['thumbnail_src'])
        self.assertIn('Owner', results[0]['content'])

        # no n size, only the z size
        json = """
        "search-photos-lite-models","photos":
        {
          "_data": [
            {
              "_flickrModelRegistry": "photo-lite-models",
              "title": "This is the title",
              "username": "Owner",
              "pathAlias": "klink692",
              "realname": "Owner",
              "license": 0,
              "ownerNsid": "[email protected]",
              "canComment": false,
              "commentCount": 14,
              "faveCount": 21,
              "id": "14001294434",
              "sizes": {
                "z": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_z.jpg",
                  "width": 433,
                  "height": 640,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_z.jpg",
                  "key": "z"
                }
              }
            }
          ],
          "fetchedStart": true,
          "fetchedEnd": false,
          "totalItems": "4386039"
        },"totalItems":
        """
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/[email protected]/14001294434')
        self.assertIn('z.jpg', results[0]['img_src'])
        self.assertIn('z.jpg', results[0]['thumbnail_src'])
        self.assertIn('Owner', results[0]['content'])

        # no z or n size
        json = """
        "search-photos-lite-models","photos":
        {
          "_data": [
            {
              "_flickrModelRegistry": "photo-lite-models",
              "title": "This is the title",
              "username": "Owner",
              "pathAlias": "klink692",
              "realname": "Owner",
              "license": 0,
              "ownerNsid": "[email protected]",
              "canComment": false,
              "commentCount": 14,
              "faveCount": 21,
              "id": "14001294434",
              "sizes": {
                "o": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
                  "width": 433,
                  "height": 640,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
                  "key": "o"
                }
              }
            }
          ],
          "fetchedStart": true,
          "fetchedEnd": false,
          "totalItems": "4386039"
        },"totalItems":
        """
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://www.flickr.com/photos/[email protected]/14001294434')
        self.assertIn('o.jpg', results[0]['img_src'])
        self.assertIn('o.jpg', results[0]['thumbnail_src'])
        self.assertIn('Owner', results[0]['content'])

        # no image test
        json = """
        "search-photos-lite-models","photos":
        {
          "_data": [
            {
              "_flickrModelRegistry": "photo-lite-models",
              "title": "This is the title",
              "username": "Owner",
              "pathAlias": "klink692",
              "realname": "Owner",
              "license": 0,
              "ownerNsid": "[email protected]",
              "canComment": false,
              "commentCount": 14,
              "faveCount": 21,
              "id": "14001294434",
              "sizes": {
              }
            }
          ],
          "fetchedStart": true,
          "fetchedEnd": false,
          "totalItems": "4386039"
        },"totalItems":
        """
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        # null test
        json = """
        "search-photos-models","photos":
        {
          "_data": [null],
          "fetchedStart": true,
          "fetchedEnd": false,
          "totalItems": "4386039"
        },"totalItems":
        """
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        # no ownerNsid test
        json = """
        "search-photos-lite-models","photos":
        {
          "_data": [
            {
              "_flickrModelRegistry": "photo-lite-models",
              "title": "This is the title",
              "username": "Owner",
              "pathAlias": "klink692",
              "realname": "Owner",
              "license": 0,
              "canComment": false,
              "commentCount": 14,
              "faveCount": 21,
              "id": "14001294434",
              "sizes": {
                "o": {
                  "displayUrl": "//farm8.staticflickr.com/7246/14001294434_410f653777_o.jpg",
                  "width": 433,
                  "height": 640,
                  "url": "//c4.staticflickr.com/8/7246/14001294434_410f653777_o.jpg",
                  "key": "o"
                }
              }
            }
          ],
          "fetchedStart": true,
          "fetchedEnd": false,
          "totalItems": "4386039"
        },"totalItems":
        """
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        # garbage test
        json = r"""
        {"toto":[
            {"id":200,"name":"Artist Name",
            "link":"http:\/\/www.flickr.com\/artist\/1217","type":"artist"}
        ]}
        """
        response = mock.Mock(text=json)
        results = flickr_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

Example 37

Project: searx
Source File: test_kickass.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, kickass.response, None)
        self.assertRaises(AttributeError, kickass.response, [])
        self.assertRaises(AttributeError, kickass.response, '')
        self.assertRaises(AttributeError, kickass.response, '[]')

        response = mock.Mock(text='<html></html>')
        self.assertEqual(kickass.response(response), [])

        html = """
        <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
            <tr class="firstr">
                <th class="width100perc nopad">torrent name</th>
                <th class="center">
                    <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
                </th>
                <th class="center"><span class="files">
                    <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
                </th>
                <th class="center"><span>
                    <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
                </th>
                <th class="center"><span class="seed">
                    <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
                </th>
                <th class="lasttd nobr center">
                    <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
                </th>
            </tr>
            <tr class="even" id="torrent_test6478745">
                <td>
                    <div class="iaconbox center floatright">
                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
                            <i class="ka ka-comment"></i>
                        </a>
                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
                            <i class="ka ka16 ka-verify ka-green"></i>
                        </a>
                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
                        </a>
                        <a title="Torrent magnet link"
                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
                            <i class="ka ka16 ka-magnet"></i>
                        </a>
                        <a title="Download torrent file"
                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
                            <i class="ka ka16 ka-arrow-down"></i>
                        </a>
                    </div>
                    <div class="torrentname">
                    <a href="/test-t6478745.html" class="torType txtType"></a>
                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
                    <div class="markeredBlock torType txtType">
                        <a href="/url.html" class="cellMainLink">
                            <strong class="red">This should be the title</strong>
                        </a>
                        <span class="font11px lightgrey block">
                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
                            <a class="plain" href="/user/riri/">riri</a> in
                            <span id="cat_6478745">
                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
                            </span>
                        </span>
                    </div>
                </td>
                <td class="nobr center">449 bytes</td>
                <td class="center">4</td>
                <td class="center">2&nbsp;years</td>
                <td class="green center">10</td>
                <td class="red lasttd center">1</td>
            </tr>
        </table>
        """
        response = mock.Mock(text=html)
        results = kickass.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This should be the title')
        self.assertEqual(results[0]['url'], 'https://kickass.cd/url.html')
        self.assertEqual(results[0]['content'], 'Posted by riri in Other > Unsorted')
        self.assertEqual(results[0]['seed'], 10)
        self.assertEqual(results[0]['leech'], 1)
        self.assertEqual(results[0]['filesize'], 449)
        self.assertEqual(results[0]['files'], 4)
        self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
        self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')

        html = """
        <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
            <tr class="firstr">
                <th class="width100perc nopad">torrent name</th>
                <th class="center">
                    <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
                </th>
                <th class="center"><span class="files">
                    <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
                </th>
                <th class="center"><span>
                    <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
                </th>
                <th class="center"><span class="seed">
                    <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
                </th>
                <th class="lasttd nobr center">
                    <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
                </th>
            </tr>
        </table>
        """
        response = mock.Mock(text=html)
        results = kickass.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        html = """
        <table cellpadding="0" cellspacing="0" class="data" style="width: 100%">
            <tr class="firstr">
                <th class="width100perc nopad">torrent name</th>
                <th class="center">
                    <a href="/search/test/?field=size&sorder=desc" rel="nofollow">size</a>
                </th>
                <th class="center"><span class="files">
                    <a href="/search/test/?field=files_count&sorder=desc" rel="nofollow">files</a></span>
                </th>
                <th class="center"><span>
                    <a href="/search/test/?field=time_add&sorder=desc" rel="nofollow">age</a></span>
                </th>
                <th class="center"><span class="seed">
                    <a href="/search/test/?field=seeders&sorder=desc" rel="nofollow">seed</a></span>
                </th>
                <th class="lasttd nobr center">
                    <a href="/search/test/?field=leechers&sorder=desc" rel="nofollow">leech</a>
                </th>
            </tr>
            <tr class="even" id="torrent_test6478745">
                <td>
                    <div class="iaconbox center floatright">
                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
                            <i class="ka ka-comment"></i>
                        </a>
                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
                            <i class="ka ka16 ka-verify ka-green"></i>
                        </a>
                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
                        </a>
                        <a title="Torrent magnet link"
                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
                            <i class="ka ka16 ka-magnet"></i>
                        </a>
                        <a title="Download torrent file"
                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
                            <i class="ka ka16 ka-arrow-down"></i>
                        </a>
                    </div>
                    <div class="torrentname">
                    <a href="/test-t6478745.html" class="torType txtType"></a>
                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
                    <div class="markeredBlock torType txtType">
                        <a href="/url.html" class="cellMainLink">
                            <strong class="red">This should be the title</strong>
                        </a>
                        <span class="font11px lightgrey block">
                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
                            <a class="plain" href="/user/riri/">riri</a> in
                            <span id="cat_6478745">
                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
                            </span>
                        </span>
                    </div>
                </td>
                <td class="nobr center">1 KiB</td>
                <td class="center">4</td>
                <td class="center">2&nbsp;years</td>
                <td class="green center">10</td>
                <td class="red lasttd center">1</td>
            </tr>
            <tr class="even" id="torrent_test6478745">
                <td>
                    <div class="iaconbox center floatright">
                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
                            <i class="ka ka-comment"></i>
                        </a>
                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
                            <i class="ka ka16 ka-verify ka-green"></i>
                        </a>
                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
                        </a>
                        <a title="Torrent magnet link"
                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
                            <i class="ka ka16 ka-magnet"></i>
                        </a>
                        <a title="Download torrent file"
                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
                            <i class="ka ka16 ka-arrow-down"></i>
                        </a>
                    </div>
                    <div class="torrentname">
                    <a href="/test-t6478745.html" class="torType txtType"></a>
                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
                    <div class="markeredBlock torType txtType">
                        <a href="/url.html" class="cellMainLink">
                            <strong class="red">This should be the title</strong>
                        </a>
                        <span class="font11px lightgrey block">
                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
                            <a class="plain" href="/user/riri/">riri</a> in
                            <span id="cat_6478745">
                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
                            </span>
                        </span>
                    </div>
                </td>
                <td class="nobr center">1 MiB</td>
                <td class="center">4</td>
                <td class="center">2&nbsp;years</td>
                <td class="green center">9</td>
                <td class="red lasttd center">1</td>
            </tr>
            <tr class="even" id="torrent_test6478745">
                <td>
                    <div class="iaconbox center floatright">
                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
                            <i class="ka ka-comment"></i>
                        </a>
                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
                            <i class="ka ka16 ka-verify ka-green"></i>
                        </a>
                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
                        </a>
                        <a title="Torrent magnet link"
                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
                            <i class="ka ka16 ka-magnet"></i>
                        </a>
                        <a title="Download torrent file"
                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
                            <i class="ka ka16 ka-arrow-down"></i>
                        </a>
                    </div>
                    <div class="torrentname">
                    <a href="/test-t6478745.html" class="torType txtType"></a>
                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
                    <div class="markeredBlock torType txtType">
                        <a href="/url.html" class="cellMainLink">
                            <strong class="red">This should be the title</strong>
                        </a>
                        <span class="font11px lightgrey block">
                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
                            <a class="plain" href="/user/riri/">riri</a> in
                            <span id="cat_6478745">
                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
                            </span>
                        </span>
                    </div>
                </td>
                <td class="nobr center">1 GiB</td>
                <td class="center">4</td>
                <td class="center">2&nbsp;years</td>
                <td class="green center">8</td>
                <td class="red lasttd center">1</td>
            </tr>
            <tr class="even" id="torrent_test6478745">
                <td>
                    <div class="iaconbox center floatright">
                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
                            <i class="ka ka-comment"></i>
                        </a>
                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
                            <i class="ka ka16 ka-verify ka-green"></i>
                        </a>
                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
                        </a>
                        <a title="Torrent magnet link"
                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
                            <i class="ka ka16 ka-magnet"></i>
                        </a>
                        <a title="Download torrent file"
                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
                            <i class="ka ka16 ka-arrow-down"></i>
                        </a>
                    </div>
                    <div class="torrentname">
                    <a href="/test-t6478745.html" class="torType txtType"></a>
                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
                    <div class="markeredBlock torType txtType">
                        <a href="/url.html" class="cellMainLink">
                            <strong class="red">This should be the title</strong>
                        </a>
                        <span class="font11px lightgrey block">
                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
                            <a class="plain" href="/user/riri/">riri</a> in
                            <span id="cat_6478745">
                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
                            </span>
                        </span>
                    </div>
                </td>
                <td class="nobr center">1 TiB</td>
                <td class="center">4</td>
                <td class="center">2&nbsp;years</td>
                <td class="green center">7</td>
                <td class="red lasttd center">1</td>
            </tr>
            <tr class="even" id="torrent_test6478745">
                <td>
                    <div class="iaconbox center floatright">
                        <a rel="6478745,0" class="icommentjs icon16" href="/test-t6478745.html#comment">
                            <em style="font-size: 12px; margin: 0 4px 0 4px;" class="iconvalue">3</em>
                            <i class="ka ka-comment"></i>
                        </a>
                        <a class="iverify icon16" href="/test-t6478745.html" title="Verified Torrent">
                            <i class="ka ka16 ka-verify ka-green"></i>
                        </a>
                        <a href="#" onclick="_scq.push([]); return false;" class="partner1Button idownload icon16">
                            <i class="ka ka16 ka-arrow-down partner1Button"></i>
                        </a>
                        <a title="Torrent magnet link"
                            href="magnet:?xt=urn:btih:MAGNETURL&dn=test" class="imagnet icon16">
                            <i class="ka ka16 ka-magnet"></i>
                        </a>
                        <a title="Download torrent file"
                            href="http://torcache.net/torrent/53917.torrent?title=test" class="idownload icon16">
                            <i class="ka ka16 ka-arrow-down"></i>
                        </a>
                    </div>
                    <div class="torrentname">
                    <a href="/test-t6478745.html" class="torType txtType"></a>
                    <a href="/test-t6478745.html" class="normalgrey font12px plain bold"></a>
                    <div class="markeredBlock torType txtType">
                        <a href="/url.html" class="cellMainLink">
                            <strong class="red">This should be the title</strong>
                        </a>
                        <span class="font11px lightgrey block">
                            Posted by <i class="ka ka-verify" style="font-size: 16px;color:orange;"></i>
                            <a class="plain" href="/user/riri/">riri</a> in
                            <span id="cat_6478745">
                                <strong><a href="/other/">Other</a> > <a href="/unsorted/">Unsorted</a></strong>
                            </span>
                        </span>
                    </div>
                </td>
                <td class="nobr center">z bytes</td>
                <td class="center">r</td>
                <td class="center">2&nbsp;years</td>
                <td class="green center">a</td>
                <td class="red lasttd center">t</td>
            </tr>
        </table>
        """
        response = mock.Mock(text=html)
        results = kickass.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 5)
        self.assertEqual(results[0]['title'], 'This should be the title')
        self.assertEqual(results[0]['url'], 'https://kickass.cd/url.html')
        self.assertEqual(results[0]['content'], 'Posted by riri in Other > Unsorted')
        self.assertEqual(results[0]['seed'], 10)
        self.assertEqual(results[0]['leech'], 1)
        self.assertEqual(results[0]['files'], 4)
        self.assertEqual(results[0]['magnetlink'], 'magnet:?xt=urn:btih:MAGNETURL&dn=test')
        self.assertEqual(results[0]['torrentfile'], 'http://torcache.net/torrent/53917.torrent?title=test')
        self.assertEqual(results[0]['filesize'], 1000)
        self.assertEqual(results[1]['filesize'], 1000000)
        self.assertEqual(results[2]['filesize'], 1000000000)
        self.assertEqual(results[3]['filesize'], 1000000000000)
        self.assertEqual(results[4]['seed'], 0)
        self.assertEqual(results[4]['leech'], 0)
        self.assertEqual(results[4]['files'], None)
        self.assertEqual(results[4]['filesize'], None)

Example 38

Project: searx
Source File: test_openstreetmap.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, openstreetmap.response, None)
        self.assertRaises(AttributeError, openstreetmap.response, [])
        self.assertRaises(AttributeError, openstreetmap.response, '')
        self.assertRaises(AttributeError, openstreetmap.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(openstreetmap.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(openstreetmap.response(response), [])

        json = """
        [
          {
            "place_id": "127732055",
            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
            "osm_type": "relation",
            "osm_id": "7444",
            "boundingbox": [
              "48.8155755",
              "48.902156",
              "2.224122",
              "2.4697602"
            ],
            "lat": "48.8565056",
            "lon": "2.3521334",
            "display_name": "This is the title",
            "class": "place",
            "type": "city",
            "importance": 0.96893459932191,
            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
            "address": {
              "city": "Paris",
              "county": "Paris",
              "state": "Île-de-France",
              "country": "France",
              "country_code": "fr"
            },
            "geojson": {
              "type": "Polygon",
              "coordinates": [
                [
                  [
                    2.224122,
                    48.854199
                  ]
                ]
              ]
            }
          }
        ]
        """
        response = mock.Mock(text=json)
        results = openstreetmap.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'https://openstreetmap.org/relation/7444')
        self.assertIn('coordinates', results[0]['geojson'])
        self.assertEqual(results[0]['geojson']['coordinates'][0][0][0], 2.224122)
        self.assertEqual(results[0]['geojson']['coordinates'][0][0][1], 48.854199)
        self.assertEqual(results[0]['address'], None)
        self.assertIn('48.8155755', results[0]['boundingbox'])
        self.assertIn('48.902156', results[0]['boundingbox'])
        self.assertIn('2.224122', results[0]['boundingbox'])
        self.assertIn('2.4697602', results[0]['boundingbox'])

        json = """
        [
          {
            "place_id": "127732055",
            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
            "osm_type": "relation",
            "osm_id": "7444",
            "boundingbox": [
              "48.8155755",
              "48.902156",
              "2.224122",
              "2.4697602"
            ],
            "lat": "48.8565056",
            "lon": "2.3521334",
            "display_name": "This is the title",
            "class": "tourism",
            "type": "city",
            "importance": 0.96893459932191,
            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
            "address": {
              "city": "Paris",
              "county": "Paris",
              "state": "Île-de-France",
              "country": "France",
              "country_code": "fr",
              "address29": "Address"
            },
            "geojson": {
              "type": "Polygon",
              "coordinates": [
                [
                  [
                    2.224122,
                    48.854199
                  ]
                ]
              ]
            }
          },
          {
            "place_id": "127732055",
            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
            "osm_type": "relation",
            "osm_id": "7444",
            "boundingbox": [
              "48.8155755",
              "48.902156",
              "2.224122",
              "2.4697602"
            ],
            "lat": "48.8565056",
            "lon": "2.3521334",
            "display_name": "This is the title",
            "class": "tourism",
            "type": "city",
            "importance": 0.96893459932191,
            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
            "address": {
              "city": "Paris",
              "county": "Paris",
              "state": "Île-de-France",
              "country": "France",
              "postcode": 75000,
              "country_code": "fr"
            },
            "geojson": {
              "type": "Polygon",
              "coordinates": [
                [
                  [
                    2.224122,
                    48.854199
                  ]
                ]
              ]
            }
          },
          {
            "place_id": "127732055",
            "licence": "Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright",
            "osm_type": "node",
            "osm_id": "7444",
            "boundingbox": [
              "48.8155755",
              "48.902156",
              "2.224122",
              "2.4697602"
            ],
            "lat": "48.8565056",
            "lon": "2.3521334",
            "display_name": "This is the title",
            "class": "tourism",
            "type": "city",
            "importance": 0.96893459932191,
            "icon": "https://nominatim.openstreetmap.org/images/mapicons/poi_place_city.p.20.png",
            "address": {
              "city": "Paris",
              "county": "Paris",
              "state": "Île-de-France",
              "country": "France",
              "country_code": "fr",
              "address29": "Address"
            }
          }
        ]
        """
        response = mock.Mock(text=json)
        results = openstreetmap.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 3)
        self.assertIn('48.8565056', results[2]['geojson']['coordinates'])
        self.assertIn('2.3521334', results[2]['geojson']['coordinates'])

Example 39

Project: searx
Source File: test_scanr_structures.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, scanr_structures.response, None)
        self.assertRaises(AttributeError, scanr_structures.response, [])
        self.assertRaises(AttributeError, scanr_structures.response, '')
        self.assertRaises(AttributeError, scanr_structures.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(scanr_structures.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(scanr_structures.response(response), [])

        json = u"""
        {
          "request":
            {
              "query":"test_query",
              "page":1,
              "pageSize":20,
              "sortOrder":"RELEVANCY",
              "sortDirection":"ASC",
              "searchField":"ALL",
              "from":0
            },
          "total":2471,
          "results":[
            {
              "id":"200711886U",
              "label":"Laboratoire d'Informatique de Grenoble",
              "kind":"RNSR",
              "publicEntity":true,
              "address":{"city":"Grenoble","departement":"38"},
              "logo":"/static/logos/200711886U.png",
              "acronym":"LIG",
              "type":{"code":"UR","label":"Unit\xe9 de recherche"},
              "level":2,
              "institutions":[
                {
                  "id":"193819125",
                  "label":"Grenoble INP",
                  "acronym":"IPG",
                  "code":"UMR 5217"
                },
                {
                  "id":"130021397",
                  "label":"Universit\xe9 de Grenoble Alpes",
                  "acronym":"UGA",
                  "code":"UMR 5217"
                },
                {
                  "id":"180089013",
                  "label":"Centre national de la recherche scientifique",
                  "acronym":"CNRS",
                  "code":"UMR 5217"
                },
                {
                  "id":"180089047",
                  "label":"Institut national de recherche en informatique et en automatique",
                  "acronym":"Inria",
                  "code":"UMR 5217"
                }
              ],
              "highlights":[
                {
                  "type":"projects",
                  "value":"linguicielles d\xe9velopp\xe9s jusqu'ici par le GETALP\
 du <strong>LIG</strong> en tant que prototypes op\xe9rationnels.\
\\r\\nDans le contexte"
                },
                {
                  "type":"acronym",
                  "value":"<strong>LIG</strong>"
                },
                {
                  "type":"websiteContents",
                  "value":"S\xe9lection\\nListe structures\\nD\xe9tail\\n\
                    Accueil\\n200711886U : <strong>LIG</strong>\
                    Laboratoire d'Informatique de Grenoble Unit\xe9 de recherche"},
                {
                  "type":"publications",
                  "value":"de noms. Nous avons d'abord d\xe9velopp\xe9 LOOV \
                    (pour <strong>Lig</strong> Overlaid OCR in Vid\xe9o), \
                    un outil d'extraction des"
                }
              ]
            },
            {
              "id":"199511665F",
              "label":"Laboratoire Bordelais de Recherche en Informatique",
              "kind":"RNSR",
              "publicEntity":true,
              "address":{"city":"Talence","departement":"33"},
              "logo":"/static/logos/199511665F.png",
              "acronym":"LaBRI",
              "type":{"code":"UR","label":"Unit\xe9 de recherche"},
              "level":2,
              "institutions":[
                {
                  "id":"130006356",
                  "label":"Institut polytechnique de Bordeaux",
                  "acronym":"IPB",
                  "code":"UMR 5800"
                },
                {
                  "id":"130018351",
                  "label":"Universit\xe9 de Bordeaux",
                  "acronym":null,
                  "code":"UMR 5800"
                },
                {
                  "id":"180089013",
                  "label":"Centre national de la recherche scientifique",
                  "acronym":"CNRS",
                  "code":"UMR 5800"
                },
                {
                  "id":"180089047",
                  "label":"Institut national de recherche en informatique et en automatique",
                  "acronym":"Inria",
                  "code":"UMR 5800"
                }
              ],
              "highlights":[
                {
                  "type":"websiteContents",
                  "value":"Samia Kerdjoudj\\n2016-07-05\\nDouble-exponential\
 and <strong>triple</strong>-exponential bounds for\
 choosability problems parameterized"
                },
                {
                  "type":"publications",
                  "value":"de cam\xe9ras install\xe9es dans les lieux publiques \
 a <strong>tripl\xe9</strong> en 2009, passant de 20 000 \
 \xe0 60 000. Malgr\xe9 le"
                }
              ]
            }
          ]
        }
        """
        response = mock.Mock(text=json)
        results = scanr_structures.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual(results[0]['title'], u"Laboratoire d'Informatique de Grenoble")
        self.assertEqual(results[0]['url'], 'https://scanr.enseignementsup-recherche.gouv.fr/structure/200711886U')
        self.assertEqual(results[0]['content'],
                         u"linguicielles d\xe9velopp\xe9s jusqu'ici par le GETALP "
                         u"du LIG en tant que prototypes "
                         u"op\xe9rationnels. Dans le contexte")
        self.assertEqual(results[1]['img_src'],
                         'https://scanr.enseignementsup-recherche.gouv.fr//static/logos/199511665F.png')
        self.assertEqual(results[1]['content'],
                         "Samia Kerdjoudj 2016-07-05 Double-exponential and"
                         " triple-exponential bounds for "
                         "choosability problems parameterized")
        self.assertEqual(results[1]['url'], 'https://scanr.enseignementsup-recherche.gouv.fr/structure/199511665F')
        self.assertEqual(results[1]['title'], u"Laboratoire Bordelais de Recherche en Informatique")

Example 40

Project: searx
Source File: test_soundcloud.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, soundcloud.response, None)
        self.assertRaises(AttributeError, soundcloud.response, [])
        self.assertRaises(AttributeError, soundcloud.response, '')
        self.assertRaises(AttributeError, soundcloud.response, '[]')

        response = mock.Mock(text='{}')
        self.assertEqual(soundcloud.response(response), [])

        response = mock.Mock(text='{"data": []}')
        self.assertEqual(soundcloud.response(response), [])

        json = """
        {
        "collection": [
            {
            "kind": "track",
            "id": 159723640,
            "created_at": "2014/07/22 00:51:21 +0000",
            "user_id": 2976616,
            "duration": 303780,
            "commentable": true,
            "state": "finished",
            "original_content_size": 13236349,
            "last_modified": "2015/01/31 15:14:50 +0000",
            "sharing": "public",
            "tag_list": "seekae flume",
            "permalink": "seekae-test-recognise-flume-re-work",
            "streamable": true,
            "embeddable_by": "all",
            "downloadable": true,
            "purchase_url": "http://www.facebook.com/seekaemusic",
            "label_id": null,
            "purchase_title": "Seekae",
            "genre": "freedownload",
            "title": "This is the title",
            "description": "This is the content",
            "label_name": "Future Classic",
            "release": "",
            "track_type": "remix",
            "key_signature": "",
            "isrc": "",
            "video_url": null,
            "bpm": null,
            "release_year": 2014,
            "release_month": 7,
            "release_day": 22,
            "original_format": "mp3",
            "license": "all-rights-reserved",
            "uri": "https://api.soundcloud.com/tracks/159723640",
            "user": {
                "id": 2976616,
                "kind": "user",
                "permalink": "flume",
                "username": "Flume",
                "last_modified": "2014/11/24 19:21:29 +0000",
                "uri": "https://api.soundcloud.com/users/2976616",
                "permalink_url": "http://soundcloud.com/flume",
                "avatar_url": "https://i1.sndcdn.com/avatars-000044475439-4zi7ii-large.jpg"
            },
            "permalink_url": "http://soundcloud.com/this.is.the.url",
            "artwork_url": "https://i1.sndcdn.com/artworks-000085857162-xdxy5c-large.jpg",
            "waveform_url": "https://w1.sndcdn.com/DWrL1lAN8BkP_m.png",
            "stream_url": "https://api.soundcloud.com/tracks/159723640/stream",
            "download_url": "https://api.soundcloud.com/tracks/159723640/download",
            "playback_count": 2190687,
            "download_count": 54856,
            "favoritings_count": 49061,
            "comment_count": 826,
            "likes_count": 49061,
            "reposts_count": 15910,
            "attachments_uri": "https://api.soundcloud.com/tracks/159723640/attachments",
            "policy": "ALLOW"
            }
        ],
        "total_results": 375750,
        "next_href": "https://api.soundcloud.com/search?&q=test",
        "tx_id": ""
        }
        """
        response = mock.Mock(text=json)
        results = soundcloud.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the title')
        self.assertEqual(results[0]['url'], 'http://soundcloud.com/this.is.the.url')
        self.assertEqual(results[0]['content'], 'This is the content')
        self.assertIn(quote_plus('https://api.soundcloud.com/tracks/159723640'), results[0]['embedded'])

        json = """
        {
        "collection": [
            {
            "kind": "user",
            "id": 159723640,
            "created_at": "2014/07/22 00:51:21 +0000",
            "user_id": 2976616,
            "duration": 303780,
            "commentable": true,
            "state": "finished",
            "original_content_size": 13236349,
            "last_modified": "2015/01/31 15:14:50 +0000",
            "sharing": "public",
            "tag_list": "seekae flume",
            "permalink": "seekae-test-recognise-flume-re-work",
            "streamable": true,
            "embeddable_by": "all",
            "downloadable": true,
            "purchase_url": "http://www.facebook.com/seekaemusic",
            "label_id": null,
            "purchase_title": "Seekae",
            "genre": "freedownload",
            "title": "This is the title",
            "description": "This is the content",
            "label_name": "Future Classic",
            "release": "",
            "track_type": "remix",
            "key_signature": "",
            "isrc": "",
            "video_url": null,
            "bpm": null,
            "release_year": 2014,
            "release_month": 7,
            "release_day": 22,
            "original_format": "mp3",
            "license": "all-rights-reserved",
            "uri": "https://api.soundcloud.com/tracks/159723640",
            "user": {
                "id": 2976616,
                "kind": "user",
                "permalink": "flume",
                "username": "Flume",
                "last_modified": "2014/11/24 19:21:29 +0000",
                "uri": "https://api.soundcloud.com/users/2976616",
                "permalink_url": "http://soundcloud.com/flume",
                "avatar_url": "https://i1.sndcdn.com/avatars-000044475439-4zi7ii-large.jpg"
            },
            "permalink_url": "http://soundcloud.com/this.is.the.url",
            "artwork_url": "https://i1.sndcdn.com/artworks-000085857162-xdxy5c-large.jpg",
            "waveform_url": "https://w1.sndcdn.com/DWrL1lAN8BkP_m.png",
            "stream_url": "https://api.soundcloud.com/tracks/159723640/stream",
            "download_url": "https://api.soundcloud.com/tracks/159723640/download",
            "playback_count": 2190687,
            "download_count": 54856,
            "favoritings_count": 49061,
            "comment_count": 826,
            "likes_count": 49061,
            "reposts_count": 15910,
            "attachments_uri": "https://api.soundcloud.com/tracks/159723640/attachments",
            "policy": "ALLOW"
            }
        ],
        "total_results": 375750,
        "next_href": "https://api.soundcloud.com/search?&q=test",
        "tx_id": ""
        }
        """
        response = mock.Mock(text=json)
        results = soundcloud.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

        json = """
        {
        "collection": [],
        "total_results": 375750,
        "next_href": "https://api.soundcloud.com/search?&q=test",
        "tx_id": ""
        }
        """
        response = mock.Mock(text=json)
        results = soundcloud.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

Example 41

Project: searx
Source File: test_subtitleseeker.py
View license
    def test_response(self):
        dicto = defaultdict(dict)
        dicto['language'] = 'fr_FR'
        response = mock.Mock(search_params=dicto)

        self.assertRaises(AttributeError, subtitleseeker.response, None)
        self.assertRaises(AttributeError, subtitleseeker.response, [])
        self.assertRaises(AttributeError, subtitleseeker.response, '')
        self.assertRaises(AttributeError, subtitleseeker.response, '[]')

        response = mock.Mock(text='<html></html>', search_params=dicto)
        self.assertEqual(subtitleseeker.response(response), [])

        html = """
        <div class="boxRows">
            <div class="boxRowsInner" style="width:600px;">
                <img src="http://static.subtitleseeker.com/images/movie.gif"
                    style="width:16px; height:16px;" class="icon">
                <a href="http://this.is.the.url/"
                    class="blue" title="Title subtitle" >
                    This is the Title
                </a>
                <br><br>
                <span class="f10b grey-dark arial" style="padding:0px 0px 5px 20px">
                    "Alternative Title"
                </span>
            </div>
            <div class="boxRowsInner f12b red" style="width:70px;">
                1998
            </div>
            <div class="boxRowsInner grey-web f12" style="width:120px;">
                <img src="http://static.subtitleseeker.com/images/basket_put.png"
                    style="width:16px; height:16px;" class="icon">
                1039 Subs
            </div>
            <div class="boxRowsInner grey-web f10" style="width:130px;">
                <img src="http://static.subtitleseeker.com/images/arrow_refresh_small.png"
                    style="width:16px; height:16px;" class="icon">
                1 hours ago
            </div>
            <div class="clear"></div>
        </div>
        """
        response = mock.Mock(text=html, search_params=dicto)
        results = subtitleseeker.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the Title')
        self.assertEqual(results[0]['url'], 'http://this.is.the.url/French/')
        self.assertIn('1998', results[0]['content'])
        self.assertIn('1039 Subs', results[0]['content'])
        self.assertIn('Alternative Title', results[0]['content'])

        html = """
        <div class="boxRows">
            <div class="boxRowsInner" style="width:600px;">
                <img src="http://static.subtitleseeker.com/images/movie.gif"
                    style="width:16px; height:16px;" class="icon">
                <a href="http://this.is.the.url/"
                    class="blue" title="Title subtitle" >
                    This is the Title
                </a>
            </div>
            <div class="boxRowsInner f12b red" style="width:70px;">
                1998
            </div>
            <div class="boxRowsInner grey-web f12" style="width:120px;">
                <img src="http://static.subtitleseeker.com/images/basket_put.png"
                    style="width:16px; height:16px;" class="icon">
                1039 Subs
            </div>
            <div class="boxRowsInner grey-web f10" style="width:130px;">
                <img src="http://static.subtitleseeker.com/images/arrow_refresh_small.png"
                    style="width:16px; height:16px;" class="icon">
                1 hours ago
            </div>
            <div class="clear"></div>
        </div>
        """
        dicto['language'] = 'all'
        response = mock.Mock(text=html, search_params=dicto)
        results = subtitleseeker.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the Title')
        self.assertEqual(results[0]['url'], 'http://this.is.the.url/')
        self.assertIn('1998', results[0]['content'])
        self.assertIn('1039 Subs', results[0]['content'])

        html = """
        <div class="boxRows">
            <div class="boxRowsInner" style="width:600px;">
                <img src="http://static.subtitleseeker.com/images/movie.gif"
                    style="width:16px; height:16px;" class="icon">
                <a href="http://this.is.the.url/"
                    class="blue" title="Title subtitle" >
                    This is the Title
                </a>
            </div>
            <div class="boxRowsInner f12b red" style="width:70px;">
                1998
            </div>
            <div class="boxRowsInner grey-web f12" style="width:120px;">
                <img src="http://static.subtitleseeker.com/images/basket_put.png"
                    style="width:16px; height:16px;" class="icon">
                1039 Subs
            </div>
            <div class="boxRowsInner grey-web f10" style="width:130px;">
                <img src="http://static.subtitleseeker.com/images/arrow_refresh_small.png"
                    style="width:16px; height:16px;" class="icon">
                1 hours ago
            </div>
            <div class="clear"></div>
        </div>
        """
        subtitleseeker.language = 'English'
        response = mock.Mock(text=html, search_params=dicto)
        results = subtitleseeker.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], 'This is the Title')
        self.assertEqual(results[0]['url'], 'http://this.is.the.url/English/')
        self.assertIn('1998', results[0]['content'])
        self.assertIn('1039 Subs', results[0]['content'])

        html = """
        <div class="boxRowsInner" style="width:600px;">
            <img src="http://static.subtitleseeker.com/images/movie.gif"
                style="width:16px; height:16px;" class="icon">
            <a href="http://this.is.the.url/"
                class="blue" title="Title subtitle" >
                This is the Title
            </a>
        </div>
        <div class="boxRowsInner f12b red" style="width:70px;">
            1998
        </div>
        <div class="boxRowsInner grey-web f12" style="width:120px;">
            <img src="http://static.subtitleseeker.com/images/basket_put.png"
                style="width:16px; height:16px;" class="icon">
            1039 Subs
        </div>
        <div class="boxRowsInner grey-web f10" style="width:130px;">
            <img src="http://static.subtitleseeker.com/images/arrow_refresh_small.png"
                style="width:16px; height:16px;" class="icon">
            1 hours ago
        </div>
        """
        response = mock.Mock(text=html, search_params=dicto)
        results = subtitleseeker.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

Example 42

Project: searx
Source File: test_twitter.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, twitter.response, None)
        self.assertRaises(AttributeError, twitter.response, [])
        self.assertRaises(AttributeError, twitter.response, '')
        self.assertRaises(AttributeError, twitter.response, '[]')

        response = mock.Mock(text='<html></html>')
        self.assertEqual(twitter.response(response), [])

        html = """
        <li class="js-stream-item stream-item stream-item expanding-stream-item" data-item-id="563005573290287105"
            id="stream-item-tweet-563005573290287105" data-item-type="tweet">
            <div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable
                js-original-tweet has-cards has-native-media" data-tweet-id="563005573290287105" data-disclosure-type=""
                data-item-id="563005573290287105" data-screen-name="Jalopnik" data-name="Jalopnik"
                data-user-id="3060631" data-has-native-media="true" data-has-cards="true" data-card-type="photo"
                data-expanded-footer="<div class=&quot;js-tweet-details-fixer
                tweet-details-fixer&quot;>&#10;&#10;&#10;
                <div class=&quot;cards-media-container js-media-container&quot;><div
                data-card-url=&quot;//twitter.com/Jalopnik/status/563005573290287105/photo/1&quot; data-card-type=&quot;
                photo&quot; class=&quot;cards-base cards-multimedia&quot; data-element-context=&quot;platform_photo_card
                &quot;>&#10;&#10;&#10;  <a class=&quot;media media-thumbnail twitter-timeline-link is-preview
                &quot; data-url=&quot;https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large&quot;
                data-resolved-url-large=&quot;https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large&quot;
                href=&quot;//twitter.com/Jalopnik/status/563005573290287105/photo/1&quot;>&#10;
                <div class=&quot;&quot;>&#10; <img src=&quot;
                https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg&quot;
                alt=&quot;Embedded image permalink&quot; width=&quot;636&quot; height=&quot;309&quot;>&#10;
                </div>&#10;&#10;  </a>&#10;&#10;  <div class=&quot;cards-content&quot;>&#10;
                <div class=&quot;byline&quot;>&#10;      &#10;    </div>&#10;    &#10;  </div>&#10;
                &#10;</div>&#10;&#10;&#10;&#10;&#10;</div>&#10;&#10;&#10;&#10;  <div
                class=&quot;js-machine-translated-tweet-container&quot;></div>&#10;    <div
                class=&quot;js-tweet-stats-container tweet-stats-container &quot;>&#10;    </div>&#10;&#10;
                <div class=&quot;client-and-actions&quot;>&#10;  <span class=&quot;metadata&quot;>&#10;
                <span>5:06 PM - 4 Feb 2015</span>&#10;&#10;       &amp;middot; <a
                class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/Jalopnik/status/563005573290287105
                &quot;tabindex=&quot;-1&quot;>Details</a>&#10;    &#10;&#10;        &#10;        &#10;
                &#10;&#10;  </span>&#10;</div>&#10;&#10;&#10;</div>&#10;" data-you-follow="false"
                data-you-block="false">
                <div class="context">
                </div>
                <div class="content">
                    <div class="stream-item-header">
                        <a class="account-group js-account-group js-action-profile js-user-profile-link js-nav"
                            href="/Jalopnik" data-user-id="3060631">
                            <img class="avatar js-action-profile-avatar"
                                src="https://pbs.twimg.com/profile_images/2976430168/5cd4a59_bigger.jpeg" alt="">
                            <strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part>
                                Jalopnik
                            </strong>
                            <span>&rlm;</span>
                            <span class="username js-action-profile-name" data-aria-label-part>
                            <s>@</s><b>TitleName</b>
                            </span>
                        </a>
                        <small class="time">
                        <a href="/this.is.the.url"
                            class="tweet-timestamp js-permalink js-nav js-tooltip" title="5:06 PM - 4 Feb 2015" >
                            <span class="u-hiddenVisually" data-aria-label-part="last">17 minutes ago</span>
                        </a>
                        </small>
                    </div>
                    <p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0">
                        This is the content étude à€
                        <a href="http://t.co/nRWsqQAwBL" rel="nofollow" dir="ltr"
                            data-expanded-url="http://jalo.ps/ReMENu4" class="twitter-timeline-link"
                            target="_blank" title="http://jalo.ps/ReMENu4" >
                        <span class="tco-ellipsis">
                        </span>
                        <span class="invisible">http://</span><span class="js-display-url">link.in.tweet</span>
                        <span class="invisible"></span>
                        <span class="tco-ellipsis">
                            <span class="invisible">&nbsp;</span>
                        </span>
                    </a>
                    <a href="http://t.co/rbFsfeE0l3" class="twitter-timeline-link u-hidden"
                        data-pre-embedded="true" dir="ltr">
                        pic.twitter.com/rbFsfeE0l3
                    </a>
                    </p>
                    <div class="expanded-content js-tweet-details-dropdown">
                    </div>
                    <div class="stream-item-footer">
                        <a class="details with-icn js-details" href="/Jalopnik/status/563005573290287105">
                            <span class="Icon Icon--photo">
                            </span>
                            <b>
                                <span class="expand-stream-item js-view-details">
                                    View photo
                                </span>
                                <span class="collapse-stream-item  js-hide-details">
                                    Hide photo
                                </span>
                            </b>
                        </a>
                        <span class="ProfileTweet-action--reply u-hiddenVisually">
                            <span class="ProfileTweet-actionCount" aria-hidden="true" data-tweet-stat-count="0">
                                <span class="ProfileTweet-actionCountForAria" >0 replies</span>
                            </span>
                        </span>
                        <span class="ProfileTweet-action--retweet u-hiddenVisually">
                            <span class="ProfileTweet-actionCount"  data-tweet-stat-count="8">
                                <span class="ProfileTweet-actionCountForAria" data-aria-label-part>8 retweets</span>
                            </span>
                        </span>
                        <span class="ProfileTweet-action--favorite u-hiddenVisually">
                            <span class="ProfileTweet-actionCount"  data-tweet-stat-count="14">
                                <span class="ProfileTweet-actionCountForAria" data-aria-label-part>14 favorites</span>
                            </span>
                        </span>
                        <div role="group" aria-label="Tweet actions" class="ProfileTweet-actionList u-cf js-actions">
                            <div class="ProfileTweet-action ProfileTweet-action--reply">
                                <button class="ProfileTweet-actionButton u-textUserColorHover js-actionButton
                                    js-actionReply" data-modal="ProfileTweet-reply" type="button" title="Reply">
                                    <span class="Icon Icon--reply">
                                    </span>
                                    <span class="u-hiddenVisually">Reply</span>
                                    <span class="ProfileTweet-actionCount u-textUserColorHover
                                        ProfileTweet-actionCount--isZero">
                                        <span class="ProfileTweet-actionCountForPresentation" aria-hidden="true">
                                        </span>
                                    </span>
                                </button>
                            </div>
                            <div class="ProfileTweet-action ProfileTweet-action--retweet js-toggleState js-toggleRt">
                                <button class="ProfileTweet-actionButton  js-actionButton js-actionRetweet js-tooltip"
                                    title="Retweet" data-modal="ProfileTweet-retweet" type="button">
                                    <span class="Icon Icon--retweet">
                                    </span>
                                    <span class="u-hiddenVisually">Retweet</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">8</span>
                                    </span>
                                </button>
                                <button class="ProfileTweet-actionButtonUndo js-actionButton js-actionRetweet"
                                    data-modal="ProfileTweet-retweet" title="Undo retweet" type="button">
                                    <span class="Icon Icon--retweet">
                                    </span>
                                    <span class="u-hiddenVisually">Retweeted</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">8</span>
                                    </span>
                                </button>
                            </div>
                            <div class="ProfileTweet-action ProfileTweet-action--favorite js-toggleState">
                                <button class="ProfileTweet-actionButton js-actionButton js-actionFavorite js-tooltip"
                                    title="Favorite" type="button">
                                    <span class="Icon Icon--favorite">
                                    </span>
                                    <span class="u-hiddenVisually">Favorite</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">14</span>
                                    </span>
                                </button>
                                <button class="ProfileTweet-actionButtonUndo u-linkClean js-actionButton
                                    js-actionFavorite" title="Undo favorite" type="button">
                                    <span class="Icon Icon--favorite">
                                    </span>
                                    <span class="u-hiddenVisually">Favorited</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">
                                            14
                                        </span>
                                    </span>
                                </button>
                            </div>
                            <div class="ProfileTweet-action ProfileTweet-action--more js-more-ProfileTweet-actions">
                                <div class="dropdown">
                                    <button class="ProfileTweet-actionButton u-textUserColorHover dropdown-toggle
                                        js-tooltip js-dropdown-toggle" type="button" title="More">
                                        <span class="Icon Icon--dots">
                                        </span>
                                        <span class="u-hiddenVisually">More</span>
                                    </button>
                                    <div class="dropdown-menu">
                                        <div class="dropdown-caret">
                                            <div class="caret-outer">
                                            </div>
                                            <div class="caret-inner">
                                            </div>
                                        </div>
                                        <ul>
                                            <li class="share-via-dm js-actionShareViaDM" data-nav="share_tweet_dm">
                                                <button type="button" class="dropdown-link">
                                                    Share via Direct Message
                                                </button>
                                            </li>
                                            <li class="embed-link js-actionEmbedTweet" data-nav="embed_tweet">
                                                <button type="button" class="dropdown-link">
                                                    Embed Tweet
                                                </button>
                                            </li>
                                            <li class="mute-user-item pretty-link">
                                                <button type="button" class="dropdown-link">
                                                    Mute
                                                </button>
                                            </li>
                                            <li class="unmute-user-item pretty-link">
                                                <button type="button" class="dropdown-link">
                                                    Unmute
                                                </button>
                                            </li>
                                            <li class="block-or-report-link js-actionBlockOrReport"
                                                data-nav="block_or_report">
                                                <button type="button" class="dropdown-link">
                                                    Block or report
                                                </button>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </li>
        """
        response = mock.Mock(text=html)
        results = twitter.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], '@TitleName')
        self.assertEqual(results[0]['url'], 'https://twitter.com/this.is.the.url')
        self.assertIn(u'This is the content', results[0]['content'])
        # self.assertIn(u'This is the content étude à€', results[0]['content'])

        html = """
        <li class="js-stream-item stream-item stream-item expanding-stream-item" data-item-id="563005573290287105"
            id="stream-item-tweet-563005573290287105" data-item-type="tweet">
            <div class="tweet original-tweet js-stream-tweet js-actionable-tweet js-profile-popup-actionable
                js-original-tweet has-cards has-native-media" data-tweet-id="563005573290287105" data-disclosure-type=""
                data-item-id="563005573290287105" data-screen-name="Jalopnik" data-name="Jalopnik"
                data-user-id="3060631" data-has-native-media="true" data-has-cards="true" data-card-type="photo"
                data-expanded-footer="<div class=&quot;js-tweet-details-fixer
                tweet-details-fixer&quot;>&#10;&#10;&#10;
                <div class=&quot;cards-media-container js-media-container&quot;><div
                data-card-url=&quot;//twitter.com/Jalopnik/status/563005573290287105/photo/1&quot; data-card-type=&quot;
                photo&quot; class=&quot;cards-base cards-multimedia&quot; data-element-context=&quot;platform_photo_card
                &quot;>&#10;&#10;&#10;  <a class=&quot;media media-thumbnail twitter-timeline-link is-preview
                &quot; data-url=&quot;https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large&quot;
                data-resolved-url-large=&quot;https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg:large&quot;
                href=&quot;//twitter.com/Jalopnik/status/563005573290287105/photo/1&quot;>&#10;
                <div class=&quot;&quot;>&#10; <img src=&quot;
                https://pbs.twimg.com/media/B9Aylf5IMAAuziP.jpg&quot;
                alt=&quot;Embedded image permalink&quot; width=&quot;636&quot; height=&quot;309&quot;>&#10;
                </div>&#10;&#10;  </a>&#10;&#10;  <div class=&quot;cards-content&quot;>&#10;
                <div class=&quot;byline&quot;>&#10;      &#10;    </div>&#10;    &#10;  </div>&#10;
                &#10;</div>&#10;&#10;&#10;&#10;&#10;</div>&#10;&#10;&#10;&#10;  <div
                class=&quot;js-machine-translated-tweet-container&quot;></div>&#10;    <div
                class=&quot;js-tweet-stats-container tweet-stats-container &quot;>&#10;    </div>&#10;&#10;
                <div class=&quot;client-and-actions&quot;>&#10;  <span class=&quot;metadata&quot;>&#10;
                <span>5:06 PM - 4 Feb 2015</span>&#10;&#10;       &amp;middot; <a
                class=&quot;permalink-link js-permalink js-nav&quot; href=&quot;/Jalopnik/status/563005573290287105
                &quot;tabindex=&quot;-1&quot;>Details</a>&#10;    &#10;&#10;        &#10;        &#10;
                &#10;&#10;  </span>&#10;</div>&#10;&#10;&#10;</div>&#10;" data-you-follow="false"
                data-you-block="false">
                <div class="context">
                </div>
                <div class="content">
                    <div class="stream-item-header">
                        <a class="account-group js-account-group js-action-profile js-user-profile-link js-nav"
                            href="/Jalopnik" data-user-id="3060631">
                            <img class="avatar js-action-profile-avatar"
                                src="https://pbs.twimg.com/profile_images/2976430168/5cd4a59_bigger.jpeg" alt="">
                            <strong class="fullname js-action-profile-name show-popup-with-id" data-aria-label-part>
                                Jalopnik
                            </strong>
                            <span>&rlm;</span>
                            <span class="username js-action-profile-name" data-aria-label-part>
                            <s>@</s><b>TitleName</b>
                            </span>
                        </a>
                        <small class="time">
                        <a href="/this.is.the.url"
                            class="tweet-timestamp js-permalink js-nav js-tooltip" title="5:06 PM - 4 Feb 2015" >
                            <span class="_timestamp js-short-timestamp js-relative-timestamp"  data-time="1423065963"
                                data-time-ms="1423065963000" data-long-form="true" aria-hidden="true">
                                17m
                            </span>
                            <span class="u-hiddenVisually" data-aria-label-part="last">17 minutes ago</span>
                        </a>
                        </small>
                    </div>
                    <p class="js-tweet-text tweet-text" lang="en" data-aria-label-part="0">
                        This is the content étude à€
                        <a href="http://t.co/nRWsqQAwBL" rel="nofollow" dir="ltr"
                            data-expanded-url="http://jalo.ps/ReMENu4" class="twitter-timeline-link"
                            target="_blank" title="http://jalo.ps/ReMENu4" >
                        <span class="tco-ellipsis">
                        </span>
                        <span class="invisible">http://</span><span class="js-display-url">link.in.tweet</span>
                        <span class="invisible"></span>
                        <span class="tco-ellipsis">
                            <span class="invisible">&nbsp;</span>
                        </span>
                    </a>
                    <a href="http://t.co/rbFsfeE0l3" class="twitter-timeline-link u-hidden"
                        data-pre-embedded="true" dir="ltr">
                        pic.twitter.com/rbFsfeE0l3
                    </a>
                    </p>
                    <div class="expanded-content js-tweet-details-dropdown">
                    </div>
                    <div class="stream-item-footer">
                        <a class="details with-icn js-details" href="/Jalopnik/status/563005573290287105">
                            <span class="Icon Icon--photo">
                            </span>
                            <b>
                                <span class="expand-stream-item js-view-details">
                                    View photo
                                </span>
                                <span class="collapse-stream-item  js-hide-details">
                                    Hide photo
                                </span>
                            </b>
                        </a>
                        <span class="ProfileTweet-action--reply u-hiddenVisually">
                            <span class="ProfileTweet-actionCount" aria-hidden="true" data-tweet-stat-count="0">
                                <span class="ProfileTweet-actionCountForAria" >0 replies</span>
                            </span>
                        </span>
                        <span class="ProfileTweet-action--retweet u-hiddenVisually">
                            <span class="ProfileTweet-actionCount"  data-tweet-stat-count="8">
                                <span class="ProfileTweet-actionCountForAria" data-aria-label-part>8 retweets</span>
                            </span>
                        </span>
                        <span class="ProfileTweet-action--favorite u-hiddenVisually">
                            <span class="ProfileTweet-actionCount"  data-tweet-stat-count="14">
                                <span class="ProfileTweet-actionCountForAria" data-aria-label-part>14 favorites</span>
                            </span>
                        </span>
                        <div role="group" aria-label="Tweet actions" class="ProfileTweet-actionList u-cf js-actions">
                            <div class="ProfileTweet-action ProfileTweet-action--reply">
                                <button class="ProfileTweet-actionButton u-textUserColorHover js-actionButton
                                    js-actionReply" data-modal="ProfileTweet-reply" type="button" title="Reply">
                                    <span class="Icon Icon--reply">
                                    </span>
                                    <span class="u-hiddenVisually">Reply</span>
                                    <span class="ProfileTweet-actionCount u-textUserColorHover
                                        ProfileTweet-actionCount--isZero">
                                        <span class="ProfileTweet-actionCountForPresentation" aria-hidden="true">
                                        </span>
                                    </span>
                                </button>
                            </div>
                            <div class="ProfileTweet-action ProfileTweet-action--retweet js-toggleState js-toggleRt">
                                <button class="ProfileTweet-actionButton  js-actionButton js-actionRetweet js-tooltip"
                                    title="Retweet" data-modal="ProfileTweet-retweet" type="button">
                                    <span class="Icon Icon--retweet">
                                    </span>
                                    <span class="u-hiddenVisually">Retweet</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">8</span>
                                    </span>
                                </button>
                                <button class="ProfileTweet-actionButtonUndo js-actionButton js-actionRetweet"
                                    data-modal="ProfileTweet-retweet" title="Undo retweet" type="button">
                                    <span class="Icon Icon--retweet">
                                    </span>
                                    <span class="u-hiddenVisually">Retweeted</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">8</span>
                                    </span>
                                </button>
                            </div>
                            <div class="ProfileTweet-action ProfileTweet-action--favorite js-toggleState">
                                <button class="ProfileTweet-actionButton js-actionButton js-actionFavorite js-tooltip"
                                    title="Favorite" type="button">
                                    <span class="Icon Icon--favorite">
                                    </span>
                                    <span class="u-hiddenVisually">Favorite</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">14</span>
                                    </span>
                                </button>
                                <button class="ProfileTweet-actionButtonUndo u-linkClean js-actionButton
                                    js-actionFavorite" title="Undo favorite" type="button">
                                    <span class="Icon Icon--favorite">
                                    </span>
                                    <span class="u-hiddenVisually">Favorited</span>
                                    <span class="ProfileTweet-actionCount">
                                        <span class="ProfileTweet-actionCountForPresentation">
                                            14
                                        </span>
                                    </span>
                                </button>
                            </div>
                            <div class="ProfileTweet-action ProfileTweet-action--more js-more-ProfileTweet-actions">
                                <div class="dropdown">
                                    <button class="ProfileTweet-actionButton u-textUserColorHover dropdown-toggle
                                        js-tooltip js-dropdown-toggle" type="button" title="More">
                                        <span class="Icon Icon--dots">
                                        </span>
                                        <span class="u-hiddenVisually">More</span>
                                    </button>
                                    <div class="dropdown-menu">
                                        <div class="dropdown-caret">
                                            <div class="caret-outer">
                                            </div>
                                            <div class="caret-inner">
                                            </div>
                                        </div>
                                        <ul>
                                            <li class="share-via-dm js-actionShareViaDM" data-nav="share_tweet_dm">
                                                <button type="button" class="dropdown-link">
                                                    Share via Direct Message
                                                </button>
                                            </li>
                                            <li class="embed-link js-actionEmbedTweet" data-nav="embed_tweet">
                                                <button type="button" class="dropdown-link">
                                                    Embed Tweet
                                                </button>
                                            </li>
                                            <li class="mute-user-item pretty-link">
                                                <button type="button" class="dropdown-link">
                                                    Mute
                                                </button>
                                            </li>
                                            <li class="unmute-user-item pretty-link">
                                                <button type="button" class="dropdown-link">
                                                    Unmute
                                                </button>
                                            </li>
                                            <li class="block-or-report-link js-actionBlockOrReport"
                                                data-nav="block_or_report">
                                                <button type="button" class="dropdown-link">
                                                    Block or report
                                                </button>
                                            </li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </li>
        """
        response = mock.Mock(text=html)
        results = twitter.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 1)
        self.assertEqual(results[0]['title'], '@TitleName')
        self.assertEqual(results[0]['url'], 'https://twitter.com/this.is.the.url')
        self.assertIn(u'This is the content', results[0]['content'])

        html = """
        <li class="b_algo" u="0|5109|4755453613245655|UAGjXgIrPH5yh-o5oNHRx_3Zta87f_QO">
            <div Class="sa_mc">
                <div class="sb_tlst">
                    <h2>
                        <a href="http://this.should.be.the.link/" h="ID=SERP,5124.1">
                        <strong>This</strong> should be the title</a>
                    </h2>
                </div>
                <div class="sb_meta">
                <cite>
                <strong>this</strong>.meta.com</cite>
                    <span class="c_tlbxTrg">
                        <span class="c_tlbxH" H="BASE:CACHEDPAGEDEFAULT" K="SERP,5125.1">
                        </span>
                    </span>
                </div>
                <p>
                <strong>This</strong> should be the content.</p>
            </div>
        </li>
        """
        response = mock.Mock(text=html)
        results = twitter.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 0)

Example 43

Project: searx
Source File: test_wolframalpha_noapi.py
View license
    def test_response(self):
        self.assertRaises(AttributeError, wolframalpha_noapi.response, None)
        self.assertRaises(AttributeError, wolframalpha_noapi.response, [])
        self.assertRaises(AttributeError, wolframalpha_noapi.response, '')
        self.assertRaises(AttributeError, wolframalpha_noapi.response, '[]')

        referer_url = 'referer_url'
        request = Request(headers={'Referer': referer_url})

        # test failure
        json = r'''
        {"queryresult" : {
            "success" : false,
            "error" : false,
            "numpods" : 0,
            "id" : "",
            "host" : "https:\/\/www5a.wolframalpha.com",
            "didyoumeans" : {}
        }}
        '''
        response = mock.Mock(text=json, request=request)
        self.assertEqual(wolframalpha_noapi.response(response), [])

        # test basic case
        json = r'''
        {"queryresult" : {
            "success" : true,
            "error" : false,
            "numpods" : 6,
            "datatypes" : "Math",
            "id" : "queryresult_id",
            "host" : "https:\/\/www5b.wolframalpha.com",
            "related" : "related_url",
            "version" : "2.6",
            "pods" : [
                {
                    "title" : "Input",
                    "scanners" : [
                        "Identity"
                    ],
                    "id" : "Input",
                    "error" : false,
                    "numsubpods" : 1,
                    "subpods" : [
                        {
                            "title" : "",
                            "img" : {
                                "src" : "input_img_src.gif",
                                "alt" : "input_img_alt",
                                "title" : "input_img_title"
                            },
                            "plaintext" : "input_plaintext",
                            "minput" : "input_minput"
                        }
                    ]
                },
                {
                    "title" : "Result",
                    "scanners" : [
                        "Simplification"
                    ],
                    "id" : "Result",
                    "error" : false,
                    "numsubpods" : 1,
                    "primary" : true,
                    "subpods" : [
                        {
                            "title" : "",
                            "img" : {
                                "src" : "result_img_src.gif",
                                "alt" : "result_img_alt",
                                "title" : "result_img_title"
                            },
                            "plaintext" : "result_plaintext",
                            "moutput" : "result_moutput"
                        }
                    ]
                },
                {
                    "title" : "Manipulatives illustration",
                    "scanners" : [
                        "Arithmetic"
                    ],
                    "id" : "Illustration",
                    "error" : false,
                    "numsubpods" : 1,
                    "subpods" : [
                        {
                            "title" : "",
                            "CDFcontent" : "Resizeable",
                            "img" : {
                                "src" : "illustration_img_src.gif",
                                "alt" : "illustration_img_alt",
                                "title" : "illustration_img_title"
                            },
                            "plaintext" : "illustration_img_plaintext"
                        }
                    ]
                }
            ]
        }}
        '''
        response = mock.Mock(text=json, request=request)
        results = wolframalpha_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual('input_plaintext', results[0]['infobox'])

        self.assertEqual(len(results[0]['attributes']), 3)
        self.assertEqual('Input', results[0]['attributes'][0]['label'])
        self.assertEqual('input_plaintext', results[0]['attributes'][0]['value'])
        self.assertEqual('Result', results[0]['attributes'][1]['label'])
        self.assertEqual('result_plaintext', results[0]['attributes'][1]['value'])
        self.assertEqual('Manipulatives illustration', results[0]['attributes'][2]['label'])
        self.assertEqual('illustration_img_src.gif', results[0]['attributes'][2]['image']['src'])
        self.assertEqual('illustration_img_alt', results[0]['attributes'][2]['image']['alt'])

        self.assertEqual(len(results[0]['urls']), 1)

        self.assertEqual(referer_url, results[0]['urls'][0]['url'])
        self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
        self.assertEqual(referer_url, results[1]['url'])
        self.assertEqual('Wolfram|Alpha (input_plaintext)', results[1]['title'])
        self.assertIn('result_plaintext', results[1]['content'])

        # test calc
        json = r"""
        {"queryresult" : {
            "success" : true,
            "error" : false,
            "numpods" : 2,
            "datatypes" : "",
            "id" : "queryresult_id",
            "host" : "https:\/\/www4b.wolframalpha.com",
            "related" : "related_url",
            "version" : "2.6",
            "pods" : [
                {
                    "title" : "Indefinite integral",
                    "scanners" : [
                        "Integral"
                    ],
                    "id" : "IndefiniteIntegral",
                    "error" : false,
                    "numsubpods" : 1,
                    "primary" : true,
                    "subpods" : [
                        {
                            "title" : "",
                            "img" : {
                                "src" : "integral_img_src.gif",
                                "alt" : "integral_img_alt",
                                "title" : "integral_img_title"
                            },
                            "plaintext" : "integral_plaintext",
                            "minput" : "integral_minput",
                            "moutput" : "integral_moutput"
                        }
                    ]
                },
                {
                    "title" : "Plot of the integral",
                    "scanners" : [
                        "Integral"
                    ],
                    "id" : "Plot",
                    "error" : false,
                    "numsubpods" : 1,
                    "subpods" : [
                        {
                            "title" : "",
                            "img" : {
                                "src" : "plot.gif",
                                "alt" : "plot_alt",
                                "title" : "plot_title"
                            },
                            "plaintext" : "",
                            "minput" : "plot_minput"
                        }
                    ]
                }
            ]
        }}
        """
        response = mock.Mock(text=json, request=request)
        results = wolframalpha_noapi.response(response)
        self.assertEqual(type(results), list)
        self.assertEqual(len(results), 2)
        self.assertEqual('integral_plaintext', results[0]['infobox'])

        self.assertEqual(len(results[0]['attributes']), 2)
        self.assertEqual('Indefinite integral', results[0]['attributes'][0]['label'])
        self.assertEqual('integral_plaintext', results[0]['attributes'][0]['value'])
        self.assertEqual('Plot of the integral', results[0]['attributes'][1]['label'])
        self.assertEqual('plot.gif', results[0]['attributes'][1]['image']['src'])
        self.assertEqual('plot_alt', results[0]['attributes'][1]['image']['alt'])

        self.assertEqual(len(results[0]['urls']), 1)

        self.assertEqual(referer_url, results[0]['urls'][0]['url'])
        self.assertEqual('Wolfram|Alpha', results[0]['urls'][0]['title'])
        self.assertEqual(referer_url, results[1]['url'])
        self.assertEqual('Wolfram|Alpha (integral_plaintext)', results[1]['title'])
        self.assertIn('integral_plaintext', results[1]['content'])

Example 44

Project: cinder
Source File: test_block_cmode.py
View license
    @ddt.data([], ['target_1', 'target_2'])
    def test_get_pool_stats(self, replication_backends):

        ssc = {
            'vola': {
                'pool_name': 'vola',
                'thick_provisioning_support': True,
                'thin_provisioning_support': False,
                'netapp_thin_provisioned': 'false',
                'netapp_compression': 'false',
                'netapp_mirrored': 'false',
                'netapp_dedup': 'true',
                'netapp_aggregate': 'aggr1',
                'netapp_raid_type': 'raid_dp',
                'netapp_disk_type': 'SSD',
            },
        }
        mock_get_ssc = self.mock_object(self.library.ssc_library,
                                        'get_ssc',
                                        mock.Mock(return_value=ssc))
        mock_get_aggrs = self.mock_object(self.library.ssc_library,
                                          'get_ssc_aggregates',
                                          mock.Mock(return_value=['aggr1']))
        self.mock_object(self.library, 'get_replication_backend_names',
                         mock.Mock(return_value=replication_backends))

        self.library.reserved_percentage = 5
        self.library.max_over_subscription_ratio = 10
        self.library.perf_library.get_node_utilization_for_pool = (
            mock.Mock(return_value=30.0))
        mock_capacities = {
            'size-total': 10737418240.0,
            'size-available': 2147483648.0,
        }
        self.mock_object(self.zapi_client,
                         'get_flexvol_capacity',
                         mock.Mock(return_value=mock_capacities))
        self.mock_object(self.zapi_client,
                         'get_flexvol_dedupe_used_percent',
                         mock.Mock(return_value=55.0))

        aggr_capacities = {
            'aggr1': {
                'percent-used': 45,
                'size-available': 59055800320.0,
                'size-total': 107374182400.0,
            },
        }
        mock_get_aggr_capacities = self.mock_object(
            self.zapi_client, 'get_aggregate_capacities',
            mock.Mock(return_value=aggr_capacities))

        result = self.library._get_pool_stats(filter_function='filter',
                                              goodness_function='goodness')

        expected = [{
            'pool_name': 'vola',
            'QoS_support': True,
            'consistencygroup_support': True,
            'reserved_percentage': 5,
            'max_over_subscription_ratio': 10.0,
            'multiattach': True,
            'total_capacity_gb': 10.0,
            'free_capacity_gb': 2.0,
            'provisioned_capacity_gb': 8.0,
            'netapp_dedupe_used_percent': 55.0,
            'netapp_aggregate_used_percent': 45,
            'utilization': 30.0,
            'filter_function': 'filter',
            'goodness_function': 'goodness',
            'thick_provisioning_support': True,
            'thin_provisioning_support': False,
            'netapp_thin_provisioned': 'false',
            'netapp_compression': 'false',
            'netapp_mirrored': 'false',
            'netapp_dedup': 'true',
            'netapp_aggregate': 'aggr1',
            'netapp_raid_type': 'raid_dp',
            'netapp_disk_type': 'SSD',
            'replication_enabled': False,
        }]
        if replication_backends:
            expected[0].update({
                'replication_enabled': True,
                'replication_count': len(replication_backends),
                'replication_targets': replication_backends,
                'replication_type': 'async',
            })

        self.assertEqual(expected, result)
        mock_get_ssc.assert_called_once_with()
        mock_get_aggrs.assert_called_once_with()
        mock_get_aggr_capacities.assert_called_once_with(['aggr1'])

Example 45

Project: dragonflow
Source File: test_db_store.py
View license
    def test_port(self):
        port1 = mock.Mock()
        port2 = mock.Mock()
        port2.get_lswitch_id.return_value = 'net1'
        port3 = mock.Mock()
        port3.get_lswitch_id.return_value = 'net1'
        port4 = mock.Mock()
        port4.get_id.return_value = 'port_id3'
        self.db_store.set_port('id1', port1, False, 'topic1')
        self.db_store.set_port('id2', port2, False, 'topic2')
        self.db_store.set_port('id3', port3, False, 'topic2')
        self.db_store.set_port('id4', port4, True, 'topic2')
        port_keys = self.db_store.get_port_keys()
        port_keys_topic2 = self.db_store.get_port_keys('topic2')
        self.assertEqual({'id1', 'id2', 'id3', 'id4'}, set(port_keys))
        self.assertIn('id2', port_keys_topic2)
        self.assertIn('id3', port_keys_topic2)
        ports = self.db_store.get_ports()
        ports_topic2 = self.db_store.get_ports('topic2')
        self.assertEqual({port1, port2, port3, port4}, set(ports))
        self.assertIn(port2, ports_topic2)
        self.assertIn(port3, ports_topic2)
        self.assertEqual(port1, self.db_store.get_port('id1'))
        self.assertEqual(port2, self.db_store.get_port('id2'))
        self.assertEqual(
            port1,
            self.db_store.get_port('id1', 'topic1'),
        )
        self.assertIsNone(self.db_store.get_local_port('id1'))
        self.assertIsNone(self.db_store.get_local_port('id2', 'topic2'))
        self.assertEqual(
            port4,
            self.db_store.get_local_port('id4', 'topic2')
        )
        self.assertEqual(
            port4,
            self.db_store.get_local_port_by_name('tapport_id3')
        )
        self.db_store.delete_port('id4', True, 'topic2')
        self.assertIsNone(
            self.db_store.get_local_port('id4', 'topic2')
        )
        self.assertIsNone(
            self.db_store.get_port('id4', 'topic2')
        )
        self.assertEqual(
            {port2, port3},
            set(self.db_store.get_ports_by_network_id('net1'))
        )
        self.db_store.delete_port('id3', False, 'topic2')
        self.assertIsNone(self.db_store.get_port('id3'))

Example 46

Project: fuel-devops
Source File: test_shell.py
View license
    def setUp(self):
        super(TestShell, self).setUp()

        self.print_mock = self.patch('devops.shell.print')
        self.tzlocal_mock = self.patch(
            'devops.helpers.helpers.tz.tzlocal',
            return_value=tz.gettz('Europe/Rome'))

        self.client_mock = self.patch('devops.client.DevopsClient',
                                      autospec=True)
        self.client_inst = self.client_mock.return_value

        def create_snap_mock(name, t):
            m = mock.Mock()
            m.name = name
            m.created = datetime.datetime(2016, 5, 12, 15, 12, t)
            return m

        def create_node_mock(name, vnc_port=5005, snapshots=None):
            m = mock.Mock(spec=models.Node)
            m.name = name
            m.group.name = 'rack-01'
            m.set_vcpu = mock.Mock(return_value=None)
            m.set_memory = mock.Mock(return_value=None)
            m.get_vnc_port = mock.Mock(return_value=vnc_port)
            m.erase_snapshot = mock.Mock(return_value=None)
            snap_mocks = []
            if snapshots:
                snap_mocks = [
                    create_snap_mock(s_name, t) for s_name, t in snapshots]
            m.get_snapshots.return_value = snap_mocks
            return m

        self.nodes = {
            'env1': {
                'admin': create_node_mock('admin', snapshots=[('snap1', 15),
                                                              ('snap2', 16)]),
                'slave-00': create_node_mock('slave-00',
                                             snapshots=[('snap1', 15)]),
                'slave-01': create_node_mock('slave-01'),
            }
        }

        def create_ap_mock(name, ip_network):
            m = mock.Mock(spec=models.AddressPool)
            m.name = name
            m.ip_network = netaddr.IPNetwork(ip_network)
            return m

        self.aps = {
            'env1': [
                create_ap_mock('fuelweb_admin-pool01', '109.10.0.0/24'),
                create_ap_mock('public-pool01', '109.10.1.0/24'),
                create_ap_mock('storage-pool01', '109.10.2.0/24'),
            ]
        }

        def create_env_mock(env_name, created, nodes, aps, admin_ip=None):
            m = mock.Mock(created=created)
            m.name = env_name
            m.get_node.side_effect = lambda name: nodes.get(name)
            m.get_nodes.side_effect = nodes.values
            m.get_address_pools.return_value = aps
            m.get_admin.side_effect = lambda: nodes['admin']
            m.get_admin_ip.return_value = admin_ip
            m.has_admin.side_effect = lambda: bool(admin_ip)
            return m

        self.env_mocks = {
            'env1': create_env_mock(
                env_name='env1',
                created=datetime.datetime(2016, 5, 12, 15, 12, 10),
                nodes=self.nodes['env1'], aps=self.aps['env1'],
                admin_ip='109.10.0.2'),
            'env2': create_env_mock(
                env_name='env2',
                created=datetime.datetime(2016, 5, 12, 15, 12, 11),
                nodes={}, aps=[], admin_ip='109.10.1.2'),
            'env3': create_env_mock(
                env_name='env3',
                created=datetime.datetime(2016, 5, 12, 15, 12, 12),
                nodes={}, aps=[]),
        }
        self.client_inst.list_env_names.side_effect = self.env_mocks.keys
        self.client_inst.get_env.side_effect = self.env_mocks.__getitem__

Example 47

View license
@pytest.mark.parametrize("enabled", [["node-enabled-1", "node-enabled-2"],
                                     ["node-enabled-1"]])
@pytest.mark.parametrize("disabled", [["node-disabled-1", "node-disabled-2"],
                                      ["node-disabled-1"]])
@pytest.mark.parametrize("node_fqdn", ["node-disabled-1", "node-enabled-1"])
@pytest.mark.parametrize("nodes_in_error_state", [True, False])
@pytest.mark.parametrize("fuel_version", ["7.0", "8.0"])
@pytest.mark.parametrize("instances", [["instance_1", "instance_2"]])
def test_evacuate_host(mocker, enabled, disabled, node_fqdn,
                       nodes_in_error_state, fuel_version, instances):
    env = mock.Mock()
    controller = mock.Mock()
    node = mock.Mock()
    node.env = env
    node.env.data = {"fuel_version": fuel_version}

    mock_get_compute_list = mocker.patch("octane.util.nova.get_compute_lists",
                                         return_value=(enabled, disabled))

    mock_get_one_controller = mocker.patch(
        "octane.util.env.get_one_controller", return_value=controller)

    run_nova_cmd = mocker.patch("octane.util.nova.run_nova_cmd")
    get_node_fqdn_mock = mocker.patch("octane.util.node.get_nova_node_handle",
                                      return_value=node_fqdn)
    mock_is_nova_state = mocker.patch(
        "octane.util.nova.do_nova_instances_exist",
        return_value=nodes_in_error_state)

    get_instances_mock = mocker.patch(
        "octane.util.nova.get_active_instances", return_value=instances)

    mock_waiting = mocker.patch(
        "octane.util.nova.waiting_for_status_completed")

    handler = compute.ComputeUpgrade(node, env, False, False)
    if [node_fqdn] == enabled:
        with pytest.raises(Exception):
            handler.evacuate_host()
        error = True
    elif nodes_in_error_state:
        with pytest.raises(Exception):
            handler.evacuate_host()
        error = True
    else:
        handler.evacuate_host()
        error = False
    nova_calls = []
    if node_fqdn not in disabled:
        nova_calls.append(mock.call(
            ["nova", "service-disable", node_fqdn, "nova-compute"],
            controller, False))
    for instance in instances:
        nova_calls.append(mock.call(
            ["nova", "live-migration", instance], controller, False))
    if error:
        assert not run_nova_cmd.called
        assert not mock_waiting.called
        assert not get_instances_mock.called
    else:
        assert run_nova_cmd.call_args_list == nova_calls
        get_instances_mock.assert_called_once_with(controller, node_fqdn)
        waiting_calls = [mock.call(controller, node_fqdn, "MIGRATING")
                         for i in instances]
        assert waiting_calls == mock_waiting.call_args_list
    if [node_fqdn] == enabled:
        assert not mock_is_nova_state.called
    else:
        if error:
            mock_is_nova_state.assert_called_once_with(
                controller, node_fqdn, "ERROR")
        else:
            assert [
                mock.call(controller, node_fqdn, "ERROR"),
                mock.call(controller, node_fqdn),
            ] == mock_is_nova_state.call_args_list
    get_node_fqdn_mock.assert_called_once_with(node)
    mock_get_compute_list.assert_called_once_with(controller)
    mock_get_one_controller.assert_called_once_with(env)

Example 48

View license
@pytest.mark.parametrize("fuel_version", ["7.0", "8.0"])
@pytest.mark.parametrize("password", ["password"])
@pytest.mark.parametrize("node_fqdn", ["node-compute"])
@pytest.mark.parametrize("cmd_output, instances", [(
    "+--------------------------------------+\n"
    "| ID                                   |\n"
    "+--------------------------------------+\n"
    "| d5c35583-f498-4841-a032-069ec066d2d5 |\n"
    "| 8d274e6b-91db-4d76-a5e8-13a23c3335c9 |\n"
    "| 093c55f2-4a30-4a74-95ea-d7c39fcb4e3a |\n"
    "+--------------------------------------+\n",
    [
        "d5c35583-f498-4841-a032-069ec066d2d5",
        "8d274e6b-91db-4d76-a5e8-13a23c3335c9",
        "093c55f2-4a30-4a74-95ea-d7c39fcb4e3a",
    ]),
])
@pytest.mark.parametrize("nodes_in_error_state", [True, False])
def test_shutoff_vms(
        mocker, fuel_version, password, node_fqdn, cmd_output,
        instances, nodes_in_error_state):
    env = mock.Mock()
    controller = mock.Mock()
    node = mock.Mock()
    node.env = env
    node.env.data = {"fuel_version": fuel_version}
    handler = compute.ComputeUpgrade(node, env, False, False)
    mock_get_one_controller = mocker.patch(
        "octane.util.env.get_one_controller", return_value=controller)
    mock_get_node_fqdn = mocker.patch(
        "octane.util.node.get_nova_node_handle", return_value=node_fqdn)
    mock_nova_run = mocker.patch(
        "octane.util.nova.run_nova_cmd", return_value=cmd_output)
    mock_waiting = mocker.patch(
        "octane.util.nova.waiting_for_status_completed")
    mock_is_nova_state = mocker.patch(
        "octane.util.nova.do_nova_instances_exist",
        return_value=nodes_in_error_state)
    nova_run_calls = []
    if nodes_in_error_state:
        with pytest.raises(Exception):
            handler.shutoff_vms()
        assert not mock_nova_run.called
        assert not mock_waiting.called
    else:
        handler.shutoff_vms()
        nova_run_calls.append(mock.call([
            "nova", "list",
            "--host", node_fqdn,
            "--limit", "-1",
            "--status", "ACTIVE",
            "--minimal"],
            controller))
        for instance in instances:
            nova_run_calls.append(mock.call(
                ["nova", "stop", instance], controller, output=False))
        assert nova_run_calls == mock_nova_run.call_args_list
        mock_waiting.assert_called_once_with(controller, node_fqdn, "ACTIVE")
    mock_get_one_controller.assert_called_once_with(env)
    mock_get_node_fqdn.assert_called_once_with(node)
    mock_is_nova_state.assert_called_once_with(controller, node_fqdn, "ERROR")

Example 49

Project: fuel-octane
Source File: test_osd_upgrade.py
View license
@pytest.mark.parametrize("orig_id", [2])
@pytest.mark.parametrize("seed_id", [3])
@pytest.mark.parametrize("user", ["user"])
@pytest.mark.parametrize("password", ["password"])
@pytest.mark.parametrize("nodes_count", [0, 10])
@pytest.mark.parametrize("priority", [100, 500])
@pytest.mark.parametrize(
    "is_same_versions_on_mon_and_osd_return_values",
    [(True, True), (False, True), (False, False)])
def test_upgrade_osd(
        mocker, nodes_count, priority, user, password, orig_id, seed_id,
        is_same_versions_on_mon_and_osd_return_values):
    orig_env = mock.Mock()
    seed_env = mock.Mock()
    nodes = []
    hostnames = []
    restart_calls = []
    controller = mock.Mock()
    for idx in range(nodes_count):
        hostname = "host_{0}".format(idx)
        hostnames.append(hostname)
        node = mock.Mock(data={'hostname': hostname})
        nodes.append(node)
        restart_calls.append(mock.call(["restart", "ceph-osd-all"], node=node))
    env_get = mocker.patch("fuelclient.objects.environment.Environment",
                           side_effect=[orig_env, seed_env])
    mocker.patch("octane.util.env.get_nodes", return_value=iter(nodes))
    mocker.patch("octane.util.env.get_one_controller", return_value=controller)
    mock_creds = mocker.patch(
        "octane.handlers.backup_restore.NailgunCredentialsContext")
    mock_auth_cntx = mocker.patch("octane.util.fuel_client.set_auth_context")
    mock_applied = mocker.patch("octane.commands.osd_upgrade.applied_repos")
    mock_get_priority = mocker.patch(
        "octane.commands.osd_upgrade.get_repo_highest_priority",
        return_value=priority)
    mock_get_env_repos = mocker.patch(
        "octane.commands.osd_upgrade.get_repos_for_upgrade")
    ssh_call_mock = mocker.patch("octane.util.ssh.call")
    mock_is_same_version = mocker.patch(
        "octane.commands.osd_upgrade.is_same_versions_on_mon_and_osd",
        side_effect=is_same_versions_on_mon_and_osd_return_values)
    mock_up_waiter = mocker.patch(
        "octane.commands.osd_upgrade.waiting_until_ceph_up")
    already_same, upgraded = is_same_versions_on_mon_and_osd_return_values
    if not upgraded and not already_same and nodes:
        with pytest.raises(Exception):
            osd_upgrade.upgrade_osd(orig_id, seed_id, user, password)
    else:
        osd_upgrade.upgrade_osd(orig_id, seed_id, user, password)

    mock_creds.assert_called_once_with(user, password)
    mock_auth_cntx.assert_called_once_with(mock_creds.return_value)
    env_get.assert_any_call(orig_id)
    env_get.assert_any_call(seed_id)
    ssh_calls = []
    if nodes and not already_same:
        ssh_calls.append(
            mock.call(["ceph", "osd", "set", "noout"], node=nodes[0]))
        ssh_calls.append(
            mock.call(
                ['ceph-deploy', 'install', '--release', 'hammer'] + hostnames,
                node=nodes[0]))
        ssh_calls.extend(restart_calls)
        ssh_calls.append(
            mock.call(["ceph", "osd", "unset", "noout"], node=nodes[0]))
        mock_get_priority.assert_called_once_with(orig_env)
        mock_applied.assert_called_once_with(
            nodes,
            priority + 1,
            mock_get_env_repos.return_value)
        assert [mock.call(controller), mock.call(controller)] == \
            mock_is_same_version.call_args_list
        mock_up_waiter.assert_called_once_with(controller)
    elif nodes and already_same:
        mock_is_same_version.assert_called_once_with(controller)
    elif not nodes:
        assert not mock_is_same_version.called

    assert ssh_calls == ssh_call_mock.mock_calls

Example 50

Project: fuel-web
Source File: test_ip_addr_validator.py
View license
    def setUp(self):
        super(TestIpAddrValidator, self).setUp()

        self.create_data = {
            'ip_addr': "192.168.0.15",
            'network': 1,
            'vip_name': 'test',
            'is_user_defined': True
        }
        self.cluster = mock.Mock()
        self.cluster.configure_mock(id=-1)

        self.ng = mock.Mock()
        self.ip_addr = mock.Mock()

        self.ng_object_patcher = mock.patch(
            'nailgun.extensions.network_manager.validators.ip_addr'
            '.objects.NetworkGroup.get_by_uid',
            new=mock.Mock(return_value=self.ng)
        )
        self.ip_addr_col_patcher = mock.patch(
            'nailgun.extensions.network_manager.validators.ip_addr'
            '.objects.IPAddrCollection.get_all_by_addr',
            new=mock.Mock(
                return_value=mock.Mock(
                    first=mock.Mock(return_value=self.ip_addr)
                )
            )
        )

        net_roles = [
            {
                'properties': {
                    'vip': [{'name': 'test'}]
                }
            }
        ]
        self.get_nr_patcher = mock.patch(
            'nailgun.extensions.network_manager.validators.ip_addr'
            '.objects.Cluster.get_network_roles',
            return_value=net_roles
        )

        self.ng_object_patcher.start()
        self.ip_addr_col_patcher.start()
        self.get_nr_patcher.start()