|
| 1 | +package core_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/alecthomas/assert" |
| 10 | + "github.com/scaleway/scaleway-cli/v2/internal/core" |
| 11 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 12 | +) |
| 13 | + |
| 14 | +type RequestEmbedding struct { |
| 15 | + EmbeddingField1 string |
| 16 | + EmbeddingField2 int |
| 17 | +} |
| 18 | + |
| 19 | +type CreateRequest struct { |
| 20 | + *RequestEmbedding |
| 21 | + CreateField1 string |
| 22 | + CreateField2 int |
| 23 | +} |
| 24 | + |
| 25 | +type ExtendedRequest struct { |
| 26 | + *CreateRequest |
| 27 | + ExtendedField1 string |
| 28 | + ExtendedField2 int |
| 29 | +} |
| 30 | + |
| 31 | +type ArrowRequest struct { |
| 32 | + PrivateNetwork *PrivateNetwork |
| 33 | +} |
| 34 | + |
| 35 | +type SpecialRequest struct { |
| 36 | + *RequestEmbedding |
| 37 | + TabRequest []*ArrowRequest |
| 38 | +} |
| 39 | + |
| 40 | +type EndpointSpecPrivateNetwork struct { |
| 41 | + PrivateNetworkID string |
| 42 | + ServiceIP *scw.IPNet |
| 43 | +} |
| 44 | + |
| 45 | +type PrivateNetwork struct { |
| 46 | + *EndpointSpecPrivateNetwork |
| 47 | + OtherValue string |
| 48 | +} |
| 49 | + |
| 50 | +func Test_getValuesForFieldByName(t *testing.T) { |
| 51 | + type TestCase struct { |
| 52 | + cmdArgs interface{} |
| 53 | + fieldName string |
| 54 | + expectedError string |
| 55 | + expectedValues []reflect.Value |
| 56 | + } |
| 57 | + |
| 58 | + expectedServiceIP := &scw.IPNet{ |
| 59 | + IPNet: net.IPNet{ |
| 60 | + IP: net.ParseIP("192.0.2.1"), |
| 61 | + Mask: net.CIDRMask(24, 32), |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + tests := []struct { |
| 66 | + name string |
| 67 | + testCase TestCase |
| 68 | + testFunc func(*testing.T, TestCase) |
| 69 | + }{ |
| 70 | + { |
| 71 | + name: "Simple test", |
| 72 | + testCase: TestCase{ |
| 73 | + cmdArgs: &ExtendedRequest{ |
| 74 | + CreateRequest: &CreateRequest{ |
| 75 | + RequestEmbedding: &RequestEmbedding{ |
| 76 | + EmbeddingField1: "value1", |
| 77 | + EmbeddingField2: 2, |
| 78 | + }, |
| 79 | + CreateField1: "value3", |
| 80 | + CreateField2: 4, |
| 81 | + }, |
| 82 | + ExtendedField1: "value5", |
| 83 | + ExtendedField2: 6, |
| 84 | + }, |
| 85 | + fieldName: "EmbeddingField1", |
| 86 | + expectedError: "", |
| 87 | + expectedValues: []reflect.Value{reflect.ValueOf("value1")}, |
| 88 | + }, |
| 89 | + testFunc: func(t *testing.T, tc TestCase) { |
| 90 | + values, err := core.GetValuesForFieldByName(reflect.ValueOf(tc.cmdArgs), strings.Split(tc.fieldName, ".")) |
| 91 | + if err != nil { |
| 92 | + assert.Equal(t, tc.expectedError, err.Error()) |
| 93 | + } else { |
| 94 | + if tc.expectedValues != nil && !reflect.DeepEqual(tc.expectedValues[0].Interface(), values[0].Interface()) { |
| 95 | + t.Errorf("Expected %v, got %v", tc.expectedValues[0].Interface(), values[0].Interface()) |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "Error test", |
| 102 | + testCase: TestCase{ |
| 103 | + cmdArgs: &ExtendedRequest{ |
| 104 | + CreateRequest: &CreateRequest{ |
| 105 | + RequestEmbedding: &RequestEmbedding{ |
| 106 | + EmbeddingField1: "value1", |
| 107 | + EmbeddingField2: 2, |
| 108 | + }, |
| 109 | + CreateField1: "value3", |
| 110 | + CreateField2: 4, |
| 111 | + }, |
| 112 | + ExtendedField1: "value5", |
| 113 | + ExtendedField2: 6, |
| 114 | + }, |
| 115 | + fieldName: "NotExist", |
| 116 | + expectedError: "field NotExist does not exist for ExtendedRequest", |
| 117 | + expectedValues: []reflect.Value{reflect.ValueOf("value1")}, |
| 118 | + }, |
| 119 | + testFunc: func(t *testing.T, tc TestCase) { |
| 120 | + values, err := core.GetValuesForFieldByName(reflect.ValueOf(tc.cmdArgs), strings.Split(tc.fieldName, ".")) |
| 121 | + if err != nil { |
| 122 | + assert.Equal(t, tc.expectedError, err.Error()) |
| 123 | + } else { |
| 124 | + if tc.expectedValues != nil && !reflect.DeepEqual(tc.expectedValues[0].Interface(), values[0].Interface()) { |
| 125 | + t.Errorf("Expected %v, got %v", tc.expectedValues[0].Interface(), values[0].Interface()) |
| 126 | + } |
| 127 | + } |
| 128 | + }, |
| 129 | + }, |
| 130 | + { |
| 131 | + |
| 132 | + name: "Special test", |
| 133 | + testCase: TestCase{ |
| 134 | + cmdArgs: &SpecialRequest{ |
| 135 | + RequestEmbedding: &RequestEmbedding{ |
| 136 | + EmbeddingField1: "value1", |
| 137 | + EmbeddingField2: 2, |
| 138 | + }, |
| 139 | + TabRequest: []*ArrowRequest{ |
| 140 | + { |
| 141 | + PrivateNetwork: &PrivateNetwork{ |
| 142 | + EndpointSpecPrivateNetwork: &EndpointSpecPrivateNetwork{ |
| 143 | + ServiceIP: &scw.IPNet{ |
| 144 | + IPNet: net.IPNet{ |
| 145 | + IP: net.ParseIP("192.0.2.1"), |
| 146 | + Mask: net.CIDRMask(24, 32), |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + { |
| 153 | + PrivateNetwork: &PrivateNetwork{ |
| 154 | + OtherValue: "hello", |
| 155 | + }, |
| 156 | + }, |
| 157 | + }, |
| 158 | + }, |
| 159 | + fieldName: "tabRequest.{index}.privateNetwork.serviceIP", |
| 160 | + expectedError: "", |
| 161 | + expectedValues: []reflect.Value{reflect.ValueOf(expectedServiceIP)}, |
| 162 | + }, |
| 163 | + testFunc: func(t *testing.T, tc TestCase) { |
| 164 | + values, err := core.GetValuesForFieldByName(reflect.ValueOf(tc.cmdArgs), strings.Split(tc.fieldName, ".")) |
| 165 | + if err != nil { |
| 166 | + assert.Equal(t, nil, err.Error()) |
| 167 | + } else { |
| 168 | + if tc.expectedValues != nil && !reflect.DeepEqual(tc.expectedValues[0].Interface(), values[0].Interface()) { |
| 169 | + t.Errorf("Expected %v, got %v", tc.expectedValues[0].Interface(), values[0].Interface()) |
| 170 | + } |
| 171 | + } |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tt := range tests { |
| 177 | + t.Run(tt.name, func(t *testing.T) { |
| 178 | + tt.testFunc(t, tt.testCase) |
| 179 | + }) |
| 180 | + } |
| 181 | +} |
0 commit comments