Search in sources :

Example 1 with UseJdbc

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

the class TransportSQLActionClassLifecycleTest method testCountWithGroupByNullArgs.

@Test
// NPE because of unused null parameter
@UseJdbc(0)
public void testCountWithGroupByNullArgs() throws Exception {
    new Setup(sqlExecutor).groupBySetup();
    SQLResponse response = execute("select count(*), race from characters group by race", new Object[] { null });
    assertEquals(3, response.rowCount());
}
Also used : SQLResponse(io.crate.testing.SQLResponse) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 2 with UseJdbc

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

the class SQLTypeMappingTest method testGetRequestMapping.

/**
 * We must fix this test to run ALL statements via JDBC or not because object/map values are NOT preserving exact
 * its elements numeric types (e.g. Long(0) becomes Integer(0)). This is caused by the usage of JSON for psql text
 * serialization. See e.g. {@link org.codehaus.jackson.io.NumberOutput#outputLong(long, byte[], int)}.
 */
@UseJdbc(0)
@Test
public void testGetRequestMapping() throws Exception {
    setUpSimple();
    execute("insert into t1 (id, string_field, boolean_field, byte_field, short_field, integer_field," + "long_field, float_field, double_field, object_field," + "timestamp_field, ip_field) values " + "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", new Object[] { 0, "Blabla", true, 120, 1000, 1200000, 120000000000L, 1.4, 3.456789, new HashMap<String, Object>() {

        {
            put("inner", "1970-01-01");
        }
    }, "1970-01-01", "127.0.0.1" });
    refresh();
    SQLResponse getResponse = execute("select * from t1 where id=0");
    SQLResponse searchResponse = execute("select * from t1 limit 1");
    for (int i = 0; i < getResponse.rows()[0].length; i++) {
        assertThat(getResponse.rows()[0][i], is(searchResponse.rows()[0][i]));
    }
}
Also used : SQLResponse(io.crate.testing.SQLResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 3 with UseJdbc

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

the class SQLTypeMappingTest method testInsertNewObjectColumn.

/**
 * Disable JDBC/PSQL as object values are streamed via JSON on the PSQL wire protocol which is not type safe.
 */
@UseJdbc(0)
@Test
public void testInsertNewObjectColumn() throws Exception {
    setUpSimple();
    execute("insert into t1 (id, new_col) values (?,?)", new Object[] { 0, new HashMap<String, Object>() {

        {
            put("a_date", "1970-01-01");
            put("an_int", 127);
            put("a_long", Long.MAX_VALUE);
            put("a_boolean", true);
        }
    } });
    refresh();
    waitForMappingUpdateOnAll("t1", "new_col");
    SQLResponse response = execute("select id, new_col from t1 where id=0");
    @SuppressWarnings("unchecked") Map<String, Object> mapped = (Map<String, Object>) response.rows()[0][1];
    assertEquals("1970-01-01", mapped.get("a_date"));
    assertEquals(0x7fffffffffffffffL, mapped.get("a_long"));
    assertEquals(true, mapped.get("a_boolean"));
    // The inner value will result in an Long type as we rely on ES mappers here and the dynamic ES parsing
    // will define integers as longs (no concrete type was specified so use long to be safe)
    assertEquals(127L, mapped.get("an_int"));
}
Also used : SQLResponse(io.crate.testing.SQLResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 4 with UseJdbc

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

the class SQLTypeMappingTest method testParseInsertObject.

/**
 * Disabled JDBC usage cause of text mode JSON encoding which is not type safe on numeric types.
 * E.g. byte values are always converted to integers,
 * see {@link com.fasterxml.jackson.core.JsonGenerator#writeNumber(short)}.
 */
@UseJdbc(0)
@Test
public void testParseInsertObject() throws Exception {
    setUpObjectTable();
    execute("insert into test12 (object_field, strict_field, " + "no_dynamic_field) values (?,?,?)", new Object[] { new HashMap<String, Object>() {

        {
            put("size", 127);
            put("created", "2013-11-19");
        }
    }, new HashMap<String, Object>() {

        {
            put("path", "/dev/null");
            put("created", "1970-01-01T00:00:00");
        }
    }, new HashMap<String, Object>() {

        {
            put("path", "/etc/shadow");
            put("dynamic_again", new HashMap<String, Object>() {

                {
                    put("field", 1384790145.289);
                }
            });
        }
    } });
    refresh();
    SQLResponse response = execute("select object_field, strict_field, no_dynamic_field from test12");
    assertEquals(1, response.rowCount());
    assertThat(response.rows()[0][0], instanceOf(Map.class));
    @SuppressWarnings("unchecked") Map<String, Object> objectMap = (Map<String, Object>) response.rows()[0][0];
    assertEquals(1384819200000L, objectMap.get("created"));
    assertEquals((byte) 127, objectMap.get("size"));
    assertThat(response.rows()[0][1], instanceOf(Map.class));
    @SuppressWarnings("unchecked") Map<String, Object> strictMap = (Map<String, Object>) response.rows()[0][1];
    assertEquals("/dev/null", strictMap.get("path"));
    assertEquals(0L, strictMap.get("created"));
    assertThat(response.rows()[0][2], instanceOf(Map.class));
    @SuppressWarnings("unchecked") Map<String, Object> noDynamicMap = (Map<String, Object>) response.rows()[0][2];
    assertEquals("/etc/shadow", noDynamicMap.get("path"));
    assertEquals(new HashMap<String, Object>() {

        {
            put("field", 1384790145289L);
        }
    }, noDynamicMap.get("dynamic_again"));
    response = execute("select object_field['created'], object_field['size'], " + "no_dynamic_field['dynamic_again']['field'] from test12");
    assertEquals(1384819200000L, response.rows()[0][0]);
    assertEquals((byte) 127, response.rows()[0][1]);
    assertEquals(1384790145289L, response.rows()[0][2]);
}
Also used : HashMap(java.util.HashMap) SQLResponse(io.crate.testing.SQLResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) HashMap(java.util.HashMap) Map(java.util.Map) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test)

Example 5 with UseJdbc

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

the class TransportSQLActionTest method test_bit_string_can_be_inserted_and_queried.

@Test
@UseJdbc(0)
@UseRandomizedSchema(random = false)
public void test_bit_string_can_be_inserted_and_queried() throws Exception {
    execute("create table tbl (xs bit(4))");
    execute("insert into tbl (xs) values (B'0000'), (B'0001'), (B'0011'), (B'0111'), (B'1111'), (B'1001')");
    assertThat(response.rowCount(), is(6L));
    execute("refresh table tbl");
    execute("SELECT _doc['xs'], xs, _raw, xs::bit(3) FROM tbl WHERE xs = B'1001'");
    assertThat(TestingHelpers.printedTable(response.rows()), is("B'1001'| B'1001'| {\"xs\":\"CQ==\"}| B'100'\n"));
    // use LIMIT 1 to hit a different execution path that should load `xs` differently
    execute("SELECT _doc['xs'], xs, _raw, xs::bit(3) FROM tbl WHERE xs = B'1001' LIMIT 1");
    assertThat(TestingHelpers.printedTable(response.rows()), is("B'1001'| B'1001'| {\"xs\":\"CQ==\"}| B'100'\n"));
    var properties = new Properties();
    properties.put("user", "crate");
    properties.put("password", "");
    ArrayList<String> results = new ArrayList<>();
    try (var conn = DriverManager.getConnection(sqlExecutor.jdbcUrl(), properties)) {
        Statement stmt = conn.createStatement();
        ResultSet result = stmt.executeQuery("select xs from tbl order by xs");
        while (result.next()) {
            String string = result.getString(1);
            results.add(string);
        }
    }
    assertThat(results, Matchers.contains("B'0000'", "B'0001'", "B'0011'", "B'0111'", "B'1001'", "B'1111'"));
}
Also used : Statement(java.sql.Statement) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) Matchers.containsString(org.hamcrest.Matchers.containsString) Properties(java.util.Properties) UseJdbc(io.crate.testing.UseJdbc) Test(org.junit.Test) UseRandomizedSchema(io.crate.testing.UseRandomizedSchema)

Aggregations

UseJdbc (io.crate.testing.UseJdbc)14 Test (org.junit.Test)14 SQLResponse (io.crate.testing.SQLResponse)8 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 List (java.util.List)3 ArrayList (java.util.ArrayList)2 SQLOperations (io.crate.action.sql.SQLOperations)1 Session (io.crate.action.sql.Session)1 JobsLogService (io.crate.execution.engine.collect.stats.JobsLogService)1 JobsLogs (io.crate.execution.engine.collect.stats.JobsLogs)1 JobContextLog (io.crate.expression.reference.sys.job.JobContextLog)1 UseRandomizedSchema (io.crate.testing.UseRandomizedSchema)1 Path (java.nio.file.Path)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 Properties (java.util.Properties)1 MockTerminal (org.elasticsearch.cli.MockTerminal)1 ClusterState (org.elasticsearch.cluster.ClusterState)1