|
| 1 | +/* |
| 2 | + * Copyright 2026 Redpanda Data, Inc. |
| 3 | + * |
| 4 | + * Licensed as a Redpanda Enterprise file under the Redpanda Community |
| 5 | + * License (the "License"); you may not use this file except in compliance with |
| 6 | + * the License. You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://github.com/redpanda-data/redpanda/blob/master/licenses/rcl.md |
| 9 | + */ |
| 10 | + |
| 11 | +package icebergx |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + "github.com/apache/iceberg-go" |
| 17 | + "github.com/stretchr/testify/assert" |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | +) |
| 20 | + |
| 21 | +func TestGoValueToLiteralDecimal(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + bytes []byte |
| 25 | + iceType iceberg.Type |
| 26 | + wantStr string |
| 27 | + wantScale int |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "positive decimal(20,0)", |
| 31 | + bytes: []byte{0x30, 0x39}, // 12345 big-endian two's complement |
| 32 | + iceType: iceberg.DecimalTypeOf(20, 0), |
| 33 | + wantStr: "12345", |
| 34 | + wantScale: 0, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "negative decimal(10,2)", |
| 38 | + bytes: []byte{0xFF}, // -1 unscaled, scale 2 => -0.01 |
| 39 | + iceType: iceberg.DecimalTypeOf(10, 2), |
| 40 | + wantStr: "-0.01", |
| 41 | + wantScale: 2, |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "zero decimal(5,0)", |
| 45 | + bytes: []byte{0x00}, |
| 46 | + iceType: iceberg.DecimalTypeOf(5, 0), |
| 47 | + wantStr: "0", |
| 48 | + wantScale: 0, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range tests { |
| 53 | + t.Run(tt.name, func(t *testing.T) { |
| 54 | + lit, err := goValueToLiteral(tt.bytes, tt.iceType) |
| 55 | + require.NoError(t, err) |
| 56 | + |
| 57 | + decLit, ok := lit.(iceberg.DecimalLiteral) |
| 58 | + require.True(t, ok, "expected iceberg.DecimalLiteral, got %T", lit) |
| 59 | + assert.Equal(t, tt.wantScale, decLit.Scale) |
| 60 | + assert.Equal(t, tt.wantStr, lit.String()) |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
0 commit comments