44#include < pqxx/pqxx>
55
66void setup (pqxx::connection &conn) {
7- pqxx::work tx (conn);
7+ pqxx::nontransaction tx (conn);
88 tx.exec (" CREATE EXTENSION IF NOT EXISTS vector" );
99 tx.exec (" DROP TABLE IF EXISTS items" );
1010 tx.exec (" CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(3))" );
11- tx.commit ();
1211}
1312
1413void before_each (pqxx::connection &conn) {
15- pqxx::work tx (conn);
14+ pqxx::nontransaction tx (conn);
1615 tx.exec (" TRUNCATE items" );
17- tx.commit ();
1816}
1917
2018void test_vector (pqxx::connection &conn) {
2119 before_each (conn);
2220
23- pqxx::work tx (conn);
21+ pqxx::nontransaction tx (conn);
2422 auto embedding = pgvector::Vector ({1 , 2 , 3 });
2523 assert (embedding.dimensions () == 3 );
2624 float arr[] = {4 , 5 , 6 };
@@ -34,13 +32,12 @@ void test_vector(pqxx::connection &conn) {
3432 assert (res[0 ][0 ].as <pgvector::Vector>() == embedding2);
3533 assert (res[1 ][0 ].as <pgvector::Vector>() == embedding);
3634 assert (!res[2 ][0 ].as <std::optional<pgvector::Vector>>().has_value ());
37- tx.commit ();
3835}
3936
4037void test_halfvec (pqxx::connection &conn) {
4138 before_each (conn);
4239
43- pqxx::work tx (conn);
40+ pqxx::nontransaction tx (conn);
4441 auto embedding = pgvector::HalfVector ({1 , 2 , 3 });
4542 assert (embedding.dimensions () == 3 );
4643 float arr[] = {4 , 5 , 6 };
@@ -54,13 +51,12 @@ void test_halfvec(pqxx::connection &conn) {
5451 assert (res[0 ][0 ].as <pgvector::HalfVector>() == embedding2);
5552 assert (res[1 ][0 ].as <pgvector::HalfVector>() == embedding);
5653 assert (!res[2 ][0 ].as <std::optional<pgvector::HalfVector>>().has_value ());
57- tx.commit ();
5854}
5955
6056void test_bit (pqxx::connection &conn) {
6157 before_each (conn);
6258
63- pqxx::work tx (conn);
59+ pqxx::nontransaction tx (conn);
6460 auto embedding = " 101" ;
6561 auto embedding2 = " 111" ;
6662 tx.exec (" INSERT INTO items (binary_embedding) VALUES ($1), ($2), ($3)" ,
@@ -77,7 +73,7 @@ void test_bit(pqxx::connection &conn) {
7773void test_sparsevec (pqxx::connection &conn) {
7874 before_each (conn);
7975
80- pqxx::work tx (conn);
76+ pqxx::nontransaction tx (conn);
8177 auto embedding = pgvector::SparseVector ({1 , 2 , 3 });
8278 auto embedding2 = pgvector::SparseVector ({4 , 5 , 6 });
8379 tx.exec (" INSERT INTO items (sparse_embedding) VALUES ($1), ($2), ($3)" ,
@@ -89,13 +85,12 @@ void test_sparsevec(pqxx::connection &conn) {
8985 assert (res[0 ][0 ].as <std::string>() == " {1:4,2:5,3:6}/3" );
9086 assert (res[1 ][0 ].as <std::string>() == " {1:1,2:2,3:3}/3" );
9187 assert (!res[2 ][0 ].as <std::optional<std::string>>().has_value ());
92- tx.commit ();
9388}
9489
9590void test_sparsevec_nnz (pqxx::connection &conn) {
9691 before_each (conn);
9792
98- pqxx::work tx (conn);
93+ pqxx::nontransaction tx (conn);
9994 std::vector<float > vec (16001 , 1 );
10095 auto embedding = pgvector::SparseVector (vec);
10196 try {
@@ -104,13 +99,12 @@ void test_sparsevec_nnz(pqxx::connection &conn) {
10499 } catch (const pqxx::conversion_overrun& e) {
105100 assert (std::strcmp (e.what (), " sparsevec cannot have more than 16000 dimensions" ) == 0 );
106101 }
107- tx.commit ();
108102}
109103
110104void test_stream (pqxx::connection &conn) {
111105 before_each (conn);
112106
113- pqxx::work tx (conn);
107+ pqxx::nontransaction tx (conn);
114108 auto embedding = pgvector::Vector ({1 , 2 , 3 });
115109 tx.exec (" INSERT INTO items (embedding) VALUES ($1)" , {embedding});
116110 int count = 0 ;
@@ -119,33 +113,30 @@ void test_stream(pqxx::connection &conn) {
119113 count++;
120114 }
121115 assert (count == 1 );
122- tx.commit ();
123116}
124117
125118void test_stream_to (pqxx::connection &conn) {
126119 before_each (conn);
127120
128- pqxx::work tx (conn);
121+ pqxx::nontransaction tx (conn);
129122 auto stream = pqxx::stream_to::table (tx, {" items" }, {" embedding" });
130123 stream << pgvector::Vector ({1 , 2 , 3 });
131124 stream << pgvector::Vector ({4 , 5 , 6 });
132125 stream.complete ();
133126 pqxx::result res = tx.exec (" SELECT embedding FROM items ORDER BY id" );
134127 assert (res[0 ][0 ].as <std::string>() == " [1,2,3]" );
135128 assert (res[1 ][0 ].as <std::string>() == " [4,5,6]" );
136- tx.commit ();
137129}
138130
139131void test_precision (pqxx::connection &conn) {
140132 before_each (conn);
141133
142- pqxx::work tx (conn);
134+ pqxx::nontransaction tx (conn);
143135 auto embedding = pgvector::Vector ({1.23456789 , 0 , 0 });
144136 tx.exec (" INSERT INTO items (embedding) VALUES ($1)" , {embedding});
145137 tx.exec (" SET extra_float_digits = 3" );
146138 pqxx::result res = tx.exec (" SELECT embedding FROM items ORDER BY id DESC LIMIT 1" );
147139 assert (res[0 ][0 ].as <pgvector::Vector>() == embedding);
148- tx.commit ();
149140}
150141
151142int main () {
0 commit comments