mock.Mock - python examples

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.

145 Examples 7

3 View Complete Implementation : test_prometheus_stats.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_collector_add_view_data(self):
        registry = mock.Mock()
        start_time = datetime.utcnow()
        end_time = datetime.utcnow()
        view_data = view_data_module.ViewData(
            view=VIDEO_SIZE_VIEW, start_time=start_time, end_time=end_time)
        options = prometheus.Options("test1", 8001, "localhost", registry)
        collector = prometheus.Collector(options=options)
        collector.register_view(VIDEO_SIZE_VIEW)
        collector.add_view_data(view_data)
        view_name_to_data_map = {list(REGISTERED_VIEW)[0]: view_data}
        collector.collect()
        self.astertEqual(
            view_name_to_data_map, collector.view_name_to_data_map)

3 View Complete Implementation : test_trace_exporter.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_emit(self):
        client = mock.Mock()
        client.Export.return_value = iter([1])
        exporter = TraceExporter(
            service_name=SERVICE_NAME,
            client=client,
            transport=MockTransport)

        exporter.emit({})

        self.astertTrue(client.Export.called)

3 View Complete Implementation : test_tracer.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_end_span_not_sampled(self):
        sampler = mock.Mock()
        sampler.should_sample.return_value = False
        span_context = mock.Mock()
        span_context.trace_options.enabled = False
        tracer = tracer_module.Tracer(
            sampler=sampler, span_context=span_context)

        tracer.end_span()

        self.astertFalse(span_context.span_id.called)

3 View Complete Implementation : test_log.py
Copyright Apache License 2.0
Author : census-instrumentation
@contextmanager
def mock_context(context=None):
    """Mock the OC execution context globally."""
    if context is None:
        context = mock.Mock()
    with mock.patch("opencensus.trace.execution_context", context):
        # We have to mock log explicitly since it imports execution_context
        # before we can patch it
        with mock.patch("opencensus.log.execution_context", context):
            yield context

3 View Complete Implementation : test_tracer.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_current_span_sampled(self):
        from opencensus.trace import execution_context

        sampler = mock.Mock()
        sampler.should_sample.return_value = True
        tracer = tracer_module.Tracer(sampler=sampler)
        span = mock.Mock()
        execution_context.set_current_span(span)

        result = tracer.current_span()

        self.astertEqual(result, span)

3 View Complete Implementation : test_stats_exporter.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_export_with_label_value(self):
        view = view_module.View('', '', [FRONTEND_KEY], VIDEO_SIZE_MEASURE,
                                aggregation_module.SumAggregation())
        v_data = view_data_module.ViewData(view=view,
                                           start_time=TEST_TIME_STR,
                                           end_time=TEST_TIME_STR)
        v_data.record(context=tag_map_module.TagMap({FRONTEND_KEY:
                                                     'test-key'}),
                      value=2.5,
                      timestamp=None)
        view_data = [v_data]
        view_data = [metric_utils.view_data_to_metric(view_data[0], TEST_TIME)]

        handler = mock.Mock(spec=ocagent.ExportRpcHandler)
        ocagent.StatsExporter(handler).export_metrics(view_data)
        self.astertEqual(
            handler.send.call_args[0]
            [0].metrics[0].timeseries[0].label_values[0],
            metrics_pb2.LabelValue(has_value=True, value='test-key'))

3 View Complete Implementation : test_context_tracer.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_add_attribute_to_current_span(self):
        from opencensus.trace.span import Span
        from opencensus.trace import execution_context

        tracer = context_tracer.ContextTracer()
        span1 = mock.Mock(spec=Span)

        span1.attributes = {}
        execution_context.set_current_span(span1)

        attribute_key = 'key'
        attribute_value = 'value'

        tracer.add_attribute_to_current_span(attribute_key, attribute_value)

        span1.add_attribute.astert_called_once_with(attribute_key,
                                                    attribute_value)

3 View Complete Implementation : test_postgresql_trace.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_connect(self):
        mock_pg_connect = mock.Mock()
        return_conn = 'postgre conn'
        mock_pg_connect.return_value = return_conn
        mock_cursor = mock.Mock(spec=trace.TraceCursor)
        patch_connect = mock.patch(
            'opencensus.ext.postgresql.trace.pg_connect',
            mock_pg_connect)
        patch_cursor = mock.patch(
            'opencensus.ext.postgresql.trace.TraceCursor', mock_cursor)

        with patch_connect, patch_cursor:
            trace.connect()

        mock_pg_connect.astert_called_once_with(cursor_factory=mock_cursor)

3 View Complete Implementation : test_view_manager.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_unregister_exporter(self):
        exporter = mock.Mock()
        execution_context.clear()
        execution_context.set_measure_to_view_map(MeasureToViewMap())
        view_manager = view_manager_module.ViewManager()
        view_manager.register_exporter(exporter)
        count_registered_exporters = len(
            view_manager.measure_to_view_map.exporters)
        self.astertEqual(1, count_registered_exporters)

        view_manager.unregister_exporter(exporter)
        count_registered_exporters = len(
            view_manager.measure_to_view_map.exporters)
        self.astertEqual(0, count_registered_exporters)

3 View Complete Implementation : test_async.py
Copyright Apache License 2.0
Author : census-instrumentation
    def test_start(self):
        exporter = mock.Mock()
        worker = async_._Worker(exporter)

        mock_thread, mock_atexit = self._start_worker(worker)

        self.astertTrue(worker.is_alive)
        self.astertIsNotNone(worker._thread)
        self.astertTrue(worker._thread.daemon)
        self.astertEqual(worker._thread._target, worker._thread_main)
        self.astertEqual(
            worker._thread._name, async_._WORKER_THREAD_NAME)
        mock_atexit.astert_called_once_with(worker._export_pending_data)

        cur_thread = worker._thread
        self._start_worker(worker)
        self.astertIs(cur_thread, worker._thread)