Refactor to remove redundant initialization and add NVS storage tests
- Removed unnecessary calls to relay_chn_create and g_is_component_initialized in multiple test cases across test_relay_chn_core_single.c, test_relay_chn_listener_multi.c, and test_relay_chn_listener_single.c. - Introduced new test files for NVS functionality: test_relay_chn_nvs_multi.c and test_relay_chn_nvs_single.c, covering initialization, direction setting, invalid parameters, and erase operations. - Updated partition table configuration to support NVS storage, including the addition of a new partition file part_nvs.csv. - Adjusted sdkconfig files to enable NVS support and configure custom partition settings for relay channels.
This commit is contained in:
@@ -1,11 +1,16 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "test_common.h"
|
||||
#include "unity.h"
|
||||
#include "unity_internals.h"
|
||||
#include "unity_test_runner.h"
|
||||
#include <stdbool.h>
|
||||
#include "test_common.h"
|
||||
|
||||
#if RELAY_CHN_ENABLE_NVS == 1
|
||||
#include "nvs_flash.h"
|
||||
#include "relay_chn_nvs.h"
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef RELAY_CHN_UNITY_TEST_GROUP_TAG
|
||||
@@ -15,20 +20,60 @@
|
||||
|
||||
void setUp()
|
||||
{
|
||||
g_is_component_initialized = false;
|
||||
|
||||
}
|
||||
|
||||
void tearDown()
|
||||
{
|
||||
// Clean up after each test
|
||||
if (g_is_component_initialized) {
|
||||
relay_chn_destroy();
|
||||
g_is_component_initialized = false;
|
||||
reset_channels_to_idle_state();
|
||||
}
|
||||
|
||||
static void test_nvs_flash_init(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
|
||||
ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
||||
ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init partition return: %s", esp_err_to_name(ret));
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
// NVS partition is truncated and needs to be erased
|
||||
ret = nvs_flash_erase_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
||||
if (ret == ESP_OK) {
|
||||
ret = nvs_flash_init_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
||||
}
|
||||
}
|
||||
#else
|
||||
ret = nvs_flash_init();
|
||||
ESP_LOGI(TEST_TAG, "test_nvs_flash_init: NVS flash init return: %s", esp_err_to_name(ret));
|
||||
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
||||
// NVS partition is truncated and needs to be erased
|
||||
ret = nvs_flash_erase();
|
||||
if (ret == ESP_OK) {
|
||||
ret = nvs_flash_init();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
TEST_ESP_OK(ret);
|
||||
}
|
||||
|
||||
static void test_nvs_flash_deinit(void)
|
||||
{
|
||||
esp_err_t ret;
|
||||
#if RELAY_CHN_NVS_CUSTOM_PARTITION == 1
|
||||
ret = nvs_flash_deinit_partition(RELAY_CHN_NVS_CUSTOM_PARTITION_NAME);
|
||||
#else
|
||||
ret = nvs_flash_deinit();
|
||||
#endif
|
||||
TEST_ESP_OK(ret);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
// Init NVS once for all tests
|
||||
test_nvs_flash_init();
|
||||
|
||||
// Create relay_chn once for all tests
|
||||
TEST_ESP_OK(relay_chn_create(gpio_map, gpio_count));
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
// Log general test information
|
||||
@@ -43,6 +88,13 @@ void app_main(void)
|
||||
}
|
||||
|
||||
UNITY_END();
|
||||
|
||||
// Destroy relay_chn
|
||||
relay_chn_destroy();
|
||||
|
||||
// Deinit NVS
|
||||
test_nvs_flash_deinit();
|
||||
|
||||
ESP_LOGI(TEST_TAG, "All tests complete.");
|
||||
|
||||
esp_restart(); // Restart to invoke qemu exit
|
||||
|
||||
Reference in New Issue
Block a user