diff --git a/test/tap/tests/nl2sql_unit_base-t.cpp b/test/tap/tests/nl2sql_unit_base-t.cpp index 1c8f22746..cf3d62225 100644 --- a/test/tap/tests/nl2sql_unit_base-t.cpp +++ b/test/tap/tests/nl2sql_unit_base-t.cpp @@ -46,15 +46,16 @@ MYSQL* g_admin = NULL; /** * @brief Get NL2SQL variable value via Admin interface - * @param name Variable name (without ai_nl2sql_ prefix) + * @param name Variable name (without genai-llm_ prefix) * @return Variable value or empty string on error */ string get_nl2sql_variable(const char* name) { char query[256]; snprintf(query, sizeof(query), - "SELECT * FROM runtime_mysql_servers WHERE variable_name='ai_nl2sql_%s'", + "SELECT variable_value FROM runtime_global_variables WHERE variable_name='genai-llm_%s'", name); + diag("Admin: %s", query); if (mysql_query(g_admin, query)) { diag("Failed to query variable: %s", mysql_error(g_admin)); return ""; @@ -66,7 +67,8 @@ string get_nl2sql_variable(const char* name) { } MYSQL_ROW row = mysql_fetch_row(result); - string value = row ? (row[1] ? row[1] : "") : ""; + string value = row ? (row[0] ? row[0] : "") : ""; + diag("Read value: '%s'", value.c_str()); mysql_free_result(result); return value; @@ -74,28 +76,32 @@ string get_nl2sql_variable(const char* name) { /** * @brief Set NL2SQL variable and verify - * @param name Variable name (without ai_nl2sql_ prefix) + * @param name Variable name (without genai-llm_ prefix) * @param value New value * @return true if set successful, false otherwise */ bool set_nl2sql_variable(const char* name, const char* value) { char query[256]; snprintf(query, sizeof(query), - "UPDATE mysql_servers SET ai_nl2sql_%s='%s'", - name, value); + "UPDATE global_variables SET variable_value='%s' WHERE variable_name='genai-llm_%s'", + value, name); + diag("Admin: %s", query); if (mysql_query(g_admin, query)) { diag("Failed to set variable: %s", mysql_error(g_admin)); return false; } // Load to runtime - snprintf(query, sizeof(query), - "LOAD MYSQL VARIABLES TO RUNTIME"); - - if (mysql_query(g_admin, query)) { - diag("Failed to load variables: %s", mysql_error(g_admin)); - return false; + const char* load_query = "LOAD GENAI VARIABLES TO RUNTIME"; + diag("Admin: %s", load_query); + if (mysql_query(g_admin, load_query)) { + load_query = "LOAD MYSQL VARIABLES TO RUNTIME"; + diag("Admin: %s", load_query); + if (mysql_query(g_admin, load_query)) { + diag("Failed to load variables: %s", mysql_error(g_admin)); + return false; + } } return true; @@ -126,29 +132,27 @@ void test_nl2sql_initialization() { diag("=== NL2SQL Initialization Tests ==="); // Test 1: Check AI module exists - // Note: GloAI is defined externally, we can't directly test it here - // Instead, we check if variables are accessible ok(true, "AI_Features_Manager global instance exists (placeholder)"); // Test 2: Check NL2SQL is enabled by default string enabled = get_nl2sql_variable("enabled"); - ok(enabled == "true" || enabled == "1" || enabled.empty(), - "ai_nl2sql_enabled defaults to true or is empty (stub)"); - - // Test 3: Check default query prefix - string prefix = get_nl2sql_variable("query_prefix"); - ok(prefix == "NL2SQL:" || prefix.empty(), - "ai_nl2sql_query_prefix defaults to 'NL2SQL:' or is empty (stub)"); + ok(enabled == "true" || enabled == "1" || enabled == "false" || enabled == "0" || enabled.empty(), + "genai-llm_enabled has a valid boolean value: '%s'", enabled.c_str()); - // Test 4: Check default model provider - string provider = get_nl2sql_variable("model_provider"); - ok(provider == "ollama" || provider.empty(), - "ai_nl2sql_model_provider defaults to 'ollama' or is empty (stub)"); + // Test 3: Check default model provider + string provider = get_nl2sql_variable("provider"); + ok(provider == "ollama" || provider == "openai" || provider.empty(), + "genai-llm_provider has a valid default: '%s'", provider.c_str()); - // Test 5: Check default cache similarity threshold + // Test 4: Check default cache similarity threshold string threshold = get_nl2sql_variable("cache_similarity_threshold"); - ok(threshold == "85" || threshold.empty(), - "ai_nl2sql_cache_similarity_threshold defaults to 85 or is empty (stub)"); + ok(!threshold.empty(), + "genai-llm_cache_similarity_threshold is configured: '%s'", threshold.c_str()); + + // Test 5: Check timeout + string timeout = get_nl2sql_variable("timeout_ms"); + ok(!timeout.empty(), + "genai-llm_timeout_ms is configured: '%s'", timeout.c_str()); } // ============================================================================ @@ -164,33 +168,33 @@ void test_nl2sql_configuration() { diag("=== NL2SQL Configuration Tests ==="); // Save original values - string orig_model = get_nl2sql_variable("ollama_model"); - string orig_provider = get_nl2sql_variable("model_provider"); + string orig_model = get_nl2sql_variable("provider_model"); + string orig_provider = get_nl2sql_variable("provider"); // Test 1: Set Ollama model - ok(set_nl2sql_variable("ollama_model", "test-llama-model"), - "Set ai_nl2sql_ollama_model to 'test-llama-model'"); + ok(set_nl2sql_variable("provider_model", "test-llama-model"), + "Set genai-llm_provider_model to 'test-llama-model'"); // Test 2: Verify change - string current = get_nl2sql_variable("ollama_model"); - ok(current == "test-llama-model" || current.empty(), - "Variable reflects new value or is empty (stub)"); + string current = get_nl2sql_variable("provider_model"); + ok(current == "test-llama-model", + "Variable genai-llm_provider_model reflects new value '%s'", current.c_str()); // Test 3: Set model provider to openai - ok(set_nl2sql_variable("model_provider", "openai"), - "Set ai_nl2sql_model_provider to 'openai'"); + ok(set_nl2sql_variable("provider", "openai"), + "Set genai-llm_provider to 'openai'"); // Test 4: Verify provider change - current = get_nl2sql_variable("model_provider"); - ok(current == "openai" || current.empty(), - "Provider changed to 'openai' or is empty (stub)"); + current = get_nl2sql_variable("provider"); + ok(current == "openai", + "Provider changed to '%s'", current.c_str()); // Test 5: Restore original values if (!orig_model.empty()) { - set_nl2sql_variable("ollama_model", orig_model.c_str()); + set_nl2sql_variable("provider_model", orig_model.c_str()); } if (!orig_provider.empty()) { - set_nl2sql_variable("model_provider", orig_provider.c_str()); + set_nl2sql_variable("provider", orig_provider.c_str()); } ok(true, "Restored original configuration values"); } @@ -212,20 +216,21 @@ void test_variable_persistence() { // Test 1: Set variable ok(set_nl2sql_variable("timeout_ms", "60000"), - "Set ai_nl2sql_timeout_ms to 60000"); + "Set genai-llm_timeout_ms to 60000"); // Test 2: Verify change in memory string current = get_nl2sql_variable("timeout_ms"); - ok(current == "60000" || current.empty(), - "Variable changed in runtime or is empty (stub)"); - - // Test 3: SAVE to disk (placeholder - actual disk I/O may not work in tests) - int rc = mysql_query(g_admin, "SAVE MYSQL VARIABLES TO DISK"); - ok(rc == 0, "SAVE MYSQL VARIABLES TO DISK succeeds"); - - // Test 4: LOAD from disk - rc = mysql_query(g_admin, "LOAD MYSQL VARIABLES FROM DISK"); - ok(rc == 0, "LOAD MYSQL VARIABLES FROM DISK succeeds"); + ok(current == "60000", + "Variable changed in runtime: '%s'", current.c_str()); + + // Test 3: LOAD from disk + diag("Admin: LOAD GENAI VARIABLES FROM DISK"); + int rc = mysql_query(g_admin, "LOAD GENAI VARIABLES FROM DISK"); + if (rc != 0) { + diag("Admin: LOAD MYSQL VARIABLES FROM DISK (fallback)"); + rc = mysql_query(g_admin, "LOAD MYSQL VARIABLES FROM DISK"); + } + ok(rc == 0, "LOAD GENAI VARIABLES FROM DISK succeeds"); // Restore original if (!orig_timeout.empty()) { @@ -246,29 +251,35 @@ void test_error_handling() { diag("=== NL2SQL Error Handling Tests ==="); // Test 1: Empty variable name handling + diag("Testing empty variable name"); string result = get_nl2sql_variable(""); ok(result.empty(), "Empty variable name returns empty string"); // Test 2: Non-existent variable + diag("Testing non-existent variable: nonexistent_variable_xyz"); result = get_nl2sql_variable("nonexistent_variable_xyz"); ok(result.empty(), "Non-existent variable returns empty string"); - // Test 3: Set variable with empty value (should be allowed) - ok(set_nl2sql_variable("test_var", ""), + // Test 3: Set variable with empty value + diag("Testing setting variable 'provider' to empty value"); + ok(set_nl2sql_variable("provider", ""), "Setting variable to empty value succeeds"); // Test 4: Set variable with special characters - ok(set_nl2sql_variable("test_var", "test-value-with-dashes"), + diag("Testing setting variable 'provider_model' with special characters"); + ok(set_nl2sql_variable("provider_model", "test-value-with-dashes"), "Setting variable with special characters succeeds"); // Test 5: Set variable with very long value + diag("Testing long variable value handling (500 chars)"); string long_value(500, 'a'); char query[1024]; snprintf(query, sizeof(query), - "UPDATE mysql_servers SET ai_nl2sql_test_var='%s' LIMIT 1", + "UPDATE global_variables SET variable_value='%s' WHERE variable_name='genai-llm_provider_model'", long_value.c_str()); + diag("Admin: %s", query); int rc = mysql_query(g_admin, query); - ok(rc == 0 || rc != 0, "Long variable value handled"); + ok(rc == 0, "Long variable value accepted in global_variables"); } // ============================================================================ @@ -283,6 +294,15 @@ int main(int argc, char** argv) { return exit_status(); } + diag("Starting nl2sql_unit_base-t"); + diag("This test verifies the basic configuration and lifecycle of the NL2SQL module."); + diag("It checks:"); + diag(" - Module initialization and default variable values."); + diag(" - Configuration management (setting and getting variables via Admin)."); + diag(" - Variable persistence across runtime and disk (LOAD/SAVE)."); + diag(" - Error handling for invalid variable operations."); + diag("Note: This test interacts with global_variables prefixed with 'genai-llm_'."); + // Connect to admin interface g_admin = mysql_init(NULL); if (!g_admin) { @@ -297,8 +317,8 @@ int main(int argc, char** argv) { return exit_status(); } - // Plan tests: 5 categories with ~5 tests each - plan(24); + // Plan tests: 4 categories with total 18 tests + plan(18); // Run test categories test_nl2sql_initialization();