Repository: ignite Updated Branches: refs/heads/master adcf3a39a -> 8130e89f6
IGNITE-8552: SQL: More tests for temporal classes as keys. This closes #4692. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/8130e89f Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/8130e89f Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/8130e89f Branch: refs/heads/master Commit: 8130e89f6f78711772b1c25ae5b0288a2c2a9d28 Parents: adcf3a3 Author: SGrimstad <[email protected]> Authored: Wed Oct 17 12:18:17 2018 +0300 Committer: devozerov <[email protected]> Committed: Wed Oct 17 12:18:17 2018 +0300 ---------------------------------------------------------------------- .../twostep/CreateTableWithDateKeySelfTest.java | 236 +++++++++++++++++++ .../IgniteCacheQuerySelfTestSuite2.java | 3 + 2 files changed, 239 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/8130e89f/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/twostep/CreateTableWithDateKeySelfTest.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/twostep/CreateTableWithDateKeySelfTest.java b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/twostep/CreateTableWithDateKeySelfTest.java new file mode 100644 index 0000000..91d4c0d --- /dev/null +++ b/modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/twostep/CreateTableWithDateKeySelfTest.java @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF 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.apache.ignite.internal.processors.query.h2.twostep; + +import java.sql.Time; +import java.sql.Timestamp; +import java.time.LocalDateTime; +import java.time.LocalTime; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCache; +import org.apache.ignite.cache.query.FieldsQueryCursor; +import org.apache.ignite.cache.query.SqlFieldsQuery; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; + +/** + * + */ +public class CreateTableWithDateKeySelfTest extends GridCommonAbstractTest { + /** */ + private static final int NODES_COUNT = 1; + + /** */ + private static Ignite ignite; + + /** */ + private static IgniteCache<?, ?> initCache; + + /** {@inheritDoc} */ + @Override protected void beforeTestsStarted() throws Exception { + ignite = startGridsMultiThreaded(NODES_COUNT, false); + + initCache = ignite.getOrCreateCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME) + .setSqlSchema("PUBLIC") + ); + } + + /** {@inheritDoc} */ + @Override protected void afterTestsStopped() throws Exception { + stopAllGrids(); + + ignite = null; + initCache = null; + } + + /** */ + public void testPassTableWithDateKeyCreation() { + final String creationQry = "CREATE TABLE %s (id DATE primary key, dateField DATE) " + + "WITH \"cache_name=%s, WRAP_VALUE=false\""; + + Map<java.sql.Date, java.sql.Date> ent = new HashMap<>(); + + ent.put(java.sql.Date.valueOf("2018-09-01"), java.sql.Date.valueOf("2018-09-02")); + + ent.put(java.sql.Date.valueOf("2018-09-03"), java.sql.Date.valueOf("2018-09-04")); + + checkInsertUpdateDelete(creationQry, "Tab1", ent); + } + + /** */ + public void testPassTableWithTimeKeyCreation() { + final String creationQry = "CREATE TABLE %s (id TIME primary key, dateField TIME) " + + "WITH \"cache_name=%s, WRAP_VALUE=false\""; + + Map<Time, Time> ent = new HashMap<>(); + + ent.put(Time.valueOf(LocalTime.now()), Time.valueOf(LocalTime.now().minusHours(1))); + + ent.put(Time.valueOf(LocalTime.now().minusHours(2)), Time.valueOf(LocalTime.now().minusHours(3))); + + checkInsertUpdateDelete(creationQry, "Tab2", ent); + } + + /** */ + public void testPassTableWithTimeStampKeyCreation() { + final String creationQry = "CREATE TABLE %s (id TIMESTAMP primary key, dateField TIMESTAMP) " + + "WITH \"cache_name=%s, WRAP_VALUE=false\""; + + Map<Timestamp, Timestamp> ent = new HashMap<>(); + + ent.put(Timestamp.valueOf(LocalDateTime.now()), Timestamp.valueOf(LocalDateTime.now().minusHours(1))); + + ent.put(Timestamp.valueOf(LocalDateTime.now().minusHours(2)), + Timestamp.valueOf(LocalDateTime.now().minusHours(3))); + + checkInsertUpdateDelete(creationQry, "Tab3", ent); + } + + /** + * Common testing logic + * + * @param creationQry Create table query. + * @param tblName Table name. + * @param entries Map with Key-Value pairs + * @param <K> Type of key. + * @param <V> Type of value. + */ + private <K, V> void checkInsertUpdateDelete( + final String creationQry, + final String tblName, + final Map<K, V> entries + ) { + final String cacheName = "TABLE_CACHE_" + tblName; + + try (FieldsQueryCursor<List<?>> cur = initCache.query( + new SqlFieldsQuery(String.format(creationQry, tblName, cacheName)))) { + + assertNotNull(cur); + + List<List<?>> rows = cur.getAll(); + + assertEquals(1, rows.size()); + + assertEquals(0L, rows.get(0).get(0)); + } + + IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName); + + for (Map.Entry<K, V> e : entries.entrySet()) { + try (FieldsQueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery( + "INSERT INTO " + tblName + " VALUES(?, ?)").setArgs(e.getKey(), e.getValue()))) { + + assertNotNull(cur); + + List<List<?>> rows = cur.getAll(); + + assertEquals(1, rows.size()); + + assertEquals(1L, rows.get(0).get(0)); + } + } + + K key = null; + V val = null; + for (Map.Entry<K, V> e : entries.entrySet()) { + assertSelection(tblName, cache, e.getKey(), e.getValue()); + + assertEquals(e.getValue(), cache.get(e.getKey())); + + key = e.getKey(); + val = e.getValue(); + } + + cache.remove(key); + + assertAbsence(tblName, cache, key); + + assertFalse(cache.containsKey(key)); + + cache.put(key, val); + + assertEquals(val, cache.get(key)); + + assertSelection(tblName, cache, key, val); + + try (FieldsQueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery( + "DELETE FROM " + tblName + " WHERE id=?").setArgs(key))) { + + assertNotNull(cur); + + List<List<?>> rows = cur.getAll(); + + assertEquals(1, rows.size()); + + assertEquals(1L, rows.get(0).get(0)); + } + + assertAbsence(tblName, cache, key); + + assertFalse(cache.containsKey(key)); + } + + /** + * Check absence of key in the table. + * + * @param tblName Table name. + * @param cache Cache. + * @param key Sample key. + * @param <K> Type of key. + * @param <V> Type of value. + */ + private <K, V> void assertAbsence(final String tblName, final IgniteCache<K, V> cache, final K key) { + try (FieldsQueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery( + "select * from " + tblName + " where id=?").setArgs(key))) { + assertNotNull(cur); + + List<List<?>> rows = cur.getAll(); + + assertEquals(0, rows.size()); + } + } + + /** + * Check presence of key in the table. + * + * @param tblName Table name. + * @param cache Cache. + * @param key Sample key. + * @param val Sample value. + * @param <K> Type of key. + * @param <V> Type of value. + */ + private <K, V> void assertSelection(final String tblName, final IgniteCache<K, V> cache, final K key, + final V val) { + try (FieldsQueryCursor<List<?>> cur = cache.query(new SqlFieldsQuery( + "select * from " + tblName + " where id=?").setArgs(key))) { + assertNotNull(cur); + + List<List<?>> rows = cur.getAll(); + + assertEquals(1, rows.size()); + + assertEquals(key, rows.get(0).get(0)); + + assertEquals(val, rows.get(0).get(1)); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/8130e89f/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java ---------------------------------------------------------------------- diff --git a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java index 4b91b43..dba046b 100644 --- a/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java +++ b/modules/indexing/src/test/java/org/apache/ignite/testsuites/IgniteCacheQuerySelfTestSuite2.java @@ -51,6 +51,7 @@ import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlDistribut import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlSegmentedIndexMultiNodeSelfTest; import org.apache.ignite.internal.processors.query.IgniteCacheGroupsSqlSegmentedIndexSelfTest; import org.apache.ignite.internal.processors.query.h2.twostep.CacheQueryMemoryLeakTest; +import org.apache.ignite.internal.processors.query.h2.twostep.CreateTableWithDateKeySelfTest; import org.apache.ignite.internal.processors.query.h2.twostep.DisappearedCacheCauseRetryMessageSelfTest; import org.apache.ignite.internal.processors.query.h2.twostep.DisappearedCacheWasNotFoundMessageSelfTest; import org.apache.ignite.internal.processors.query.h2.twostep.NonCollocatedRetryMessageSelfTest; @@ -115,6 +116,8 @@ public class IgniteCacheQuerySelfTestSuite2 extends TestSuite { suite.addTestSuite(CacheQueryMemoryLeakTest.class); + suite.addTestSuite(CreateTableWithDateKeySelfTest.class); + suite.addTestSuite(NonCollocatedRetryMessageSelfTest.class); suite.addTestSuite(RetryCauseMessageSelfTest.class); suite.addTestSuite(DisappearedCacheCauseRetryMessageSelfTest.class);
