Files
relay_chn/test_apps/main/test_relay_chn_nvs_multi.c
ismail 639533cbb6 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
2025-09-04 15:06:17 +03:00

189 lines
7.3 KiB
C

/*
* 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"
#include "test_common.h"
#define TEST_NVS_TASK_TIME_OUT_MS 300
TEST_CASE("Test direction setting and getting", "[relay_chn][nvs]")
{
// Test all channels
relay_chn_direction_t dir, expect;
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));
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 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, RELAY_CHN_DIRECTION_DEFAULT));
}
}
TEST_CASE("Test relay_chn_nvs_erase_all", "[relay_chn][nvs]")
{
// Store some test data first
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
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
for (int channel = 0; channel < CONFIG_RELAY_CHN_COUNT; channel++) {
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);
}
#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
#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]")
{
// 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++) {
test_limits[i] = 30 + i; // e.g., 30, 31, 32...
}
// 1. Set all values first
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
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, 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, 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
}
#endif
#if CONFIG_RELAY_CHN_ENABLE_TILTING
TEST_CASE("Test sensitivity setting and getting", "[relay_chn][nvs][tilt]")
{
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...
}
// 1. Set all values first
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
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, 0));
TEST_ASSERT_EQUAL_UINT8(test_sensitivities[i], sensitivity_read);
}
}
TEST_CASE("Test tilt counter operations", "[relay_chn][nvs][tilt]")
{
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...
}
// 1. Set all values first
for (uint8_t i = 0; i < CONFIG_RELAY_CHN_COUNT; i++) {
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, 0));
TEST_ASSERT_EQUAL_UINT16(test_counts[i], count_read);
}
}
TEST_CASE("Test tilting invalid parameters", "[relay_chn][nvs][tilt]")
{
// 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, 0));
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, relay_chn_nvs_get_tilt_count(channel, NULL, 0));
}
}
#endif // CONFIG_RELAY_CHN_ENABLE_TILTING