Skip to content

Commit

Permalink
Unit tests for RawEntity.toCrdtEntityData.
Browse files Browse the repository at this point in the history
java/arcs/core/crdt/extension/ConversionExtensions.kt had two functions:
- a simple extension function: RawEntity.toCrdtEntityData()
- a private extension: Any?.toReferencable()

The first one is a one-liner and the second is unused so I'ved removed that file, put toCrdtEntityData in java/arcs/core/crdt/CrdtEntity.kt and tested it in CrdtEntityTest.kt

PiperOrigin-RevId: 347913559
  • Loading branch information
Michael Martin authored and arcs-c3po committed Dec 16, 2020
1 parent 4913bc6 commit bd88941
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 78 deletions.
6 changes: 6 additions & 0 deletions java/arcs/core/crdt/CrdtEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -427,3 +427,9 @@ class CrdtEntity(
) : Operation(actor, versionMap)
}
}

/** Converts the [RawEntity] into a [CrdtEntity.Data] model, at the given version. */
fun RawEntity.toCrdtEntityData(
versionMap: VersionMap,
referenceBuilder: (Referencable) -> CrdtEntity.Reference = { CrdtEntity.ReferenceImpl(it.id) }
): CrdtEntity.Data = CrdtEntity.Data(versionMap.copy(), this, referenceBuilder)
21 changes: 0 additions & 21 deletions java/arcs/core/crdt/extension/BUILD

This file was deleted.

46 changes: 0 additions & 46 deletions java/arcs/core/crdt/extension/ConversionExtensions.kt

This file was deleted.

