-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Tests: Add unit test for InternalScriptedMetricAggregator #23404
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cbuescher
merged 1 commit into
elastic:master
from
cbuescher:addScriptedMetricAggregatorTests
Feb 28, 2017
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
171 changes: 171 additions & 0 deletions
171
...org/elasticsearch/search/aggregations/metrics/scripted/ScriptedMetricAggregatorTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations.metrics.scripted; | ||
|
|
||
| import org.apache.lucene.document.SortedNumericDocValuesField; | ||
| import org.apache.lucene.index.DirectoryReader; | ||
| import org.apache.lucene.index.IndexReader; | ||
| import org.apache.lucene.index.RandomIndexWriter; | ||
| import org.apache.lucene.search.MatchAllDocsQuery; | ||
| import org.apache.lucene.store.Directory; | ||
| import org.elasticsearch.ElasticsearchException; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.env.Environment; | ||
| import org.elasticsearch.index.IndexSettings; | ||
| import org.elasticsearch.index.mapper.MappedFieldType; | ||
| import org.elasticsearch.index.query.QueryShardContext; | ||
| import org.elasticsearch.indices.breaker.CircuitBreakerService; | ||
| import org.elasticsearch.script.MockScriptEngine; | ||
| import org.elasticsearch.script.Script; | ||
| import org.elasticsearch.script.ScriptContextRegistry; | ||
| import org.elasticsearch.script.ScriptEngineRegistry; | ||
| import org.elasticsearch.script.ScriptService; | ||
| import org.elasticsearch.script.ScriptSettings; | ||
| import org.elasticsearch.script.ScriptType; | ||
| import org.elasticsearch.search.aggregations.AggregatorTestCase; | ||
| import org.junit.BeforeClass; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
|
|
||
| import static java.util.Collections.singleton; | ||
|
|
||
| public class ScriptedMetricAggregatorTests extends AggregatorTestCase { | ||
|
|
||
| private static final String AGG_NAME = "scriptedMetric"; | ||
| private static final Script INIT_SCRIPT = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "initScript", Collections.emptyMap()); | ||
| private static final Script MAP_SCRIPT = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "mapScript", Collections.emptyMap()); | ||
| private static final Script COMBINE_SCRIPT = new Script(ScriptType.INLINE, MockScriptEngine.NAME, "combineScript", Collections.emptyMap()); | ||
| private static final Map<String, Function<Map<String, Object>, Object>> SCRIPTS = new HashMap<>(); | ||
|
|
||
|
|
||
| @BeforeClass | ||
| @SuppressWarnings("unchecked") | ||
| public static void initMockScripts() { | ||
| SCRIPTS.put("initScript", params -> { | ||
| Map<String, Object> agg = (Map<String, Object>) params.get("_agg"); | ||
| agg.put("collector", new ArrayList<Integer>()); | ||
| return agg; | ||
| }); | ||
| SCRIPTS.put("mapScript", params -> { | ||
| Map<String, Object> agg = (Map<String, Object>) params.get("_agg"); | ||
| ((List<Integer>) agg.get("collector")).add(1); // just add 1 for each doc the script is run on | ||
| return agg; | ||
| }); | ||
| SCRIPTS.put("combineScript", params -> { | ||
| Map<String, Object> agg = (Map<String, Object>) params.get("_agg"); | ||
| return ((List<Integer>) agg.get("collector")).stream().mapToInt(Integer::intValue).sum(); | ||
| }); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public void testNoDocs() throws IOException { | ||
| try (Directory directory = newDirectory()) { | ||
| try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { | ||
| // intentionally not writing any docs | ||
| } | ||
| try (IndexReader indexReader = DirectoryReader.open(directory)) { | ||
| ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME); | ||
| aggregationBuilder.mapScript(MAP_SCRIPT); // map script is mandatory, even if its not used in this case | ||
| ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder); | ||
| assertEquals(AGG_NAME, scriptedMetric.getName()); | ||
| assertNotNull(scriptedMetric.aggregation()); | ||
| assertEquals(0, ((HashMap<Object, String>) scriptedMetric.aggregation()).size()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * without combine script, the "_aggs" map should contain a list of the size of the number of documents matched | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public void testScriptedMetricWithoutCombine() throws IOException { | ||
| try (Directory directory = newDirectory()) { | ||
| int numDocs = randomInt(100); | ||
| try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { | ||
| for (int i = 0; i < numDocs; i++) { | ||
| indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i))); | ||
| } | ||
| } | ||
| try (IndexReader indexReader = DirectoryReader.open(directory)) { | ||
| ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME); | ||
| aggregationBuilder.initScript(INIT_SCRIPT).mapScript(MAP_SCRIPT); | ||
| ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder); | ||
| assertEquals(AGG_NAME, scriptedMetric.getName()); | ||
| assertNotNull(scriptedMetric.aggregation()); | ||
| Map<String, Object> agg = (Map<String, Object>) scriptedMetric.aggregation(); | ||
| assertEquals(numDocs, ((List<Integer>) agg.get("collector")).size()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * test that combine script sums the list produced by the "mapScript" | ||
| */ | ||
| public void testScriptedMetricWithCombine() throws IOException { | ||
| try (Directory directory = newDirectory()) { | ||
| Integer numDocs = randomInt(100); | ||
| try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) { | ||
| for (int i = 0; i < numDocs; i++) { | ||
| indexWriter.addDocument(singleton(new SortedNumericDocValuesField("number", i))); | ||
| } | ||
| } | ||
| try (IndexReader indexReader = DirectoryReader.open(directory)) { | ||
| ScriptedMetricAggregationBuilder aggregationBuilder = new ScriptedMetricAggregationBuilder(AGG_NAME); | ||
| aggregationBuilder.initScript(INIT_SCRIPT).mapScript(MAP_SCRIPT).combineScript(COMBINE_SCRIPT); | ||
| ScriptedMetric scriptedMetric = search(newSearcher(indexReader, true, true), new MatchAllDocsQuery(), aggregationBuilder); | ||
| assertEquals(AGG_NAME, scriptedMetric.getName()); | ||
| assertNotNull(scriptedMetric.aggregation()); | ||
| assertEquals(numDocs, scriptedMetric.aggregation()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * We cannot use Mockito for mocking QueryShardContext in this case because | ||
| * script-related methods (e.g. QueryShardContext#getLazyExecutableScript) | ||
| * is final and cannot be mocked | ||
| */ | ||
| @Override | ||
| protected QueryShardContext queryShardContextMock(final MappedFieldType[] fieldTypes, IndexSettings idxSettings, | ||
| CircuitBreakerService circuitBreakerService) { | ||
| Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) | ||
| // no file watching, so we don't need a ResourceWatcherService | ||
| .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), "false").build(); | ||
| MockScriptEngine scriptEngine = new MockScriptEngine(MockScriptEngine.NAME, SCRIPTS); | ||
| ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(scriptEngine)); | ||
| ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(Collections.emptyList()); | ||
| ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); | ||
| ScriptService scriptService; | ||
| try { | ||
| scriptService = new ScriptService(settings, new Environment(settings), null, scriptEngineRegistry, scriptContextRegistry, | ||
| scriptSettings); | ||
| } catch (IOException e) { | ||
| throw new ElasticsearchException(e); | ||
| } | ||
| return new QueryShardContext(0, idxSettings, null, null, null, null, scriptService, xContentRegistry(), | ||
| null, null, System::currentTimeMillis); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