|
| 1 | +package com.redis.testcontainers; |
| 2 | + |
| 3 | +import java.util.HashMap; |
| 4 | +import java.util.Map; |
| 5 | + |
| 6 | +import org.junit.jupiter.api.AfterAll; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.api.BeforeAll; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.TestInstance; |
| 12 | +import org.junit.jupiter.api.TestInstance.Lifecycle; |
| 13 | +import org.testcontainers.junit.jupiter.Testcontainers; |
| 14 | + |
| 15 | +import com.redis.lettucemod.RedisModulesClient; |
| 16 | +import com.redis.lettucemod.api.StatefulRedisModulesConnection; |
| 17 | +import com.redis.lettucemod.api.sync.RedisModulesCommands; |
| 18 | +import com.redis.lettucemod.cluster.RedisModulesClusterClient; |
| 19 | +import com.redis.lettucemod.search.Field; |
| 20 | +import com.redis.lettucemod.search.SearchResults; |
| 21 | +import com.redis.lettucemod.timeseries.CreateOptions; |
| 22 | +import com.redis.lettucemod.timeseries.Label; |
| 23 | +import com.redis.lettucemod.timeseries.Sample; |
| 24 | +import com.redis.lettucemod.util.RedisModulesUtils; |
| 25 | + |
| 26 | +import io.lettuce.core.AbstractRedisClient; |
| 27 | + |
| 28 | +@Testcontainers |
| 29 | +@TestInstance(Lifecycle.PER_CLASS) |
| 30 | +@SuppressWarnings("unchecked") |
| 31 | +abstract class AbstractModulesTestBase { |
| 32 | + |
| 33 | + private RedisServer redis; |
| 34 | + private AbstractRedisClient client; |
| 35 | + private StatefulRedisModulesConnection<String, String> connection; |
| 36 | + private RedisModulesCommands<String, String> commands; |
| 37 | + |
| 38 | + protected abstract RedisServer getRedisServer(); |
| 39 | + |
| 40 | + @BeforeAll |
| 41 | + public void setup() { |
| 42 | + redis = getRedisServer(); |
| 43 | + redis.start(); |
| 44 | + client = client(redis); |
| 45 | + connection = RedisModulesUtils.connection(client); |
| 46 | + commands = connection.sync(); |
| 47 | + } |
| 48 | + |
| 49 | + private AbstractRedisClient client(RedisServer redis) { |
| 50 | + if (redis.isCluster()) { |
| 51 | + return RedisModulesClusterClient.create(redis.getRedisURI()); |
| 52 | + } |
| 53 | + return RedisModulesClient.create(redis.getRedisURI()); |
| 54 | + } |
| 55 | + |
| 56 | + @AfterAll |
| 57 | + public void teardown() { |
| 58 | + commands = null; |
| 59 | + connection.close(); |
| 60 | + client.close(); |
| 61 | + redis.stop(); |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + void flushall() { |
| 66 | + commands.flushall(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void ping() { |
| 71 | + Assertions.assertEquals("PONG", commands.ping()); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void search() { |
| 76 | + commands.ftCreate("test", Field.text("name").build(), Field.tag("id").build()); |
| 77 | + int count = 10; |
| 78 | + for (int index = 0; index < count; index++) { |
| 79 | + Map<String, String> doc = new HashMap<>(); |
| 80 | + doc.put("name", "name " + index); |
| 81 | + doc.put("id", String.valueOf(index + 1)); |
| 82 | + commands.hset("hash:" + index, doc); |
| 83 | + } |
| 84 | + SearchResults<String, String> results = commands.ftSearch("test", "*"); |
| 85 | + Assertions.assertEquals(count, results.getCount()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void timeseries() { |
| 90 | + // TimeSeries tests |
| 91 | + commands.tsCreate("temperature:3:11", CreateOptions.<String, String>builder().retentionPeriod(6000) |
| 92 | + .labels(Label.of("sensor_id", "2"), Label.of("area_id", "32")).build()); |
| 93 | + // TS.ADD temperature:3:11 1548149181 30 |
| 94 | + Long add1 = commands.tsAdd("temperature:3:11", Sample.of(1548149181, 30)); |
| 95 | + Assertions.assertEquals(1548149181, add1); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void writeHash() { |
| 100 | + // Write test |
| 101 | + Map<String, String> map = new HashMap<>(); |
| 102 | + map.put("field1", "value1"); |
| 103 | + String key = "testhash"; |
| 104 | + commands.hset(key, map); |
| 105 | + Assertions.assertEquals(map, commands.hgetall(key)); |
| 106 | + } |
| 107 | + |
| 108 | +} |
0 commit comments