16
16
17
17
package org .springframework .ai .chat .memory .jdbc ;
18
18
19
- import java .sql .Timestamp ;
20
- import java .util .List ;
21
- import java .util .UUID ;
22
-
23
- import javax .sql .DataSource ;
24
-
25
19
import org .junit .jupiter .api .BeforeAll ;
26
20
import org .junit .jupiter .api .Test ;
27
21
import org .junit .jupiter .params .ParameterizedTest ;
28
22
import org .junit .jupiter .params .provider .CsvSource ;
29
- import org .testcontainers .containers .PostgreSQLContainer ;
30
- import org .testcontainers .junit .jupiter .Container ;
31
- import org .testcontainers .junit .jupiter .Testcontainers ;
32
- import org .testcontainers .utility .MountableFile ;
33
-
34
23
import org .springframework .ai .chat .memory .ChatMemory ;
35
- import org .springframework .ai .chat .messages .AssistantMessage ;
36
- import org .springframework .ai .chat .messages .Message ;
37
- import org .springframework .ai .chat .messages .MessageType ;
38
- import org .springframework .ai .chat .messages .SystemMessage ;
39
- import org .springframework .ai .chat .messages .UserMessage ;
24
+ import org .springframework .ai .chat .messages .*;
40
25
import org .springframework .boot .SpringBootConfiguration ;
41
26
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
42
27
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
46
31
import org .springframework .context .annotation .Bean ;
47
32
import org .springframework .context .annotation .Primary ;
48
33
import org .springframework .jdbc .core .JdbcTemplate ;
34
+ import org .testcontainers .containers .PostgreSQLContainer ;
35
+ import org .testcontainers .junit .jupiter .Container ;
36
+ import org .testcontainers .junit .jupiter .Testcontainers ;
37
+ import org .testcontainers .utility .MountableFile ;
38
+
39
+ import javax .sql .DataSource ;
40
+ import java .sql .Timestamp ;
41
+ import java .util .List ;
42
+ import java .util .UUID ;
49
43
50
44
import static org .assertj .core .api .Assertions .assertThat ;
51
45
@@ -147,10 +141,11 @@ void get_shouldReturnMessages() {
147
141
this .contextRunner .run (context -> {
148
142
var chatMemory = context .getBean (ChatMemory .class );
149
143
var conversationId = UUID .randomUUID ().toString ();
150
- var messages = List .<Message >of (new AssistantMessage ("Message from assistant 1 - " + conversationId ),
151
- new AssistantMessage ("Message from assistant 2 - " + conversationId ),
152
- new UserMessage ("Message from user - " + conversationId ),
153
- new SystemMessage ("Message from system - " + conversationId ));
144
+ var messages = List .<Message >of (new SystemMessage ("Message from system - " + conversationId ),
145
+ new UserMessage ("Message from user 1 - " + conversationId ),
146
+ new AssistantMessage ("Message from assistant 1 - " + conversationId ),
147
+ new UserMessage ("Message from user 2 - " + conversationId ),
148
+ new AssistantMessage ("Message from assistant 2 - " + conversationId ));
154
149
155
150
chatMemory .add (conversationId , messages );
156
151
@@ -161,6 +156,24 @@ void get_shouldReturnMessages() {
161
156
});
162
157
}
163
158
159
+ @ Test
160
+ void get_afterMultipleAdds_shouldReturnMessagesInSameOrder () {
161
+ this .contextRunner .run (context -> {
162
+ var chatMemory = context .getBean (ChatMemory .class );
163
+ var conversationId = UUID .randomUUID ().toString ();
164
+ var userMessage = new UserMessage ("Message from user - " + conversationId );
165
+ var assistantMessage = new AssistantMessage ("Message from assistant - " + conversationId );
166
+
167
+ chatMemory .add (conversationId , userMessage );
168
+ chatMemory .add (conversationId , assistantMessage );
169
+
170
+ var results = chatMemory .get (conversationId , Integer .MAX_VALUE );
171
+
172
+ assertThat (results .size ()).isEqualTo (2 );
173
+ assertThat (results ).isEqualTo (List .of (userMessage , assistantMessage ));
174
+ });
175
+ }
176
+
164
177
@ Test
165
178
void clear_shouldDeleteMessages () {
166
179
this .contextRunner .run (context -> {
0 commit comments