sal/src/rhai_tests/redisclient/run_all_tests.rhai
Mahmoud Emad f002445c9e
Some checks failed
Rhai Tests / Run Rhai Tests (push) Waiting to run
Rhai Tests / Run Rhai Tests (pull_request) Has been cancelled
feat: Add PostgreSQL and Redis client support
- Add PostgreSQL client functionality for database interactions.
- Add Redis client functionality for cache and data store operations.
- Extend Rhai scripting with PostgreSQL and Redis client modules.
- Add documentation and test cases for both clients.
2025-05-09 09:45:50 +03:00

155 lines
4.8 KiB
Plaintext
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// run_all_tests.rhai
// Runs all Redis client module tests
print("=== Running Redis Client Module Tests ===");
// Custom assert function
fn assert_true(condition, message) {
if !condition {
print(`ASSERTION FAILED: ${message}`);
throw message;
}
}
// Helper function to check if Redis is available
fn is_redis_available() {
try {
// Try to execute a simple PING command
let ping_result = redis_ping();
return ping_result == "PONG";
} catch(err) {
print(`Redis connection error: ${err}`);
return false;
}
}
// Run each test directly
let passed = 0;
let failed = 0;
let skipped = 0;
// Check if Redis is available
let redis_available = is_redis_available();
if !redis_available {
print("Redis server is not available. Skipping all Redis tests.");
skipped = 3; // Skip all three tests
} else {
// Test 1: Redis Connection
print("\n--- Running Redis Connection Tests ---");
try {
// Test redis_ping function
print("Testing redis_ping()...");
let ping_result = redis_ping();
assert_true(ping_result == "PONG", "PING should return PONG");
print(`✓ redis_ping(): Returned ${ping_result}`);
// Test redis_set and redis_get functions
print("Testing redis_set() and redis_get()...");
let test_key = "rhai_test_key";
let test_value = "Hello from Rhai test";
// Set a value
let set_result = redis_set(test_key, test_value);
assert_true(set_result, "SET operation should succeed");
print(`✓ redis_set(): Successfully set key ${test_key}`);
// Get the value back
let get_result = redis_get(test_key);
assert_true(get_result == test_value, "GET should return the value we set");
print(`✓ redis_get(): Successfully retrieved value for key ${test_key}`);
// Clean up
redis_del(test_key);
print("--- Redis Connection Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Redis Connection Tests: ${err}`);
failed += 1;
}
// Test 2: Redis Operations
print("\n--- Running Redis Operations Tests ---");
try {
// Test prefix for all keys to avoid conflicts
let prefix = "rhai_test_";
// Test redis_hset and redis_hget functions
print("Testing redis_hset() and redis_hget()...");
let hash_key = prefix + "hash";
let field = "field1";
let value = "value1";
// Set hash field
let hset_result = redis_hset(hash_key, field, value);
assert_true(hset_result, "HSET operation should succeed");
print(`✓ redis_hset(): Successfully set field in hash ${hash_key}`);
// Get hash field
let hget_result = redis_hget(hash_key, field);
assert_true(hget_result == value, "HGET should return the value we set");
print(`✓ redis_hget(): Successfully retrieved value from hash ${hash_key}`);
// Clean up
redis_del(hash_key);
print("--- Redis Operations Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Redis Operations Tests: ${err}`);
failed += 1;
}
// Test 3: Redis Authentication
print("\n--- Running Redis Authentication Tests ---");
try {
print("Authentication support will be implemented in a future update.");
print("The backend implementation is ready, but the Rhai bindings are still in development.");
// For now, just test basic Redis functionality
print("\nTesting basic Redis functionality...");
// Test a simple operation
let test_key = "auth_test_key";
let test_value = "auth_test_value";
let set_result = redis_set(test_key, test_value);
assert_true(set_result, "Should be able to set a key");
print("✓ Set key");
let get_result = redis_get(test_key);
assert_true(get_result == test_value, "Should be able to get the key");
print("✓ Got key");
// Clean up
let del_result = redis_del(test_key);
assert_true(del_result, "Should be able to delete the key");
print("✓ Deleted test key");
print("--- Redis Authentication Tests completed successfully ---");
passed += 1;
} catch(err) {
print(`!!! Error in Redis Authentication Tests: ${err}`);
failed += 1;
}
}
print("\n=== Test Summary ===");
print(`Passed: ${passed}`);
print(`Failed: ${failed}`);
print(`Skipped: ${skipped}`);
print(`Total: ${passed + failed + skipped}`);
if failed == 0 {
if skipped > 0 {
print("\n⚠ All tests skipped or passed!");
} else {
print("\n✅ All tests passed!");
}
} else {
print("\n❌ Some tests failed!");
}
// Return the number of failed tests (0 means success)
failed;