Skip to content

Commit

Permalink
Test serialization and deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mairas committed Oct 8, 2024
1 parent 3666b46 commit 5ea6352
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions test/system/test_config_item/config_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,57 @@

using namespace sensesp;

// PersistingObservableValue saves itself on change
void test_persisting_observable_value_schema() {
SensESPMinimalAppBuilder builder;
SensESPMinimalApp* sensesp_app = builder.get_app();
TEST_ASSERT_NOT_NULL(sensesp_app);


PersistingObservableValue<int> value{2, "/test"};

auto config_item = ConfigItem(&value);

value.set(3);

String ref_schema = R"###({"type":"object","properties":{"value":{"title":"Value","type":"number"}}})###";
String schema = config_item->get_config_schema();
ESP_LOGD("test_schema", "schema: %s", schema.c_str());

TEST_ASSERT_EQUAL_STRING(ref_schema.c_str(), schema.c_str());

value.set(0);
value.save();
value.set(4);

value.set(3);
value.load();

TEST_ASSERT_EQUAL(3, value.get());

value.set(4);
value.load();

TEST_ASSERT_EQUAL(4, value.get());

JsonDocument doc;
JsonObject root = doc.to<JsonObject>();
bool result = value.to_json(root);
TEST_ASSERT_TRUE(result);

String json_str;
serializeJson(doc, json_str);

String ref_json = R"###({"value":4})###";

TEST_ASSERT_EQUAL_STRING(ref_json.c_str(), json_str.c_str());

String json_input = R"###({"value":5})###";

DeserializationError error = deserializeJson(doc, json_input);
TEST_ASSERT_TRUE(error == DeserializationError::Ok);

result = value.from_json(doc.as<JsonObject>());
TEST_ASSERT_TRUE(result);

TEST_ASSERT_EQUAL(5, value.get());
}

void setup() {
Expand Down

0 comments on commit 5ea6352

Please sign in to comment.