| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491 |
- /**
- * ConnectionStringTest - Unit tests for ConnectionString
- */
- using Implexus.Core;
- using Implexus.Engine;
- public static int main(string[] args) {
- int passed = 0;
- int failed = 0;
- // Test 1: Embedded LMDB short form
- if (test_embedded_lmdb_short()) {
- passed++;
- stdout.puts("PASS: test_embedded_lmdb_short\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_embedded_lmdb_short\n");
- }
- // Test 2: Embedded GDBM short form
- if (test_embedded_gdbm_short()) {
- passed++;
- stdout.puts("PASS: test_embedded_gdbm_short\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_embedded_gdbm_short\n");
- }
- // Test 3: Embedded filesystem short form
- if (test_embedded_filesystem_short()) {
- passed++;
- stdout.puts("PASS: test_embedded_filesystem_short\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_embedded_filesystem_short\n");
- }
- // Test 4: Embedded full form
- if (test_embedded_full_form()) {
- passed++;
- stdout.puts("PASS: test_embedded_full_form\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_embedded_full_form\n");
- }
- // Test 5: Remote default port
- if (test_remote_default_port()) {
- passed++;
- stdout.puts("PASS: test_remote_default_port\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_remote_default_port\n");
- }
- // Test 6: Remote custom port
- if (test_remote_custom_port()) {
- passed++;
- stdout.puts("PASS: test_remote_custom_port\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_remote_custom_port\n");
- }
- // Test 7: Remote with timeout
- if (test_remote_with_timeout()) {
- passed++;
- stdout.puts("PASS: test_remote_with_timeout\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_remote_with_timeout\n");
- }
- // Test 8: Remote IP address
- if (test_remote_ip_address()) {
- passed++;
- stdout.puts("PASS: test_remote_ip_address\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_remote_ip_address\n");
- }
- // Test 9: Try parse success
- if (test_try_parse_success()) {
- passed++;
- stdout.puts("PASS: test_try_parse_success\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_try_parse_success\n");
- }
- // Test 10: Try parse failure
- if (test_try_parse_failure()) {
- passed++;
- stdout.puts("PASS: test_try_parse_failure\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_try_parse_failure\n");
- }
- // Test 11: To string embedded
- if (test_to_string_embedded()) {
- passed++;
- stdout.puts("PASS: test_to_string_embedded\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_to_string_embedded\n");
- }
- // Test 12: To string remote
- if (test_to_string_remote()) {
- passed++;
- stdout.puts("PASS: test_to_string_remote\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_to_string_remote\n");
- }
- // Test 13: To configuration embedded
- if (test_to_configuration_embedded()) {
- passed++;
- stdout.puts("PASS: test_to_configuration_embedded\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_to_configuration_embedded\n");
- }
- // Test 14: To configuration remote
- if (test_to_configuration_remote()) {
- passed++;
- stdout.puts("PASS: test_to_configuration_remote\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_to_configuration_remote\n");
- }
- // Test 15: Invalid format error
- if (test_invalid_format_error()) {
- passed++;
- stdout.puts("PASS: test_invalid_format_error\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_invalid_format_error\n");
- }
- // Test 16: Unknown backend error
- if (test_unknown_backend_error()) {
- passed++;
- stdout.puts("PASS: test_unknown_backend_error\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_unknown_backend_error\n");
- }
- // Test 17: Missing path error
- if (test_missing_path_error()) {
- passed++;
- stdout.puts("PASS: test_missing_path_error\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_missing_path_error\n");
- }
- // Test 18: Describe method
- if (test_describe()) {
- passed++;
- stdout.puts("PASS: test_describe\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_describe\n");
- }
- // Test 19: Backend options
- if (test_backend_options()) {
- passed++;
- stdout.puts("PASS: test_backend_options\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_backend_options\n");
- }
- // Test 20: Relative path
- if (test_relative_path()) {
- passed++;
- stdout.puts("PASS: test_relative_path\n");
- } else {
- failed++;
- stdout.puts("FAIL: test_relative_path\n");
- }
- stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
- return failed > 0 ? 1 : 0;
- }
- // Test 1: Embedded LMDB short form
- bool test_embedded_lmdb_short() {
- try {
- var cs = new ConnectionString("lmdb:///var/lib/db");
-
- if (cs.is_remote) return false;
- if (cs.backend != "lmdb") return false;
- if (cs.path != "/var/lib/db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 2: Embedded GDBM short form
- bool test_embedded_gdbm_short() {
- try {
- var cs = new ConnectionString("gdbm:///var/lib/db");
-
- if (cs.is_remote) return false;
- if (cs.backend != "gdbm") return false;
- if (cs.path != "/var/lib/db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 3: Embedded filesystem short form
- bool test_embedded_filesystem_short() {
- try {
- var cs = new ConnectionString("filesystem:///var/lib/db");
-
- if (cs.is_remote) return false;
- if (cs.backend != "filesystem") return false;
- if (cs.path != "/var/lib/db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 4: Embedded full form
- bool test_embedded_full_form() {
- try {
- var cs = new ConnectionString("implexus://embedded?backend=lmdb&path=/var/lib/db");
-
- if (cs.is_remote) return false;
- if (cs.backend != "lmdb") return false;
- if (cs.path != "/var/lib/db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 5: Remote default port
- bool test_remote_default_port() {
- try {
- var cs = new ConnectionString("implexus://server.example.com");
-
- if (!cs.is_remote) return false;
- if (cs.host != "server.example.com") return false;
- if (cs.port != null) return false; // Should be null, use default
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 6: Remote custom port
- bool test_remote_custom_port() {
- try {
- var cs = new ConnectionString("implexus://server.example.com:9999");
-
- if (!cs.is_remote) return false;
- if (cs.host != "server.example.com") return false;
- if (cs.port == null) return false;
- if (cs.port != 9999) return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 7: Remote with timeout
- bool test_remote_with_timeout() {
- try {
- var cs = new ConnectionString("implexus://server.example.com:9999?timeout=60");
-
- if (!cs.is_remote) return false;
- if (cs.host != "server.example.com") return false;
- if (cs.port != 9999) return false;
- if (cs.timeout == null) return false;
- if (cs.timeout != 60) return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 8: Remote IP address
- bool test_remote_ip_address() {
- try {
- var cs = new ConnectionString("implexus://192.168.1.100:9876");
-
- if (!cs.is_remote) return false;
- if (cs.host != "192.168.1.100") return false;
- if (cs.port != 9876) return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 9: Try parse success
- bool test_try_parse_success() {
- var cs = ConnectionString.try_parse("lmdb:///var/lib/db");
-
- if (cs == null) return false;
-
- return true;
- }
- // Test 10: Try parse failure
- bool test_try_parse_failure() {
- var cs = ConnectionString.try_parse("invalid://format");
-
- if (cs != null) return false;
-
- return true;
- }
- // Test 11: To string embedded
- bool test_to_string_embedded() {
- try {
- var cs = new ConnectionString("lmdb:///var/lib/db");
- string str = cs.to_connection_string();
-
- if (str != "lmdb:///var/lib/db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 12: To string remote
- bool test_to_string_remote() {
- try {
- var cs = new ConnectionString("implexus://server.example.com:9999?timeout=60");
- string str = cs.to_connection_string();
-
- // Should contain host and port
- if (!str.contains("server.example.com")) return false;
- if (!str.contains("9999")) return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 13: To configuration embedded
- bool test_to_configuration_embedded() {
- try {
- var cs = new ConnectionString("filesystem:///tmp/test-db");
- var config = cs.to_configuration();
-
- if (config.mode != EngineMode.EMBEDDED) return false;
- if (config.storage_path != "/tmp/test-db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 14: To configuration remote
- bool test_to_configuration_remote() {
- try {
- var cs = new ConnectionString("implexus://server.example.com:9999?timeout=60");
- var config = cs.to_configuration();
-
- if (config.mode != EngineMode.REMOTE) return false;
- if (config.host != "server.example.com") return false;
- if (config.port != 9999) return false;
- if (config.timeout_ms != 60000) return false; // 60 seconds in ms
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 15: Invalid format error
- bool test_invalid_format_error() {
- try {
- new ConnectionString("not-a-valid-format");
- return false; // Should have thrown
- } catch (ConnectionStringError e) {
- if (e is ConnectionStringError.INVALID_FORMAT) {
- return true;
- }
- return false;
- } catch (Error e) {
- return false;
- }
- }
- // Test 16: Unknown backend error
- bool test_unknown_backend_error() {
- try {
- new ConnectionString("implexus://embedded?backend=unknown&path=/tmp/db");
- return false; // Should have thrown
- } catch (ConnectionStringError e) {
- if (e is ConnectionStringError.UNKNOWN_BACKEND) {
- return true;
- }
- return false;
- } catch (Error e) {
- return false;
- }
- }
- // Test 17: Missing path error
- bool test_missing_path_error() {
- try {
- new ConnectionString("implexus://embedded?backend=lmdb");
- return false; // Should have thrown
- } catch (ConnectionStringError e) {
- if (e is ConnectionStringError.MISSING_PARAMETER) {
- return true;
- }
- return false;
- } catch (Error e) {
- return false;
- }
- }
- // Test 18: Describe method
- bool test_describe() {
- try {
- var cs1 = new ConnectionString("lmdb:///var/lib/db");
- string desc1 = cs1.describe();
-
- if (!desc1.contains("EMBEDDED")) return false;
- if (!desc1.contains("lmdb")) return false;
-
- var cs2 = new ConnectionString("implexus://server.example.com:9999");
- string desc2 = cs2.describe();
-
- if (!desc2.contains("REMOTE")) return false;
- if (!desc2.contains("server.example.com")) return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 19: Backend options
- bool test_backend_options() {
- try {
- var cs = new ConnectionString("implexus://embedded?backend=lmdb&path=/var/lib/db&map_size=2048&enable_cache=true&cache_size=5000");
-
- if (cs.map_size == null || cs.map_size != 2048) return false;
- if (cs.enable_cache == null || cs.enable_cache != true) return false;
- if (cs.cache_size == null || cs.cache_size != 5000) return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
- // Test 20: Relative path
- bool test_relative_path() {
- try {
- var cs = new ConnectionString("filesystem://./data/db");
-
- if (cs.is_remote) return false;
- if (cs.path != "./data/db") return false;
-
- return true;
- } catch (Error e) {
- return false;
- }
- }
|