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

@@ -1,4 +1,6 @@
#include "test_common.h"
#include "relay_chn_ctl.h" // For resetting the channels
#include "relay_chn_tilt.h" // For resetting tilt count
const char *TEST_TAG = "RELAY_CHN_TEST";
@@ -32,36 +34,45 @@ const uint8_t gpio_map[] = {4, 5};
const uint8_t gpio_count = sizeof(gpio_map) / sizeof(gpio_map[0]);
static void reset_channel(relay_chn_ctl_t *ctl)
{
ctl->pending_cmd = RELAY_CHN_CMD_NONE;
ctl->state = RELAY_CHN_STATE_IDLE;
ctl->output->direction = RELAY_CHN_DIRECTION_DEFAULT;
ctl->run_info->last_run_cmd = RELAY_CHN_CMD_NONE;
ctl->run_info->last_run_cmd_time_ms = 0;
esp_timer_stop(ctl->inertia_timer);
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
esp_timer_stop(ctl->run_limit_timer);
ctl->run_limit_sec = CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC;
#endif
#if CONFIG_RELAY_CHN_ENABLE_TILTING
relay_chn_tilt_reset_count(ctl->tilt_ctl);
#endif
#if CONFIG_RELAY_CHN_COUNT > 1
#else
#endif
}
void reset_channels_to_defaults()
{
#if CONFIG_RELAY_CHN_COUNT > 1
relay_chn_stop_all();
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
relay_chn_set_run_limit_all_with(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC);
#endif
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i));
relay_chn_ctl_t *ctls = relay_chn_ctl_get_all();
TEST_ASSERT_NOT_NULL_MESSAGE(ctls, "reset_channels_to_defaults: relay_chn_ctl_get_all() returned NULL");
// Reset directions
if (relay_chn_get_direction(i) != RELAY_CHN_DIRECTION_DEFAULT) {
relay_chn_flip_direction(i);
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i));
}
for (int i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
relay_chn_ctl_t *ctl = &ctls[i];
TEST_ASSERT_NOT_NULL_MESSAGE(ctl, "ctl is NULL");
reset_channel(ctl);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state(i));
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction(i));
}
#else
relay_chn_stop();
#if CONFIG_RELAY_CHN_ENABLE_RUN_LIMIT
relay_chn_set_run_limit(CONFIG_RELAY_CHN_RUN_LIMIT_DEFAULT_SEC);
#endif
// Reset direction
if (relay_chn_get_direction() != RELAY_CHN_DIRECTION_DEFAULT) {
relay_chn_flip_direction();
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction());
}
vTaskDelay(pdMS_TO_TICKS(CONFIG_RELAY_CHN_OPPOSITE_INERTIA_MS + TEST_DELAY_MARGIN_MS));
relay_chn_ctl_t *ctl = relay_chn_ctl_get();
TEST_ASSERT_NOT_NULL_MESSAGE(ctl, "reset_channels_to_defaults: relay_chn_ctl_get() returned NULL");
reset_channel(ctl);
TEST_ASSERT_EQUAL(RELAY_CHN_STATE_IDLE, relay_chn_get_state());
TEST_ASSERT_EQUAL(RELAY_CHN_DIRECTION_DEFAULT, relay_chn_get_direction());
#endif
}