1
1
package com .microsoft .semantickernel .tests .connectors .memory .jdbc ;
2
2
3
+ import com .microsoft .semantickernel .connectors .data .hsqldb .HSQLDBVectorStoreQueryProvider ;
3
4
import com .microsoft .semantickernel .connectors .data .jdbc .JDBCVectorStore ;
4
5
import com .microsoft .semantickernel .connectors .data .jdbc .JDBCVectorStoreOptions ;
5
6
import com .microsoft .semantickernel .connectors .data .jdbc .SQLVectorStoreQueryProvider ;
8
9
import com .microsoft .semantickernel .connectors .data .postgres .PostgreSQLVectorStoreQueryProvider ;
9
10
import com .microsoft .semantickernel .connectors .data .sqlite .SQLiteVectorStoreQueryProvider ;
10
11
import com .mysql .cj .jdbc .MysqlDataSource ;
12
+ import org .hsqldb .jdbc .JDBCDataSourceFactory ;
11
13
import org .junit .jupiter .params .ParameterizedTest ;
12
14
import org .junit .jupiter .params .provider .EnumSource ;
13
15
import org .postgresql .ds .PGSimpleDataSource ;
19
21
import org .testcontainers .utility .DockerImageName ;
20
22
21
23
import javax .sql .DataSource ;
24
+ import java .nio .file .Files ;
25
+ import java .nio .file .Path ;
22
26
import java .util .Arrays ;
23
27
import java .util .List ;
28
+ import java .util .Map ;
29
+ import java .util .Properties ;
24
30
25
31
import com .microsoft .semantickernel .tests .connectors .memory .jdbc .JDBCVectorStoreRecordCollectionTest .QueryProvider ;
32
+
26
33
import static org .junit .jupiter .api .Assertions .assertEquals ;
27
34
import static org .junit .jupiter .api .Assertions .assertNotNull ;
28
35
import static org .junit .jupiter .api .Assertions .assertTrue ;
29
36
30
37
@ Testcontainers
31
38
public class JDBCVectorStoreTest {
39
+
32
40
@ Container
33
41
private static final MySQLContainer <?> MYSQL_CONTAINER = new MySQLContainer <>("mysql:5.7.34" );
34
42
35
- private static final DockerImageName PGVECTOR = DockerImageName .parse ("pgvector/pgvector:pg16" ).asCompatibleSubstituteFor ("postgres" );
43
+ private static final DockerImageName PGVECTOR = DockerImageName .parse ("pgvector/pgvector:pg16" )
44
+ .asCompatibleSubstituteFor ("postgres" );
36
45
@ Container
37
- private static final PostgreSQLContainer <?> POSTGRESQL_CONTAINER = new PostgreSQLContainer <>(PGVECTOR );
46
+ private static final PostgreSQLContainer <?> POSTGRESQL_CONTAINER = new PostgreSQLContainer <>(
47
+ PGVECTOR );
38
48
39
49
private JDBCVectorStore buildVectorStore (QueryProvider provider ) {
40
50
SQLVectorStoreQueryProvider queryProvider ;
@@ -48,8 +58,8 @@ private JDBCVectorStore buildVectorStore(QueryProvider provider) {
48
58
mysqlDataSource .setPassword (MYSQL_CONTAINER .getPassword ());
49
59
dataSource = mysqlDataSource ;
50
60
queryProvider = MySQLVectorStoreQueryProvider .builder ()
51
- .withDataSource (dataSource )
52
- .build ();
61
+ .withDataSource (dataSource )
62
+ .build ();
53
63
break ;
54
64
case PostgreSQL :
55
65
PGSimpleDataSource pgSimpleDataSource = new PGSimpleDataSource ();
@@ -58,30 +68,53 @@ private JDBCVectorStore buildVectorStore(QueryProvider provider) {
58
68
pgSimpleDataSource .setPassword (POSTGRESQL_CONTAINER .getPassword ());
59
69
dataSource = pgSimpleDataSource ;
60
70
queryProvider = PostgreSQLVectorStoreQueryProvider .builder ()
61
- .withDataSource (dataSource )
62
- .build ();
71
+ .withDataSource (dataSource )
72
+ .build ();
63
73
break ;
64
74
case SQLite :
65
75
SQLiteDataSource sqliteDataSource = new SQLiteDataSource ();
66
76
sqliteDataSource .setUrl ("jdbc:sqlite:file:test" );
67
77
dataSource = sqliteDataSource ;
68
78
queryProvider = SQLiteVectorStoreQueryProvider .builder ()
69
- .withDataSource (sqliteDataSource )
70
- .build ();
79
+ .withDataSource (sqliteDataSource )
80
+ .build ();
81
+ break ;
82
+ case HSQLDB :
83
+ try {
84
+ Path file = Files .createTempFile ("testdb" , ".db" );
85
+ file .toFile ().deleteOnExit ();
86
+
87
+ Properties properties = new Properties ();
88
+ properties .putAll (
89
+ Map .of (
90
+ "url" , "jdbc:hsqldb:file:" + file .toFile ().getAbsolutePath ()
91
+ + ";sql.syntax_mys=true" ,
92
+ "user" , "SA" ,
93
+ "password" , ""
94
+ )
95
+ );
96
+
97
+ dataSource = JDBCDataSourceFactory .createDataSource (properties );
98
+ } catch (Exception e ) {
99
+ throw new RuntimeException (e );
100
+ }
101
+
102
+ queryProvider = HSQLDBVectorStoreQueryProvider .builder ()
103
+ .withDataSource (dataSource )
104
+ .build ();
71
105
break ;
72
106
default :
73
107
throw new IllegalArgumentException ("Unknown query provider: " + provider );
74
108
}
75
109
76
-
77
110
JDBCVectorStore vectorStore = JDBCVectorStore .builder ()
78
- .withDataSource (dataSource )
79
- .withOptions (
80
- JDBCVectorStoreOptions .builder ()
81
- .withQueryProvider (queryProvider )
82
- .build ()
83
- )
84
- .build ();
111
+ .withDataSource (dataSource )
112
+ .withOptions (
113
+ JDBCVectorStoreOptions .builder ()
114
+ .withQueryProvider (queryProvider )
115
+ .build ()
116
+ )
117
+ .build ();
85
118
86
119
vectorStore .prepareAsync ().block ();
87
120
return vectorStore ;
@@ -99,7 +132,7 @@ public void getCollectionNamesAsync(QueryProvider provider) {
99
132
100
133
for (String collectionName : collectionNames ) {
101
134
vectorStore .getCollection (collectionName ,
102
- JDBCVectorStoreRecordCollectionOptions .<Hotel >builder ()
135
+ JDBCVectorStoreRecordCollectionOptions .<Hotel >builder ()
103
136
.withRecordClass (Hotel .class )
104
137
.build ()).createCollectionAsync ().block ();
105
138
}
0 commit comments