Skip to content

Commit 7cbd38e

Browse files
authored
feat: Improves testing in search-index resource (#1635) INTMDB-1298
* create Mappings if not created (e.g. changing from vector to search index) * move JSON comparison logic to a check function * add tests
1 parent 921c7b2 commit 7cbd38e

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

mongodbatlas/resource_mongodbatlas_search_index.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,6 @@ func resourceMongoDBAtlasSearchIndexUpdate(ctx context.Context, d *schema.Resour
218218
searchIndex.SearchAnalyzer = stringPtr(d.Get("search_analyzer").(string))
219219
}
220220

221-
if d.HasChange("mappings_dynamic") {
222-
dynamic := d.Get("mappings_dynamic").(bool)
223-
searchIndex.Mappings.Dynamic = &dynamic
224-
}
225-
226221
if d.HasChange("analyzers") {
227222
analyzers, err := unmarshalSearchIndexAnalyzersFields(d.Get("analyzers").(string))
228223
if err != nil {
@@ -231,11 +226,22 @@ func resourceMongoDBAtlasSearchIndexUpdate(ctx context.Context, d *schema.Resour
231226
searchIndex.Analyzers = analyzers
232227
}
233228

229+
if d.HasChange("mappings_dynamic") {
230+
dynamic := d.Get("mappings_dynamic").(bool)
231+
if searchIndex.Mappings == nil {
232+
searchIndex.Mappings = &admin.ApiAtlasFTSMappings{}
233+
}
234+
searchIndex.Mappings.Dynamic = &dynamic
235+
}
236+
234237
if d.HasChange("mappings_fields") {
235238
mappingsFields, err := unmarshalSearchIndexMappingFields(d.Get("mappings_fields").(string))
236239
if err != nil {
237240
return err
238241
}
242+
if searchIndex.Mappings == nil {
243+
searchIndex.Mappings = &admin.ApiAtlasFTSMappings{}
244+
}
239245
searchIndex.Mappings.Fields = mappingsFields
240246
}
241247

mongodbatlas/resource_mongodbatlas_search_index_test.go

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package mongodbatlas
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"os"
8-
"reflect"
97
"testing"
108

119
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
1210
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1311
"github.com/hashicorp/terraform-plugin-testing/terraform"
12+
"github.com/mongodb/terraform-provider-mongodbatlas/mongodbatlas/testutils"
1413
)
1514

1615
const (
@@ -257,7 +256,7 @@ func TestAccSearchIndexRS_withVector(t *testing.T) {
257256
resource.TestCheckResourceAttr(resourceName, "collection_name", collectionName),
258257
resource.TestCheckResourceAttr(resourceName, "type", "vectorSearch"),
259258
resource.TestCheckResourceAttrSet(resourceName, "fields"),
260-
testCheckResourceAttrJSON(resourceName, "fields", fields),
259+
resource.TestCheckResourceAttrWith(resourceName, "fields", testutils.JSONEquals(fields)),
261260

262261
resource.TestCheckResourceAttr(datasourceName, "type", "vectorSearch"),
263262
resource.TestCheckResourceAttr(datasourceName, "name", indexName),
@@ -268,7 +267,7 @@ func TestAccSearchIndexRS_withVector(t *testing.T) {
268267
resource.TestCheckResourceAttr(datasourceName, "name", indexName),
269268
resource.TestCheckResourceAttrSet(datasourceName, "index_id"),
270269
resource.TestCheckResourceAttrSet(datasourceName, "fields"),
271-
testCheckResourceAttrJSON(datasourceName, "fields", fields),
270+
resource.TestCheckResourceAttrWith(datasourceName, "fields", testutils.JSONEquals(fields)),
272271
),
273272
},
274273
},
@@ -522,28 +521,3 @@ func getClusterInfo(projectID string) (clusterName, clusterNameStr, clusterTerra
522521
}
523522
return clusterName, clusterNameStr, clusterTerraformStr
524523
}
525-
526-
func testCheckResourceAttrJSON[T any](resourceName, attribute string, expected T) resource.TestCheckFunc {
527-
return func(s *terraform.State) error {
528-
rs, ok := s.RootModule().Resources[resourceName]
529-
if !ok {
530-
return fmt.Errorf("Not found: %s", resourceName)
531-
}
532-
533-
attr, ok := rs.Primary.Attributes[attribute]
534-
if !ok {
535-
return fmt.Errorf("Attribute not found: %s", attribute)
536-
}
537-
538-
var actual T
539-
if err := json.Unmarshal([]byte(attr), &actual); err != nil {
540-
return fmt.Errorf("Could not unmarshal json: %s", err)
541-
}
542-
543-
if !reflect.DeepEqual(actual, expected) {
544-
return fmt.Errorf("Expected `%v`, got `%v`", expected, actual)
545-
}
546-
547-
return nil
548-
}
549-
}

mongodbatlas/testutils/attribute_checks.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package testutils
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"reflect"
57
"regexp"
68
"strconv"
79

@@ -33,3 +35,18 @@ func IntGreatThan(value int) resource.CheckResourceAttrWithFunc {
3335
return nil
3436
}
3537
}
38+
39+
func JSONEquals[T any](value T) resource.CheckResourceAttrWithFunc {
40+
return func(input string) error {
41+
var actual T
42+
if err := json.Unmarshal([]byte(input), &actual); err != nil {
43+
return fmt.Errorf("could not unmarshal json: %s", err)
44+
}
45+
46+
if !reflect.DeepEqual(actual, value) {
47+
return fmt.Errorf("expected `%v`, got `%v`", value, actual)
48+
}
49+
50+
return nil
51+
}
52+
}

mongodbatlas/testutils/attribute_checks_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,25 @@ func TestIntGreaterThan(t *testing.T) {
2929
})
3030
}
3131
}
32+
33+
func TestJSONEquals(t *testing.T) {
34+
objMap := map[string]interface{}{
35+
"str": "my_string",
36+
"number": float64(1234),
37+
"bool1": true,
38+
"bool2": false,
39+
"nilvar": nil,
40+
}
41+
strMap := `
42+
{
43+
"str": "my_string",
44+
"number": 1234,
45+
"bool1": true,
46+
"bool2": false,
47+
"nilvar": null
48+
}
49+
`
50+
if err := JSONEquals(objMap)(strMap); err != nil {
51+
t.Errorf("JSONEquals() error = %v", err)
52+
}
53+
}

0 commit comments

Comments
 (0)