Skip to content

Commit

Permalink
test: Improves tests for mongodbatlas_search_index (#2384)
Browse files Browse the repository at this point in the history
* checkVector

* checkBasic

* checkWithMapping

* checkWithSynonyms

* checkAdditional

* checkAdditionalAnalyzers and checkAdditionalMappingsFields

* remove addAttrChecks and addAttrSetChecks

* use commonChecks in all checks

* test checks cleanup
  • Loading branch information
lantoli authored Jul 3, 2024
1 parent 15143f0 commit d6ac0c8
Showing 1 changed file with 111 additions and 104 deletions.
215 changes: 111 additions & 104 deletions internal/service/searchindex/resource_search_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,15 @@ func TestAccSearchIndex_withSearchType(t *testing.T) {
projectID, clusterName = acc.ClusterNameExecution(t)
indexName = acc.RandomName()
databaseName = acc.RandomName()
indexType = "search"
mappingsDynamic = "true"
)
checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName)
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroySearchIndex,
Steps: []resource.TestStep{
{
Config: configBasic(projectID, indexName, databaseName, clusterName, true),
Check: resource.ComposeAggregateTestCheckFunc(checks...),
Config: configBasic(projectID, clusterName, indexName, "search", databaseName),
Check: checkBasic(projectID, clusterName, indexName, "search", databaseName),
},
},
})
Expand All @@ -42,19 +39,15 @@ func TestAccSearchIndex_withMapping(t *testing.T) {
projectID, clusterName = acc.ClusterNameExecution(t)
indexName = acc.RandomName()
databaseName = acc.RandomName()
indexType = ""
mappingsDynamic = "false"
)
checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName)
checks = addAttrSetChecks(checks, "mappings_fields", "analyzers")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroySearchIndex,
Steps: []resource.TestStep{
{
Config: configWithMapping(projectID, indexName, databaseName, clusterName),
Check: resource.ComposeAggregateTestCheckFunc(checks...),
Check: checkWithMapping(projectID, indexName, databaseName, clusterName),
},
},
})
Expand All @@ -65,26 +58,15 @@ func TestAccSearchIndex_withSynonyms(t *testing.T) {
projectID, clusterName = acc.ClusterNameExecution(t)
indexName = acc.RandomName()
databaseName = acc.RandomName()
indexType = ""
mappingsDynamic = "true"
mapChecks = map[string]string{
"synonyms.#": "1",
"synonyms.0.analyzer": "lucene.simple",
"synonyms.0.name": "synonym_test",
"synonyms.0.source_collection": collectionName,
}
)
checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName)
checks = addAttrChecks(checks, mapChecks)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroySearchIndex,
Steps: []resource.TestStep{
{
Config: configWithSynonyms(projectID, indexName, databaseName, clusterName, with),
Check: resource.ComposeAggregateTestCheckFunc(checks...),
Check: checkWithSynonyms(projectID, indexName, databaseName, clusterName, with),
},
},
})
Expand All @@ -95,29 +77,19 @@ func TestAccSearchIndex_updatedToEmptySynonyms(t *testing.T) {
projectID, clusterName = acc.ClusterNameExecution(t)
indexName = acc.RandomName()
databaseName = acc.RandomName()
indexType = ""
mappingsDynamic = "true"
)
checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName)
checks1 := addAttrChecks(checks, map[string]string{
"synonyms.#": "1",
"synonyms.0.analyzer": "lucene.simple",
"synonyms.0.name": "synonym_test",
"synonyms.0.source_collection": collectionName,
})
checks2 := addAttrChecks(checks, map[string]string{"synonyms.#": "0"})
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(t) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroySearchIndex,
Steps: []resource.TestStep{
{
Config: configWithSynonyms(projectID, indexName, databaseName, clusterName, with),
Check: resource.ComposeAggregateTestCheckFunc(checks1...),
Check: checkWithSynonyms(projectID, indexName, databaseName, clusterName, with),
},
{
Config: configWithSynonyms(projectID, indexName, databaseName, clusterName, without),
Check: resource.ComposeAggregateTestCheckFunc(checks2...),
Check: checkWithSynonyms(projectID, indexName, databaseName, clusterName, without),
},
},
})
Expand All @@ -136,17 +108,11 @@ func TestAccSearchIndex_updatedToEmptyAnalyzers(t *testing.T) {
Steps: []resource.TestStep{
{
Config: configAdditional(projectID, indexName, databaseName, clusterName, analyzersTF),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON)),
),
Check: checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName, true),
},
{
Config: configAdditional(projectID, indexName, databaseName, clusterName, ""),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "analyzers", ""),
),
Check: checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName, false),
},
},
})
Expand All @@ -165,17 +131,11 @@ func TestAccSearchIndex_updatedToEmptyMappingsFields(t *testing.T) {
Steps: []resource.TestStep{
{
Config: configAdditional(projectID, indexName, databaseName, clusterName, mappingsFieldsTF),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttrWith(resourceName, "mappings_fields", acc.JSONEquals(mappingsFieldsJSON)),
),
Check: checkAdditionalMappingsFields(projectID, indexName, databaseName, clusterName, true),
},
{
Config: configAdditional(projectID, indexName, databaseName, clusterName, ""),
Check: resource.ComposeAggregateTestCheckFunc(
checkExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "mappings_fields", ""),
),
Check: checkAdditionalMappingsFields(projectID, indexName, databaseName, clusterName, false),
},
},
})
Expand All @@ -191,22 +151,18 @@ func basicTestCase(tb testing.TB) *resource.TestCase {
projectID, clusterName = acc.ClusterNameExecution(tb)
indexName = acc.RandomName()
databaseName = acc.RandomName()
indexType = ""
mappingsDynamic = "true"
)
checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName)

