laskoviymishka commented on code in PR #2762: URL: https://github.com/apache/iceberg-go/pull/2762#discussion_r4746536333
########## schema_verification_test.go: ########## @@ -0,0 +1,199 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package iceberg_test + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "reflect" + "slices" + "testing" + + "github.com/apache/iceberg-go" + "github.com/stretchr/testify/require" +) + +// schemaFixturesSkip names schema cases iceberg-go does not pass yet, with +// the reason. Listed cases still run, and fail once they pass so the entry +// gets removed. +var schemaFixturesSkip = map[string]string{ + "unknown/reject-unknown-required": "accepts a required unknown field", +} + +type schemaFixtureCase struct { + ID string `json:"id"` + Valid bool `json:"valid"` + Input json.RawMessage `json:"input"` + Decoded json.RawMessage `json:"decoded"` +} + +type decodedSchema struct { + SchemaID int `json:"schema-id"` + IdentifierFieldIDs []int `json:"identifier-field-ids"` + Fields []map[string]any `json:"fields"` +} + +// TestVerificationSchema runs the table-spec/schema cases of an +// iceberg-verification checkout, as table-spec/schema/README.md describes. +func TestVerificationSchema(t *testing.T) { + dir := os.Getenv("ICEBERG_VERIFICATION_DIR") + if dir == "" { + t.Skip("ICEBERG_VERIFICATION_DIR is not set") + } + + seen := make(map[string]bool) + for _, surface := range []string{"core", "unknown"} { + data, err := os.ReadFile(filepath.Join(dir, "table-spec", "schema", surface, "cases.json")) + require.NoError(t, err) + var file struct { + Cases []schemaFixtureCase `json:"cases"` + } + require.NoError(t, json.Unmarshal(data, &file), surface) + require.NotEmpty(t, file.Cases, surface) + + for _, c := range file.Cases { + name := surface + "/" + c.ID + seen[name] = true + t.Run(name, func(t *testing.T) { + err := checkSchemaFixtureCase(c) + if reason, ok := schemaFixturesSkip[name]; ok { + if err == nil { + t.Fatal("case passes now; remove it from schemaFixturesSkip") + } + t.Skipf("%s: %v", reason, err) + } + if err != nil { + t.Error(err) + } + }) + } + } + + for name := range schemaFixturesSkip { + if !seen[name] { + t.Errorf("schemaFixturesSkip names %s, which is not a case", name) + } + } +} + +func checkSchemaFixtureCase(c schemaFixtureCase) error { + // Schema.UnmarshalJSON is what table metadata uses to read "schemas". + var sc iceberg.Schema + err := json.Unmarshal(c.Input, &sc) + if !c.Valid { + if err == nil { + return fmt.Errorf("input parsed as %s, want a rejection", &sc) + } + + return nil + } + if err != nil { + return fmt.Errorf("input: %w", err) + } + + var want decodedSchema + if err := json.Unmarshal(c.Decoded, &want); err != nil { + return err + } + got, err := decodedSchemaOf(&sc) + if err != nil { + return err + } + + var diffs []error + if got.SchemaID != want.SchemaID { + diffs = append(diffs, fmt.Errorf("schema-id: got %d, want %d", got.SchemaID, want.SchemaID)) + } + gotIDs, wantIDs := slices.Sorted(slices.Values(got.IdentifierFieldIDs)), slices.Sorted(slices.Values(want.IdentifierFieldIDs)) + if !slices.Equal(gotIDs, wantIDs) { + diffs = append(diffs, fmt.Errorf("identifier-field-ids: got %v, want %v", gotIDs, wantIDs)) + } + if len(got.Fields) != len(want.Fields) { + diffs = append(diffs, fmt.Errorf("fields: got %d rows, want %d", len(got.Fields), len(want.Fields))) + } + for i := range min(len(got.Fields), len(want.Fields)) { + for _, key := range []string{"id", "name", "parent", "required", "type", "doc"} { Review Comment: Fixed in c23c7f6b5d -- 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]
