From 5ea635259d7fc78d64986b50bacb358dcc35c578 Mon Sep 17 00:00:00 2001 From: Matti Airas Date: Tue, 8 Oct 2024 11:38:05 +0300 Subject: [PATCH] Test serialization and deserialization --- test/system/test_config_item/config_item.cpp | 35 ++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/test/system/test_config_item/config_item.cpp b/test/system/test_config_item/config_item.cpp index 3bfd20ca2..d806a9411 100644 --- a/test/system/test_config_item/config_item.cpp +++ b/test/system/test_config_item/config_item.cpp @@ -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 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(); + 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()); + TEST_ASSERT_TRUE(result); + + TEST_ASSERT_EQUAL(5, value.get()); } void setup() {