|
| 1 | +/* |
| 2 | +Copyright 2025 Gravitational, Inc. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package events |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "strings" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/gogo/protobuf/types" |
| 25 | + "github.com/stretchr/testify/require" |
| 26 | +) |
| 27 | + |
| 28 | +func TestStructTrimToMaxSize(t *testing.T) { |
| 29 | + testCases := []struct { |
| 30 | + name string |
| 31 | + maxSize int |
| 32 | + in *Struct |
| 33 | + want *Struct |
| 34 | + }{ |
| 35 | + { |
| 36 | + name: "Field key exceeds max limit size", |
| 37 | + maxSize: 10, |
| 38 | + in: &Struct{ |
| 39 | + Struct: types.Struct{ |
| 40 | + Fields: map[string]*types.Value{ |
| 41 | + strings.Repeat("A", 100): { |
| 42 | + Kind: &types.Value_StringValue{ |
| 43 | + StringValue: "A", |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + want: &Struct{ |
| 50 | + Struct: types.Struct{ |
| 51 | + Fields: map[string]*types.Value{ |
| 52 | + strings.Repeat("A", 8): { |
| 53 | + Kind: &types.Value_StringValue{ |
| 54 | + StringValue: "A", |
| 55 | + }, |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, tc := range testCases { |
| 64 | + t.Run(tc.name, func(t *testing.T) { |
| 65 | + got := tc.in.trimToMaxSize(tc.maxSize) |
| 66 | + require.True(t, reflect.DeepEqual(got, tc.want)) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments