Skip to content

Commit

Permalink
moved test_json_parse from test_config to test_variables
Browse files Browse the repository at this point in the history
  • Loading branch information
chl33 committed Feb 9, 2024
1 parent 43b8c89 commit b41b875
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 39 deletions.
38 changes: 38 additions & 0 deletions test/test_config/test_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ArduinoFake.h>

#include "og3/config_interface.h"
#include "og3/constants.h"
#include "og3/flash_support.h"
#include "og3/logger.h"
#include "og3/module_system.h"
Expand Down Expand Up @@ -53,9 +54,46 @@ void test_config() {
TEST_ASSERT_EQUAL_STRING("home", loc.value().c_str());
}

void test_json_parse() {
constexpr unsigned kCfgSet =
og3::VariableBase::Flags::kConfig | og3::VariableBase::Flags::kSettable;
og3::VariableGroup vg("watering");
og3::FloatVariable max_moisture_target("max_moisture_target", 80.0f, og3::units::kPercentage,
"Max moisture", kCfgSet, 0, &vg);
og3::FloatVariable min_moisture_target("min_moisture_target", 70.0f, og3::units::kPercentage,
"Min moisture", kCfgSet, 0, &vg);
og3::FloatVariable pump_dose_msec("pump_on_msec", 3 * og3::kMsecInSec, og3::units::kMilliseconds,
"Pump on time", kCfgSet, 0, &vg);
og3::BoolVariable watering_enabled("watering_enabled", false, "watering enabled", kCfgSet, &vg);
og3::BoolVariable reservoir_check_enabled("res_check_enabled", false, "reservior check enabled",
kCfgSet, &vg);
og3::FloatVariable pump_seconds_after_low("pump_after_low", 10.0f, og3::units::kSeconds,
"pump seconds after low water", kCfgSet, 0, &vg);
const char json[] =
"{\"soil_moisture_in_min\":2900,\"soil_moisture_in_max\":1470,\"soil_moisture_out_min\":0,"
"\"soil_moisture_out_max\":100,\"soil_moisture_delta_per_deg\":0.075000003,\"max_moisture_"
"target\":75,\"min_moisture_target\":65,\"pump_on_msec\":3000,\"watering_enabled\":\"true\","
"\"res_check_enabled\":\"true\",\"pump_after_low\":10}";

JsonDocument doc;
deserializeJson(doc, json);
for (auto* var : vg.variables()) {
if (!var->config()) {
continue;
}
const JsonVariant val = (doc)[var->name()];
if (val.isNull()) {
continue;
}
TEST_ASSERT_TRUE(var->fromJson(val));
}
TEST_ASSERT_TRUE(reservoir_check_enabled.value());
}

int runUnityTests() {
UNITY_BEGIN();
RUN_TEST(test_config);
RUN_TEST(test_json_parse);
return UNITY_END();
}

Expand Down
39 changes: 0 additions & 39 deletions test/test_variables/test_variables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for details.

#include <ArduinoFake.h>
#include <ArduinoJson.h>

#include "og3/constants.h"
#include "og3/html_table.h"
#include "og3/units.h"
#include "og3/variable.h"
Expand Down Expand Up @@ -122,42 +120,6 @@ void test_html_table() {
#endif
}

void test_json_parse() {
constexpr unsigned kCfgSet =
og3::VariableBase::Flags::kConfig | og3::VariableBase::Flags::kSettable;
og3::VariableGroup vg("watering");
og3::FloatVariable max_moisture_target("max_moisture_target", 80.0f, og3::units::kPercentage,
"Max moisture", kCfgSet, 0, &vg);
og3::FloatVariable min_moisture_target("min_moisture_target", 70.0f, og3::units::kPercentage,
"Min moisture", kCfgSet, 0, &vg);
og3::FloatVariable pump_dose_msec("pump_on_msec", 3 * og3::kMsecInSec, og3::units::kMilliseconds,
"Pump on time", kCfgSet, 0, &vg);
og3::BoolVariable watering_enabled("watering_enabled", false, "watering enabled", kCfgSet, &vg);
og3::BoolVariable reservoir_check_enabled("res_check_enabled", false, "reservior check enabled",
kCfgSet, &vg);
og3::FloatVariable pump_seconds_after_low("pump_after_low", 10.0f, og3::units::kSeconds,
"pump seconds after low water", kCfgSet, 0, &vg);
const char json[] =
"{\"soil_moisture_in_min\":2900,\"soil_moisture_in_max\":1470,\"soil_moisture_out_min\":0,"
"\"soil_moisture_out_max\":100,\"soil_moisture_delta_per_deg\":0.075000003,\"max_moisture_"
"target\":75,\"min_moisture_target\":65,\"pump_on_msec\":3000,\"watering_enabled\":\"true\","
"\"res_check_enabled\":\"true\",\"pump_after_low\":10}";

JsonDocument doc;
deserializeJson(doc, json);
for (auto* var : vg.variables()) {
if (!var->config()) {
continue;
}
const JsonVariant val = (doc)[var->name()];
if (val.isNull()) {
continue;
}
TEST_ASSERT_TRUE(var->fromJson(val));
}
TEST_ASSERT_TRUE(reservoir_check_enabled.value());
}

int runUnityTests() {
UNITY_BEGIN();
RUN_TEST(test_int_vars);
Expand All @@ -167,7 +129,6 @@ int runUnityTests() {
RUN_TEST(test_string_vars);
RUN_TEST(test_bool_vars);
RUN_TEST(test_html_table);
RUN_TEST(test_json_parse);
return UNITY_END();
}

Expand Down

0 comments on commit b41b875

Please sign in to comment.