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,50 +9,43 @@
#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 channel 0
TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_DEFAULT));
relay_chn_direction_t dir;
TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir));
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit
TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir, RELAY_CHN_DIRECTION_DEFAULT));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, dir);
// Test channel 1
TEST_ESP_OK(relay_chn_nvs_set_direction(0, RELAY_CHN_DIRECTION_FLIPPED));
TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir));
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit
TEST_ESP_OK(relay_chn_nvs_get_direction(0, &dir, RELAY_CHN_DIRECTION_DEFAULT));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_FLIPPED, dir);
TEST_ESP_OK(relay_chn_nvs_deinit());
}
TEST_CASE("Test invalid parameters", "[relay_chn][nvs]")
{
TEST_ESP_OK(relay_chn_nvs_init());
// Test NULL pointer
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(0, NULL));
TEST_ESP_OK(relay_chn_nvs_deinit());
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_direction(0, NULL, RELAY_CHN_DIRECTION_DEFAULT));
}
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;
TEST_ESP_OK(relay_chn_nvs_set_direction(0, direction));
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
uint16_t run_limit = 123;
TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, run_limit));
#endif
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t sensitivity = 50;
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity));
@@ -62,76 +55,71 @@ TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
// Test erase all
TEST_ESP_OK(relay_chn_nvs_erase_all());
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit
// 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));
TEST_ESP_OK(relay_chn_nvs_get_direction(0, &read_direction, RELAY_CHN_DIRECTION_DEFAULT));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, read_direction);
#if CONFIG_RELAY_CHN_ENABLE_TILTING
uint8_t read_sensitivity;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_sensitivity(0, &read_sensitivity));
uint16_t tilt_count;
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &tilt_count));
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
uint16_t read_run_limit;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &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;
uint8_t read_sensitivity;
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &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(0, &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());
const uint16_t run_limit_sec = 32;
TEST_ESP_OK(relay_chn_nvs_set_run_limit(0, run_limit_sec));
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit
uint16_t run_limit_read;
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &run_limit_read));
TEST_ESP_OK(relay_chn_nvs_get_run_limit(0, &run_limit_read, CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC));
TEST_ASSERT_EQUAL(run_limit_sec, run_limit_read);
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());
const uint8_t test_sensitivity = 75;
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, test_sensitivity));
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit
uint8_t sensitivity;
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &sensitivity));
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &sensitivity, 0));
TEST_ASSERT_EQUAL(test_sensitivity, sensitivity);
TEST_ESP_OK(relay_chn_nvs_deinit());
}
TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
{
TEST_ESP_OK(relay_chn_nvs_init());
const uint16_t tilt_count = 100;
// Test setting counters
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, tilt_count));
vTaskDelay(pdMS_TO_TICKS(TEST_NVS_TASK_TIME_OUT_MS)); // Allow NVS task to write and commit
uint16_t tilt_count_read;
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &tilt_count_read));
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &tilt_count_read, 0));
TEST_ASSERT_EQUAL(tilt_count, tilt_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
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(0, NULL));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL));
TEST_ESP_OK(relay_chn_nvs_deinit());
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_sensitivity(0, NULL, 0));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, NULL, 0));
}
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING