Skip to content

Commit b83a162

Browse files
quaffmarkpollack
authored andcommitted
JdbcChatMemoryRepository should use the provided JdbcTemplate
Before this commit, the underlying `JdbcTemplate` is created like `new JdbcTemplate(providedJdbcTemplate.getDataSource())`, it means that settings on provided `JdbcTemplate` will lose. Signed-off-by: Yanming Zhou <[email protected]>
1 parent bbc2bbf commit b83a162

File tree

3 files changed

+22
-96
lines changed

3 files changed

+22
-96
lines changed

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/AbstractJdbcChatMemoryRepositoryIT.java

+16-19
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,32 @@
3939
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
4040
import org.springframework.context.annotation.Bean;
4141
import org.springframework.jdbc.core.JdbcTemplate;
42+
import org.springframework.test.context.ContextConfiguration;
43+
44+
import java.sql.Timestamp;
45+
import java.util.List;
46+
import java.util.UUID;
47+
import java.util.stream.Collectors;
48+
49+
import javax.sql.DataSource;
4250

4351
import static org.assertj.core.api.Assertions.assertThat;
4452

4553
/**
4654
* Base class for integration tests for {@link JdbcChatMemoryRepository}.
4755
*
4856
* @author Mark Pollack
57+
* @author Yanming Zhou
4958
*/
59+
@ContextConfiguration(classes = AbstractJdbcChatMemoryRepositoryIT.TestConfiguration.class)
5060
public abstract class AbstractJdbcChatMemoryRepositoryIT {
5161

5262
@Autowired
53-
protected ChatMemoryRepository chatMemoryRepository;
63+
protected JdbcChatMemoryRepository chatMemoryRepository;
5464

5565
@Autowired
5666
protected JdbcTemplate jdbcTemplate;
5767

58-
@Test
59-
void correctChatMemoryRepositoryInstance() {
60-
assertThat(this.chatMemoryRepository).isInstanceOf(ChatMemoryRepository.class);
61-
}
62-
6368
@ParameterizedTest
6469
@CsvSource({ "Message from assistant,ASSISTANT", "Message from user,USER", "Message from system,SYSTEM" })
6570
void saveMessagesSingleMessage(String content, MessageType messageType) {
@@ -163,11 +168,6 @@ void deleteMessagesByConversationId() {
163168

164169
@Test
165170
void testMessageOrder() {
166-
// Create a repository using the from method to detect the dialect
167-
JdbcChatMemoryRepository repository = JdbcChatMemoryRepository.builder()
168-
.jdbcTemplate(this.jdbcTemplate)
169-
.dialect(JdbcChatMemoryRepositoryDialect.from(this.jdbcTemplate.getDataSource()))
170-
.build();
171171

172172
var conversationId = UUID.randomUUID().toString();
173173

@@ -179,10 +179,10 @@ void testMessageOrder() {
179179

180180
// Save messages in the expected order
181181
List<Message> orderedMessages = List.of(firstMessage, secondMessage, thirdMessage, fourthMessage);
182-
repository.saveAll(conversationId, orderedMessages);
182+
chatMemoryRepository.saveAll(conversationId, orderedMessages);
183183

184184
// Retrieve messages using the repository
185-
List<Message> retrievedMessages = repository.findByConversationId(conversationId);
185+
List<Message> retrievedMessages = chatMemoryRepository.findByConversationId(conversationId);
186186
assertThat(retrievedMessages).hasSize(4);
187187

188188
// Get the actual order from the retrieved messages
@@ -197,14 +197,11 @@ void testMessageOrder() {
197197
* Base configuration for all integration tests.
198198
*/
199199
@ImportAutoConfiguration({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class })
200-
static abstract class BaseTestConfiguration {
200+
static class TestConfiguration {
201201

202202
@Bean
203-
ChatMemoryRepository chatMemoryRepository(JdbcTemplate jdbcTemplate, DataSource dataSource) {
204-
return JdbcChatMemoryRepository.builder()
205-
.jdbcTemplate(jdbcTemplate)
206-
.dialect(JdbcChatMemoryRepositoryDialect.from(dataSource))
207-
.build();
203+
ChatMemoryRepository chatMemoryRepository(DataSource dataSource) {
204+
return JdbcChatMemoryRepository.builder().dataSource(dataSource).build();
208205
}
209206

210207
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryMysqlIT.java

+3-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.ai.chat.memory.repository.jdbc;
1818

19-
import org.springframework.boot.SpringBootConfiguration;
2019
import org.springframework.boot.test.context.SpringBootTest;
2120
import org.springframework.test.context.TestPropertySource;
2221
import org.springframework.test.context.jdbc.Sql;
@@ -27,16 +26,11 @@
2726
* @author Jonathan Leijendekker
2827
* @author Thomas Vitale
2928
* @author Mark Pollack
29+
* @author Yanming Zhou
3030
*/
31-
@SpringBootTest(classes = JdbcChatMemoryRepositoryMysqlIT.TestConfiguration.class)
32-
@TestPropertySource(properties = { "spring.datasource.url=jdbc:tc:mariadb:10.3.39:///",
33-
"spring.datasource.hikari.maximum-pool-size=20", "spring.datasource.hikari.minimum-idle=5" })
31+
@SpringBootTest
32+
@TestPropertySource(properties = { "spring.datasource.url=jdbc:tc:mariadb:10.3.39:///" })
3433
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-mariadb.sql")
3534
class JdbcChatMemoryRepositoryMysqlIT extends AbstractJdbcChatMemoryRepositoryIT {
3635

37-
@SpringBootConfiguration
38-
static class TestConfiguration extends BaseTestConfiguration {
39-
40-
}
41-
4236
}

memory/repository/spring-ai-model-chat-memory-repository-jdbc/src/test/java/org/springframework/ai/chat/memory/repository/jdbc/JdbcChatMemoryRepositoryPostgresqlIT.java

+3-68
Original file line numberDiff line numberDiff line change
@@ -16,86 +16,21 @@
1616

1717
package org.springframework.ai.chat.memory.repository.jdbc;
1818

19-
import java.util.List;
20-
import java.util.UUID;
21-
22-
import javax.sql.DataSource;
23-
24-
import org.junit.jupiter.api.Test;
25-
26-
import org.springframework.ai.chat.memory.ChatMemoryRepository;
27-
import org.springframework.ai.chat.messages.AssistantMessage;
28-
import org.springframework.ai.chat.messages.Message;
29-
import org.springframework.ai.chat.messages.SystemMessage;
30-
import org.springframework.ai.chat.messages.UserMessage;
31-
import org.springframework.boot.SpringBootConfiguration;
3219
import org.springframework.boot.test.context.SpringBootTest;
33-
import org.springframework.context.annotation.Bean;
34-
import org.springframework.jdbc.core.JdbcTemplate;
35-
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
3620
import org.springframework.test.context.TestPropertySource;
3721
import org.springframework.test.context.jdbc.Sql;
3822

39-
import static org.assertj.core.api.Assertions.assertThat;
40-
4123
/**
4224
* Integration tests for {@link JdbcChatMemoryRepository} with PostgreSQL.
4325
*
4426
* @author Jonathan Leijendekker
4527
* @author Thomas Vitale
4628
* @author Mark Pollack
29+
* @author Yanming Zhou
4730
*/
48-
@SpringBootTest(classes = JdbcChatMemoryRepositoryPostgresqlIT.TestConfiguration.class)
49-
@TestPropertySource(properties = { "spring.datasource.url=jdbc:tc:postgresql:17:///",
50-
"spring.datasource.hikari.maximum-pool-size=20", "spring.datasource.hikari.minimum-idle=5" })
31+
@SpringBootTest
32+
@TestPropertySource(properties = "spring.datasource.url=jdbc:tc:postgresql:17:///")
5133
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-postgresql.sql")
5234
class JdbcChatMemoryRepositoryPostgresqlIT extends AbstractJdbcChatMemoryRepositoryIT {
5335

54-
@Test
55-
void repositoryWithExplicitTransactionManager() {
56-
// Get the repository with explicit transaction manager
57-
ChatMemoryRepository repositoryWithTxManager = TestConfiguration
58-
.chatMemoryRepositoryWithTransactionManager(this.jdbcTemplate, this.jdbcTemplate.getDataSource());
59-
60-
var conversationId = UUID.randomUUID().toString();
61-
var messages = List.<Message>of(new AssistantMessage("Message with transaction manager - " + conversationId),
62-
new UserMessage("User message with transaction manager - " + conversationId));
63-
64-
// Save messages using the repository with explicit transaction manager
65-
repositoryWithTxManager.saveAll(conversationId, messages);
66-
67-
// Verify messages were saved correctly
68-
var savedMessages = repositoryWithTxManager.findByConversationId(conversationId);
69-
assertThat(savedMessages).hasSize(2);
70-
assertThat(savedMessages).isEqualTo(messages);
71-
72-
// Verify transaction works by updating and checking atomicity
73-
var newMessages = List.<Message>of(new SystemMessage("New system message - " + conversationId));
74-
repositoryWithTxManager.saveAll(conversationId, newMessages);
75-
76-
// The old messages should be deleted and only the new one should exist
77-
var updatedMessages = repositoryWithTxManager.findByConversationId(conversationId);
78-
assertThat(updatedMessages).hasSize(1);
79-
assertThat(updatedMessages).isEqualTo(newMessages);
80-
}
81-
82-
@SpringBootConfiguration
83-
static class TestConfiguration extends BaseTestConfiguration {
84-
85-
@Bean
86-
ChatMemoryRepository chatMemoryRepositoryWithTxManager(JdbcTemplate jdbcTemplate, DataSource dataSource) {
87-
return chatMemoryRepositoryWithTransactionManager(jdbcTemplate, dataSource);
88-
}
89-
90-
static ChatMemoryRepository chatMemoryRepositoryWithTransactionManager(JdbcTemplate jdbcTemplate,
91-
DataSource dataSource) {
92-
return JdbcChatMemoryRepository.builder()
93-
.jdbcTemplate(jdbcTemplate)
94-
.dialect(JdbcChatMemoryRepositoryDialect.from(dataSource))
95-
.transactionManager(new DataSourceTransactionManager(dataSource))
96-
.build();
97-
}
98-
99-
}
100-
10136
}

0 commit comments

Comments
 (0)