Skip to content

Commit 15a43c4

Browse files
committed
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 b219c21 commit 15a43c4

File tree

3 files changed

+13
-93
lines changed

3 files changed

+13
-93
lines changed

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

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,12 @@
2929
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
3030
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3131
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
32-
import org.springframework.boot.test.context.SpringBootTest;
3332
import org.springframework.context.annotation.Bean;
3433
import org.springframework.jdbc.core.JdbcTemplate;
34+
import org.springframework.test.context.ContextConfiguration;
3535

3636
import java.sql.Timestamp;
3737
import java.util.List;
38-
import java.util.Map;
3938
import java.util.UUID;
4039
import java.util.stream.Collectors;
4140

@@ -47,20 +46,17 @@
4746
* Base class for integration tests for {@link JdbcChatMemoryRepository}.
4847
*
4948
* @author Mark Pollack
49+
* @author Yanming Zhou
5050
*/
51+
@ContextConfiguration(classes = AbstractJdbcChatMemoryRepositoryIT.TestConfiguration.class)
5152
public abstract class AbstractJdbcChatMemoryRepositoryIT {
5253

5354
@Autowired
54-
protected ChatMemoryRepository chatMemoryRepository;
55+
protected JdbcChatMemoryRepository chatMemoryRepository;
5556

5657
@Autowired
5758
protected JdbcTemplate jdbcTemplate;
5859

59-
@Test
60-
void correctChatMemoryRepositoryInstance() {
61-
assertThat(chatMemoryRepository).isInstanceOf(ChatMemoryRepository.class);
62-
}
63-
6460
@ParameterizedTest
6561
@CsvSource({ "Message from assistant,ASSISTANT", "Message from user,USER", "Message from system,SYSTEM" })
6662
void saveMessagesSingleMessage(String content, MessageType messageType) {
@@ -158,11 +154,6 @@ void deleteMessagesByConversationId() {
158154

159155
@Test
160156
void testMessageOrder() {
161-
// Create a repository using the from method to detect the dialect
162-
JdbcChatMemoryRepository repository = JdbcChatMemoryRepository.builder()
163-
.jdbcTemplate(jdbcTemplate)
164-
.dialect(JdbcChatMemoryRepositoryDialect.from(jdbcTemplate.getDataSource()))
165-
.build();
166157

167158
var conversationId = UUID.randomUUID().toString();
168159

@@ -174,10 +165,10 @@ void testMessageOrder() {
174165

175166
// Save messages in the expected order
176167
List<Message> orderedMessages = List.of(firstMessage, secondMessage, thirdMessage, fourthMessage);
177-
repository.saveAll(conversationId, orderedMessages);
168+
chatMemoryRepository.saveAll(conversationId, orderedMessages);
178169

179170
// Retrieve messages using the repository
180-
List<Message> retrievedMessages = repository.findByConversationId(conversationId);
171+
List<Message> retrievedMessages = chatMemoryRepository.findByConversationId(conversationId);
181172
assertThat(retrievedMessages).hasSize(4);
182173

183174
// Get the actual order from the retrieved messages
@@ -192,14 +183,11 @@ void testMessageOrder() {
192183
* Base configuration for all integration tests.
193184
*/
194185
@ImportAutoConfiguration({ DataSourceAutoConfiguration.class, JdbcTemplateAutoConfiguration.class })
195-
static abstract class BaseTestConfiguration {
186+
static class TestConfiguration {
196187

197188
@Bean
198-
ChatMemoryRepository chatMemoryRepository(JdbcTemplate jdbcTemplate, DataSource dataSource) {
199-
return JdbcChatMemoryRepository.builder()
200-
.jdbcTemplate(jdbcTemplate)
201-
.dialect(JdbcChatMemoryRepositoryDialect.from(dataSource))
202-
.build();
189+
ChatMemoryRepository chatMemoryRepository(DataSource dataSource) {
190+
return JdbcChatMemoryRepository.builder().dataSource(dataSource).build();
203191
}
204192

205193
}

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

Lines changed: 2 additions & 7 deletions
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,15 +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)
31+
@SpringBootTest
3232
@TestPropertySource(properties = { "spring.datasource.url=jdbc:tc:mariadb:10.3.39:///" })
3333
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-mariadb.sql")
3434
class JdbcChatMemoryRepositoryMysqlIT extends AbstractJdbcChatMemoryRepositoryIT {
3535

36-
@SpringBootConfiguration
37-
static class TestConfiguration extends BaseTestConfiguration {
38-
39-
}
40-
4136
}

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

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -16,84 +16,21 @@
1616

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

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

38-
import static org.assertj.core.api.Assertions.assertThat;
39-
4023
/**
4124
* Integration tests for {@link JdbcChatMemoryRepository} with PostgreSQL.
4225
*
4326
* @author Jonathan Leijendekker
4427
* @author Thomas Vitale
4528
* @author Mark Pollack
29+
* @author Yanming Zhou
4630
*/
47-
@SpringBootTest(classes = JdbcChatMemoryRepositoryPostgresqlIT.TestConfiguration.class)
31+
@SpringBootTest
4832
@TestPropertySource(properties = "spring.datasource.url=jdbc:tc:postgresql:17:///")
4933
@Sql(scripts = "classpath:org/springframework/ai/chat/memory/repository/jdbc/schema-postgresql.sql")
5034
class JdbcChatMemoryRepositoryPostgresqlIT extends AbstractJdbcChatMemoryRepositoryIT {
5135

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

0 commit comments

Comments
 (0)