Refactor and improve unit tests

- Refactored and improved NVS tests to accomodate latest changes in NVS module. See #1098
- Improved channel reset function that is called from `tearDown` to minimize public API calls that trigger a chain of internal calls that involve NVS and timer operations.
- Fixed test case bugs that make some test cases to fail.

Refs #1096, #1098
This commit is contained in:
2025-09-04 15:06:17 +03:00
parent 2c9ee40ff4
commit 639533cbb6
6 changed files with 164 additions and 159 deletions

View File

@@ -9,88 +9,95 @@
#include "esp_system.h"
#include "nvs_flash.h"
#include "relay_chn_nvs.h"
#include "test_common.h"
TEST_CASE("Test relay storage init/deinit", "[relay_chn][nvs]")
{
TEST_ESP_OK(relay_chn_nvs_init());
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#define TEST_NVS_TASK_TIME_OUT_MS 300
TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]")
{
TEST_ESP_OK(relay_chn_nvs_init());
// Test all channels
relay_chn_direction_t dir;
relay_chn_direction_t test_directions[] = {
RELAY_CHN_DIRECTION_DEFAULT,
RELAY_CHN_DIRECTION_FLIPPED
};
relay_chn_direction_t dir, expect;
for (int channel = 0; channel < 2; channel++) {
TEST_ESP_OK(relay_chn_nvs_set_direction(channel, test_directions[channel]));
TEST_ESP_OK(relay_chn_nvs_get_direction(channel, &dir));
TEST_ASSERT_EQUAL(test_directions[channel], dir);
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
dir = channel % 2 == 0 ? RELAY_CHN_DIRECTION_DEFAULT : RELAY_CHN_DIRECTION_FLIPPED;
TEST_ESP_OK(relay_chn_nvs_set_direction(channel, dir));
}
// Wait for the batch commit timeout to ensure the value is written
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS));
TEST_ESP_OK(relay_chn_nvs_deinit());
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
expect = channel % 2 == 0 ? RELAY_CHN_DIRECTION_DEFAULT : RELAY_CHN_DIRECTION_FLIPPED;
TEST_ESP_OK(relay_chn_nvs_get_direction(channel, &dir, RELAY_CHN_DIRECTION_DEFAULT));
TEST_ASSERT_EQUAL(expect, dir);
}
}
TEST_CASE("Test invalid parameters", "[relay_chn][nvs]")
{
TEST_ESP_OK(relay_chn_nvs_init());
// Test NULL pointer for all channels
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(channel, NULL, RELAY_CHN_DIRECTION_DEFAULT));
}
TEST_ESP_OK(relay_chn_nvs_deinit());
}
TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
{
TEST_ESP_OK(relay_chn_nvs_init());
// Store some test data first
relay_chn_direction_t direction = RELAY_CHN_DIRECTION_FLIPPED;
// Set direction for all channels
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
TEST_ESP_OK(relay_chn_nvs_set_direction(1, direction));
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
// Set direction for all channels
TEST_ESP_OK(relay_chn_nvs_set_direction(channel, RELAY_CHN_DIRECTION_FLIPPED));
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
TEST_ESP_OK(relay_chn_nvs_set_run_limit(channel, 100 + channel));
#endif
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t sensitivity = 50;
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, sensitivity));
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(channel, 50));
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(channel, 100 + channel));
}
#endif
// Wait for the set operations and subsequent commits to complete
// Wait 4 times more since 4 x 8 = 32 operations to process
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS * 8));
// Test erase all
TEST_ESP_OK(relay_chn_nvs_erase_all());
// Wait for the erase operation and subsequent commit to complete
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS));
// Verify data was erased by trying to read it back
relay_chn_direction_t read_direction;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_direction(0, &read_direction));
#if CONFIG_RELAY_CHN_ENABLE_TILTING
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(channel, &read_sensitivity));
relay_chn_direction_t read_direction;
TEST_ESP_OK(relay_chn_nvs_get_direction(0, &read_direction, RELAY_CHN_DIRECTION_DEFAULT));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, read_direction);
}
uint16_t tilt_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(channel, &tilt_count));
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
uint16_t read_run_limit;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(channel, &read_run_limit, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC));
TEST_ASSERT_EQUAL(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC, read_run_limit);
}
#endif
TEST_ESP_OK(relay_chn_nvs_deinit());
#if CONFIG_RELAY_CHN_ENABLE_TILTING
const uint8_t default_sensitivity_for_test = 42;
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
uint8_t read_sensitivity;
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(channel, &read_sensitivity, default_sensitivity_for_test));
TEST_ASSERT_EQUAL(default_sensitivity_for_test, read_sensitivity);
uint16_t tilt_count;
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(channel, &tilt_count, 0));
TEST_ASSERT_EQUAL(0, tilt_count);
}
#endif
}
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
{
TEST_ESP_OK(relay_chn_nvs_init());
// Use different values for each channel to detect overwrites
uint16_t test_limits[CONFIG_RELAY_CHN_COUNT];
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
@@ -102,32 +109,31 @@ TEST_CASE("Test run limit setting and getting", "[relay_chn][nvs][run_limit]")
TEST_ESP_OK(relay_chn_nvs_set_run_limit(i, test_limits[i]));
}
// Allow the NVS task to process the batch and commit
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS));
// 2. Then, read them all back and verify
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint16_t run_limit_read;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(i, &run_limit_read));
TEST_ESP_OK(relay_chn_nvs_get_run_limit(i, &run_limit_read, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC));
TEST_ASSERT_EQUAL_UINT16(test_limits[i], run_limit_read);
}
// 3. Verify that changing one channel doesn't affect another
uint16_t new_limit_ch0 = 99;
TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, new_limit_ch0));
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write
uint16_t read_val_ch0, read_val_ch1;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &read_val_ch0));
TEST_ESP_OK(relay_chn_nvs_get_run_limit(1, &read_val_ch1));
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &read_val_ch0, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC));
TEST_ESP_OK(relay_chn_nvs_get_run_limit(1, &read_val_ch1, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC));
TEST_ASSERT_EQUAL_UINT16(new_limit_ch0, read_val_ch0);
TEST_ASSERT_EQUAL_UINT16(test_limits[1], read_val_ch1); // Should still be the old value
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif
#if CONFIG_RELAY_CHN_ENABLE_TILTING
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
uint8_t test_sensitivities[CONFIG_RELAY_CHN_COUNT];
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
test_sensitivities[i] = 70 + i; // e.g., 70, 71, 72...
@@ -138,20 +144,19 @@ TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(i, test_sensitivities[i]));
}
// Allow the NVS task to process the batch and commit
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS));
// 2. Then, read them all back and verify
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint8_t sensitivity_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(i, &sensitivity_read));
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(i, &sensitivity_read, 0));
TEST_ASSERT_EQUAL_UINT8(test_sensitivities[i], sensitivity_read);
}
TEST_ESP_OK(relay_chn_nvs_deinit());
}
TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
uint16_t test_counts[CONFIG_RELAY_CHN_COUNT];
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
test_counts[i] = 100 + i; // e.g., 100, 101, 102...
@@ -162,26 +167,23 @@ TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(i, test_counts[i]));
}
// Allow the NVS task to process the batch and commit
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS));
// 2. Then, read them all back and verify
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
uint16_t count_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(i, &count_read));
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(i, &count_read, 0));
TEST_ASSERT_EQUAL_UINT16(test_counts[i], count_read);
}
TEST_ESP_OK(relay_chn_nvs_deinit());
}
TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
// Test NULL pointers for all channels
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(channel, NULL, 0));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL, 0));
}
TEST_ESP_OK(relay_chn_nvs_deinit());
}
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING