Skip to content

Commit

Permalink
Clean up test
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-artie committed Apr 6, 2024
1 parent da372a2 commit 89e24a3
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions lib/debezium/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,81 +270,82 @@ func TestDecodeDecimal(t *testing.T) {

func TestDecodeDebeziumVariableDecimal(t *testing.T) {
type _testCase struct {
name string
value any
expectValue string
expectError string
expectScale int
name string
value any

expectedValue string
expectedScale int
expectedErr string
}

testCases := []_testCase{
{
name: "empty val (nil)",
expectError: "value is not map[string]any type",
expectedErr: "value is not map[string]any type",
},
{
name: "empty map",
value: map[string]any{},
expectError: "object is empty",
expectedErr: "object is empty",
},
{
name: "scale is not an integer",
value: map[string]any{
"scale": "foo",
},
expectError: "key: scale is not type integer:",
expectedErr: "key: scale is not type integer:",
},
{
name: "value exists (scale 3)",
value: map[string]any{
"scale": 3,
"value": "SOx4FQ==",
},
expectValue: "1223456.789",
expectScale: 3,
expectedValue: "1223456.789",
expectedScale: 3,
},
{
name: "value exists (scale 2)",
value: map[string]any{
"scale": 2,
"value": "MDk=",
},
expectValue: "123.45",
expectScale: 2,
expectedValue: "123.45",
expectedScale: 2,
},
{
name: "negative numbers (scale 7)",
value: map[string]any{
"scale": 7,
"value": "wT9Wmw==",
},
expectValue: "-105.2813669",
expectScale: 7,
expectedValue: "-105.2813669",
expectedScale: 7,
},
{
name: "malformed base64 value",
value: map[string]any{
"scale": 7,
"value": "==wT9Wmw==",
},
expectError: "failed to base64 decode",
expectedErr: "failed to base64 decode",
},
{
name: "[]byte value",
value: map[string]any{
"scale": 7,
"value": []byte{193, 63, 86, 155},
},
expectValue: "-105.2813669",
expectScale: 7,
expectedValue: "-105.2813669",
expectedScale: 7,
},
}

for _, testCase := range testCases {
field := Field{}
dec, err := field.DecodeDebeziumVariableDecimal(testCase.value)
if testCase.expectError != "" {
assert.ErrorContains(t, err, testCase.expectError, testCase.name)
if testCase.expectedErr != "" {
assert.ErrorContains(t, err, testCase.expectedErr, testCase.name)
continue
}

Expand All @@ -356,8 +357,8 @@ func TestDecodeDebeziumVariableDecimal(t *testing.T) {
_, isOk = dec.Value().(string)
assert.True(t, isOk, testCase.name)
assert.Equal(t, -1, *dec.Precision(), testCase.name)
assert.Equal(t, testCase.expectScale, dec.Scale(), testCase.name)
assert.Equal(t, testCase.expectValue, dec.Value(), testCase.name)
assert.Equal(t, testCase.expectedScale, dec.Scale(), testCase.name)
assert.Equal(t, testCase.expectedValue, dec.Value(), testCase.name)
}

}

0 comments on commit 89e24a3

Please sign in to comment.