Add complete task test with sum of weights == 0
This commit is contained in:
parent
5d803bb01c
commit
b56738653d
@ -594,6 +594,38 @@ func TestCompleteTask(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompleteTaskAllowanceWeightsSumTo0(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
taskId := createTestTaskWithAmount(e, 101)
|
||||||
|
|
||||||
|
e.GET("/tasks").Expect().Status(200).JSON().Array().Length().IsEqual(1)
|
||||||
|
|
||||||
|
// Update rest allowance
|
||||||
|
e.PUT("/user/1/allowance/0").WithJSON(UpdateAllowanceRequest{
|
||||||
|
Weight: 0,
|
||||||
|
}).Expect().Status(200)
|
||||||
|
// Create two allowance goals
|
||||||
|
e.POST("/user/1/allowance").WithJSON(CreateAllowanceRequest{
|
||||||
|
Name: "Test Allowance 1",
|
||||||
|
Target: 1000,
|
||||||
|
Weight: 0,
|
||||||
|
}).Expect().Status(201)
|
||||||
|
|
||||||
|
// Complete the task
|
||||||
|
e.POST("/task/" + strconv.Itoa(taskId) + "/complete").Expect().Status(200)
|
||||||
|
|
||||||
|
// Verify the task is marked as completed
|
||||||
|
e.GET("/task/" + strconv.Itoa(taskId)).Expect().Status(404)
|
||||||
|
|
||||||
|
// Verify the allowances are updated for user 1
|
||||||
|
allowances := e.GET("/user/1/allowance").Expect().Status(200).JSON().Array()
|
||||||
|
allowances.Length().IsEqual(2)
|
||||||
|
allowances.Value(0).Object().Value("id").Number().IsEqual(0)
|
||||||
|
allowances.Value(0).Object().Value("progress").Number().IsEqual(101)
|
||||||
|
allowances.Value(1).Object().Value("id").Number().IsEqual(1)
|
||||||
|
allowances.Value(1).Object().Value("progress").Number().IsEqual(0)
|
||||||
|
}
|
||||||
|
|
||||||
func TestCompleteTaskInvalidId(t *testing.T) {
|
func TestCompleteTaskInvalidId(t *testing.T) {
|
||||||
e := startServer(t)
|
e := startServer(t)
|
||||||
e.POST("/task/999/complete").Expect().Status(404)
|
e.POST("/task/999/complete").Expect().Status(404)
|
||||||
@ -628,6 +660,16 @@ func TestCompleteAllowance(t *testing.T) {
|
|||||||
history.Value(1).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
history.Value(1).Object().Value("timestamp").String().AsDateTime().InRange(getDelta(time.Now(), 2.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCompleteAllowanceInvalidUserId(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
e.POST("/user/999/allowance/1/complete").Expect().Status(404)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCompleteAllowanceInvalidAllowanceId(t *testing.T) {
|
||||||
|
e := startServer(t)
|
||||||
|
e.POST("/user/1/allowance/999/complete").Expect().Status(404)
|
||||||
|
}
|
||||||
|
|
||||||
func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
|
func getDelta(base time.Time, delta float64) (time.Time, time.Time) {
|
||||||
start := base.Add(-time.Duration(delta) * time.Second)
|
start := base.Add(-time.Duration(delta) * time.Second)
|
||||||
end := base.Add(time.Duration(delta) * time.Second)
|
end := base.Add(time.Duration(delta) * time.Second)
|
||||||
|
@ -389,24 +389,26 @@ func (db *Db) CompleteTask(taskId int) error {
|
|||||||
|
|
||||||
remainingReward := reward
|
remainingReward := reward
|
||||||
|
|
||||||
// Distribute the reward to the allowances
|
if sumOfWeights > 0 {
|
||||||
for allowanceRow := range tx.Query("select id, weight from allowances where user_id = ? and weight > 0").Bind(userId).Range(&err) {
|
// Distribute the reward to the allowances
|
||||||
var allowanceId int
|
for allowanceRow := range tx.Query("select id, weight from allowances where user_id = ? and weight > 0").Bind(userId).Range(&err) {
|
||||||
var allowanceWeight float64
|
var allowanceId int
|
||||||
err = allowanceRow.Scan(&allowanceId, &allowanceWeight)
|
var allowanceWeight float64
|
||||||
if err != nil {
|
err = allowanceRow.Scan(&allowanceId, &allowanceWeight)
|
||||||
return err
|
if err != nil {
|
||||||
}
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// Calculate the amount to add to the allowance
|
// Calculate the amount to add to the allowance
|
||||||
amount := int((allowanceWeight / sumOfWeights) * float64(remainingReward))
|
amount := int((allowanceWeight / sumOfWeights) * float64(remainingReward))
|
||||||
sumOfWeights -= allowanceWeight
|
sumOfWeights -= allowanceWeight
|
||||||
err = tx.Query("update allowances set balance = balance + ? where id = ? and user_id = ?").
|
err = tx.Query("update allowances set balance = balance + ? where id = ? and user_id = ?").
|
||||||
Bind(amount, allowanceId, userId).Exec()
|
Bind(amount, allowanceId, userId).Exec()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
|
remainingReward -= amount
|
||||||
}
|
}
|
||||||
remainingReward -= amount
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add the remaining reward to the user
|
// Add the remaining reward to the user
|
||||||
|
Loading…
x
Reference in New Issue
Block a user