Skip to content

Commit

Permalink
Added Redis testcontainer and enabled tests (langchain4j#265)
Browse files Browse the repository at this point in the history
Added a Redis Stack testcontainer instance to the unit tests and enabled
the test suite
  • Loading branch information
jruaux authored Nov 10, 2023
1 parent 7c12b39 commit 3a002a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
7 changes: 7 additions & 0 deletions langchain4j-redis/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.redis.testcontainers</groupId>
<artifactId>testcontainers-redis</artifactId>
<version>1.6.4</version>
<scope>test</scope>
</dependency>

</dependencies>

<licenses>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.RelevanceScore;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

import com.redis.testcontainers.RedisStackContainer;

import redis.clients.jedis.JedisPooled;

import java.util.List;
Expand All @@ -22,7 +29,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.data.Percentage.withPercentage;

@Disabled("needs Redis running locally")
@TestInstance(Lifecycle.PER_CLASS)
class RedisEmbeddingStoreTest {

/**
Expand All @@ -31,28 +38,39 @@ class RedisEmbeddingStoreTest {
* docker run -d -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
*/

private static final String HOST = "localhost";
private static final int PORT = 6379;
private static final String METADATA_KEY = "test-key";

private final RedisStackContainer redis = new RedisStackContainer(RedisStackContainer.DEFAULT_IMAGE_NAME.withTag(RedisStackContainer.DEFAULT_TAG));;

private EmbeddingStore<TextSegment> embeddingStore;

private final EmbeddingModel embeddingModel = new AllMiniLmL6V2QuantizedEmbeddingModel();

@BeforeAll
void setup() {
// Redis container setup
redis.start();
}

@AfterAll
void teardown() {
redis.close();
}

@BeforeEach
void initEmptyRedisEmbeddingStore() {

flushDB();

embeddingStore = RedisEmbeddingStore.builder()
.host(HOST)
.port(PORT)
.host(redis.getHost())
.port(redis.getFirstMappedPort())
.dimension(384)
.build();
}

private static void flushDB() {
try (JedisPooled jedis = new JedisPooled(HOST, PORT)) {
private void flushDB() {
try (JedisPooled jedis = new JedisPooled(redis.getHost(), redis.getFirstMappedPort())) {
jedis.flushDB();
}
}
Expand Down Expand Up @@ -118,8 +136,8 @@ void should_add_embedding_with_segment_with_metadata() {
flushDB();

embeddingStore = RedisEmbeddingStore.builder()
.host(HOST)
.port(PORT)
.host(redis.getHost())
.port(redis.getFirstMappedPort())
.dimension(384)
.metadataFieldsName(singletonList(METADATA_KEY))
.build();
Expand Down

0 comments on commit 3a002a6

Please sign in to comment.