Skip to content

Commit

Permalink
Add test for division by zero error handling
Browse files Browse the repository at this point in the history
This commit adds a new test case, `testDivisionByZero`, to ensure that the system correctly handles division by zero errors during index change calculations. The test sets up a primitive stream and verifies that an error is raised when division by zero occurs, aligning with our expected behavior.
  • Loading branch information
outerlook committed Sep 24, 2024
1 parent 6e298c7 commit 77780a7
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions internal/contracts/tests/index_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestIndexChange(t *testing.T) {
FunctionTests: []kwilTesting.TestFunc{
withTestIndexChangeSetup(testIndexChange(t)),
withTestIndexChangeSetup(testYoYIndexChange(t)),
testDivisionByZero(t),
},
})
}
Expand Down Expand Up @@ -204,3 +205,42 @@ func testYoYIndexChange(t *testing.T) func(ctx context.Context, platform *kwilTe
return nil
}
}

// testing division by zero
// we expect this error to happen, unless our production data expects a different behavior
func testDivisionByZero(t *testing.T) func(ctx context.Context, platform *kwilTesting.Platform) error {
return func(ctx context.Context, platform *kwilTesting.Platform) error {
streamName := "primitive_stream_db_name"
streamId := util.GenerateStreamId(streamName)
dbid := utils.GenerateDBID(streamId.String(), platform.Deployer)

if err := setup.SetupPrimitiveFromMarkdown(ctx, setup.MarkdownPrimitiveSetupInput{
Platform: platform,
Height: 0,
PrimitiveStreamName: streamName,
MarkdownData: `
| date | value |
|------------|--------|
| 2023-01-01 | 100.00 |
| 2023-01-02 | 0.00 |
| 2023-01-03 | 103.00 |
`,
}); err != nil {
return errors.Wrap(err, "error setting up primitive stream")
}

_, err := platform.Engine.Procedure(ctx, platform.DB, &common.ExecutionData{
Procedure: "get_index_change",
Dataset: dbid,
Args: []any{"2023-01-01", "2023-01-03", nil, nil, 1},
TransactionData: common.TransactionData{
Signer: platform.Deployer,
TxID: platform.Txid(),
Height: 0,
},
})

assert.Error(t, err, "division by zero")
return nil
}
}

0 comments on commit 77780a7

Please sign in to comment.