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:
126
test_apps/main/test_relay_chn_nvs_single.c
Normal file
126
test_apps/main/test_relay_chn_nvs_single.c
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2025 Kozmotronik Tech
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "unity.h"
|
||||
#include "esp_system.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "relay_chn_nvs.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());
|
||||
}
|
||||
|
||||
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));
|
||||
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));
|
||||
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_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));
|
||||
|
||||
#ifdef RELAY_CHN_ENABLE_TILTING
|
||||
uint8_t sensitivity = 50;
|
||||
TEST_ESP_OK(relay_chn_nvs_set_tilt_sensitivity(0, sensitivity));
|
||||
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, 100, 200));
|
||||
#endif
|
||||
|
||||
// Test erase all
|
||||
TEST_ESP_OK(relay_chn_nvs_erase_all());
|
||||
|
||||
// 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));
|
||||
|
||||
#ifdef 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));
|
||||
|
||||
uint32_t fwd_count, rev_count;
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_NVS_NOT_FOUND, relay_chn_nvs_get_tilt_count(0, &fwd_count, &rev_count));
|
||||
#endif
|
||||
|
||||
TEST_ESP_OK(relay_chn_nvs_deinit());
|
||||
}
|
||||
|
||||
#ifdef 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));
|
||||
|
||||
uint8_t sensitivity;
|
||||
TEST_ESP_OK(relay_chn_nvs_get_tilt_sensitivity(0, &sensitivity));
|
||||
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 uint32_t fwd_count = 100;
|
||||
const uint32_t rev_count = 200;
|
||||
|
||||
// Test setting counters
|
||||
TEST_ESP_OK(relay_chn_nvs_set_tilt_count(0, fwd_count, rev_count));
|
||||
|
||||
uint32_t fwd_read, rev_read;
|
||||
TEST_ESP_OK(relay_chn_nvs_get_tilt_count(0, &fwd_read, &rev_read));
|
||||
TEST_ASSERT_EQUAL(fwd_count, fwd_read);
|
||||
TEST_ASSERT_EQUAL(rev_count, rev_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());
|
||||
|
||||
uint32_t fwd_count, rev_count;
|
||||
|
||||
// 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, &rev_count));
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(0, &fwd_count, NULL));
|
||||
|
||||
TEST_ESP_OK(relay_chn_nvs_deinit());
|
||||
}
|
||||
#endif // RELAY_CHN_ENABLE_TILTING
|
||||
Reference in New Issue
Block a user