ConnectionStringTest.vala 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /**
  2. * ConnectionStringTest - Unit tests for ConnectionString
  3. */
  4. using Implexus.Core;
  5. using Implexus.Engine;
  6. public static int main(string[] args) {
  7. int passed = 0;
  8. int failed = 0;
  9. // Test 1: Embedded LMDB short form
  10. if (test_embedded_lmdb_short()) {
  11. passed++;
  12. stdout.puts("PASS: test_embedded_lmdb_short\n");
  13. } else {
  14. failed++;
  15. stdout.puts("FAIL: test_embedded_lmdb_short\n");
  16. }
  17. // Test 2: Embedded GDBM short form
  18. if (test_embedded_gdbm_short()) {
  19. passed++;
  20. stdout.puts("PASS: test_embedded_gdbm_short\n");
  21. } else {
  22. failed++;
  23. stdout.puts("FAIL: test_embedded_gdbm_short\n");
  24. }
  25. // Test 3: Embedded filesystem short form
  26. if (test_embedded_filesystem_short()) {
  27. passed++;
  28. stdout.puts("PASS: test_embedded_filesystem_short\n");
  29. } else {
  30. failed++;
  31. stdout.puts("FAIL: test_embedded_filesystem_short\n");
  32. }
  33. // Test 4: Embedded full form
  34. if (test_embedded_full_form()) {
  35. passed++;
  36. stdout.puts("PASS: test_embedded_full_form\n");
  37. } else {
  38. failed++;
  39. stdout.puts("FAIL: test_embedded_full_form\n");
  40. }
  41. // Test 5: Remote default port
  42. if (test_remote_default_port()) {
  43. passed++;
  44. stdout.puts("PASS: test_remote_default_port\n");
  45. } else {
  46. failed++;
  47. stdout.puts("FAIL: test_remote_default_port\n");
  48. }
  49. // Test 6: Remote custom port
  50. if (test_remote_custom_port()) {
  51. passed++;
  52. stdout.puts("PASS: test_remote_custom_port\n");
  53. } else {
  54. failed++;
  55. stdout.puts("FAIL: test_remote_custom_port\n");
  56. }
  57. // Test 7: Remote with timeout
  58. if (test_remote_with_timeout()) {
  59. passed++;
  60. stdout.puts("PASS: test_remote_with_timeout\n");
  61. } else {
  62. failed++;
  63. stdout.puts("FAIL: test_remote_with_timeout\n");
  64. }
  65. // Test 8: Remote IP address
  66. if (test_remote_ip_address()) {
  67. passed++;
  68. stdout.puts("PASS: test_remote_ip_address\n");
  69. } else {
  70. failed++;
  71. stdout.puts("FAIL: test_remote_ip_address\n");
  72. }
  73. // Test 9: Try parse success
  74. if (test_try_parse_success()) {
  75. passed++;
  76. stdout.puts("PASS: test_try_parse_success\n");
  77. } else {
  78. failed++;
  79. stdout.puts("FAIL: test_try_parse_success\n");
  80. }
  81. // Test 10: Try parse failure
  82. if (test_try_parse_failure()) {
  83. passed++;
  84. stdout.puts("PASS: test_try_parse_failure\n");
  85. } else {
  86. failed++;
  87. stdout.puts("FAIL: test_try_parse_failure\n");
  88. }
  89. // Test 11: To string embedded
  90. if (test_to_string_embedded()) {
  91. passed++;
  92. stdout.puts("PASS: test_to_string_embedded\n");
  93. } else {
  94. failed++;
  95. stdout.puts("FAIL: test_to_string_embedded\n");
  96. }
  97. // Test 12: To string remote
  98. if (test_to_string_remote()) {
  99. passed++;
  100. stdout.puts("PASS: test_to_string_remote\n");
  101. } else {
  102. failed++;
  103. stdout.puts("FAIL: test_to_string_remote\n");
  104. }
  105. // Test 13: To configuration embedded
  106. if (test_to_configuration_embedded()) {
  107. passed++;
  108. stdout.puts("PASS: test_to_configuration_embedded\n");
  109. } else {
  110. failed++;
  111. stdout.puts("FAIL: test_to_configuration_embedded\n");
  112. }
  113. // Test 14: To configuration remote
  114. if (test_to_configuration_remote()) {
  115. passed++;
  116. stdout.puts("PASS: test_to_configuration_remote\n");
  117. } else {
  118. failed++;
  119. stdout.puts("FAIL: test_to_configuration_remote\n");
  120. }
  121. // Test 15: Invalid format error
  122. if (test_invalid_format_error()) {
  123. passed++;
  124. stdout.puts("PASS: test_invalid_format_error\n");
  125. } else {
  126. failed++;
  127. stdout.puts("FAIL: test_invalid_format_error\n");
  128. }
  129. // Test 16: Unknown backend error
  130. if (test_unknown_backend_error()) {
  131. passed++;
  132. stdout.puts("PASS: test_unknown_backend_error\n");
  133. } else {
  134. failed++;
  135. stdout.puts("FAIL: test_unknown_backend_error\n");
  136. }
  137. // Test 17: Missing path error
  138. if (test_missing_path_error()) {
  139. passed++;
  140. stdout.puts("PASS: test_missing_path_error\n");
  141. } else {
  142. failed++;
  143. stdout.puts("FAIL: test_missing_path_error\n");
  144. }
  145. // Test 18: Describe method
  146. if (test_describe()) {
  147. passed++;
  148. stdout.puts("PASS: test_describe\n");
  149. } else {
  150. failed++;
  151. stdout.puts("FAIL: test_describe\n");
  152. }
  153. // Test 19: Backend options
  154. if (test_backend_options()) {
  155. passed++;
  156. stdout.puts("PASS: test_backend_options\n");
  157. } else {
  158. failed++;
  159. stdout.puts("FAIL: test_backend_options\n");
  160. }
  161. // Test 20: Relative path
  162. if (test_relative_path()) {
  163. passed++;
  164. stdout.puts("PASS: test_relative_path\n");
  165. } else {
  166. failed++;
  167. stdout.puts("FAIL: test_relative_path\n");
  168. }
  169. stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
  170. return failed > 0 ? 1 : 0;
  171. }
  172. // Test 1: Embedded LMDB short form
  173. bool test_embedded_lmdb_short() {
  174. try {
  175. var cs = new ConnectionString("lmdb:///var/lib/db");
  176. if (cs.is_remote) return false;
  177. if (cs.backend != "lmdb") return false;
  178. if (cs.path != "/var/lib/db") return false;
  179. return true;
  180. } catch (Error e) {
  181. return false;
  182. }
  183. }
  184. // Test 2: Embedded GDBM short form
  185. bool test_embedded_gdbm_short() {
  186. try {
  187. var cs = new ConnectionString("gdbm:///var/lib/db");
  188. if (cs.is_remote) return false;
  189. if (cs.backend != "gdbm") return false;
  190. if (cs.path != "/var/lib/db") return false;
  191. return true;
  192. } catch (Error e) {
  193. return false;
  194. }
  195. }
  196. // Test 3: Embedded filesystem short form
  197. bool test_embedded_filesystem_short() {
  198. try {
  199. var cs = new ConnectionString("filesystem:///var/lib/db");
  200. if (cs.is_remote) return false;
  201. if (cs.backend != "filesystem") return false;
  202. if (cs.path != "/var/lib/db") return false;
  203. return true;
  204. } catch (Error e) {
  205. return false;
  206. }
  207. }
  208. // Test 4: Embedded full form
  209. bool test_embedded_full_form() {
  210. try {
  211. var cs = new ConnectionString("implexus://embedded?backend=lmdb&path=/var/lib/db");
  212. if (cs.is_remote) return false;
  213. if (cs.backend != "lmdb") return false;
  214. if (cs.path != "/var/lib/db") return false;
  215. return true;
  216. } catch (Error e) {
  217. return false;
  218. }
  219. }
  220. // Test 5: Remote default port
  221. bool test_remote_default_port() {
  222. try {
  223. var cs = new ConnectionString("implexus://server.example.com");
  224. if (!cs.is_remote) return false;
  225. if (cs.host != "server.example.com") return false;
  226. if (cs.port != null) return false; // Should be null, use default
  227. return true;
  228. } catch (Error e) {
  229. return false;
  230. }
  231. }
  232. // Test 6: Remote custom port
  233. bool test_remote_custom_port() {
  234. try {
  235. var cs = new ConnectionString("implexus://server.example.com:9999");
  236. if (!cs.is_remote) return false;
  237. if (cs.host != "server.example.com") return false;
  238. if (cs.port == null) return false;
  239. if (cs.port != 9999) return false;
  240. return true;
  241. } catch (Error e) {
  242. return false;
  243. }
  244. }
  245. // Test 7: Remote with timeout
  246. bool test_remote_with_timeout() {
  247. try {
  248. var cs = new ConnectionString("implexus://server.example.com:9999?timeout=60");
  249. if (!cs.is_remote) return false;
  250. if (cs.host != "server.example.com") return false;
  251. if (cs.port != 9999) return false;
  252. if (cs.timeout == null) return false;
  253. if (cs.timeout != 60) return false;
  254. return true;
  255. } catch (Error e) {
  256. return false;
  257. }
  258. }
  259. // Test 8: Remote IP address
  260. bool test_remote_ip_address() {
  261. try {
  262. var cs = new ConnectionString("implexus://192.168.1.100:9876");
  263. if (!cs.is_remote) return false;
  264. if (cs.host != "192.168.1.100") return false;
  265. if (cs.port != 9876) return false;
  266. return true;
  267. } catch (Error e) {
  268. return false;
  269. }
  270. }
  271. // Test 9: Try parse success
  272. bool test_try_parse_success() {
  273. var cs = ConnectionString.try_parse("lmdb:///var/lib/db");
  274. if (cs == null) return false;
  275. return true;
  276. }
  277. // Test 10: Try parse failure
  278. bool test_try_parse_failure() {
  279. var cs = ConnectionString.try_parse("invalid://format");
  280. if (cs != null) return false;
  281. return true;
  282. }
  283. // Test 11: To string embedded
  284. bool test_to_string_embedded() {
  285. try {
  286. var cs = new ConnectionString("lmdb:///var/lib/db");
  287. string str = cs.to_connection_string();
  288. if (str != "lmdb:///var/lib/db") return false;
  289. return true;
  290. } catch (Error e) {
  291. return false;
  292. }
  293. }
  294. // Test 12: To string remote
  295. bool test_to_string_remote() {
  296. try {
  297. var cs = new ConnectionString("implexus://server.example.com:9999?timeout=60");
  298. string str = cs.to_connection_string();
  299. // Should contain host and port
  300. if (!str.contains("server.example.com")) return false;
  301. if (!str.contains("9999")) return false;
  302. return true;
  303. } catch (Error e) {
  304. return false;
  305. }
  306. }
  307. // Test 13: To configuration embedded
  308. bool test_to_configuration_embedded() {
  309. try {
  310. var cs = new ConnectionString("filesystem:///tmp/test-db");
  311. var config = cs.to_configuration();
  312. if (config.mode != EngineMode.EMBEDDED) return false;
  313. if (config.storage_path != "/tmp/test-db") return false;
  314. return true;
  315. } catch (Error e) {
  316. return false;
  317. }
  318. }
  319. // Test 14: To configuration remote
  320. bool test_to_configuration_remote() {
  321. try {
  322. var cs = new ConnectionString("implexus://server.example.com:9999?timeout=60");
  323. var config = cs.to_configuration();
  324. if (config.mode != EngineMode.REMOTE) return false;
  325. if (config.host != "server.example.com") return false;
  326. if (config.port != 9999) return false;
  327. if (config.timeout_ms != 60000) return false; // 60 seconds in ms
  328. return true;
  329. } catch (Error e) {
  330. return false;
  331. }
  332. }
  333. // Test 15: Invalid format error
  334. bool test_invalid_format_error() {
  335. try {
  336. new ConnectionString("not-a-valid-format");
  337. return false; // Should have thrown
  338. } catch (ConnectionStringError e) {
  339. if (e is ConnectionStringError.INVALID_FORMAT) {
  340. return true;
  341. }
  342. return false;
  343. } catch (Error e) {
  344. return false;
  345. }
  346. }
  347. // Test 16: Unknown backend error
  348. bool test_unknown_backend_error() {
  349. try {
  350. new ConnectionString("implexus://embedded?backend=unknown&path=/tmp/db");
  351. return false; // Should have thrown
  352. } catch (ConnectionStringError e) {
  353. if (e is ConnectionStringError.UNKNOWN_BACKEND) {
  354. return true;
  355. }
  356. return false;
  357. } catch (Error e) {
  358. return false;
  359. }
  360. }
  361. // Test 17: Missing path error
  362. bool test_missing_path_error() {
  363. try {
  364. new ConnectionString("implexus://embedded?backend=lmdb");
  365. return false; // Should have thrown
  366. } catch (ConnectionStringError e) {
  367. if (e is ConnectionStringError.MISSING_PARAMETER) {
  368. return true;
  369. }
  370. return false;
  371. } catch (Error e) {
  372. return false;
  373. }
  374. }
  375. // Test 18: Describe method
  376. bool test_describe() {
  377. try {
  378. var cs1 = new ConnectionString("lmdb:///var/lib/db");
  379. string desc1 = cs1.describe();
  380. if (!desc1.contains("EMBEDDED")) return false;
  381. if (!desc1.contains("lmdb")) return false;
  382. var cs2 = new ConnectionString("implexus://server.example.com:9999");
  383. string desc2 = cs2.describe();
  384. if (!desc2.contains("REMOTE")) return false;
  385. if (!desc2.contains("server.example.com")) return false;
  386. return true;
  387. } catch (Error e) {
  388. return false;
  389. }
  390. }
  391. // Test 19: Backend options
  392. bool test_backend_options() {
  393. try {
  394. var cs = new ConnectionString("implexus://embedded?backend=lmdb&path=/var/lib/db&map_size=2048&enable_cache=true&cache_size=5000");
  395. if (cs.map_size == null || cs.map_size != 2048) return false;
  396. if (cs.enable_cache == null || cs.enable_cache != true) return false;
  397. if (cs.cache_size == null || cs.cache_size != 5000) return false;
  398. return true;
  399. } catch (Error e) {
  400. return false;
  401. }
  402. }
  403. // Test 20: Relative path
  404. bool test_relative_path() {
  405. try {
  406. var cs = new ConnectionString("filesystem://./data/db");
  407. if (cs.is_remote) return false;
  408. if (cs.path != "./data/db") return false;
  409. return true;
  410. } catch (Error e) {
  411. return false;
  412. }
  413. }