6 changes: 0 additions & 6 deletions java/arcs/core/storage/driver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ arcs_kt_library(
":ramdisk",
"//java/arcs/core/common",
"//java/arcs/core/crdt",
"//java/arcs/core/crdt/extension",
"//java/arcs/core/data",
"//java/arcs/core/data:rawentity",
"//java/arcs/core/data/util:data-util",
Expand All @@ -28,11 +27,9 @@ arcs_kt_library(
"//java/arcs/core/storage/driver/volatiles",
"//java/arcs/core/storage/driver/volatiles:volatile_impl",
"//java/arcs/core/storage/keys",
"//java/arcs/core/storage/referencemode",
"//java/arcs/core/type",
"//java/arcs/core/util",
"//third_party/java/androidx/annotation",
"//third_party/kotlin/kotlinx_atomicfu",
"//third_party/kotlin/kotlinx_coroutines",
],
)
Expand All @@ -56,7 +53,6 @@ arcs_kt_library(
deps = [
"//java/arcs/core/common",
"//java/arcs/core/crdt",
"//java/arcs/core/crdt/extension",
"//java/arcs/core/data",
"//java/arcs/core/data:rawentity",
"//java/arcs/core/data/util:data-util",
Expand All @@ -67,11 +63,9 @@ arcs_kt_library(
"//java/arcs/core/storage/driver/volatiles",
"//java/arcs/core/storage/driver/volatiles:volatile_impl",
"//java/arcs/core/storage/keys",
"//java/arcs/core/storage/referencemode",
"//java/arcs/core/type",
"//java/arcs/core/util",
"//third_party/java/androidx/annotation",
"//third_party/kotlin/kotlinx_atomicfu",
"//third_party/kotlin/kotlinx_coroutines",
],
)
2 changes: 1 addition & 1 deletion java/arcs/core/storage/driver/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import arcs.core.crdt.CrdtEntity
import arcs.core.crdt.CrdtSet
import arcs.core.crdt.CrdtSingleton
import arcs.core.crdt.VersionMap
import arcs.core.crdt.extension.toCrdtEntityData
import arcs.core.crdt.toCrdtEntityData
import arcs.core.data.RawEntity
import arcs.core.data.Schema
import arcs.core.data.util.ReferencableList
Expand Down
41 changes: 40 additions & 1 deletion javatests/arcs/core/crdt/CrdtEntityTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import arcs.core.crdt.CrdtEntity.Operation.RemoveFromSet
import arcs.core.crdt.CrdtEntity.Operation.SetSingleton
import arcs.core.crdt.CrdtEntity.ReferenceImpl as Reference
import arcs.core.data.RawEntity
import arcs.core.data.util.toReferencable
import com.google.common.truth.Truth.assertThat
import kotlin.test.assertFailsWith
import org.junit.Ignore
Expand Down Expand Up @@ -470,8 +471,46 @@ class CrdtEntityTest {
val emptyEntity = CrdtEntity(VersionMap(), emptyRawEntity)

val changes = emptyEntity.merge(entity.data)
println(changes)
assertThat(changes.modelChange.isEmpty()).isFalse()
assertThat(changes.otherChange.isEmpty()).isTrue()
}

@Test
fun toCrdtEntityData_empty() {
assertThat(RawEntity().toCrdtEntityData(VersionMap())).isEqualTo(CrdtEntity.Data())
}

@Test
fun toCrdtEntityData_populated() {
val entity = RawEntity(
id = "entityId",
singletons = mapOf(
"txt" to "foo".toReferencable(),
"ent" to RawEntity("id1", mapOf("val" to 3.toReferencable()))
),
collections = mapOf(
"num" to setOf(3.toReferencable(), 7.toReferencable())
),
creationTimestamp = 30,
expirationTimestamp = 70
)
val versionMap = VersionMap("me" to 1)
val result = entity.toCrdtEntityData(versionMap) { CrdtEntity.ReferenceImpl("#" + it.id) }

// Constructing the full expected CrdtEntity.Data is cumbersome; just check the fields manually.
assertThat(result.id).isEqualTo("entityId")
assertThat(result.versionMap).isEqualTo(versionMap)
assertThat(result.creationTimestamp).isEqualTo(30)
assertThat(result.expirationTimestamp).isEqualTo(70)

val singletons = result.singletons
assertThat(singletons).hasSize(2)
assertThat(singletons["txt"]!!.consumerView!!.id).isEqualTo("#Primitive<kotlin.String>(foo)")
assertThat(singletons["ent"]!!.consumerView!!.id).isEqualTo("#id1")

val collections = result.collections
assertThat(collections).hasSize(1)
assertThat(collections["num"]!!.consumerView.map { it.id })
.containsExactly("#Primitive<kotlin.Int>(3)", "#Primitive<kotlin.Int>(7)")
}
}
2 changes: 0 additions & 2 deletions javatests/arcs/core/storage/driver/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ arcs_kt_jvm_test_suite(
deps = [
"//java/arcs/core/common",
"//java/arcs/core/crdt",
"//java/arcs/core/crdt/extension",
"//java/arcs/core/data",
"//java/arcs/core/data:schema_fields",
"//java/arcs/core/data/util:data-util",
Expand All @@ -26,7 +25,6 @@ arcs_kt_jvm_test_suite(
"//java/arcs/core/storage/keys",
"//java/arcs/core/testutil",
"//java/arcs/core/type",
"//java/arcs/core/util",
"//java/arcs/core/util/testutil",
"//java/arcs/jvm/storage/database/testutil",
"//third_party/java/junit:junit-android",
Expand Down
2 changes: 1 addition & 1 deletion javatests/arcs/core/storage/driver/DatabaseDriverTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import arcs.core.crdt.CrdtEntity.Reference.Companion.buildReference
import arcs.core.crdt.CrdtSet
import arcs.core.crdt.CrdtSingleton
import arcs.core.crdt.VersionMap
import arcs.core.crdt.extension.toCrdtEntityData
import arcs.core.crdt.toCrdtEntityData
import arcs.core.data.FieldType
import arcs.core.data.RawEntity
import arcs.core.data.Schema
Expand Down

0 comments on commit bd88941

Please sign in to comment.