18
18
*/
19
19
package biz.ganttproject.storage
20
20
21
- import biz.ganttproject.customproperty.CustomPropertyClass
22
- import biz.ganttproject.customproperty.CustomPropertyHolder
23
- import biz.ganttproject.customproperty.CustomPropertyManager
24
- import biz.ganttproject.customproperty.SimpleSelect
21
+ import biz.ganttproject.customproperty.*
25
22
import biz.ganttproject.storage.db.tables.Task
26
23
import net.sourceforge.ganttproject.TestSetupHelper
24
+ import net.sourceforge.ganttproject.resource.HumanResourceManager
27
25
import net.sourceforge.ganttproject.storage.*
26
+ import net.sourceforge.ganttproject.task.CostStub
28
27
import net.sourceforge.ganttproject.task.TaskManager
29
28
import org.h2.jdbcx.JdbcDataSource
30
29
import org.jooq.SQLDialect
@@ -34,6 +33,8 @@ import org.junit.jupiter.api.Assertions.assertEquals
34
33
import org.junit.jupiter.api.BeforeEach
35
34
import org.junit.jupiter.api.Test
36
35
import org.junit.jupiter.api.assertThrows
36
+ import org.w3c.util.DateParser
37
+ import java.math.BigDecimal
37
38
import java.sql.SQLException
38
39
import java.util.*
39
40
import javax.sql.DataSource
@@ -82,7 +83,7 @@ class CalculatedPropertyTest {
82
83
@Test
83
84
fun `calculated property value` () {
84
85
val foo = customPropertyManager.createDefinition(CustomPropertyClass .TEXT , " foo" )
85
- val bar = customPropertyManager.createDefinition(CustomPropertyClass .INTEGER , " bar" ).also {
86
+ val bar = customPropertyManager.createDefinition(CustomPropertyClass .INTEGER , " bar" ).also {
86
87
it.calculationMethod = SimpleSelect (it.id, " duration + 1" , resultClass = CustomPropertyClass .INTEGER .javaClass)
87
88
}
88
89
val baz = customPropertyManager.createDefinition(CustomPropertyClass .TEXT , " baz" ).also {
@@ -99,17 +100,50 @@ class CalculatedPropertyTest {
99
100
assertEquals(1 , tasks.size)
100
101
101
102
val propertyHolders = createPropertyHolders(taskManager)
102
- val updaters = customPropertyManager.definitions.map {def ->
103
- ColumnConsumer (SimpleSelect (def.id, def.id, resultClass = def.type)) { taskNum, value ->
104
- propertyHolders[taskNum]?.setValue(def, value)
105
- }
106
- }.toList()
107
- projectDatabase.mapTasks(* (updaters.toTypedArray()))
103
+ val updater = CalculatedPropertyUpdater (projectDatabase, {customPropertyManager}, {propertyHolders})
104
+ updater.update()
108
105
109
106
assertEquals(2 , task.customValues.getValue(bar))
110
107
assertEquals(" foo--" , task.customValues.getValue(baz))
111
108
}
112
109
110
+ @Test
111
+ fun `builtin calculated property value` () {
112
+ val task = taskManager.newTaskBuilder().withName(" task1" ).withStartDate(Date ()).build()
113
+ projectDatabase.insertTask(task)
114
+
115
+ H2Functions .taskManager.set(taskManager)
116
+ task.createMutator().let {
117
+ it.setCost(CostStub (BigDecimal .ZERO , true ))
118
+ it.setDuration(taskManager.createLength(2 ))
119
+ it.commit()
120
+ }
121
+
122
+ val updater = CalculatedPropertyUpdater (projectDatabase, {customPropertyManager}, { emptyMap() })
123
+ updater.update()
124
+
125
+ DSL .using(dataSource, SQLDialect .H2 ).also {
126
+ val tasks = it.selectFrom(Task .TASK ).fetch()
127
+ assertEquals(1 , tasks.size)
128
+ assertEquals(task.end.time, DateParser .toJavaDate(tasks[0 ].endDate))
129
+ }
130
+
131
+ val humanResourceManager = HumanResourceManager (null , CustomColumnsManager ())
132
+ val resource = humanResourceManager.newResourceBuilder().withName(" foo" ).withID(1 ).withStandardRate(BigDecimal .valueOf(100 )).build()
133
+ task.assignmentCollection.addAssignment(resource).also {
134
+ it.load = 100f ;
135
+ }
136
+
137
+ updater.update()
138
+ DSL .using(dataSource, SQLDialect .H2 ).also {
139
+ val tasks = it.selectFrom(Task .TASK ).fetch()
140
+ assertEquals(1 , tasks.size)
141
+ assertEquals(task.cost.value.toDouble(), tasks[0 ].cost.toDouble())
142
+ assertEquals(200.0 , tasks[0 ].cost.toDouble())
143
+ }
144
+
145
+ }
146
+
113
147
@Test
114
148
fun `column used in a generated column can't be dropped` () {
115
149
customPropertyManager.createDefinition(CustomPropertyClass .INTEGER , " bar" ).also {
0 commit comments