-
Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(JdbcChatMemory): get query for MSSQL Server #2806
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2024-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.ai.chat.memory.jdbc.JdbcChatMemory; | ||
import org.springframework.ai.chat.messages.AssistantMessage; | ||
import org.springframework.ai.chat.messages.Message; | ||
import org.springframework.ai.chat.messages.UserMessage; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.testcontainers.containers.MSSQLServerContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Xavier Chopin | ||
*/ | ||
@Testcontainers | ||
class JdbcChatMemoryAutoConfigurationMSSQLServerIT { | ||
|
||
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-latest"); | ||
|
||
@Container | ||
@SuppressWarnings("resource") | ||
static MSSQLServerContainer<?> mssqlContainer = new MSSQLServerContainer<>(DEFAULT_IMAGE_NAME) | ||
.acceptLicense() | ||
.withEnv("MSSQL_DATABASE", "chat_memory_auto_configuration_test") | ||
.withPassword("Strong!NotR34LLyPassword"); | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(JdbcChatMemoryAutoConfiguration.class, | ||
JdbcTemplateAutoConfiguration.class, DataSourceAutoConfiguration.class)) | ||
.withPropertyValues(String.format("spring.datasource.url=%s", mssqlContainer.getJdbcUrl()), | ||
String.format("spring.datasource.username=%s", mssqlContainer.getUsername()), | ||
String.format("spring.datasource.password=%s", mssqlContainer.getPassword())); | ||
|
||
@Test | ||
void jdbcChatMemoryScriptDatabaseInitializer_shouldBeLoaded() { | ||
this.contextRunner.withPropertyValues("spring.ai.chat.memory.jdbc.initialize-schema=true") | ||
.run(context -> assertThat(context.containsBean("jdbcChatMemoryScriptDatabaseInitializer")).isTrue()); | ||
} | ||
|
||
@Test | ||
void jdbcChatMemoryScriptDatabaseInitializer_shouldNotBeLoaded() { | ||
this.contextRunner.withPropertyValues("spring.ai.chat.memory.jdbc.initialize-schema=false") | ||
.run(context -> assertThat(context.containsBean("jdbcChatMemoryScriptDatabaseInitializer")).isFalse()); | ||
} | ||
|
||
@Test | ||
void addGetAndClear_shouldAllExecute() { | ||
this.contextRunner.withPropertyValues("spring.ai.chat.memory.jdbc.initialize-schema=true").run(context -> { | ||
var chatMemory = context.getBean(JdbcChatMemory.class); | ||
var conversationId = UUID.randomUUID().toString(); | ||
var userMessage = new UserMessage("Message from the user"); | ||
|
||
chatMemory.add(conversationId, userMessage); | ||
|
||
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).hasSize(1); | ||
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).isEqualTo(List.of(userMessage)); | ||
|
||
chatMemory.clear(conversationId); | ||
|
||
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).isEmpty(); | ||
|
||
var multipleMessages = List.<Message>of(new UserMessage("Message from the user 1"), | ||
new AssistantMessage("Message from the assistant 1")); | ||
|
||
chatMemory.add(conversationId, multipleMessages); | ||
|
||
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).hasSize(multipleMessages.size()); | ||
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).isEqualTo(multipleMessages); | ||
}); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
* Copyright 2024-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.testcontainers.containers.MSSQLServerContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Xavier Chopin | ||
*/ | ||
@Testcontainers | ||
class JdbcChatMemoryDataSourceScriptDatabaseMSSQLServerIT { | ||
|
||
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-latest"); | ||
|
||
@Container | ||
@SuppressWarnings("resource") | ||
static MSSQLServerContainer<?> mssqlContainer = new MSSQLServerContainer<>(DEFAULT_IMAGE_NAME) | ||
.acceptLicense() | ||
.withEnv("MSSQL_DATABASE", "chat_memory_test") | ||
.withPassword("Strong!NotR34LLyPassword"); | ||
|
||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(JdbcChatMemoryAutoConfiguration.class, | ||
JdbcTemplateAutoConfiguration.class, DataSourceAutoConfiguration.class)) | ||
.withPropertyValues(String.format("spring.datasource.url=%s", mssqlContainer.getJdbcUrl()), | ||
String.format("spring.datasource.username=%s", mssqlContainer.getUsername()), | ||
String.format("spring.datasource.password=%s", mssqlContainer.getPassword())); | ||
|
||
@Test | ||
void getSettings_shouldHaveSchemaLocations() { | ||
this.contextRunner.run(context -> { | ||
var dataSource = context.getBean(DataSource.class); | ||
var settings = JdbcChatMemoryDataSourceScriptDatabaseInitializer.getSettings(dataSource); | ||
|
||
assertThat(settings.getSchemaLocations()) | ||
.containsOnly("classpath:org/springframework/ai/chat/memory/jdbc/schema-sqlserver.sql"); | ||
}); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,26 +16,25 @@ | |
|
||
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure; | ||
|
||
import javax.sql.DataSource; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import javax.sql.DataSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* @author Jonathan Leijendekker | ||
*/ | ||
@Testcontainers | ||
class JdbcChatMemoryDataSourceScriptDatabaseInitializerTests { | ||
class JdbcChatMemoryDataSourceScriptDatabasePostgreSQLIT { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be preferable to name this |
||
|
||
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("postgres:17"); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -46,8 +46,8 @@ | |
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-jdbc</artifactId> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jdbc</artifactId> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will have to change org.springframework.boot.jdbc.DatabaseDriver then There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xchopin btw, it is worth considering There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leijendary is it still worth it to move to spring-data-jdbc with this incoming PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @xchopin Yes it is still worth it. The implementation in that PR is the same as the current implementation wherein it uses driver specific syntax. We should probably wait for that PR to be merged though as it is a conflict. |
||
</dependency> | ||
|
||
<dependency> | ||
|
@@ -69,6 +69,12 @@ | |
<optional>true</optional> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.microsoft.sqlserver</groupId> | ||
<artifactId>mssql-jdbc</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- TESTING --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
|
@@ -82,6 +88,12 @@ | |
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>mssqlserver</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.testcontainers</groupId> | ||
<artifactId>postgresql</artifactId> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be preferable to name it
JdbcChatMemoryAutoConfigurationPostgresIT