Skip to content

Commit ee7b6d2

Browse files
committed
test(model-sync-lib): added test case for random changes
1 parent 5e4e570 commit ee7b6d2

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

model-sync-lib/src/test/kotlin/org/modelix/model/sync/ModelImporterTest.kt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import org.modelix.model.api.PBranch
88
import org.modelix.model.api.getRootNode
99
import org.modelix.model.client.IdGenerator
1010
import org.modelix.model.data.ModelData
11+
import org.modelix.model.data.NodeData
1112
import org.modelix.model.lazy.CLTree
1213
import org.modelix.model.lazy.ObjectStoreCache
1314
import org.modelix.model.persistent.MapBaseStore
15+
import org.modelix.model.test.RandomModelChangeGenerator
1416
import java.io.File
17+
import kotlin.random.Random
1518
import kotlin.test.assertEquals
1619
import kotlin.test.fail
1720

@@ -81,7 +84,7 @@ class ModelImporterTest {
8184
@Test
8285
fun `model conforms to spec`() {
8386
branch.runRead {
84-
assertAllNodeConformToSpec(newModel.root, branch.getRootNode())
87+
assertAllNodesConformToSpec(newModel.root, branch.getRootNode())
8588
}
8689
}
8790

@@ -122,4 +125,43 @@ class ModelImporterTest {
122125
}
123126
}
124127
}
128+
129+
@Test
130+
fun `can handle random changes`() {
131+
val tree0 = CLTree(ObjectStoreCache(MapBaseStore()))
132+
val branch0 = PBranch(tree0, IdGenerator.getInstance(1))
133+
134+
val seed = Random.nextInt()
135+
println("Seed for random change test: $seed")
136+
lateinit var initialState: NodeData
137+
lateinit var specification: NodeData
138+
val numChanges = 50
139+
140+
branch0.runWrite {
141+
val rootNode = branch0.getRootNode()
142+
val grower = RandomModelChangeGenerator(rootNode, Random(seed)).growingOperationsOnly()
143+
for (i in 1..50) {
144+
grower.applyRandomChange()
145+
}
146+
initialState = rootNode.asExported()
147+
148+
val changer = RandomModelChangeGenerator(rootNode, Random(seed))
149+
for (i in 1..numChanges) {
150+
changer.applyRandomChange()
151+
}
152+
specification = rootNode.asExported()
153+
}
154+
155+
val tree1 = CLTree(ObjectStoreCache(MapBaseStore()))
156+
val branch1 = PBranch(tree1, IdGenerator.getInstance(1))
157+
158+
branch1.runWrite {
159+
val importer = ModelImporter(branch1.getRootNode(), ImportStats())
160+
importer.import(ModelData(root = initialState))
161+
importer.import(ModelData(root = specification))
162+
163+
assertAllNodesConformToSpec(specification, branch1.getRootNode())
164+
assert(importer.stats!!.getTotal() <= numChanges)
165+
}
166+
}
125167
}

model-sync-lib/src/test/kotlin/org/modelix/model/sync/SyncTestUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fun NodeData.toJson() : String {
2121
return SyncTestUtil.json.encodeToString(this)
2222
}
2323

24-
internal fun assertAllNodeConformToSpec(expectedRoot: NodeData, actualRoot: INode) {
24+
internal fun assertAllNodesConformToSpec(expectedRoot: NodeData, actualRoot: INode) {
2525
assertNodeConformsToSpec(expectedRoot, actualRoot)
2626
for ((expectedChild, actualChild) in expectedRoot.children zip actualRoot.allChildren) {
2727
assertNodeConformsToSpec(expectedChild, actualChild)

0 commit comments

Comments
 (0)