def test_record_with_exporter(self):
        exporter = mock.Mock()
        measure_name = "test_measure"
        measure_description = "test_description"
        measure = BaseMeasure(name=measure_name,
                              description=measure_description)

        view_name = "test_view"
        view_description = "test_description"
        view_columns = ["testTag1", "testColumn2"]
        view_measure = measure
        view_aggregation = mock.Mock()
        View(name=view_name,
             description=view_description,
             columns=view_columns,
             measure=view_measure,
             aggregation=view_aggregation)

        measure_value = 5
        tags = {"testTag1": "testTag1Value"}
        measurement_map = {measure: measure_value}
        timestamp = mock.Mock()

        measure_to_view_map = measure_to_view_map_module.MeasureToViewMap()
        measure_to_view_map.exporters.append(exporter)
        measure_to_view_map._registered_measures = {}
        record = measure_to_view_map.record(tags=tags,
                                            measurement_map=measurement_map,
                                            timestamp=timestamp)
        self.assertNotEqual(
            measure,
            measure_to_view_map._registered_measures.get(measure.name))
        self.assertIsNone(record)
    def test_export(self):
        exporter = mock.Mock()
        view_data = []
        measure_name = "test_measure"
        measure_description = "test_description"
        measure = BaseMeasure(
            name=measure_name, description=measure_description)

        view_name = "test_view"
        view_description = "test_description"
        view_columns = ["testTag1", "testColumn2"]
        view_measure = measure
        view_aggregation = mock.Mock()
        view = View(
            name=view_name,
            description=view_description,
            columns=view_columns,
            measure=view_measure,
            aggregation=view_aggregation)

        measure_value = 5
        tags = {"testTag1": "testTag1Value"}
        measurement_map = {measure: measure_value}
        timestamp = mock.Mock()

        measure_to_view_map = measure_to_view_map_module.MeasureToViewMap()
        measure_to_view_map.exporters.append(exporter)
        measure_to_view_map._registered_measures = {}
        measure_to_view_map.export(view_data)
        self.assertTrue(True)
    def test_record(self):
        measure_name = "test_measure"
        measure_description = "test_description"
        measure = BaseMeasure(name=measure_name,
                              description=measure_description)

        view_name = "test_view"
        view_description = "test_description"
        view_columns = ["testTag1", "testColumn2"]
        view_measure = measure
        view_aggregation = mock.Mock()
        view = View(name=view_name,
                    description=view_description,
                    columns=view_columns,
                    measure=view_measure,
                    aggregation=view_aggregation)

        measure_value = 5
        value = "testValue"
        tags = {"testTag1": "testTag1Value"}
        stats = {Measurement(measure=measure, value=measure_value): value}
        timestamp = mock.Mock()

        measure_to_view_map = measure_to_view_map_module.MeasureToViewMap()
        measure_to_view_map._registered_measures = {}
        record = measure_to_view_map.record(tags=tags,
                                            stats=stats,
                                            timestamp=timestamp)
        self.assertNotEqual(
            measure,
            measure_to_view_map._registered_measures.get(measure.name))
        self.assertIsNone(record)

        measure_to_view_map._registered_measures = {measure.name: measure}
        measure_to_view_map._map = {}
        record = measure_to_view_map.record(tags=tags,
                                            stats=stats,
                                            timestamp=timestamp)
        self.assertEqual(
            measure,
            measure_to_view_map._registered_measures.get(measure.name))
        self.assertIsNone(record)

        measure_to_view_map._map = {measure.name: mock.Mock()}
        measure_to_view_map.record(tags=tags, stats=stats, timestamp=timestamp)
        self.assertEqual(
            measure,
            measure_to_view_map._registered_measures.get(measure.name))
        self.assertTrue(measure.name in measure_to_view_map._map)

        measure_to_view_map._map = {"testing": mock.Mock()}
        measure_to_view_map.record(tags=tags, stats=stats, timestamp=timestamp)
        self.assertTrue(measure.name not in measure_to_view_map._map)

        measure_to_view_map_mock = mock.Mock()
        measure_to_view_map = measure_to_view_map_mock
        measure_to_view_map._registered_measures = {measure.name: measure}
        measure_to_view_map._map = mock.Mock()
        measure_to_view_map.record(tags=mock.Mock(),
                                   stats=mock.Mock(),
                                   timestamp=mock.Mock())
        self.assertEqual(
            measure,
            measure_to_view_map._registered_measures.get(measure.name))
        self.assertIsNotNone(measure_to_view_map.view_datas)
        self.assertTrue(measure_to_view_map_mock.record.called)

        tags = {"testTag1": "testTag1Value"}
        stats = {}
        measure_to_view_map = measure_to_view_map_module.MeasureToViewMap()
        record = measure_to_view_map.record(tags=tags,
                                            stats=stats,
                                            timestamp=timestamp)
        self.assertIsNone(record)