Skip to content

Commit 2a23eca

Browse files
committed
fix: get query for MSSQL Server
Signed-off-by: Xavier Chopin <[email protected]>
1 parent c0bc623 commit 2a23eca

File tree

11 files changed

+465
-35
lines changed

11 files changed

+465
-35
lines changed

auto-configurations/models/chat/memory/spring-ai-autoconfigure-model-chat-memory-jdbc/pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,13 @@
5555
<scope>test</scope>
5656
</dependency>
5757

58+
59+
<dependency>
60+
<groupId>com.microsoft.sqlserver</groupId>
61+
<artifactId>mssql-jdbc</artifactId>
62+
<scope>test</scope>
63+
</dependency>
64+
5865
<dependency>
5966
<groupId>org.testcontainers</groupId>
6067
<artifactId>junit-jupiter</artifactId>
@@ -66,6 +73,12 @@
6673
<artifactId>postgresql</artifactId>
6774
<scope>test</scope>
6875
</dependency>
76+
77+
<dependency>
78+
<groupId>org.testcontainers</groupId>
79+
<artifactId>mssqlserver</artifactId>
80+
<scope>test</scope>
81+
</dependency>
6982
</dependencies>
7083

7184
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.ai.chat.memory.jdbc.JdbcChatMemory;
21+
import org.springframework.ai.chat.messages.AssistantMessage;
22+
import org.springframework.ai.chat.messages.Message;
23+
import org.springframework.ai.chat.messages.UserMessage;
24+
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
26+
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
27+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
28+
import org.testcontainers.containers.MSSQLServerContainer;
29+
import org.testcontainers.junit.jupiter.Container;
30+
import org.testcontainers.junit.jupiter.Testcontainers;
31+
import org.testcontainers.utility.DockerImageName;
32+
33+
import java.util.List;
34+
import java.util.UUID;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
/**
39+
* @author Xavier Chopin
40+
*/
41+
@Testcontainers
42+
class JdbcChatMemoryAutoConfigurationMSSQLServerIT {
43+
44+
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-latest");
45+
46+
@Container
47+
@SuppressWarnings("resource")
48+
static MSSQLServerContainer<?> mssqlContainer = new MSSQLServerContainer<>(DEFAULT_IMAGE_NAME)
49+
.acceptLicense()
50+
.withEnv("MSSQL_DATABASE", "chat_memory_auto_configuration_test")
51+
.withPassword("Strong!NotR34LLyPassword");
52+
53+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
54+
.withConfiguration(AutoConfigurations.of(JdbcChatMemoryAutoConfiguration.class,
55+
JdbcTemplateAutoConfiguration.class, DataSourceAutoConfiguration.class))
56+
.withPropertyValues(String.format("spring.datasource.url=%s", mssqlContainer.getJdbcUrl()),
57+
String.format("spring.datasource.username=%s", mssqlContainer.getUsername()),
58+
String.format("spring.datasource.password=%s", mssqlContainer.getPassword()));
59+
60+
@Test
61+
void jdbcChatMemoryScriptDatabaseInitializer_shouldBeLoaded() {
62+
this.contextRunner.withPropertyValues("spring.ai.chat.memory.jdbc.initialize-schema=true")
63+
.run(context -> assertThat(context.containsBean("jdbcChatMemoryScriptDatabaseInitializer")).isTrue());
64+
}
65+
66+
@Test
67+
void jdbcChatMemoryScriptDatabaseInitializer_shouldNotBeLoaded() {
68+
this.contextRunner.withPropertyValues("spring.ai.chat.memory.jdbc.initialize-schema=false")
69+
.run(context -> assertThat(context.containsBean("jdbcChatMemoryScriptDatabaseInitializer")).isFalse());
70+
}
71+
72+
@Test
73+
void addGetAndClear_shouldAllExecute() {
74+
this.contextRunner.withPropertyValues("spring.ai.chat.memory.jdbc.initialize-schema=true").run(context -> {
75+
var chatMemory = context.getBean(JdbcChatMemory.class);
76+
var conversationId = UUID.randomUUID().toString();
77+
var userMessage = new UserMessage("Message from the user");
78+
79+
chatMemory.add(conversationId, userMessage);
80+
81+
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).hasSize(1);
82+
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).isEqualTo(List.of(userMessage));
83+
84+
chatMemory.clear(conversationId);
85+
86+
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).isEmpty();
87+
88+
var multipleMessages = List.<Message>of(new UserMessage("Message from the user 1"),
89+
new AssistantMessage("Message from the assistant 1"));
90+
91+
chatMemory.add(conversationId, multipleMessages);
92+
93+
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).hasSize(multipleMessages.size());
94+
assertThat(chatMemory.get(conversationId, Integer.MAX_VALUE)).isEqualTo(multipleMessages);
95+
});
96+
}
97+
98+
}
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@
1616

1717
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure;
1818

