- Add a new Redis client module to the SAL library. - Implement Rhai wrappers for Redis connection and operations. - Add comprehensive test suite for the Redis client module. - Update documentation to include Redis client module details. - Add .gitignore entries to exclude test logs and files.
3.6 KiB
Redis Client Module Tests
This document describes the test scripts for the Redis client module in the SAL library. These tests verify the functionality of the Redis client module's connection management and Redis operations.
Test Structure
The tests are organized into two main scripts:
- Redis Connection (
01_redis_connection.rhai
): Tests basic Redis connection and simple operations like PING, SET, GET, and DEL. - Redis Operations (
02_redis_operations.rhai
): Tests more advanced Redis operations like hash operations (HSET, HGET, HGETALL, HDEL) and list operations (RPUSH, LLEN, LRANGE).
Additionally, there's a runner script (run_all_tests.rhai
) that executes all tests and reports results. The runner script contains simplified versions of the individual tests to avoid dependency issues.
Running the Tests
To run all tests, execute the following command from the project root:
herodo --path src/rhai_tests/redisclient/run_all_tests.rhai
To run individual test scripts:
herodo --path src/rhai_tests/redisclient/01_redis_connection.rhai
Test Details
Redis Connection Test
The Redis connection test (01_redis_connection.rhai
) verifies the following functions:
redis_ping
: Checking if the Redis server is availableredis_set
: Setting a key-value pairredis_get
: Getting a value by keyredis_del
: Deleting a key
The test creates a temporary key, performs operations on it, and then cleans up after itself.
Redis Operations Test
The Redis operations test (02_redis_operations.rhai
) verifies the following functions:
-
Hash operations:
redis_hset
: Setting a field in a hashredis_hget
: Getting a field from a hashredis_hgetall
: Getting all fields and values from a hashredis_hdel
: Deleting a field from a hash
-
List operations:
redis_rpush
: Adding elements to a listredis_llen
: Getting the length of a listredis_lrange
: Getting a range of elements from a list
The test creates temporary keys with a unique prefix, performs operations on them, and then cleans up after itself.
Test Runner
The test runner script (run_all_tests.rhai
) provides a framework for executing all tests and reporting results. It:
- Checks if Redis is available before running tests
- Skips tests if Redis is not available
- Contains simplified versions of each test
- Runs each test in a try/catch block to handle errors
- Catches and reports any errors
- Provides a summary of passed, failed, and skipped tests
Redis Server Requirements
These tests require a Redis server to be running and accessible. The tests will attempt to connect to Redis using the following strategy:
- First, try to connect via Unix socket at
$HOME/hero/var/myredis.sock
- If that fails, try to connect via TCP to
127.0.0.1
on the default Redis port (6379)
If no Redis server is available, the tests will be skipped rather than failing.
Adding New Tests
To add a new test:
- Create a new Rhai script in the
src/rhai_tests/redisclient
directory - Add a new test section to the
run_all_tests.rhai
script - Update this documentation to include information about the new test
Best Practices for Writing Tests
When writing tests for the Redis client module:
- Always check if Redis is available before running tests
- Use a unique prefix for test keys to avoid conflicts
- Clean up any keys created during testing
- Use assertions to verify expected behavior
- Print clear messages about what's being tested
- Handle errors gracefully
- Make tests independent of each other
- Keep tests focused on specific functionality