|
| 1 | +// |
| 2 | +// Copyright (C) 2025 IOTech Ltd |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +package common |
| 7 | + |
| 8 | +import ( |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +type TestItem struct { |
| 15 | + Name string |
| 16 | + UniqueArray []string `validate:"unique"` |
| 17 | + UniqueSliceOfStruct []TestItem `validate:"unique=Name"` // only one field is supported |
| 18 | +} |
| 19 | + |
| 20 | +// The test only covers the validation of the "unique" tag at the time of writing this comment. |
| 21 | +// TODO: Add more tests to cover other validation tags if needed |
| 22 | + |
| 23 | +// TestValidate tests the Validate function |
| 24 | +func TestValidate(t *testing.T) { |
| 25 | + |
| 26 | + validUniqueArray := TestItem{ |
| 27 | + UniqueArray: []string{"a", "b", "c"}, |
| 28 | + } |
| 29 | + invalidUniqueArray := TestItem{ |
| 30 | + UniqueArray: []string{"a", "a", "c"}, |
| 31 | + } |
| 32 | + validUniqueSliceOfSturct := TestItem{ |
| 33 | + UniqueSliceOfStruct: []TestItem{ |
| 34 | + {Name: "a"}, |
| 35 | + {Name: "b"}, |
| 36 | + }, |
| 37 | + } |
| 38 | + invalidUniqueSliceOfSturct := TestItem{ |
| 39 | + UniqueSliceOfStruct: []TestItem{ |
| 40 | + {Name: "a"}, |
| 41 | + {Name: "a"}, |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + tests := []struct { |
| 46 | + name string |
| 47 | + input any |
| 48 | + expected bool |
| 49 | + }{ |
| 50 | + {"valid, unique tag for arrays and slices", validUniqueArray, false}, |
| 51 | + {"invalid, unique tag for arrays and slices", invalidUniqueArray, true}, |
| 52 | + {"valid, unique tag for slices of struct", validUniqueSliceOfSturct, false}, |
| 53 | + {"invalid, unique tag for slices of struct", invalidUniqueSliceOfSturct, true}, |
| 54 | + } |
| 55 | + for _, tt := range tests { |
| 56 | + t.Run(tt.name, func(t *testing.T) { |
| 57 | + err := Validate(tt.input) |
| 58 | + if tt.expected { |
| 59 | + require.Error(t, err) |
| 60 | + } |
| 61 | + }) |
| 62 | + } |
| 63 | +} |
0 commit comments