/** * 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; } }