laskoviymishka commented on code in PR #1647:
URL: https://github.com/apache/iceberg-go/pull/1647#discussion_r3740294219
##########
partitions_test.go:
##########
@@ -778,6 +677,63 @@ func TestPartitionFieldUnmarshalJSON(t *testing.T) {
})
}
+func TestPartitionFieldUnmarshalPreservesStateOnError(t *testing.T) {
+ for _, test := range []struct {
+ name string
+ data string
+ }{
+ {
+ name: "invalid transform",
+ data:
`{"source-id":1,"field-id":1000,"transform":"not-a-transform","name":"new"}`,
+ },
+ {
+ name: "non-positive source ID",
+ data:
`{"source-id":0,"field-id":1000,"transform":"identity","name":"new"}`,
+ },
+ {
+ name: "missing source ID",
+ data:
`{"field-id":1000,"transform":"identity","name":"new"}`,
+ },
+ {
+ name: "empty name",
+ data:
`{"source-id":1,"field-id":1000,"transform":"identity","name":""}`,
+ },
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ initial := iceberg.PartitionField{
+ SourceIDs: []int{7},
+ FieldID: 1007,
+ Name: "old",
+ Transform: iceberg.IdentityTransform{},
+ }
+ field := initial
+ require.Error(t, json.Unmarshal([]byte(test.data),
&field))
+ assert.Equal(t, initial, field)
+ })
+ }
+}
+
+func TestPartitionFieldUnmarshalReplacesStateOnSuccess(t *testing.T) {
+ spec := iceberg.NewPartitionSpecID(0, iceberg.PartitionField{
+ SourceIDs: []int{1},
+ FieldID: 1000,
+ Name: "old field",
+ Transform: iceberg.IdentityTransform{},
+ })
+ field := spec.Field(0)
+
+ require.NoError(t, json.Unmarshal([]byte(`{
+ "source-id": 2,
+ "field-id": 1001,
+ "transform": "identity",
+ "name": "new field"
+ }`), &field))
+
+ assert.Equal(t, []int{2}, field.SourceIDs)
+ assert.Equal(t, "new field", field.Name)
+ assert.Equal(t, "new+field", field.EscapedName())
Review Comment:
building on zeroshade's point that this test really rests on `EscapedName()`
— I'd also pin `FieldID` and `Transform` here. On the reverted build
`SourceIDs`/`Name` both still pass, so a regression that cleared the cache but
copied the wrong `FieldID` or left a stale `Transform` would slip through, and
this is the one test covering `*p = next`.
```go
assert.Equal(t, 1001, field.FieldID)
assert.Equal(t, iceberg.IdentityTransform{}, field.Transform)
```
wdyt?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]