FIX: prepare FindMap query for proper bean cache access

This commit is contained in:
Roland Praml
2023-01-04 16:59:31 +01:00
parent d84c9dc1a9
commit 9e42cfd7d7
2 changed files with 35 additions and 4 deletions
+33 -3
View File
@@ -1,11 +1,12 @@
package org.tests.cache;
import io.ebean.xtest.BaseTestCase;
import io.ebean.DB;
import io.ebean.Query;
import io.ebean.Transaction;
import io.ebean.cache.ServerCache;
import io.ebean.cache.ServerCacheStatistics;
import io.ebean.test.LoggedSql;
import io.ebean.xtest.BaseTestCase;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -54,10 +55,39 @@ public class TestBeanCache extends BaseTestCase {
assertThat(sql).isEmpty();
}
@Test
public void idsInFindMap() {
List<OCachedBean> beans = createBeans(Arrays.asList("m0", "m1", "m2", "m3", "m4", "m5", "m6"));
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
beanCache.clear();
beanCache.statistics(true);
Query<OCachedBean> query = DB.find(OCachedBean.class).setUseCache(true);
// Test findIds
LoggedSql.start();
query.copy()
.where().idIn(ids.subList(0, 1))
.findMap(); // cache key is: 3/d[{/c1000}]/w[List[IdIn[?1],]]
assertThat(LoggedSql.stop().get(0)).contains("in (?)");
LoggedSql.start();
query.copy()
.where().idIn(ids.subList(0, 4))
.findMap(); // cache key is: 3/d[{/c1000}]/w[List[IdIn[?5],]]
assertThat(LoggedSql.stop().get(0)).contains("in (?,?,?,?,?)");
LoggedSql.start();
query.copy()
.where().idIn(ids.subList(2, 6))
.findMap(); // same cache key as above and same SQL above
assertThat(LoggedSql.stop().get(0)).contains("in (?,?,?,?,?)");
}
@Test
public void idsIn_explicitCache_expect_cachePut() {
List<OCachedBean> beans = createBeans(Arrays.asList("k0","k1"));
List<OCachedBean> beans = createBeans(Arrays.asList("k0", "k1"));
List<Long> ids = beans.stream().map(OCachedBean::getId).collect(Collectors.toList());
beanCache.clear();
@@ -183,7 +213,7 @@ public class TestBeanCache extends BaseTestCase {
Country country = DB.find(Country.class)
.where()
.eq("name","NotValid")
.eq("name", "NotValid")
.findOne();
assertThat(country).isNull();