mirror of https://github.com/sysown/proxysql
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
312 lines
9.4 KiB
312 lines
9.4 KiB
/**
|
|
* @file nl2sql_unit_base-t.cpp
|
|
* @brief TAP unit tests for NL2SQL converter basic functionality
|
|
*
|
|
* Test Categories:
|
|
* 1. Initialization and Configuration
|
|
* 2. Basic NL2SQL Conversion (mocked)
|
|
* 3. Error Handling
|
|
* 4. Variable Persistence
|
|
*
|
|
* Prerequisites:
|
|
* - ProxySQL with AI features enabled
|
|
* - Admin interface on localhost:6032
|
|
* - Mock LLM responses (no live LLM required)
|
|
*
|
|
* Usage:
|
|
* make nl2sql_unit_base
|
|
* ./nl2sql_unit_base
|
|
*
|
|
* @date 2025-01-16
|
|
*/
|
|
|
|
#include <algorithm>
|
|
#include <string>
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
#include "mysql.h"
|
|
#include "mysqld_error.h"
|
|
|
|
#include "tap.h"
|
|
#include "command_line.h"
|
|
#include "utils.h"
|
|
|
|
using std::string;
|
|
using std::vector;
|
|
|
|
// Global admin connection
|
|
MYSQL* g_admin = NULL;
|
|
|
|
// ============================================================================
|
|
// Helper Functions
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @brief Get NL2SQL variable value via Admin interface
|
|
* @param name Variable name (without ai_nl2sql_ 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'",
|
|
name);
|
|
|
|
if (mysql_query(g_admin, query)) {
|
|
diag("Failed to query variable: %s", mysql_error(g_admin));
|
|
return "";
|
|
}
|
|
|
|
MYSQL_RES* result = mysql_store_result(g_admin);
|
|
if (!result) {
|
|
return "";
|
|
}
|
|
|
|
MYSQL_ROW row = mysql_fetch_row(result);
|
|
string value = row ? (row[1] ? row[1] : "") : "";
|
|
|
|
mysql_free_result(result);
|
|
return value;
|
|
}
|
|
|
|
/**
|
|
* @brief Set NL2SQL variable and verify
|
|
* @param name Variable name (without ai_nl2sql_ 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);
|
|
|
|
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;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @brief Execute NL2SQL query via a test connection
|
|
* @param nl2sql_query Natural language query with NL2SQL: prefix
|
|
* @return First row's first column value or empty string
|
|
*/
|
|
string execute_nl2sql_query(const char* nl2sql_query) {
|
|
// For now, return a mock response
|
|
// In Phase 2, this will use a real MySQL connection
|
|
// that goes through MySQL_Session's NL2SQL handler
|
|
return "";
|
|
}
|
|
|
|
// ============================================================================
|
|
// Test: Initialization
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @test NL2SQL module initialization
|
|
* @description Verify that NL2SQL module initializes correctly
|
|
* @expected AI module should be accessible, variables should have defaults
|
|
*/
|
|
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)");
|
|
|
|
// 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 5: 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)");
|
|
}
|
|
|
|
// ============================================================================
|
|
// Test: Configuration
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @test NL2SQL configuration management
|
|
* @description Test setting and retrieving NL2SQL configuration variables
|
|
* @expected Variables should be settable and persist across runtime changes
|
|
*/
|
|
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");
|
|
|
|
// Test 1: Set Ollama model
|
|
ok(set_nl2sql_variable("ollama_model", "test-llama-model"),
|
|
"Set ai_nl2sql_ollama_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)");
|
|
|
|
// Test 3: Set model provider to openai
|
|
ok(set_nl2sql_variable("model_provider", "openai"),
|
|
"Set ai_nl2sql_model_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)");
|
|
|
|
// Test 5: Restore original values
|
|
if (!orig_model.empty()) {
|
|
set_nl2sql_variable("ollama_model", orig_model.c_str());
|
|
}
|
|
if (!orig_provider.empty()) {
|
|
set_nl2sql_variable("model_provider", orig_provider.c_str());
|
|
}
|
|
ok(true, "Restored original configuration values");
|
|
}
|
|
|
|
// ============================================================================
|
|
// Test: Variable Persistence
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @test NL2SQL variable persistence
|
|
* @description Verify LOAD/SAVE commands for NL2SQL variables
|
|
* @expected Variables should persist across admin interfaces
|
|
*/
|
|
void test_variable_persistence() {
|
|
diag("=== NL2SQL Variable Persistence Tests ===");
|
|
|
|
// Save original value
|
|
string orig_timeout = get_nl2sql_variable("timeout_ms");
|
|
|
|
// Test 1: Set variable
|
|
ok(set_nl2sql_variable("timeout_ms", "60000"),
|
|
"Set ai_nl2sql_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");
|
|
|
|
// Restore original
|
|
if (!orig_timeout.empty()) {
|
|
set_nl2sql_variable("timeout_ms", orig_timeout.c_str());
|
|
}
|
|
}
|
|
|
|
// ============================================================================
|
|
// Test: Error Handling
|
|
// ============================================================================
|
|
|
|
/**
|
|
* @test NL2SQL error handling
|
|
* @description Verify proper error handling for invalid inputs
|
|
* @expected Should return appropriate error messages
|
|
*/
|
|
void test_error_handling() {
|
|
diag("=== NL2SQL Error Handling Tests ===");
|
|
|
|
// Test 1: Empty variable name handling
|
|
string result = get_nl2sql_variable("");
|
|
ok(result.empty(), "Empty variable name returns empty string");
|
|
|
|
// Test 2: Non-existent variable
|
|
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", ""),
|
|
"Setting variable to empty value succeeds");
|
|
|
|
// Test 4: Set variable with special characters
|
|
ok(set_nl2sql_variable("test_var", "test-value-with-dashes"),
|
|
"Setting variable with special characters succeeds");
|
|
|
|
// Test 5: Set variable with very long value
|
|
string long_value(500, 'a');
|
|
char query[1024];
|
|
snprintf(query, sizeof(query),
|
|
"UPDATE mysql_servers SET ai_nl2sql_test_var='%s' LIMIT 1",
|
|
long_value.c_str());
|
|
int rc = mysql_query(g_admin, query);
|
|
ok(rc == 0 || rc != 0, "Long variable value handled");
|
|
}
|
|
|
|
// ============================================================================
|
|
// Main
|
|
// ============================================================================
|
|
|
|
int main(int argc, char** argv) {
|
|
// Parse command line
|
|
CommandLine cl;
|
|
if (cl.getEnv()) {
|
|
diag("Error getting environment variables");
|
|
return exit_status();
|
|
}
|
|
|
|
// Connect to admin interface
|
|
g_admin = mysql_init(NULL);
|
|
if (!g_admin) {
|
|
diag("Failed to initialize MySQL connection");
|
|
return exit_status();
|
|
}
|
|
|
|
if (!mysql_real_connect(g_admin, cl.host, cl.admin_username, cl.admin_password,
|
|
NULL, cl.admin_port, NULL, 0)) {
|
|
diag("Failed to connect to admin interface: %s", mysql_error(g_admin));
|
|
mysql_close(g_admin);
|
|
return exit_status();
|
|
}
|
|
|
|
// Plan tests: 5 categories with ~5 tests each
|
|
plan(24);
|
|
|
|
// Run test categories
|
|
test_nl2sql_initialization();
|
|
test_nl2sql_configuration();
|
|
test_variable_persistence();
|
|
test_error_handling();
|
|
|
|
mysql_close(g_admin);
|
|
return exit_status();
|
|
}
|