return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroySearchIndex,
Steps: []resource.TestStep{
{
Config: configBasic(projectID, indexName, databaseName, clusterName, false),
Check: resource.ComposeAggregateTestCheckFunc(checks...),
Config: configBasic(projectID, clusterName, indexName, "", databaseName),
Check: checkBasic(projectID, clusterName, indexName, "", databaseName),
},
{
Config: configBasic(projectID, indexName, databaseName, clusterName, false),
Config: configBasic(projectID, clusterName, indexName, "", databaseName),
ResourceName: resourceName,
ImportStateIdFunc: importStateIDFunc(resourceName),
ImportState: true,
Expand All @@ -221,56 +177,42 @@ func basicVectorTestCase(tb testing.TB) *resource.TestCase {
var (
projectID, clusterName = acc.ClusterNameExecution(tb)
indexName = acc.RandomName()
indexType = "vectorSearch"
databaseName = acc.RandomName()
attributes = map[string]string{
"name": indexName,
"cluster_name": clusterName,
"database": databaseName,
"collection_name": collectionName,
"type": indexType,
}
)
checks := addAttrChecks(nil, attributes)
checks = acc.AddAttrSetChecks(resourceName, checks, "project_id")
checks = acc.AddAttrSetChecks(datasourceName, checks, "project_id", "index_id")
checks = append(checks, resource.TestCheckResourceAttrWith(datasourceName, "fields", acc.JSONEquals(fieldsJSON)))

return &resource.TestCase{
PreCheck: func() { acc.PreCheckBasic(tb) },
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
CheckDestroy: acc.CheckDestroySearchIndex,
Steps: []resource.TestStep{
{
Config: configVector(projectID, indexName, databaseName, clusterName),
Check: resource.ComposeAggregateTestCheckFunc(checks...),
Check: checkVector(projectID, indexName, databaseName, clusterName),
},
},
}
}

func commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName string) []resource.TestCheckFunc {
func checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic string, extra ...resource.TestCheckFunc) resource.TestCheckFunc {
attributes := map[string]string{
"name": indexName,
"cluster_name": clusterName,
"database": databaseName,
"collection_name": collectionName,
"type": indexType,
"mappings_dynamic": mappingsDynamic,
"project_id": projectID,
"cluster_name": clusterName,
"name": indexName,
"type": indexType,
"database": databaseName,
"collection_name": collectionName,
}
checks := addAttrChecks(nil, attributes)
checks = acc.AddAttrSetChecks(resourceName, checks, "project_id")
return acc.AddAttrSetChecks(datasourceName, checks, "project_id", "index_id")
}

func addAttrChecks(checks []resource.TestCheckFunc, mapChecks map[string]string) []resource.TestCheckFunc {
checks = acc.AddAttrChecks(resourceName, checks, mapChecks)
return acc.AddAttrChecks(datasourceName, checks, mapChecks)
}

func addAttrSetChecks(checks []resource.TestCheckFunc, attrNames ...string) []resource.TestCheckFunc {
checks = acc.AddAttrSetChecks(resourceName, checks, attrNames...)
return acc.AddAttrSetChecks(datasourceName, checks, attrNames...)
if indexType != "vectorSearch" {
attributes["mappings_dynamic"] = mappingsDynamic
}
checks := []resource.TestCheckFunc{
checkExists(resourceName),
}
checks = acc.AddAttrChecks(resourceName, checks, attributes)
checks = acc.AddAttrChecks(datasourceName, checks, attributes)
checks = acc.AddAttrSetChecks(resourceName, checks, "index_id")
checks = acc.AddAttrSetChecks(datasourceName, checks, "index_id")
checks = append(checks, extra...)
return resource.ComposeAggregateTestCheckFunc(checks...)
}

func checkExists(resourceName string) resource.TestCheckFunc {
Expand All @@ -291,10 +233,10 @@ func checkExists(resourceName string) resource.TestCheckFunc {
}
}

func configBasic(projectID, indexName, databaseName, clusterName string, explicitType bool) string {
var indexType string
if explicitType {
indexType = `type="search"`
func configBasic(projectID, clusterName, indexName, indexType, databaseName string) string {
var indexTypeStr string
if indexType != "" {
indexTypeStr = fmt.Sprintf("type=%q", indexType)
}
return fmt.Sprintf(`
resource "mongodbatlas_search_index" "test" {
Expand All @@ -309,11 +251,16 @@ func configBasic(projectID, indexName, databaseName, clusterName string, explici
}
data "mongodbatlas_search_index" "data_index" {
cluster_name = %[1]q
project_id = %[2]q
cluster_name = mongodbatlas_search_index.test.cluster_name
project_id = mongodbatlas_search_index.test.project_id
index_id = mongodbatlas_search_index.test.index_id
}
`, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, indexType)
`, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, indexTypeStr)
}

func checkBasic(projectID, clusterName, indexName, indexType, databaseName string) resource.TestCheckFunc {
mappingsDynamic := "true"
return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic)
}

func configWithMapping(projectID, indexName, databaseName, clusterName string) string {
Expand All @@ -331,13 +278,22 @@ func configWithMapping(projectID, indexName, databaseName, clusterName string) s
}
data "mongodbatlas_search_index" "data_index" {
cluster_name = %[1]q
project_id = %[2]q
cluster_name = mongodbatlas_search_index.test.cluster_name
project_id = mongodbatlas_search_index.test.project_id
index_id = mongodbatlas_search_index.test.index_id
}
`, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, analyzersTF, mappingsFieldsTF)
}

func checkWithMapping(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc {
indexType := ""
mappingsDynamic := "false"
attrNames := []string{"mappings_fields", "analyzers"}
checks := acc.AddAttrSetChecks(resourceName, nil, attrNames...)
checks = acc.AddAttrSetChecks(datasourceName, checks, attrNames...)
return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, checks...)
}

func configWithSynonyms(projectID, indexName, databaseName, clusterName string, has bool) string {
var synonymsStr string
if has {
Expand All @@ -363,13 +319,30 @@ func configWithSynonyms(projectID, indexName, databaseName, clusterName string,
}
data "mongodbatlas_search_index" "data_index" {
cluster_name = %[1]q
project_id = %[2]q
cluster_name = mongodbatlas_search_index.test.cluster_name
project_id = mongodbatlas_search_index.test.project_id
index_id = mongodbatlas_search_index.test.index_id
}
`, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, synonymsStr)
}

func checkWithSynonyms(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc {
indexType := ""
mappingsDynamic := "true"
attrs := map[string]string{"synonyms.#": "0"}
if has {
attrs = map[string]string{
"synonyms.#": "1",
"synonyms.0.analyzer": "lucene.simple",
"synonyms.0.name": "synonym_test",
"synonyms.0.source_collection": collectionName,
}
}
checks := acc.AddAttrChecks(resourceName, nil, attrs)
checks = acc.AddAttrChecks(datasourceName, checks, attrs)
return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, checks...)
}

func configAdditional(projectID, indexName, databaseName, clusterName, additional string) string {
return fmt.Sprintf(`
resource "mongodbatlas_search_index" "test" {
Expand All @@ -382,9 +355,35 @@ func configAdditional(projectID, indexName, databaseName, clusterName, additiona
mappings_dynamic = true
%[7]s
}
data "mongodbatlas_search_index" "data_index" {
cluster_name = mongodbatlas_search_index.test.cluster_name
project_id = mongodbatlas_search_index.test.project_id
index_id = mongodbatlas_search_index.test.index_id
}
`, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, additional)
}

func checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc {
indexType := ""
mappingsDynamic := "true"
check := resource.TestCheckResourceAttr(resourceName, "analyzers", "")
if has {
check = resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON))
}
return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, check)
}

func checkAdditionalMappingsFields(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc {
indexType := ""
mappingsDynamic := "true"
check := resource.TestCheckResourceAttr(resourceName, "mappings_fields", "")
if has {
check = resource.TestCheckResourceAttrWith(resourceName, "mappings_fields", acc.JSONEquals(mappingsFieldsJSON))
}
return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, check)
}

func configVector(projectID, indexName, databaseName, clusterName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_search_index" "test" {
Expand All @@ -402,13 +401,21 @@ func configVector(projectID, indexName, databaseName, clusterName string) string
}
data "mongodbatlas_search_index" "data_index" {
cluster_name = %[1]q
project_id = %[2]q
cluster_name = mongodbatlas_search_index.test.cluster_name
project_id = mongodbatlas_search_index.test.project_id
index_id = mongodbatlas_search_index.test.index_id
}
`, clusterName, projectID, indexName, databaseName, collectionName, fieldsJSON)
}

func checkVector(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc {
indexType := "vectorSearch"
mappingsDynamic := "true"
return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic,
resource.TestCheckResourceAttrWith(resourceName, "fields", acc.JSONEquals(fieldsJSON)),
resource.TestCheckResourceAttrWith(datasourceName, "fields", acc.JSONEquals(fieldsJSON)))
}

func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
Expand Down

0 comments on commit d6ac0c8

Please sign in to comment.