From 4ffc241767eb01d3c1dab1fc291e5f48331e175e Mon Sep 17 00:00:00 2001 From: Philip Zhang Date: Fri, 17 Jul 2026 13:51:54 +0800 Subject: [PATCH] test(core): cover EntityRoot lifecycle --- .../java/io/teaql/core/EntityRootTest.java | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 teaql-core/src/test/java/io/teaql/core/EntityRootTest.java diff --git a/teaql-core/src/test/java/io/teaql/core/EntityRootTest.java b/teaql-core/src/test/java/io/teaql/core/EntityRootTest.java new file mode 100644 index 0000000..a471f2b --- /dev/null +++ b/teaql-core/src/test/java/io/teaql/core/EntityRootTest.java @@ -0,0 +1,75 @@ +package io.teaql.core; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class EntityRootTest { + + private static final EntityKey ORDER = new EntityKey("Order", 1L); + private static final EntityKey OTHER_ORDER = new EntityKey("Order", 2L); + + @Test + public void markAsNewRecordsOnlyRequestedKeyAndIsIdempotent() { + EntityRoot root = new EntityRoot(); + + root.markAsNew(ORDER); + root.markAsNew(ORDER); + + assertTrue(root.isNew(ORDER)); + assertFalse(root.isNew(OTHER_ORDER)); + assertEquals(1, root.newKeys().size()); + } + + @Test + public void newKeysViewIsReadOnly() { + EntityRoot root = new EntityRoot(); + root.markAsNew(ORDER); + + assertThrows(UnsupportedOperationException.class, () -> root.newKeys().add(OTHER_ORDER)); + } + + @Test + public void markAsDeleteRecordsOnlyRequestedKeyAndIsIdempotent() { + EntityRoot root = new EntityRoot(); + + root.markAsDelete(ORDER); + root.markAsDelete(ORDER); + + assertTrue(root.isMarkedAsDelete(ORDER)); + assertFalse(root.isMarkedAsDelete(OTHER_ORDER)); + assertEquals(1, root.deletedKeys().size()); + } + + @Test + public void deletedKeysViewIsReadOnly() { + EntityRoot root = new EntityRoot(); + root.markAsDelete(ORDER); + + assertThrows( + UnsupportedOperationException.class, + () -> root.deletedKeys().add(OTHER_ORDER)); + } + + @Test + public void markingEntityDeletedClearsItsChangesFromEveryScopeOnly() { + EntityRoot root = new EntityRoot(); + root.set(ORDER, "status", "CREATED"); + root.set(OTHER_ORDER, "status", "CREATED"); + root.pushChangeSet(); + root.set(ORDER, "total", 100L); + root.set(OTHER_ORDER, "total", 200L); + + root.markAsDelete(ORDER); + + assertTrue(root.changedFieldNames(ORDER).isEmpty()); + assertNull(root.get(ORDER, "status")); + assertNull(root.get(ORDER, "total")); + assertEquals("CREATED", root.get(OTHER_ORDER, "status")); + assertEquals(200L, root.get(OTHER_ORDER, "total")); + } +}