|
1 | 1 | package autogen_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "testing" |
5 | 6 |
|
6 | 7 | "github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes" |
7 | 8 | "github.com/hashicorp/terraform-plugin-framework/attr" |
8 | 9 | "github.com/hashicorp/terraform-plugin-framework/types" |
9 | 10 | "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" |
| 11 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen/customtype" |
10 | 12 | "github.com/stretchr/testify/assert" |
11 | 13 | "github.com/stretchr/testify/require" |
12 | 14 | ) |
@@ -282,6 +284,91 @@ func TestMarshalUpdateNull(t *testing.T) { |
282 | 284 | assert.JSONEq(t, expectedJSONCreate, string(rawCreate)) |
283 | 285 | } |
284 | 286 |
|
| 287 | +func TestMarshalCustomTypeObject(t *testing.T) { |
| 288 | + ctx := context.Background() |
| 289 | + |
| 290 | + type modelEmptyTest struct{} |
| 291 | + |
| 292 | + type modelCustomTypeTest struct { |
| 293 | + AttrPrimitiveOmit types.String `tfsdk:"attr_primitive_omit" autogen:"omitjson"` |
| 294 | + AttrObjectOmit customtype.ObjectValue[modelEmptyTest] `tfsdk:"attr_object_omit" autogen:"omitjson"` |
| 295 | + AttrObjectOmitUpdate customtype.ObjectValue[modelEmptyTest] `tfsdk:"attr_object_omit_update" autogen:"omitjsonupdate"` |
| 296 | + AttrNull customtype.ObjectValue[modelEmptyTest] `tfsdk:"attr_null" autogen:"includenullonupdate"` |
| 297 | + AttrInt types.Int64 `tfsdk:"attr_int"` |
| 298 | + } |
| 299 | + |
| 300 | + type modelCustomTypeParentTest struct { |
| 301 | + AttrString types.String `tfsdk:"attr_string"` |
| 302 | + AttrObject customtype.ObjectValue[modelCustomTypeTest] `tfsdk:"attr_object"` |
| 303 | + } |
| 304 | + |
| 305 | + nullObject := customtype.NewObjectValueNull[modelEmptyTest](ctx) |
| 306 | + emptyObject := customtype.NewObjectValue[modelEmptyTest](ctx, modelEmptyTest{}) |
| 307 | + |
| 308 | + model := struct { |
| 309 | + AttrObjectBasic customtype.ObjectValue[modelCustomTypeTest] `tfsdk:"attr_object_basic"` |
| 310 | + AttrObjectNull customtype.ObjectValue[modelCustomTypeTest] `tfsdk:"attr_object_null"` |
| 311 | + AttrObjectNested customtype.ObjectValue[modelCustomTypeParentTest] `tfsdk:"attr_object_nested"` |
| 312 | + }{ |
| 313 | + AttrObjectBasic: customtype.NewObjectValue[modelCustomTypeTest](ctx, modelCustomTypeTest{ |
| 314 | + AttrInt: types.Int64Value(1), |
| 315 | + AttrPrimitiveOmit: types.StringValue("omitted"), |
| 316 | + AttrObjectOmit: emptyObject, |
| 317 | + AttrObjectOmitUpdate: emptyObject, |
| 318 | + AttrNull: nullObject, |
| 319 | + }), |
| 320 | + AttrObjectNull: customtype.NewObjectValueNull[modelCustomTypeTest](ctx), |
| 321 | + AttrObjectNested: customtype.NewObjectValue[modelCustomTypeParentTest](ctx, modelCustomTypeParentTest{ |
| 322 | + AttrString: types.StringValue("parent"), |
| 323 | + AttrObject: customtype.NewObjectValue[modelCustomTypeTest](ctx, modelCustomTypeTest{ |
| 324 | + AttrInt: types.Int64Value(2), |
| 325 | + AttrPrimitiveOmit: types.StringValue("omitted"), |
| 326 | + AttrObjectOmit: emptyObject, |
| 327 | + AttrObjectOmitUpdate: emptyObject, |
| 328 | + AttrNull: nullObject, |
| 329 | + }), |
| 330 | + }), |
| 331 | + } |
| 332 | + |
| 333 | + const expectedCreateJSON = ` |
| 334 | + { |
| 335 | + "attrObjectBasic": { |
| 336 | + "attrInt": 1, |
| 337 | + "attrObjectOmitUpdate": {} |
| 338 | + }, |
| 339 | + "attrObjectNested": { |
| 340 | + "attrObject": { |
| 341 | + "attrInt": 2, |
| 342 | + "attrObjectOmitUpdate": {} |
| 343 | + }, |
| 344 | + "attrString": "parent" |
| 345 | + } |
| 346 | + } |
| 347 | + ` |
| 348 | + rawCreate, err := autogen.Marshal(&model, false) |
| 349 | + require.NoError(t, err) |
| 350 | + assert.JSONEq(t, expectedCreateJSON, string(rawCreate)) |
| 351 | + |
| 352 | + const expectedUpdateJSON = ` |
| 353 | + { |
| 354 | + "attrObjectBasic": { |
| 355 | + "attrInt": 1, |
| 356 | + "attrNull": null |
| 357 | + }, |
| 358 | + "attrObjectNested": { |
| 359 | + "attrObject": { |
| 360 | + "attrInt": 2, |
| 361 | + "attrNull": null |
| 362 | + }, |
| 363 | + "attrString": "parent" |
| 364 | + } |
| 365 | + } |
| 366 | + ` |
| 367 | + rawUpdate, err := autogen.Marshal(&model, true) |
| 368 | + require.NoError(t, err) |
| 369 | + assert.JSONEq(t, expectedUpdateJSON, string(rawUpdate)) |
| 370 | +} |
| 371 | + |
285 | 372 | func TestMarshalUnsupported(t *testing.T) { |
286 | 373 | testCases := map[string]any{ |
287 | 374 | "Int32 not supported yet as it's not being used in any model": &struct { |
|
0 commit comments