Skip to content

Commit 255cda1

Browse files
committed
Improved tests for from_string
1 parent 847c4ba commit 255cda1

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

test/pqxx_test.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,29 @@ void test_vector_to_string() {
141141

142142
void test_vector_from_string() {
143143
assert(pqxx::from_string<pgvector::Vector>("[1,2,3]") == pgvector::Vector({1, 2, 3}));
144+
145+
try {
146+
auto unused = pqxx::from_string<pgvector::Vector>("");
147+
assert(false);
148+
} catch (const pqxx::conversion_error& e) {
149+
assert(std::string_view(e.what()) == "Malformed vector literal");
150+
}
151+
152+
// TODO change to pqxx::conversion_error
153+
try {
154+
auto unused = pqxx::from_string<pgvector::Vector>("[hello]");
155+
assert(false);
156+
} catch (const std::invalid_argument& e) {
157+
assert(true);
158+
}
159+
160+
// TODO change to pqxx::conversion_error
161+
try {
162+
auto unused = pqxx::from_string<pgvector::Vector>("[4e38]");
163+
assert(false);
164+
} catch (const std::out_of_range& e) {
165+
assert(true);
166+
}
144167
}
145168

146169
void test_halfvec_to_string() {
@@ -149,6 +172,21 @@ void test_halfvec_to_string() {
149172

150173
void test_halfvec_from_string() {
151174
assert(pqxx::from_string<pgvector::HalfVector>("[1,2,3]") == pgvector::HalfVector({1, 2, 3}));
175+
176+
try {
177+
auto unused = pqxx::from_string<pgvector::HalfVector>("");
178+
assert(false);
179+
} catch (const pqxx::conversion_error& e) {
180+
assert(std::string_view(e.what()) == "Malformed halfvec literal");
181+
}
182+
183+
// TODO change to pqxx::conversion_error
184+
try {
185+
auto unused = pqxx::from_string<pgvector::HalfVector>("[hello]");
186+
assert(false);
187+
} catch (const std::invalid_argument& e) {
188+
assert(true);
189+
}
152190
}
153191

154192
void test_sparsevec_to_string() {

0 commit comments

Comments
 (0)