|
| 1 | +//go:build plugins |
| 2 | +// +build plugins |
| 3 | + |
| 4 | +package structmap |
| 5 | + |
| 6 | +import ( |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + v1beta2 "github.com/odpf/meteor/models/odpf/assets/v1beta2" |
| 11 | + testutils "github.com/odpf/meteor/test/utils" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "google.golang.org/protobuf/types/known/anypb" |
| 14 | + "google.golang.org/protobuf/types/known/structpb" |
| 15 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAssetWrapper_AsMap(t *testing.T) { |
| 19 | + cases := []struct { |
| 20 | + name string |
| 21 | + w AssetWrapper |
| 22 | + expected map[string]interface{} |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "AssetWithFeatureTable", |
| 26 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 27 | + Urn: "urn:caramlstore:test-caramlstore:feature_table:avg_dispatch_arrival_time_10_mins", |
| 28 | + Name: "avg_dispatch_arrival_time_10_mins", |
| 29 | + Service: "caramlstore", |
| 30 | + Type: "feature_table", |
| 31 | + Data: testutils.BuildAny(t, &v1beta2.FeatureTable{ |
| 32 | + Namespace: "sauron", |
| 33 | + Entities: []*v1beta2.FeatureTable_Entity{ |
| 34 | + {Name: "merchant_uuid", Labels: map[string]string{ |
| 35 | + "description": "merchant uuid", |
| 36 | + "value_type": "STRING", |
| 37 | + }}, |
| 38 | + }, |
| 39 | + Features: []*v1beta2.Feature{ |
| 40 | + {Name: "ongoing_placed_and_waiting_acceptance_orders", DataType: "INT64"}, |
| 41 | + {Name: "ongoing_orders", DataType: "INT64"}, |
| 42 | + {Name: "merchant_avg_dispatch_arrival_time_10m", DataType: "FLOAT"}, |
| 43 | + {Name: "ongoing_accepted_orders", DataType: "INT64"}, |
| 44 | + }, |
| 45 | + CreateTime: timestamppb.New(time.Date(2022, time.September, 19, 22, 42, 0o4, 0, time.UTC)), |
| 46 | + UpdateTime: timestamppb.New(time.Date(2022, time.September, 21, 13, 23, 0o2, 0, time.UTC)), |
| 47 | + }), |
| 48 | + Lineage: &v1beta2.Lineage{ |
| 49 | + Upstreams: []*v1beta2.Resource{ |
| 50 | + { |
| 51 | + Urn: "urn:kafka:int-dagstream-kafka.yonkou.io:topic:GO_FOOD-delay-allocation-merchant-feature-10m-log", |
| 52 | + Service: "kafka", |
| 53 | + Type: "topic", |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }}, |
| 58 | + expected: map[string]interface{}{ |
| 59 | + "data": map[string]interface{}{ |
| 60 | + "@type": "type.googleapis.com/odpf.assets.v1beta2.FeatureTable", |
| 61 | + "create_time": "2022-09-19T22:42:04Z", |
| 62 | + "entities": []interface{}{ |
| 63 | + map[string]interface{}{ |
| 64 | + "labels": map[string]interface{}{"description": "merchant uuid", "value_type": "STRING"}, |
| 65 | + "name": "merchant_uuid", |
| 66 | + }, |
| 67 | + }, |
| 68 | + "features": []interface{}{ |
| 69 | + map[string]interface{}{"data_type": "INT64", "name": "ongoing_placed_and_waiting_acceptance_orders"}, |
| 70 | + map[string]interface{}{"data_type": "INT64", "name": "ongoing_orders"}, |
| 71 | + map[string]interface{}{"data_type": "FLOAT", "name": "merchant_avg_dispatch_arrival_time_10m"}, |
| 72 | + map[string]interface{}{"data_type": "INT64", "name": "ongoing_accepted_orders"}, |
| 73 | + }, |
| 74 | + "namespace": "sauron", |
| 75 | + "update_time": "2022-09-21T13:23:02Z", |
| 76 | + }, |
| 77 | + "lineage": map[string]interface{}{ |
| 78 | + "upstreams": []interface{}{ |
| 79 | + map[string]interface{}{ |
| 80 | + "service": "kafka", |
| 81 | + "type": "topic", |
| 82 | + "urn": "urn:kafka:int-dagstream-kafka.yonkou.io:topic:GO_FOOD-delay-allocation-merchant-feature-10m-log", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + "name": "avg_dispatch_arrival_time_10_mins", |
| 87 | + "service": "caramlstore", |
| 88 | + "type": "feature_table", |
| 89 | + "urn": "urn:caramlstore:test-caramlstore:feature_table:avg_dispatch_arrival_time_10_mins", |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "AssetWithTable", |
| 94 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 95 | + Urn: "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 96 | + Name: "applicant", |
| 97 | + Type: "table", |
| 98 | + Service: "cassandra", |
| 99 | + Data: testutils.BuildAny(t, &v1beta2.Table{ |
| 100 | + Columns: []*v1beta2.Column{ |
| 101 | + {Name: "applicantid", DataType: "int"}, |
| 102 | + {Name: "first_name", DataType: "text"}, |
| 103 | + {Name: "last_name", DataType: "text"}, |
| 104 | + }, |
| 105 | + Attributes: &structpb.Struct{ |
| 106 | + Fields: map[string]*structpb.Value{ |
| 107 | + "id": structpb.NewStringValue("test-id"), |
| 108 | + "name": structpb.NewStringValue("test-name"), |
| 109 | + }, |
| 110 | + }, |
| 111 | + }), |
| 112 | + }}, |
| 113 | + expected: map[string]interface{}{ |
| 114 | + "data": map[string]interface{}{ |
| 115 | + "@type": "type.googleapis.com/odpf.assets.v1beta2.Table", |
| 116 | + "columns": []interface{}{ |
| 117 | + map[string]interface{}{"data_type": "int", "name": "applicantid"}, |
| 118 | + map[string]interface{}{"data_type": "text", "name": "first_name"}, |
| 119 | + map[string]interface{}{"data_type": "text", "name": "last_name"}, |
| 120 | + }, |
| 121 | + "attributes": map[string]interface{}{ |
| 122 | + "id": "test-id", |
| 123 | + "name": "test-name", |
| 124 | + }, |
| 125 | + }, |
| 126 | + "name": "applicant", |
| 127 | + "service": "cassandra", |
| 128 | + "type": "table", |
| 129 | + "urn": "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 130 | + }, |
| 131 | + }, |
| 132 | + } |
| 133 | + for _, tc := range cases { |
| 134 | + t.Run(tc.name, func(t *testing.T) { |
| 135 | + res, err := tc.w.AsMap() |
| 136 | + assert.NoError(t, err) |
| 137 | + assert.Equal(t, tc.expected, res) |
| 138 | + }) |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func TestAssetWrapper_OverwriteWith(t *testing.T) { |
| 143 | + cases := []struct { |
| 144 | + name string |
| 145 | + w AssetWrapper |
| 146 | + input map[string]interface{} |
| 147 | + expected *v1beta2.Asset |
| 148 | + expectedErr bool |
| 149 | + }{ |
| 150 | + { |
| 151 | + name: "AssetWithFeatureTable", |
| 152 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 153 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.FeatureTable"}, |
| 154 | + }}, |
| 155 | + input: map[string]interface{}{ |
| 156 | + "data": map[string]interface{}{ |
| 157 | + "@type": "type.googleapis.com/odpf.assets.v1beta2.FeatureTable", |
| 158 | + "create_time": "2022-09-19T22:42:04Z", |
| 159 | + "entities": []interface{}{ |
| 160 | + map[string]interface{}{ |
| 161 | + "labels": map[string]interface{}{"description": "merchant uuid", "value_type": "STRING"}, |
| 162 | + "name": "merchant_uuid", |
| 163 | + }, |
| 164 | + }, |
| 165 | + "features": []interface{}{ |
| 166 | + map[string]interface{}{"data_type": "INT64", "name": "ongoing_placed_and_waiting_acceptance_orders"}, |
| 167 | + map[string]interface{}{"data_type": "INT64", "name": "ongoing_orders"}, |
| 168 | + map[string]interface{}{"data_type": "FLOAT", "name": "merchant_avg_dispatch_arrival_time_10m"}, |
| 169 | + map[string]interface{}{"data_type": "INT64", "name": "ongoing_accepted_orders"}, |
| 170 | + }, |
| 171 | + "namespace": "sauron", |
| 172 | + "update_time": "2022-09-21T13:23:02Z", |
| 173 | + }, |
| 174 | + "lineage": map[string]interface{}{ |
| 175 | + "upstreams": []interface{}{ |
| 176 | + map[string]interface{}{ |
| 177 | + "service": "kafka", |
| 178 | + "type": "topic", |
| 179 | + "urn": "urn:kafka:int-dagstream-kafka.yonkou.io:topic:GO_FOOD-delay-allocation-merchant-feature-10m-log", |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + "create_time": "2022-10-19T22:42:04Z", |
| 184 | + "name": "avg_dispatch_arrival_time_10_mins", |
| 185 | + "service": "caramlstore", |
| 186 | + "type": "feature_table", |
| 187 | + "urn": "urn:caramlstore:test-caramlstore:feature_table:avg_dispatch_arrival_time_10_mins", |
| 188 | + }, |
| 189 | + expected: &v1beta2.Asset{ |
| 190 | + Urn: "urn:caramlstore:test-caramlstore:feature_table:avg_dispatch_arrival_time_10_mins", |
| 191 | + Name: "avg_dispatch_arrival_time_10_mins", |
| 192 | + Service: "caramlstore", |
| 193 | + Type: "feature_table", |
| 194 | + Data: testutils.BuildAny(t, &v1beta2.FeatureTable{ |
| 195 | + Namespace: "sauron", |
| 196 | + Entities: []*v1beta2.FeatureTable_Entity{ |
| 197 | + { |
| 198 | + Name: "merchant_uuid", |
| 199 | + Labels: map[string]string{"description": "merchant uuid", "value_type": "STRING"}, |
| 200 | + }, |
| 201 | + }, |
| 202 | + Features: []*v1beta2.Feature{ |
| 203 | + {Name: "ongoing_placed_and_waiting_acceptance_orders", DataType: "INT64"}, |
| 204 | + {Name: "ongoing_orders", DataType: "INT64"}, |
| 205 | + {Name: "merchant_avg_dispatch_arrival_time_10m", DataType: "FLOAT"}, |
| 206 | + {Name: "ongoing_accepted_orders", DataType: "INT64"}, |
| 207 | + }, |
| 208 | + CreateTime: timestamppb.New(time.Date(2022, time.September, 19, 22, 42, 4, 0, time.UTC)), |
| 209 | + UpdateTime: timestamppb.New(time.Date(2022, time.September, 21, 13, 23, 2, 0, time.UTC)), |
| 210 | + }), |
| 211 | + Lineage: &v1beta2.Lineage{ |
| 212 | + Upstreams: []*v1beta2.Resource{ |
| 213 | + { |
| 214 | + Urn: "urn:kafka:int-dagstream-kafka.yonkou.io:topic:GO_FOOD-delay-allocation-merchant-feature-10m-log", |
| 215 | + Service: "kafka", |
| 216 | + Type: "topic", |
| 217 | + }, |
| 218 | + }, |
| 219 | + }, |
| 220 | + CreateTime: timestamppb.New(time.Date(2022, time.October, 19, 22, 42, 4, 0, time.UTC)), |
| 221 | + }, |
| 222 | + }, |
| 223 | + { |
| 224 | + name: "AssetWithTable", |
| 225 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 226 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.Table"}, |
| 227 | + }}, |
| 228 | + input: map[string]interface{}{ |
| 229 | + "data": map[string]interface{}{ |
| 230 | + "@type": "type.googleapis.com/odpf.assets.v1beta2.Table", |
| 231 | + "columns": []interface{}{ |
| 232 | + map[string]interface{}{"data_type": "int", "name": "applicantid"}, |
| 233 | + map[string]interface{}{"data_type": "text", "name": "first_name"}, |
| 234 | + map[string]interface{}{"data_type": "text", "name": "last_name"}, |
| 235 | + }, |
| 236 | + "attributes": map[string]interface{}{"id": "test-id", "name": "test-name"}, |
| 237 | + }, |
| 238 | + "name": "applicant", |
| 239 | + "service": "cassandra", |
| 240 | + "type": "table", |
| 241 | + "urn": "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 242 | + }, |
| 243 | + expected: &v1beta2.Asset{ |
| 244 | + Urn: "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 245 | + Name: "applicant", |
| 246 | + Type: "table", |
| 247 | + Service: "cassandra", |
| 248 | + Data: testutils.BuildAny(t, &v1beta2.Table{ |
| 249 | + Columns: []*v1beta2.Column{ |
| 250 | + {Name: "applicantid", DataType: "int"}, |
| 251 | + {Name: "first_name", DataType: "text"}, |
| 252 | + {Name: "last_name", DataType: "text"}, |
| 253 | + }, |
| 254 | + Attributes: &structpb.Struct{ |
| 255 | + Fields: map[string]*structpb.Value{ |
| 256 | + "id": structpb.NewStringValue("test-id"), |
| 257 | + "name": structpb.NewStringValue("test-name"), |
| 258 | + }, |
| 259 | + }, |
| 260 | + }), |
| 261 | + }, |
| 262 | + }, |
| 263 | + { |
| 264 | + name: "WithoutData", |
| 265 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 266 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.Table"}, |
| 267 | + }}, |
| 268 | + input: map[string]interface{}{ |
| 269 | + "name": "applicant", |
| 270 | + "service": "cassandra", |
| 271 | + "type": "table", |
| 272 | + "urn": "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 273 | + }, |
| 274 | + expected: &v1beta2.Asset{ |
| 275 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.Table"}, |
| 276 | + }, |
| 277 | + expectedErr: true, |
| 278 | + }, |
| 279 | + { |
| 280 | + name: "UnknownKeys", |
| 281 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 282 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.Table"}, |
| 283 | + }}, |
| 284 | + input: map[string]interface{}{ |
| 285 | + "does-not-exist": "value", |
| 286 | + "urn": "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 287 | + "type": "table", |
| 288 | + "data": map[string]interface{}{}, |
| 289 | + }, |
| 290 | + expected: &v1beta2.Asset{ |
| 291 | + Urn: "urn:cassandra:test-cassandra:table:cassandra_meteor_test.applicant", |
| 292 | + Type: "table", |
| 293 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.Table"}, |
| 294 | + }, |
| 295 | + expectedErr: true, |
| 296 | + }, |
| 297 | + { |
| 298 | + name: "UnknownMessageName", |
| 299 | + w: AssetWrapper{A: &v1beta2.Asset{ |
| 300 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.DoesNotExist"}, |
| 301 | + }}, |
| 302 | + input: map[string]interface{}{ |
| 303 | + "data": map[string]interface{}{}, |
| 304 | + }, |
| 305 | + expected: &v1beta2.Asset{ |
| 306 | + Data: &anypb.Any{TypeUrl: "type.googleapis.com/odpf.assets.v1beta2.DoesNotExist"}, |
| 307 | + }, |
| 308 | + expectedErr: true, |
| 309 | + }, |
| 310 | + } |
| 311 | + for _, tc := range cases { |
| 312 | + t.Run(tc.name, func(t *testing.T) { |
| 313 | + err := tc.w.OverwriteWith(tc.input) |
| 314 | + assert.Equalf(t, tc.expectedErr, (err != nil), |
| 315 | + "AssetWrapper.OverwriteWith() err = %v,\nexpectedErr %v", err, tc.expectedErr) |
| 316 | + testutils.AssertEqualProto(t, tc.expected, tc.w.A) |
| 317 | + }) |
| 318 | + } |
| 319 | +} |
0 commit comments