19-
import java.util.List;
20-
import java.util.UUID;
21-
2219
import org.junit.jupiter.api.Test;
23-
import org.testcontainers.containers.PostgreSQLContainer;
24-
import org.testcontainers.junit.jupiter.Container;
25-
import org.testcontainers.junit.jupiter.Testcontainers;
26-
import org.testcontainers.utility.DockerImageName;
27-
2820
import org.springframework.ai.chat.memory.jdbc.JdbcChatMemory;
2921
import org.springframework.ai.chat.messages.AssistantMessage;
3022
import org.springframework.ai.chat.messages.Message;
@@ -33,14 +25,21 @@
3325
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
3426
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
3527
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
28+
import org.testcontainers.containers.PostgreSQLContainer;
29+
import org.testcontainers.junit.jupiter.Container;
30+
import org.testcontainers.junit.jupiter.Testcontainers;
31+
import org.testcontainers.utility.DockerImageName;
32+
33+
import java.util.List;
34+
import java.util.UUID;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
3837

3938
/**
4039
* @author Jonathan Leijendekker
4140
*/
4241
@Testcontainers
43-
class JdbcChatMemoryAutoConfigurationIT {
42+
class JdbcChatMemoryAutoConfigurationPostgreSQLIT {
4443

4544
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("postgres:17");
4645

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2024-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.springframework.boot.autoconfigure.AutoConfigurations;
21+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.testcontainers.containers.MSSQLServerContainer;
25+
import org.testcontainers.junit.jupiter.Container;
26+
import org.testcontainers.junit.jupiter.Testcontainers;
27+
import org.testcontainers.utility.DockerImageName;
28+
29+
import javax.sql.DataSource;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* @author Xavier Chopin
35+
*/
36+
@Testcontainers
37+
class JdbcChatMemoryDataSourceScriptDatabaseMSSQLServerIT {
38+
39+
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("mcr.microsoft.com/mssql/server:2022-latest");
40+
41+
@Container
42+
@SuppressWarnings("resource")
43+
static MSSQLServerContainer<?> mssqlContainer = new MSSQLServerContainer<>(DEFAULT_IMAGE_NAME)
44+
.acceptLicense()
45+
.withEnv("MSSQL_DATABASE", "chat_memory_test")
46+
.withPassword("Strong!NotR34LLyPassword");
47+
48+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
49+
.withConfiguration(AutoConfigurations.of(JdbcChatMemoryAutoConfiguration.class,
50+
JdbcTemplateAutoConfiguration.class, DataSourceAutoConfiguration.class))
51+
.withPropertyValues(String.format("spring.datasource.url=%s", mssqlContainer.getJdbcUrl()),
52+
String.format("spring.datasource.username=%s", mssqlContainer.getUsername()),
53+
String.format("spring.datasource.password=%s", mssqlContainer.getPassword()));
54+
55+
@Test
56+
void getSettings_shouldHaveSchemaLocations() {
57+
this.contextRunner.run(context -> {
58+
var dataSource = context.getBean(DataSource.class);
59+
var settings = JdbcChatMemoryDataSourceScriptDatabaseInitializer.getSettings(dataSource);
60+
61+
assertThat(settings.getSchemaLocations())
62+
.containsOnly("classpath:org/springframework/ai/chat/memory/jdbc/schema-mssql.sql");
63+
});
64+
}
65+
66+
}
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,25 @@
1616

1717
package org.springframework.ai.model.chat.memory.jdbc.autoconfigure;
1818

19-
import javax.sql.DataSource;
20-
2119
import org.junit.jupiter.api.Test;
20+
import org.springframework.boot.autoconfigure.AutoConfigurations;
21+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
22+
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
23+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2224
import org.testcontainers.containers.PostgreSQLContainer;
2325
import org.testcontainers.junit.jupiter.Container;
2426
import org.testcontainers.junit.jupiter.Testcontainers;
2527
import org.testcontainers.utility.DockerImageName;
2628

27-
import org.springframework.boot.autoconfigure.AutoConfigurations;
28-
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
29-
import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration;
30-
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
29+
import javax.sql.DataSource;
3130

3231
import static org.assertj.core.api.Assertions.assertThat;
3332

3433
/**
3534
* @author Jonathan Leijendekker
3635
*/
3736
@Testcontainers
38-
class JdbcChatMemoryDataSourceScriptDatabaseInitializerTests {
37+
class JdbcChatMemoryDataSourceScriptDatabasePostgreSQLIT {
3938

4039
static final DockerImageName DEFAULT_IMAGE_NAME = DockerImageName.parse("postgres:17");
4140

memory/spring-ai-model-chat-memory-jdbc/pom.xml

+14-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
</dependency>
4747

4848
<dependency>
49-
<groupId>org.springframework</groupId>
50-
<artifactId>spring-jdbc</artifactId>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-data-jdbc</artifactId>
5151
</dependency>
5252

5353
<dependency>
@@ -69,6 +69,12 @@
6969
<optional>true</optional>
7070
</dependency>
7171

72+
<dependency>
73+
<groupId>com.microsoft.sqlserver</groupId>
74+
<artifactId>mssql-jdbc</artifactId>
75+
<optional>true</optional>
76+
</dependency>
77+
7278
<!-- TESTING -->
7379
<dependency>
7480
<groupId>org.springframework.boot</groupId>
@@ -82,6 +88,12 @@
8288
<scope>test</scope>
8389
</dependency>
8490

91+
<dependency>
92+
<groupId>org.testcontainers</groupId>
93+
<artifactId>mssqlserver</artifactId>
94+
<scope>test</scope>
95+
</dependency>
96+
8597
<dependency>
8698
<groupId>org.testcontainers</groupId>
8799
<artifactId>postgresql</artifactId>

0 commit comments

Comments
 (0)