How to use the mock.mock.MagicMock function in mock

To help you get started, we’ve selected a few mock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github peterbrittain / asciimatics / tests / test_particles.py View on Github external
def test_serpent_firework(self):
        """
        Test that SerpentFirework works as expected.
        """
        screen = MagicMock(spec=Screen, colours=8)
        canvas = Canvas(screen, 10, 40, 0, 0)
        effect = SerpentFirework(canvas, 4, 4, 25)
        self.check_effect(canvas,
                          effect,
                          lambda value: self.assertIn(chr(value[0]), '|+- '),
                          iterations=20)
github openattic / openattic / backend / ceph_deployment / tests.py View on Github external
"work-directory": "/run/cephfs_bench"
                        },
                        "master_minion": "master_minion.openattic.org",
                        "time_server": "master_minion.openattic.org",
                        "igw_config": "default-ui",
                        "cluster": "ceph",
                        "public_network": "10.0.0.0/19",
                        "cluster_network": "10.0.0.0/19",
                        "stage_prep_master": "default",
                        "fsid": "c0f85b6a-70d7-4c49-81fa-64ed80069e24"
                    },
                }]
            }
            mock_requests_session().post.side_effect = [self._login_resp, resp_pillar]

            resp = mock.MagicMock()
            resp.ok = True
            resp.status_code = 200
            resp.json.return_value = {
                'return': {
                    'minions': ['minion1'],
                    'minions_pre': [],
                    'minions_denied': [],
                    'minions_rejected': []
                }
            }
            mock_requests_session().get.return_value = resp

            api = DeepSea('localhost', 8000, 'auto', 'hello', 'world')
            res = api.get_minions()

            self.assertEqual(res, [
github peterbrittain / asciimatics / tests / test_effects.py View on Github external
def test_sprite(self):
        """
        Check that Sprites work.
        """
        # Check that we can move a Sprite around the screen.
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        path = Path()
        path.jump_to(10, 5)
        path.move_straight_to(20, 10, 5)
        path.move_straight_to(30, 5, 5)
        path.move_straight_to(20, 0, 5)
        path.move_straight_to(10, 5, 5)
        effect = Sam(canvas, path)
        effect.reset()
        self.assert_blank(canvas)
        my_buffer = [[(32, 7, 0, 0) for _ in range(40)] for _ in range(10)]
        for i in range(30):
            effect.update(i)
            self.assertEqual(self.check_canvas(
                canvas,
                my_buffer,
github elifesciences / elife-bot / tests / activity / test_activity_publication_email.py View on Github external
fake_find_latest_s3_file_name,
                         fake_ejp_get_s3key,
                         fake_download_email_templates,
                         fake_article_versions,
                         fake_email_smtp_connect,
                         fake_download_xml):

        directory = TempDirectory()
        fake_storage_context.return_value = FakeStorageContext()
        fake_download_xml.return_value = False

        # Basic fake data for all activity passes
        fake_ejp_get_s3key.return_value = fake_get_s3key(
            directory, self.activity.get_tmp_dir(), "authors.csv",
            "tests/test_data/ejp_author_file.csv")
        fake_find_latest_s3_file_name.return_value = mock.MagicMock()
        fake_email_smtp_connect.return_value = FakeSMTPServer(self.activity.get_tmp_dir())

        # do_activity
        for pass_test_data in self.do_activity_passes:

            # Prime the related article property for when needed
            if pass_test_data.get("related_article"):
                related_article = article()
                related_article.parse_article_file(pass_test_data.get("related_article"))
                self.activity.related_articles = [related_article]

            fake_article_versions.return_value = (
                200, pass_test_data.get("lax_article_versions_response_data"))

            self.fake_download_email_templates_from_s3(
                self.activity.get_tmp_dir(), pass_test_data["templates_warmed"])
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_frame_focus_widget_property(self):
        """
        Check the frame exposes the focussed widget
        """
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        form = TestFrame(canvas)
        form.reset()

        # If the Frame loses the focus it must not return a focussed widget.
        form._has_focus = False
        self.assertIsNone(form.focussed_widget)
github elifesciences / elife-bot / tests / activity / test_activity_convert_jats.py View on Github external
def test_do_activity(self, fake_session_mock, fake_s3_mock, fake_key_mock, fake_get_article_xml_key, fake_add_update_date_to_json):
        directory = TempDirectory()

        #preparing Mocks
        fake_session_mock.return_value = FakeSession(data.session_example)
        fake_s3_mock.return_value = FakeS3Connection()
        fake_key_mock.return_value = FakeKey(directory, data.bucket_dest_file_name)
        fake_get_article_xml_key.return_value = FakeKey(directory), data.bucket_origin_file_name
        fake_add_update_date_to_json.return_value = data.json_output_return_example_string
        self.jats.emit_monitor_event = mock.MagicMock()
        self.jats.set_dashboard_properties = mock.MagicMock()

        success = self.jats.do_activity(testdata.ExpandArticle_data)
        self.assertEqual(success, True)
        output_json = json.loads(directory.read("test_dest.json"))
        expected = data.json_output_return_example
        self.assertDictEqual(output_json, expected)
github cernanalysispreservation / analysispreservation.cern.ch / tests / unit / experiments / test_cadi_utils.py View on Github external
       MagicMock(return_value=dict(cookies_are='example_cookie')))
def test_get_from_cadi_by_id_when_cadi_server_down_returns_503(app):
    cadi_id = 'ANA-00-000'

    responses.add(responses.GET,
                  current_app.config['CADI_GET_RECORD_URL'].format(id=cadi_id),
                  status=500)

    with raises(ExternalAPIException) as e:
        get_from_cadi_by_id(cadi_id)
github nansencenter / django-geo-spaas / geospaas / vocabularies / tests.py View on Github external
def tearDown(self):
        self.patcher.stop()

    def test_create_from_vocabularies(self):
        """ Test shared with all vocabularies """
        self.model.objects.create_from_vocabularies(force=True)
        self.model.objects.create_from_vocabularies()
        self.model.objects.get_list.assert_called()
        self.model.objects.update.assert_called()
        self.assertIn('Successfully added', self.mock_print.call_args[0][0])


class ParameterTests(VocabulariesTestBase, TestCase):
    model = Parameter
    model_list = [{'standard_name': 'surface_radial_doppler_sea_water_velocity', 'long_name': 'Radial Doppler Current', 'short_name': 'Ur', 'units': 'm s-1', 'minmax': '-1 1', 'colormap': 'jet'}]
    model.objects.update2 = MagicMock(return_value=None)
    model.objects.get_list2 = MagicMock(return_value=[])

    def test_get_parameter(self):
        p = Parameter.objects.get(standard_name='surface_backwards_doppler_frequency_shift_of_radar_wave_due_to_surface_velocity')
        self.assertEqual(p.units, 'Hz')

    def test_get_or_create(self):
        dc0 = Parameter.objects.get(standard_name='surface_backwards_doppler_frequency_shift_of_radar_wave_due_to_surface_velocity')
        dc1 = dict(
                standard_name=dc0.standard_name,
                short_name=dc0.short_name,
                units=dc0.units)
        dc2, cr = Parameter.objects.get_or_create(dc1)
        self.assertEqual(dc0, dc2)
        self.assertFalse(cr)
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_form_input(self):
        """
        Check Frame input works as expected.
        """
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        form = TestFrame(canvas)
        form.reset()
        self.process_keys(form,
                          ["ABC\nDEF", "GHI", "jkl", "MN", " ", " ", "", " "],
                          Screen.KEY_TAB)
        form.save()
        self.assertEqual(
            form.data,
            {
                'CA': True,
                'CB': False,
                'CC': True,
                'TA': ['ABC', 'DEF'],
                'TB': 'GHI',
                'TC': 'jkl',
github elifesciences / elife-bot / tests / activity / test_activity_archive_article.py View on Github external
def test_do_activity_download_failure(self, fake_download_files):
        "test failure downloading a file"
        fake_download_files.return_value = None
        self.activity.emit_monitor_event = mock.MagicMock()
        success = self.activity.do_activity(activity_test_data.data_example_before_publish)
        self.assertEqual(success, self.activity.ACTIVITY_PERMANENT_FAILURE)