Skip to content

Commit 59950ba

Browse files
author
Cristina Sánchez Sánchez
committed
Empty/nil test cases
1 parent c48e857 commit 59950ba

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

internal/service/clouduserprojectassignment/model.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func NewTFModel(ctx context.Context, projectID string, apiResp *admin.GroupUserR
3838
}
3939

4040
func NewProjectUserReq(ctx context.Context, plan *TFModel) (*admin.GroupUserRequest, diag.Diagnostics) {
41-
var roleNames []string
41+
roleNames := []string{}
4242
if !plan.Roles.IsNull() && !plan.Roles.IsUnknown() {
4343
roleNames = conversion.TypesSetToString(ctx, plan.Roles)
4444
}

internal/service/clouduserprojectassignment/model_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55
"time"
66

7+
"github.com/hashicorp/terraform-plugin-framework/attr"
78
"github.com/hashicorp/terraform-plugin-framework/types"
89
"github.com/mongodb/terraform-provider-mongodbatlas/internal/service/clouduserprojectassignment"
910
"github.com/stretchr/testify/assert"
@@ -92,6 +93,39 @@ func TestCloudUserProjectAssignmentSDKToTFModel(t *testing.T) {
9293
SDKResp: fullResp,
9394
expectedTFModel: expectedFullModel,
9495
},
96+
"Empty SDK response": {
97+
SDKResp: &admin.GroupUserResponse{
98+
Id: "",
99+
Username: "",
100+
FirstName: nil,
101+
LastName: nil,
102+
Country: nil,
103+
MobileNumber: nil,
104+
OrgMembershipStatus: "",
105+
CreatedAt: nil,
106+
LastAuth: nil,
107+
InvitationCreatedAt: nil,
108+
InvitationExpiresAt: nil,
109+
InviterUsername: nil,
110+
Roles: nil,
111+
},
112+
expectedTFModel: &clouduserprojectassignment.TFModel{
113+
UserId: types.StringValue(""),
114+
Username: types.StringValue(""),
115+
ProjectId: types.StringValue(testProjectID),
116+
FirstName: types.StringNull(),
117+
LastName: types.StringNull(),
118+
Country: types.StringNull(),
119+
MobileNumber: types.StringNull(),
120+
OrgMembershipStatus: types.StringValue(""),
121+
CreatedAt: types.StringNull(),
122+
LastAuth: types.StringNull(),
123+
InvitationCreatedAt: types.StringNull(),
124+
InvitationExpiresAt: types.StringNull(),
125+
InviterUsername: types.StringNull(),
126+
Roles: types.SetNull(types.StringType),
127+
},
128+
},
95129
}
96130

97131
for testName, tc := range testCases {
@@ -133,6 +167,26 @@ func TestNewProjectUserRequest(t *testing.T) {
133167
Roles: testProjectRoles,
134168
},
135169
},
170+
"Nil model": {
171+
plan: &clouduserprojectassignment.TFModel{
172+
Username: types.StringNull(),
173+
Roles: types.SetNull(types.StringType),
174+
},
175+
expected: &admin.GroupUserRequest{
176+
Username: "",
177+
Roles: []string{},
178+
},
179+
},
180+
"Empty model": {
181+
plan: &clouduserprojectassignment.TFModel{
182+
Username: types.StringValue(""),
183+
Roles: types.SetValueMust(types.StringType, []attr.Value{}),
184+
},
185+
expected: &admin.GroupUserRequest{
186+
Username: "",
187+
Roles: []string{},
188+
},
189+
},
136190
}
137191

138192
for name, tc := range testCases {
@@ -193,6 +247,15 @@ func TestNewAtlasUpdateReq(t *testing.T) {
193247
wantAddRoles: []string{"GROUP_OWNER"},
194248
wantRemoveRoles: []string{},
195249
},
250+
{
251+
name: "nil roles",
252+
args: args{
253+
stateRoles: nil,
254+
planRoles: []string{},
255+
},
256+
wantAddRoles: []string{},
257+
wantRemoveRoles: []string{},
258+
},
196259
}
197260

198261
for _, tt := range tests {

0 commit comments

Comments
 (0)