|
| 1 | +package mariadb |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/google/go-cmp/cmp" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 10 | + mariadb "github.com/stackitcloud/stackit-sdk-go/services/mariadb/v1api" |
| 11 | +) |
| 12 | + |
| 13 | +func TestMapDataSourceFields(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + description string |
| 16 | + state DataSourceModel |
| 17 | + input *mariadb.CredentialsResponse |
| 18 | + expected DataSourceModel |
| 19 | + isValid bool |
| 20 | + }{ |
| 21 | + { |
| 22 | + "default_values", |
| 23 | + DataSourceModel{ |
| 24 | + InstanceId: types.StringValue("iid"), |
| 25 | + ProjectId: types.StringValue("pid"), |
| 26 | + }, |
| 27 | + &mariadb.CredentialsResponse{ |
| 28 | + Id: "cid", |
| 29 | + Raw: &mariadb.RawCredentials{}, |
| 30 | + }, |
| 31 | + DataSourceModel{ |
| 32 | + Id: types.StringValue("pid,iid,cid"), |
| 33 | + CredentialId: types.StringValue("cid"), |
| 34 | + InstanceId: types.StringValue("iid"), |
| 35 | + ProjectId: types.StringValue("pid"), |
| 36 | + Host: types.StringValue(""), |
| 37 | + Hosts: types.ListNull(types.StringType), |
| 38 | + Name: types.StringNull(), |
| 39 | + Password: types.StringValue(""), |
| 40 | + Port: types.Int32Null(), |
| 41 | + Uri: types.StringNull(), |
| 42 | + Username: types.StringValue(""), |
| 43 | + }, |
| 44 | + true, |
| 45 | + }, |
| 46 | + { |
| 47 | + "simple_values", |
| 48 | + DataSourceModel{ |
| 49 | + InstanceId: types.StringValue("iid"), |
| 50 | + ProjectId: types.StringValue("pid"), |
| 51 | + }, |
| 52 | + &mariadb.CredentialsResponse{ |
| 53 | + Id: "cid", |
| 54 | + Raw: &mariadb.RawCredentials{ |
| 55 | + Credentials: mariadb.Credentials{ |
| 56 | + Host: "host", |
| 57 | + Hosts: []string{ |
| 58 | + "host_1", |
| 59 | + "", |
| 60 | + }, |
| 61 | + Name: new("name"), |
| 62 | + Password: "password", |
| 63 | + Port: new(int32(1234)), |
| 64 | + Uri: new("uri"), |
| 65 | + Username: "username", |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + DataSourceModel{ |
| 70 | + Id: types.StringValue("pid,iid,cid"), |
| 71 | + CredentialId: types.StringValue("cid"), |
| 72 | + InstanceId: types.StringValue("iid"), |
| 73 | + ProjectId: types.StringValue("pid"), |
| 74 | + Host: types.StringValue("host"), |
| 75 | + Hosts: types.ListValueMust(types.StringType, []attr.Value{ |
| 76 | + types.StringValue("host_1"), |
| 77 | + types.StringValue(""), |
| 78 | + }), |
| 79 | + Name: types.StringValue("name"), |
| 80 | + Password: types.StringValue("password"), |
| 81 | + Port: types.Int32Value(1234), |
| 82 | + Uri: types.StringValue("uri"), |
| 83 | + Username: types.StringValue("username"), |
| 84 | + }, |
| 85 | + true, |
| 86 | + }, |
| 87 | + { |
| 88 | + "hosts_unordered", |
| 89 | + DataSourceModel{ |
| 90 | + InstanceId: types.StringValue("iid"), |
| 91 | + ProjectId: types.StringValue("pid"), |
| 92 | + Hosts: types.ListValueMust(types.StringType, []attr.Value{ |
| 93 | + types.StringValue("host_2"), |
| 94 | + types.StringValue(""), |
| 95 | + types.StringValue("host_1"), |
| 96 | + }), |
| 97 | + }, |
| 98 | + &mariadb.CredentialsResponse{ |
| 99 | + Id: "cid", |
| 100 | + Raw: &mariadb.RawCredentials{ |
| 101 | + Credentials: mariadb.Credentials{ |
| 102 | + Host: "host", |
| 103 | + Hosts: []string{ |
| 104 | + "", |
| 105 | + "host_1", |
| 106 | + "host_2", |
| 107 | + }, |
| 108 | + Name: new("name"), |
| 109 | + Password: "password", |
| 110 | + Port: new(int32(1234)), |
| 111 | + Uri: new("uri"), |
| 112 | + Username: "username", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + DataSourceModel{ |
| 117 | + Id: types.StringValue("pid,iid,cid"), |
| 118 | + CredentialId: types.StringValue("cid"), |
| 119 | + InstanceId: types.StringValue("iid"), |
| 120 | + ProjectId: types.StringValue("pid"), |
| 121 | + Host: types.StringValue("host"), |
| 122 | + Hosts: types.ListValueMust(types.StringType, []attr.Value{ |
| 123 | + types.StringValue("host_2"), |
| 124 | + types.StringValue(""), |
| 125 | + types.StringValue("host_1"), |
| 126 | + }), |
| 127 | + Name: types.StringValue("name"), |
| 128 | + Password: types.StringValue("password"), |
| 129 | + Port: types.Int32Value(1234), |
| 130 | + Uri: types.StringValue("uri"), |
| 131 | + Username: types.StringValue("username"), |
| 132 | + }, |
| 133 | + true, |
| 134 | + }, |
| 135 | + { |
| 136 | + "null_fields_and_int_conversions", |
| 137 | + DataSourceModel{ |
| 138 | + InstanceId: types.StringValue("iid"), |
| 139 | + ProjectId: types.StringValue("pid"), |
| 140 | + }, |
| 141 | + &mariadb.CredentialsResponse{ |
| 142 | + Id: "cid", |
| 143 | + Raw: &mariadb.RawCredentials{ |
| 144 | + Credentials: mariadb.Credentials{ |
| 145 | + Host: "", |
| 146 | + Hosts: []string{}, |
| 147 | + Name: nil, |
| 148 | + Password: "", |
| 149 | + Port: new(int32(2123456789)), |
| 150 | + Uri: nil, |
| 151 | + Username: "", |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + DataSourceModel{ |
| 156 | + Id: types.StringValue("pid,iid,cid"), |
| 157 | + CredentialId: types.StringValue("cid"), |
| 158 | + InstanceId: types.StringValue("iid"), |
| 159 | + ProjectId: types.StringValue("pid"), |
| 160 | + Host: types.StringValue(""), |
| 161 | + Hosts: types.ListValueMust(types.StringType, []attr.Value{}), |
| 162 | + Name: types.StringNull(), |
| 163 | + Password: types.StringValue(""), |
| 164 | + Port: types.Int32Value(2123456789), |
| 165 | + Uri: types.StringNull(), |
| 166 | + Username: types.StringValue(""), |
| 167 | + }, |
| 168 | + true, |
| 169 | + }, |
| 170 | + { |
| 171 | + "nil_response", |
| 172 | + DataSourceModel{ |
| 173 | + InstanceId: types.StringValue("iid"), |
| 174 | + ProjectId: types.StringValue("pid"), |
| 175 | + }, |
| 176 | + nil, |
| 177 | + DataSourceModel{}, |
| 178 | + false, |
| 179 | + }, |
| 180 | + { |
| 181 | + "no_resource_id", |
| 182 | + DataSourceModel{ |
| 183 | + InstanceId: types.StringValue("iid"), |
| 184 | + ProjectId: types.StringValue("pid"), |
| 185 | + }, |
| 186 | + &mariadb.CredentialsResponse{}, |
| 187 | + DataSourceModel{}, |
| 188 | + false, |
| 189 | + }, |
| 190 | + { |
| 191 | + "nil_raw_credential", |
| 192 | + DataSourceModel{ |
| 193 | + InstanceId: types.StringValue("iid"), |
| 194 | + ProjectId: types.StringValue("pid"), |
| 195 | + }, |
| 196 | + &mariadb.CredentialsResponse{ |
| 197 | + Id: "cid", |
| 198 | + }, |
| 199 | + DataSourceModel{}, |
| 200 | + false, |
| 201 | + }, |
| 202 | + } |
| 203 | + for _, tt := range tests { |
| 204 | + t.Run(tt.description, func(t *testing.T) { |
| 205 | + err := mapDataSourceFields(context.Background(), tt.input, &tt.state) |
| 206 | + if !tt.isValid && err == nil { |
| 207 | + t.Fatalf("Should have failed") |
| 208 | + } |
| 209 | + if tt.isValid && err != nil { |
| 210 | + t.Fatalf("Should not have failed: %v", err) |
| 211 | + } |
| 212 | + if tt.isValid { |
| 213 | + diff := cmp.Diff(tt.state, tt.expected) |
| 214 | + if diff != "" { |
| 215 | + t.Fatalf("Data does not match: %s", diff) |
| 216 | + } |
| 217 | + } |
| 218 | + }) |
| 219 | + } |
| 220 | +} |
0 commit comments