go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tools/cmd/bqschemaupdater/main_test.go (about) 1 // Copyright 2018 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "context" 19 "os" 20 "testing" 21 22 "google.golang.org/protobuf/proto" 23 "google.golang.org/protobuf/types/descriptorpb" 24 25 "cloud.google.com/go/bigquery" 26 . "github.com/smartystreets/goconvey/convey" 27 ) 28 29 func TestBQSchemaUpdater(t *testing.T) { 30 ctx := context.Background() 31 Convey("Update", t, func() { 32 ts := localTableStore{} 33 datasetID := "test_dataset" 34 tableID := "test_table" 35 36 field := &bigquery.FieldSchema{ 37 Name: "test_field", 38 Description: "test description", 39 Type: bigquery.StringFieldType, 40 } 41 anotherField := &bigquery.FieldSchema{ 42 Name: "field_2", 43 Description: "another field", 44 Type: bigquery.StringFieldType, 45 } 46 tcs := []bigquery.Schema{ 47 {field}, 48 {field, anotherField}, 49 } 50 for _, tc := range tcs { 51 td := tableDef{ 52 DataSetID: "test_dataset", 53 TableID: tableID, 54 Schema: tc, 55 } 56 err := updateFromTableDef(ctx, true, ts, td) 57 So(err, ShouldBeNil) 58 got, err := ts.getTableMetadata(ctx, datasetID, tableID) 59 So(err, ShouldBeNil) 60 want := &bigquery.TableMetadata{ 61 Schema: tc, 62 TimePartitioning: &bigquery.TimePartitioning{}, 63 } 64 So(got, ShouldResemble, want) 65 } 66 }) 67 Convey("Schema", t, func() { 68 descBytes, err := os.ReadFile("testdata/event.desc") 69 So(err, ShouldBeNil) 70 var desc descriptorpb.FileDescriptorSet 71 So(proto.Unmarshal(descBytes, &desc), ShouldBeNil) 72 schema, description, err := schemaFromMessage(&desc, "testdata.BuildEvent") 73 So(err, ShouldBeNil) 74 So(description, ShouldEqual, "Build events.\n\nLine after blank line.") 75 ioSchema := bigquery.Schema{ 76 { 77 Name: "properties", 78 Type: bigquery.RecordFieldType, 79 Repeated: true, 80 Schema: bigquery.Schema{ 81 { 82 Name: "name", 83 Type: bigquery.StringFieldType, 84 }, 85 { 86 Name: "value_json", 87 Type: bigquery.StringFieldType, 88 }, 89 }, 90 }, 91 } 92 So(schema, ShouldResemble, bigquery.Schema{ 93 { 94 Name: "build_id", 95 Description: "Universal build id.", 96 Type: bigquery.StringFieldType, 97 }, 98 { 99 Name: "builder", 100 Description: "Builder name.", 101 Type: bigquery.StringFieldType, 102 }, 103 { 104 Name: "status", 105 Description: "Valid values: SUCCESS, FAILURE, ERROR.", 106 Type: bigquery.StringFieldType, 107 }, 108 { 109 Name: "input", 110 Type: bigquery.RecordFieldType, 111 Schema: ioSchema, 112 }, 113 { 114 Name: "output", 115 Type: bigquery.RecordFieldType, 116 Schema: ioSchema, 117 }, 118 { 119 Name: "timestamp", 120 Type: bigquery.TimestampFieldType, 121 }, 122 { 123 Name: "struct", 124 Type: bigquery.StringFieldType, 125 }, 126 { 127 Name: "duration", 128 Type: bigquery.FloatFieldType, 129 }, 130 { 131 Name: "bq_type_override", 132 Type: bigquery.TimestampFieldType, 133 }, 134 }) 135 }) 136 }