Search in sources :

Example 1 with QueryTester

use of io.crate.testing.QueryTester in project crate by crate.

the class IntegerColumnReferenceTest method testIntegerExpression.

@Test
public void testIntegerExpression() throws Exception {
    QueryTester.Builder builder = new QueryTester.Builder(createTempDir(), THREAD_POOL, clusterService, Version.CURRENT, "create table t (x int)");
    builder.indexValues("x", IntStream.range(-2, 3).boxed().toArray());
    try (QueryTester tester = builder.build()) {
        List<Object> objects = tester.runQuery("x", "true");
        assertThat(objects, Matchers.contains(-2, -1, 0, 1, 2));
    }
}
Also used : QueryTester(io.crate.testing.QueryTester) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 2 with QueryTester

use of io.crate.testing.QueryTester in project crate by crate.

the class GenericFunctionQueryTest method test_generic_function_query_can_be_cached_if_deterministic.

@Test
public void test_generic_function_query_can_be_cached_if_deterministic() throws Exception {
    QueryTester.Builder builder = new QueryTester.Builder(createTempDir(), THREAD_POOL, clusterService, Version.CURRENT, "create table t (x int)");
    builder.indexValues("x", 1, 2, 3);
    try (QueryTester tester = builder.build()) {
        var query = tester.toQuery("abs(x) = 1");
        var searcher = tester.searcher();
        var weight = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
        assertThat(weight.isCacheable(searcher.getTopReaderContext().leaves().get(0)), is(true));
        assertThat(tester.runQuery("x", "abs(x) = 1"), contains(1));
    }
}
Also used : QueryTester(io.crate.testing.QueryTester) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 3 with QueryTester

use of io.crate.testing.QueryTester in project crate by crate.

the class ArrayLengthQueryTest method testArrayLengthWithAllSupportedTypes.

@Test
public void testArrayLengthWithAllSupportedTypes() throws Exception {
    for (DataType<?> type : DataTypeTesting.ALL_TYPES_EXCEPT_ARRAYS) {
        // This is temporary as long as interval is not fully implemented
        if (type.storageSupport() == null) {
            continue;
        }
        Supplier dataGenerator = DataTypeTesting.getDataGenerator(type);
        Object val1 = dataGenerator.get();
        Object val2 = dataGenerator.get();
        Object[] arr = { val1, val2 };
        Object[] values = new Object[] { arr };
        // ensure the test is operating on a fresh, empty cluster state (no tables)
        resetClusterService();
        try (QueryTester tester = new QueryTester.Builder(createTempDir(), THREAD_POOL, clusterService, Version.CURRENT, "create table \"t_" + type.getName() + "\" (xs array(\"" + type.getName() + "\"))").indexValues("xs", values).build()) {
            System.out.println(type);
            List<Object> result = tester.runQuery("xs", "array_length(xs, 1) >= 2");
            assertThat(result.size(), is(1));
            ArrayType arrayType = new ArrayType<>(type);
            // having the result size check should be sufficient anyway
            if (type.id() != ObjectType.ID) {
                assertThat(arrayType.compare((List) result.get(0), Arrays.asList(arr)), is(0));
            }
        }
    }
}
Also used : ArrayType(io.crate.types.ArrayType) QueryTester(io.crate.testing.QueryTester) Supplier(java.util.function.Supplier) List(java.util.List) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Example 4 with QueryTester

use of io.crate.testing.QueryTester in project crate by crate.

the class CIDRRangeQueryTest method test.

private void test(Object[] valuesToIndex, String queryStr, Object... expectedResults) throws Throwable {
    QueryTester.Builder builder = new QueryTester.Builder(createTempDir(), THREAD_POOL, clusterService, Version.CURRENT, "create table t (ip_addr ip)");
    builder.indexValues("ip_addr", valuesToIndex);
    try (QueryTester tester = builder.build()) {
        assertThat(tester.runQuery("ip_addr", queryStr), Matchers.contains(expectedResults));
    }
}
Also used : QueryTester(io.crate.testing.QueryTester)

Example 5 with QueryTester

use of io.crate.testing.QueryTester in project crate by crate.

the class GenericFunctionQueryTest method test_generic_function_query_cannot_be_cached_with_un_deterministic_functions_present.

@Test
public void test_generic_function_query_cannot_be_cached_with_un_deterministic_functions_present() throws Exception {
    QueryTester.Builder builder = new QueryTester.Builder(createTempDir(), THREAD_POOL, clusterService, Version.CURRENT, "create table t (x int)");
    builder.indexValues("x", 1, 2, 3);
    try (QueryTester tester = builder.build()) {
        var query = tester.toQuery("x = random()");
        var searcher = tester.searcher();
        var weight = query.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1.0f);
        assertThat(weight.isCacheable(searcher.getTopReaderContext().leaves().get(0)), is(false));
    }
}
Also used : QueryTester(io.crate.testing.QueryTester) Test(org.junit.Test) CrateDummyClusterServiceUnitTest(io.crate.test.integration.CrateDummyClusterServiceUnitTest)

Aggregations

QueryTester (io.crate.testing.QueryTester)6 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)5 Test (org.junit.Test)5 ArrayType (io.crate.types.ArrayType)1 List (java.util.List)1 Supplier (java.util.function.Supplier)1