|
| 1 | +# Copyright 2026 Google Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Tests for aggregator interface.""" |
| 15 | + |
| 16 | +import unittest |
| 17 | +from unittest import mock |
| 18 | + |
| 19 | +from timesketch.lib.aggregators import interface |
| 20 | +from timesketch.lib.charts import interface as chart_interface |
| 21 | + |
| 22 | + |
| 23 | +class MockChart(chart_interface.BaseChart): |
| 24 | + """Mock chart class.""" |
| 25 | + |
| 26 | + NAME = "mockchart" |
| 27 | + |
| 28 | + def generate(self): |
| 29 | + return mock.Mock( |
| 30 | + to_dict=lambda: {"chart": "data"}, to_html=lambda: "<html></html>" |
| 31 | + ) |
| 32 | + |
| 33 | + |
| 34 | +class TestAggregationResult(unittest.TestCase): |
| 35 | + """Tests for AggregationResult.""" |
| 36 | + |
| 37 | + def setUp(self): |
| 38 | + self.values = [{"field": "foo", "count": 10}, {"field": "bar", "count": 20}] |
| 39 | + self.encoding = { |
| 40 | + "x": {"field": "field", "type": "nominal"}, |
| 41 | + "y": {"field": "count", "type": "quantitative"}, |
| 42 | + } |
| 43 | + self.result = interface.AggregationResult( |
| 44 | + encoding=self.encoding, values=self.values |
| 45 | + ) |
| 46 | + |
| 47 | + @mock.patch("timesketch.lib.charts.manager.ChartManager.get_chart") |
| 48 | + def test_to_chart_valid(self, mock_get_chart): |
| 49 | + """Test to_chart with valid data.""" |
| 50 | + mock_get_chart.return_value = MockChart |
| 51 | + |
| 52 | + # Call to_chart |
| 53 | + result = self.result.to_chart(chart_name="mockchart") |
| 54 | + |
| 55 | + # Assert chart was generated and returned as dict by default |
| 56 | + self.assertEqual(result, {"chart": "data"}) |
| 57 | + |
| 58 | + @mock.patch("timesketch.lib.aggregators.interface.logger") |
| 59 | + @mock.patch("timesketch.lib.charts.manager.ChartManager.get_chart") |
| 60 | + def test_to_chart_missing_encoding(self, mock_get_chart, mock_logger): |
| 61 | + """Test to_chart with missing encoding (should skip generation).""" |
| 62 | + mock_get_chart.return_value = MockChart |
| 63 | + |
| 64 | + # Result without encoding |
| 65 | + result_obj = interface.AggregationResult(encoding=None, values=self.values) |
| 66 | + |
| 67 | + result = result_obj.to_chart(chart_name="mockchart") |
| 68 | + |
| 69 | + # Should return empty dict |
| 70 | + self.assertEqual(result, {}) |
| 71 | + |
| 72 | + # Verify logger.warning was called |
| 73 | + mock_logger.warning.assert_called_with( |
| 74 | + "No encoding found for chart [%s] with title [%s]. " |
| 75 | + "Skipping chart generation.", |
| 76 | + "mockchart", |
| 77 | + "", |
| 78 | + ) |
| 79 | + |
| 80 | + @mock.patch("timesketch.lib.aggregators.interface.logger") |
| 81 | + @mock.patch("timesketch.lib.charts.manager.ChartManager.get_chart") |
| 82 | + def test_to_chart_missing_values_and_encoding(self, mock_get_chart, mock_logger): |
| 83 | + """Test to_chart with missing values and encoding.""" |
| 84 | + mock_get_chart.return_value = MockChart |
| 85 | + |
| 86 | + # Result with empty values and no encoding |
| 87 | + result_obj = interface.AggregationResult(encoding=None, values=[]) |
| 88 | + |
| 89 | + result = result_obj.to_chart(chart_name="mockchart") |
| 90 | + |
| 91 | + # Should return empty dict |
| 92 | + self.assertEqual(result, {}) |
| 93 | + |
| 94 | + # Verify logger.warning was called |
| 95 | + mock_logger.warning.assert_called_with( |
| 96 | + "No encoding found for chart [%s] with title [%s]. " |
| 97 | + "Skipping chart generation.", |
| 98 | + "mockchart", |
| 99 | + "", |
| 100 | + ) |
| 101 | + |
| 102 | + @mock.patch("timesketch.lib.aggregators.interface.logger") |
| 103 | + @mock.patch("timesketch.lib.charts.manager.ChartManager.get_chart") |
| 104 | + def test_to_chart_missing_encoding_unable_to_guess( |
| 105 | + self, mock_get_chart, mock_logger |
| 106 | + ): |
| 107 | + """Test to_chart when unable to guess encoding (e.g. 1 column).""" |
| 108 | + mock_get_chart.return_value = MockChart |
| 109 | + |
| 110 | + values = [{"field": "foo"}] # Only 1 column |
| 111 | + result_obj = interface.AggregationResult(encoding=None, values=values) |
| 112 | + |
| 113 | + result = result_obj.to_chart(chart_name="mockchart") |
| 114 | + self.assertEqual(result, {}) |
| 115 | + |
| 116 | + # Verify logger.warning was called |
| 117 | + mock_logger.warning.assert_called() |
| 118 | + |
| 119 | + |
| 120 | +class TestBaseAggregator(unittest.TestCase): |
| 121 | + """Tests for BaseAggregator.""" |
| 122 | + |
| 123 | + @mock.patch("timesketch.lib.aggregators.interface.OpenSearchDataStore") |
| 124 | + @mock.patch("timesketch.lib.aggregators.interface.SQLSketch") |
| 125 | + def test_init_no_sketch_id(self, _mock_sketch, _mock_ds): |
| 126 | + """Test initialization without sketch_id.""" |
| 127 | + agg = interface.BaseAggregator(indices=["index1"]) |
| 128 | + self.assertIsNone(agg.sketch) |
| 129 | + self.assertEqual(agg.indices, ["index1"]) |
| 130 | + # pylint: disable=protected-access |
| 131 | + self.assertEqual(agg._sketch_url, "") |
| 132 | + |
| 133 | + @mock.patch("timesketch.lib.aggregators.interface.OpenSearchDataStore") |
| 134 | + @mock.patch("timesketch.lib.aggregators.interface.SQLSketch") |
| 135 | + def test_init_with_sketch_id(self, mock_sketch, _mock_ds): |
| 136 | + """Test initialization with sketch_id.""" |
| 137 | + mock_sketch_obj = mock.Mock() |
| 138 | + mock_t1 = mock.Mock() |
| 139 | + mock_t1.searchindex.index_name = "index1" |
| 140 | + mock_t1.id = 1 |
| 141 | + mock_sketch_obj.active_timelines = [mock_t1] |
| 142 | + mock_sketch.get_by_id.return_value = mock_sketch_obj |
| 143 | + |
| 144 | + agg = interface.BaseAggregator(sketch_id=1) |
| 145 | + self.assertEqual(agg.sketch, mock_sketch_obj) |
| 146 | + self.assertEqual(agg.indices, ["index1"]) |
| 147 | + # pylint: disable=protected-access |
| 148 | + self.assertEqual(agg._sketch_url, "/sketch/1/explore") |
0 commit comments