use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class TimeValueTests method testCompareValue.
public void testCompareValue() {
long firstRandom = randomNonNegativeLong();
long secondRandom = randomValueOtherThan(firstRandom, OpenSearchTestCase::randomNonNegativeLong);
TimeUnit unit = randomFrom(TimeUnit.values());
TimeValue firstValue = new TimeValue(firstRandom, unit);
TimeValue secondValue = new TimeValue(secondRandom, unit);
assertEquals(firstRandom > secondRandom, firstValue.compareTo(secondValue) > 0);
assertEquals(secondRandom > firstRandom, secondValue.compareTo(firstValue) > 0);
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class FunctionScoreIT method testMinScoreFunctionScoreBasic.
public void testMinScoreFunctionScoreBasic() throws IOException {
float score = randomValueOtherThanMany((f) -> Float.compare(f, 0) < 0, OpenSearchTestCase::randomFloat);
float minScore = randomValueOtherThanMany((f) -> Float.compare(f, 0) < 0, OpenSearchTestCase::randomFloat);
index(INDEX, TYPE, jsonBuilder().startObject().field("num", 2).field("random_score", // Pass the random score as a document field so that it can be extracted in the script
score).endObject());
refresh();
ensureYellow();
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['random_score']", Collections.emptyMap());
SearchResponse searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(scriptFunction(script)).setMinScore(minScore)))).actionGet();
if (score < minScore) {
assertThat(searchResponse.getHits().getTotalHits().value, is(0L));
} else {
assertThat(searchResponse.getHits().getTotalHits().value, is(1L));
}
searchResponse = client().search(searchRequest().source(searchSource().query(functionScoreQuery(new MatchAllQueryBuilder(), new FilterFunctionBuilder[] { new FilterFunctionBuilder(scriptFunction(script)), new FilterFunctionBuilder(scriptFunction(script)) }).scoreMode(FunctionScoreQuery.ScoreMode.AVG).setMinScore(minScore)))).actionGet();
if (score < minScore) {
assertThat(searchResponse.getHits().getTotalHits().value, is(0L));
} else {
assertThat(searchResponse.getHits().getTotalHits().value, is(1L));
}
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class AbstractBulkByScrollRequestTestCase method testForSlice.
public void testForSlice() {
R original = newRequest();
extraRandomizationForSlice(original);
original.setAbortOnVersionConflict(randomBoolean());
original.setRefresh(randomBoolean());
original.setTimeout(parseTimeValue(randomPositiveTimeValue(), "timeout"));
original.setWaitForActiveShards(randomFrom(ActiveShardCount.ALL, ActiveShardCount.NONE, ActiveShardCount.ONE, ActiveShardCount.DEFAULT));
original.setRetryBackoffInitialTime(parseTimeValue(randomPositiveTimeValue(), "retry_backoff_initial_time"));
original.setMaxRetries(between(0, 1000));
original.setRequestsPerSecond(randomBoolean() ? Float.POSITIVE_INFINITY : randomValueOtherThanMany(r -> r < 0, OpenSearchTestCase::randomFloat));
if (randomBoolean()) {
if (randomBoolean()) {
original.setMaxDocs(between(0, Integer.MAX_VALUE));
} else {
original.setSize(between(0, Integer.MAX_VALUE));
}
}
// it's not important how many slices there are, we just need a number for forSlice
int actualSlices = between(2, 1000);
original.setSlices(randomBoolean() ? actualSlices : AbstractBulkByScrollRequest.AUTO_SLICES);
TaskId slicingTask = new TaskId(randomAlphaOfLength(5), randomLong());
SearchRequest sliceRequest = new SearchRequest();
R forSliced = original.forSlice(slicingTask, sliceRequest, actualSlices);
assertEquals(original.isAbortOnVersionConflict(), forSliced.isAbortOnVersionConflict());
assertEquals(original.isRefresh(), forSliced.isRefresh());
assertEquals(original.getTimeout(), forSliced.getTimeout());
assertEquals(original.getWaitForActiveShards(), forSliced.getWaitForActiveShards());
assertEquals(original.getRetryBackoffInitialTime(), forSliced.getRetryBackoffInitialTime());
assertEquals(original.getMaxRetries(), forSliced.getMaxRetries());
assertEquals("only the parent task should store results", false, forSliced.getShouldStoreResult());
assertEquals("slice requests always have a single worker", 1, forSliced.getSlices());
assertEquals("requests_per_second is split between all workers", original.getRequestsPerSecond() / actualSlices, forSliced.getRequestsPerSecond(), Float.MIN_NORMAL);
assertEquals("max_docs is split evenly between all workers", original.getMaxDocs() == AbstractBulkByScrollRequest.MAX_DOCS_ALL_MATCHES ? AbstractBulkByScrollRequest.MAX_DOCS_ALL_MATCHES : original.getMaxDocs() / actualSlices, forSliced.getMaxDocs());
assertEquals(slicingTask, forSliced.getParentTask());
extraForSliceAssertions(original, forSliced);
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class UpdateRequestTests method testUpdateScript.
public void testUpdateScript() throws Exception {
ShardId shardId = new ShardId("test", "", 0);
GetResult getResult = new GetResult("test", "1", 0, 1, 0, true, new BytesArray("{\"body\": \"bar\"}"), null, null);
UpdateRequest request = new UpdateRequest("test", "1").script(mockInlineScript("ctx._source.body = \"foo\""));
UpdateHelper.Result result = updateHelper.prepareUpdateScriptRequest(shardId, request, getResult, OpenSearchTestCase::randomNonNegativeLong);
assertThat(result.action(), instanceOf(IndexRequest.class));
assertThat(result.getResponseResult(), equalTo(DocWriteResponse.Result.UPDATED));
assertThat(result.updatedSourceAsMap().get("body").toString(), equalTo("foo"));
// Now where the script changes the op to "delete"
request = new UpdateRequest("test", "1").script(mockInlineScript("ctx.op = delete"));
result = updateHelper.prepareUpdateScriptRequest(shardId, request, getResult, OpenSearchTestCase::randomNonNegativeLong);
assertThat(result.action(), instanceOf(DeleteRequest.class));
assertThat(result.getResponseResult(), equalTo(DocWriteResponse.Result.DELETED));
// We treat everything else as a No-op
boolean goodNoop = randomBoolean();
if (goodNoop) {
request = new UpdateRequest("test", "1").script(mockInlineScript("ctx.op = none"));
} else {
request = new UpdateRequest("test", "1").script(mockInlineScript("ctx.op = bad"));
}
result = updateHelper.prepareUpdateScriptRequest(shardId, request, getResult, OpenSearchTestCase::randomNonNegativeLong);
assertThat(result.action(), instanceOf(UpdateResponse.class));
assertThat(result.getResponseResult(), equalTo(DocWriteResponse.Result.NOOP));
}
use of org.opensearch.test.OpenSearchTestCase in project OpenSearch by opensearch-project.
the class CoordinationMetadataTests method testCoordinationMetadataSerializationEqualsHashCode.
public void testCoordinationMetadataSerializationEqualsHashCode() {
CoordinationMetadata initialMetadata = new CoordinationMetadata(randomNonNegativeLong(), randomVotingConfig(), randomVotingConfig(), randomVotingTombstones());
// Note: the explicit cast of the CopyFunction is needed for some IDE (specifically Eclipse 4.8.0) to infer the right type
EqualsHashCodeTestUtils.checkEqualsAndHashCode(initialMetadata, (CopyFunction<CoordinationMetadata>) orig -> OpenSearchTestCase.copyWriteable(orig, new NamedWriteableRegistry(Collections.emptyList()), CoordinationMetadata::new), meta -> {
CoordinationMetadata.Builder builder = CoordinationMetadata.builder(meta);
switch(randomInt(3)) {
case 0:
builder.term(randomValueOtherThan(meta.term(), OpenSearchTestCase::randomNonNegativeLong));
break;
case 1:
builder.lastCommittedConfiguration(randomlyChangeVotingConfiguration(meta.getLastCommittedConfiguration()));
break;
case 2:
builder.lastAcceptedConfiguration(randomlyChangeVotingConfiguration(meta.getLastAcceptedConfiguration()));
break;
case 3:
if (meta.getVotingConfigExclusions().isEmpty() == false && randomBoolean()) {
builder.clearVotingConfigExclusions();
} else {
randomVotingTombstones().forEach(dn -> builder.addVotingConfigExclusion(dn));
}
break;
}
return builder.build();
});
}
Aggregations