1
1
package org.modelix.model.sync
2
2
3
- import org.junit.jupiter.api.Assertions.assertEquals
4
3
import org.junit.jupiter.api.BeforeAll
5
4
import org.junit.jupiter.api.Test
6
5
import org.junit.jupiter.api.assertDoesNotThrow
@@ -9,19 +8,20 @@ import org.modelix.model.api.PBranch
9
8
import org.modelix.model.api.getRootNode
10
9
import org.modelix.model.client.IdGenerator
11
10
import org.modelix.model.data.ModelData
12
- import org.modelix.model.data.NodeData
13
- import org.modelix.model.data.asData
14
11
import org.modelix.model.lazy.CLTree
15
12
import org.modelix.model.lazy.ObjectStoreCache
16
13
import org.modelix.model.persistent.MapBaseStore
17
14
import java.io.File
15
+ import kotlin.test.assertEquals
16
+ import kotlin.test.fail
18
17
19
18
class ModelImporterTest {
20
19
21
20
companion object {
22
21
private lateinit var model: ModelData
23
22
private lateinit var newModel: ModelData
24
23
private lateinit var branch: IBranch
24
+ private lateinit var importer: ModelImporter
25
25
26
26
@JvmStatic
27
27
@BeforeAll
@@ -37,7 +37,7 @@ class ModelImporterTest {
37
37
model.load(branch)
38
38
// println("PRE-SPEC ${model.toJson()}")
39
39
// println("PRE-LOADED ${branch.getRootNode().toJson()}")
40
- ModelImporter (branch.getRootNode()). import(newModelFile)
40
+ importer = ModelImporter (branch.getRootNode(), ImportStats ()). apply { import(newModelFile) }
41
41
// println("POST-SPEC ${newModel.root.toJson()}")
42
42
// println("POST-LOADED ${branch.getRootNode().toJson()}")
43
43
}
@@ -48,10 +48,8 @@ class ModelImporterTest {
48
48
fun `can sync properties` () {
49
49
branch.runRead {
50
50
val expectedNode = newModel.root.children[0 ]
51
- val expectedProperties = expectedNode.properties
52
- val actualProperties = branch.getRootNode().allChildren.first().asData().properties
53
- assertEquals(expectedProperties, actualProperties.filterKeys { it != NodeData .idPropertyKey })
54
- assertEquals(expectedNode.id, actualProperties[NodeData .idPropertyKey])
51
+ val actualNode = branch.getRootNode().allChildren.first()
52
+ assertNodePropertiesConformToSpec(expectedNode, actualNode)
55
53
}
56
54
}
57
55
@@ -60,24 +58,55 @@ class ModelImporterTest {
60
58
branch.runRead {
61
59
val node0 = branch.getRootNode().allChildren.first()
62
60
val node1 = branch.getRootNode().allChildren.toList()[1 ]
61
+ val node1Spec = newModel.root.children[1 ]
63
62
64
63
assertEquals(node1, node0.getReferenceTarget(" sibling" ))
65
64
assertEquals(node0, node1.getReferenceTarget(" sibling" ))
66
65
assertEquals(branch.getRootNode(), node1.getReferenceTarget(" root" ))
66
+
67
+ assertNodeReferencesConformToSpec(node1Spec, node1)
67
68
}
68
69
}
69
70
70
71
@Test
71
72
fun `can sync children` () {
72
73
branch.runRead {
73
74
val index = 2
74
- val node = branch.getRootNode().allChildren.toList()[index]
75
- val specifiedOrder = newModel.root.children[index].children.map { it.properties[NodeData .idPropertyKey] ? : it.id }
76
- val actualOrder = node.allChildren.map { it.getPropertyValue(NodeData .idPropertyKey) }
77
- assertEquals(specifiedOrder, actualOrder)
75
+ val actualNode = branch.getRootNode().allChildren.toList()[index]
76
+ val specifiedNode = newModel.root.children[index]
77
+ assertNodeChildOrderConformsToSpec(specifiedNode, actualNode)
78
78
}
79
79
}
80
80
81
+ @Test
82
+ fun `model conforms to spec` () {
83
+ branch.runRead {
84
+ assertAllNodeConformToSpec(newModel.root, branch.getRootNode())
85
+ }
86
+ }
87
+
88
+ @Test
89
+ fun `uses minimal amount of operations` () {
90
+ val stats = importer.stats ? : fail(" No import stats found." )
91
+ assertEquals(1 , stats.additions.size)
92
+ assertEquals(1 , stats.deletions.size)
93
+ assertEquals(4 , stats.moves.size)
94
+ assertEquals(4 , stats.propertyChanges.size)
95
+ assertEquals(3 , stats.referenceChanges.size)
96
+ }
97
+
98
+ @Test
99
+ fun `operations do not overlap` () {
100
+ val stats = importer.stats ? : fail(" No import stats found." )
101
+ val additionsSet = stats.additions.toSet()
102
+ val deletionsSet = stats.deletions.toSet()
103
+ val movesSet = stats.moves.toSet()
104
+
105
+ assert (additionsSet.intersect(deletionsSet).isEmpty())
106
+ assert (deletionsSet.intersect(movesSet).isEmpty())
107
+ assert (movesSet.intersect(additionsSet).isEmpty())
108
+ }
109
+
81
110
@Test
82
111
fun `can bulk import` () {
83
112
val tree = CLTree (ObjectStoreCache (MapBaseStore ()))
0 commit comments