From 7c597106858cf6e58a2d2502d4d257fbbf5d1d14 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 11:42:47 +0200 Subject: [PATCH 1/9] checkVector --- .../searchindex/resource_search_index_test.go | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index dc0cc3c0ac..f3afb1c857 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -221,21 +221,8 @@ 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, @@ -243,7 +230,7 @@ func basicVectorTestCase(tb testing.TB) *resource.TestCase { Steps: []resource.TestStep{ { Config: configVector(projectID, indexName, databaseName, clusterName), - Check: resource.ComposeAggregateTestCheckFunc(checks...), + Check: checkVector(projectID, indexName, databaseName, clusterName), }, }, } @@ -409,6 +396,25 @@ func configVector(projectID, indexName, databaseName, clusterName string) string `, clusterName, projectID, indexName, databaseName, collectionName, fieldsJSON) } +func checkVector(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc { + indexType := "vectorSearch" + attributes := map[string]string{ + "project_id": projectID, + "name": indexName, + "cluster_name": clusterName, + "database": databaseName, + "collection_name": collectionName, + "type": indexType, + } + checks := addAttrChecks(nil, attributes) + checks = acc.AddAttrSetChecks(resourceName, checks, "index_id") + checks = acc.AddAttrSetChecks(datasourceName, checks, "index_id") + checks = append(checks, + resource.TestCheckResourceAttrWith(resourceName, "fields", acc.JSONEquals(fieldsJSON)), + resource.TestCheckResourceAttrWith(datasourceName, "fields", acc.JSONEquals(fieldsJSON))) + return resource.ComposeAggregateTestCheckFunc(checks...) +} + func importStateIDFunc(resourceName string) resource.ImportStateIdFunc { return func(s *terraform.State) (string, error) { rs, ok := s.RootModule().Resources[resourceName] From 337f0a2662bc99956293f20fcbf2bebc89009e27 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:26:58 +0200 Subject: [PATCH 2/9] checkBasic --- .../searchindex/resource_search_index_test.go | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index f3afb1c857..e2462d56ca 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -20,10 +20,7 @@ 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, @@ -31,7 +28,7 @@ func TestAccSearchIndex_withSearchType(t *testing.T) { Steps: []resource.TestStep{ { Config: configBasic(projectID, indexName, databaseName, clusterName, true), - Check: resource.ComposeAggregateTestCheckFunc(checks...), + Check: checkBasic(projectID, indexName, databaseName, clusterName, true), }, }, }) @@ -45,7 +42,7 @@ func TestAccSearchIndex_withMapping(t *testing.T) { indexType = "" mappingsDynamic = "false" ) - checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName) + checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) checks = addAttrSetChecks(checks, "mappings_fields", "analyzers") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, @@ -74,7 +71,7 @@ func TestAccSearchIndex_withSynonyms(t *testing.T) { "synonyms.0.source_collection": collectionName, } ) - checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName) + checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) checks = addAttrChecks(checks, mapChecks) resource.ParallelTest(t, resource.TestCase{ @@ -98,7 +95,7 @@ func TestAccSearchIndex_updatedToEmptySynonyms(t *testing.T) { indexType = "" mappingsDynamic = "true" ) - checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName) + checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) checks1 := addAttrChecks(checks, map[string]string{ "synonyms.#": "1", "synonyms.0.analyzer": "lucene.simple", @@ -191,11 +188,7 @@ 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, @@ -203,7 +196,7 @@ func basicTestCase(tb testing.TB) *resource.TestCase { Steps: []resource.TestStep{ { Config: configBasic(projectID, indexName, databaseName, clusterName, false), - Check: resource.ComposeAggregateTestCheckFunc(checks...), + Check: checkBasic(projectID, indexName, databaseName, clusterName, false), }, { Config: configBasic(projectID, indexName, databaseName, clusterName, false), @@ -236,17 +229,23 @@ func basicVectorTestCase(tb testing.TB) *resource.TestCase { } } -func commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName string) []resource.TestCheckFunc { +func commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName string, explicitType bool) []resource.TestCheckFunc { attributes := map[string]string{ "name": indexName, "cluster_name": clusterName, "database": databaseName, "collection_name": collectionName, - "type": indexType, "mappings_dynamic": mappingsDynamic, } + indexTypeEffective := "" + if explicitType { + indexTypeEffective = indexType + } checks := addAttrChecks(nil, attributes) - checks = acc.AddAttrSetChecks(resourceName, checks, "project_id") + checks = append(checks, + resource.TestCheckResourceAttr(resourceName, "type", indexTypeEffective), + resource.TestCheckResourceAttr(datasourceName, "type", indexTypeEffective)) + checks = acc.AddAttrSetChecks(resourceName, checks, "project_id", "index_id") return acc.AddAttrSetChecks(datasourceName, checks, "project_id", "index_id") } @@ -303,6 +302,13 @@ func configBasic(projectID, indexName, databaseName, clusterName string, explici `, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, indexType) } +func checkBasic(projectID, indexName, databaseName, clusterName string, explicitType bool) resource.TestCheckFunc { + indexType := "search" + mappingsDynamic := "true" + checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, explicitType) + return resource.ComposeAggregateTestCheckFunc(checks...) +} + func configWithMapping(projectID, indexName, databaseName, clusterName string) string { return fmt.Sprintf(` resource "mongodbatlas_search_index" "test" { From e8fafa06abd515a329e3e1ad6593065e22639e92 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:33:19 +0200 Subject: [PATCH 3/9] checkWithMapping --- .../searchindex/resource_search_index_test.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index e2462d56ca..181a5ef9bb 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -39,11 +39,7 @@ 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, false) - checks = addAttrSetChecks(checks, "mappings_fields", "analyzers") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, @@ -51,7 +47,7 @@ func TestAccSearchIndex_withMapping(t *testing.T) { Steps: []resource.TestStep{ { Config: configWithMapping(projectID, indexName, databaseName, clusterName), - Check: resource.ComposeAggregateTestCheckFunc(checks...), + Check: checkWithMapping(projectID, indexName, databaseName, clusterName), }, }, }) @@ -331,6 +327,14 @@ func configWithMapping(projectID, indexName, databaseName, clusterName string) s `, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, analyzersTF, mappingsFieldsTF) } +func checkWithMapping(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc { + indexType := "" + mappingsDynamic := "false" + checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) + checks = addAttrSetChecks(checks, "mappings_fields", "analyzers") + return resource.ComposeAggregateTestCheckFunc(checks...) +} + func configWithSynonyms(projectID, indexName, databaseName, clusterName string, has bool) string { var synonymsStr string if has { From ad349a3042e24e585d40d300f9e72fb8c19a575f Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:39:49 +0200 Subject: [PATCH 4/9] checkWithSynonyms --- .../searchindex/resource_search_index_test.go | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index 181a5ef9bb..ebfb66bc19 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -58,18 +58,7 @@ 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, false) - checks = addAttrChecks(checks, mapChecks) - resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acc.PreCheckBasic(t) }, ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, @@ -77,7 +66,7 @@ func TestAccSearchIndex_withSynonyms(t *testing.T) { Steps: []resource.TestStep{ { Config: configWithSynonyms(projectID, indexName, databaseName, clusterName, with), - Check: resource.ComposeAggregateTestCheckFunc(checks...), + Check: checkWithSynonyms(projectID, indexName, databaseName, clusterName, with), }, }, }) @@ -88,17 +77,7 @@ 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, false) - 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, @@ -106,11 +85,11 @@ func TestAccSearchIndex_updatedToEmptySynonyms(t *testing.T) { 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), }, }, }) @@ -367,6 +346,23 @@ func configWithSynonyms(projectID, indexName, databaseName, clusterName string, `, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, synonymsStr) } +func checkWithSynonyms(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { + indexType := "" + mappingsDynamic := "true" + checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) + 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"}) + if has { + return resource.ComposeAggregateTestCheckFunc(checks1...) + } + return resource.ComposeAggregateTestCheckFunc(checks2...) +} + func configAdditional(projectID, indexName, databaseName, clusterName, additional string) string { return fmt.Sprintf(` resource "mongodbatlas_search_index" "test" { From b7e0d99076839bd35b732e1a1fe9e01ac0b3114e Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 12:55:58 +0200 Subject: [PATCH 5/9] checkAdditional --- .../searchindex/resource_search_index_test.go | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index ebfb66bc19..53b3f1749f 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -108,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: checkAdditional(projectID, indexName, databaseName, clusterName, true), }, { Config: configAdditional(projectID, indexName, databaseName, clusterName, ""), - Check: resource.ComposeAggregateTestCheckFunc( - checkExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "analyzers", ""), - ), + Check: checkAdditional(projectID, indexName, databaseName, clusterName, false), }, }, }) @@ -378,6 +372,15 @@ func configAdditional(projectID, indexName, databaseName, clusterName, additiona `, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, additional) } +func checkAdditional(projectID, indexName, databaseName, clusterName string, hasAnalyzers bool) resource.TestCheckFunc { + checks := []resource.TestCheckFunc{checkExists(resourceName)} + if hasAnalyzers { + checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON))) + } else { + checks = append(checks, resource.TestCheckResourceAttr(resourceName, "analyzers", "")) + } + return resource.ComposeAggregateTestCheckFunc(checks...) +} func configVector(projectID, indexName, databaseName, clusterName string) string { return fmt.Sprintf(` resource "mongodbatlas_search_index" "test" { From cd3bb193831fe651799d52a339ae5fb4fc735d1d Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:05:19 +0200 Subject: [PATCH 6/9] checkAdditionalAnalyzers and checkAdditionalMappingsFields --- .../searchindex/resource_search_index_test.go | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index 53b3f1749f..cca2ae0d8d 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -108,11 +108,11 @@ func TestAccSearchIndex_updatedToEmptyAnalyzers(t *testing.T) { Steps: []resource.TestStep{ { Config: configAdditional(projectID, indexName, databaseName, clusterName, analyzersTF), - Check: checkAdditional(projectID, indexName, databaseName, clusterName, true), + Check: checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName, true), }, { Config: configAdditional(projectID, indexName, databaseName, clusterName, ""), - Check: checkAdditional(projectID, indexName, databaseName, clusterName, false), + Check: checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName, false), }, }, }) @@ -131,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), }, }, }) @@ -372,15 +366,26 @@ func configAdditional(projectID, indexName, databaseName, clusterName, additiona `, clusterName, projectID, indexName, databaseName, collectionName, searchAnalyzer, additional) } -func checkAdditional(projectID, indexName, databaseName, clusterName string, hasAnalyzers bool) resource.TestCheckFunc { +func checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { checks := []resource.TestCheckFunc{checkExists(resourceName)} - if hasAnalyzers { + if has { checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON))) } else { checks = append(checks, resource.TestCheckResourceAttr(resourceName, "analyzers", "")) } return resource.ComposeAggregateTestCheckFunc(checks...) } + +func checkAdditionalMappingsFields(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { + checks := []resource.TestCheckFunc{checkExists(resourceName)} + if has { + checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "mappings_fields", acc.JSONEquals(mappingsFieldsJSON))) + } else { + checks = append(checks, resource.TestCheckResourceAttr(resourceName, "mappings_fields", "")) + } + return resource.ComposeAggregateTestCheckFunc(checks...) +} + func configVector(projectID, indexName, databaseName, clusterName string) string { return fmt.Sprintf(` resource "mongodbatlas_search_index" "test" { From a70153b0301d94c3435d077f44bcdf04329a457a Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:23:02 +0200 Subject: [PATCH 7/9] remove addAttrChecks and addAttrSetChecks --- .../searchindex/resource_search_index_test.go | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index cca2ae0d8d..552c7b1910 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -192,8 +192,9 @@ func basicVectorTestCase(tb testing.TB) *resource.TestCase { } } -func commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName string, explicitType bool) []resource.TestCheckFunc { +func commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName string, explicitType bool) []resource.TestCheckFunc { attributes := map[string]string{ + "project_id": projectID, "name": indexName, "cluster_name": clusterName, "database": databaseName, @@ -204,22 +205,14 @@ func commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterNa if explicitType { indexTypeEffective = indexType } - checks := addAttrChecks(nil, attributes) - checks = append(checks, + checks := []resource.TestCheckFunc{ resource.TestCheckResourceAttr(resourceName, "type", indexTypeEffective), - resource.TestCheckResourceAttr(datasourceName, "type", indexTypeEffective)) - checks = acc.AddAttrSetChecks(resourceName, checks, "project_id", "index_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...) + resource.TestCheckResourceAttr(datasourceName, "type", indexTypeEffective), + } + checks = acc.AddAttrChecks(resourceName, checks, attributes) + checks = acc.AddAttrChecks(datasourceName, checks, attributes) + checks = acc.AddAttrSetChecks(resourceName, checks, "index_id") + return acc.AddAttrSetChecks(datasourceName, checks, "index_id") } func checkExists(resourceName string) resource.TestCheckFunc { @@ -268,7 +261,7 @@ func configBasic(projectID, indexName, databaseName, clusterName string, explici func checkBasic(projectID, indexName, databaseName, clusterName string, explicitType bool) resource.TestCheckFunc { indexType := "search" mappingsDynamic := "true" - checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, explicitType) + checks := commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName, explicitType) return resource.ComposeAggregateTestCheckFunc(checks...) } @@ -297,8 +290,10 @@ func configWithMapping(projectID, indexName, databaseName, clusterName string) s func checkWithMapping(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc { indexType := "" mappingsDynamic := "false" - checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) - checks = addAttrSetChecks(checks, "mappings_fields", "analyzers") + attrNames := []string{"mappings_fields", "analyzers"} + checks := commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName, false) + checks = acc.AddAttrSetChecks(resourceName, checks, attrNames...) + checks = acc.AddAttrSetChecks(datasourceName, checks, attrNames...) return resource.ComposeAggregateTestCheckFunc(checks...) } @@ -337,18 +332,19 @@ func configWithSynonyms(projectID, indexName, databaseName, clusterName string, func checkWithSynonyms(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { indexType := "" mappingsDynamic := "true" - checks := commonChecks(indexName, indexType, mappingsDynamic, databaseName, clusterName, false) - 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"}) + attrs := map[string]string{"synonyms.#": "0"} if has { - return resource.ComposeAggregateTestCheckFunc(checks1...) + attrs = map[string]string{ + "synonyms.#": "1", + "synonyms.0.analyzer": "lucene.simple", + "synonyms.0.name": "synonym_test", + "synonyms.0.source_collection": collectionName, + } } - return resource.ComposeAggregateTestCheckFunc(checks2...) + checks := commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName, false) + checks = acc.AddAttrChecks(resourceName, checks, attrs) + checks = acc.AddAttrChecks(datasourceName, checks, attrs) + return resource.ComposeAggregateTestCheckFunc(checks...) } func configAdditional(projectID, indexName, databaseName, clusterName, additional string) string { @@ -420,7 +416,8 @@ func checkVector(projectID, indexName, databaseName, clusterName string) resourc "collection_name": collectionName, "type": indexType, } - checks := addAttrChecks(nil, attributes) + checks := acc.AddAttrChecks(resourceName, nil, attributes) + checks = acc.AddAttrChecks(datasourceName, checks, attributes) checks = acc.AddAttrSetChecks(resourceName, checks, "index_id") checks = acc.AddAttrSetChecks(datasourceName, checks, "index_id") checks = append(checks, From 231f38e7ad1e4b10a3b95b8ecea24f52f918fb66 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 13:59:28 +0200 Subject: [PATCH 8/9] use commonChecks in all checks --- .../searchindex/resource_search_index_test.go | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index 552c7b1910..5203f3136c 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -192,20 +192,23 @@ func basicVectorTestCase(tb testing.TB) *resource.TestCase { } } -func commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName string, explicitType bool) []resource.TestCheckFunc { +func commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic string, explicitType bool) []resource.TestCheckFunc { attributes := map[string]string{ - "project_id": projectID, - "name": indexName, - "cluster_name": clusterName, - "database": databaseName, - "collection_name": collectionName, - "mappings_dynamic": mappingsDynamic, + "project_id": projectID, + "name": indexName, + "cluster_name": clusterName, + "database": databaseName, + "collection_name": collectionName, + } + if indexType != "vectorSearch" { + attributes["mappings_dynamic"] = mappingsDynamic } indexTypeEffective := "" if explicitType { indexTypeEffective = indexType } checks := []resource.TestCheckFunc{ + checkExists(resourceName), resource.TestCheckResourceAttr(resourceName, "type", indexTypeEffective), resource.TestCheckResourceAttr(datasourceName, "type", indexTypeEffective), } @@ -251,8 +254,8 @@ 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) @@ -261,7 +264,7 @@ func configBasic(projectID, indexName, databaseName, clusterName string, explici func checkBasic(projectID, indexName, databaseName, clusterName string, explicitType bool) resource.TestCheckFunc { indexType := "search" mappingsDynamic := "true" - checks := commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName, explicitType) + checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, explicitType) return resource.ComposeAggregateTestCheckFunc(checks...) } @@ -280,8 +283,8 @@ 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) @@ -291,7 +294,7 @@ func checkWithMapping(projectID, indexName, databaseName, clusterName string) re indexType := "" mappingsDynamic := "false" attrNames := []string{"mappings_fields", "analyzers"} - checks := commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName, false) + checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) checks = acc.AddAttrSetChecks(resourceName, checks, attrNames...) checks = acc.AddAttrSetChecks(datasourceName, checks, attrNames...) return resource.ComposeAggregateTestCheckFunc(checks...) @@ -322,8 +325,8 @@ 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) @@ -341,7 +344,7 @@ func checkWithSynonyms(projectID, indexName, databaseName, clusterName string, h "synonyms.0.source_collection": collectionName, } } - checks := commonChecks(projectID, indexName, indexType, mappingsDynamic, databaseName, clusterName, false) + checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) checks = acc.AddAttrChecks(resourceName, checks, attrs) checks = acc.AddAttrChecks(datasourceName, checks, attrs) return resource.ComposeAggregateTestCheckFunc(checks...) @@ -359,11 +362,19 @@ 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 { - checks := []resource.TestCheckFunc{checkExists(resourceName)} + indexType := "" + mappingsDynamic := "true" + checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) if has { checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON))) } else { @@ -373,7 +384,9 @@ func checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName st } func checkAdditionalMappingsFields(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { - checks := []resource.TestCheckFunc{checkExists(resourceName)} + indexType := "" + mappingsDynamic := "true" + checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) if has { checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "mappings_fields", acc.JSONEquals(mappingsFieldsJSON))) } else { @@ -399,8 +412,8 @@ 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) @@ -408,6 +421,7 @@ func configVector(projectID, indexName, databaseName, clusterName string) string func checkVector(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc { indexType := "vectorSearch" + mappingsDynamic := "true" attributes := map[string]string{ "project_id": projectID, "name": indexName, @@ -416,7 +430,8 @@ func checkVector(projectID, indexName, databaseName, clusterName string) resourc "collection_name": collectionName, "type": indexType, } - checks := acc.AddAttrChecks(resourceName, nil, attributes) + checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, true) + 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") From 29571c0a5c20af1c77d25fd8ad92fb3edcf7752a Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:30:15 +0200 Subject: [PATCH 9/9] test checks cleanup --- .../searchindex/resource_search_index_test.go | 83 +++++++------------ 1 file changed, 29 insertions(+), 54 deletions(-) diff --git a/internal/service/searchindex/resource_search_index_test.go b/internal/service/searchindex/resource_search_index_test.go index 5203f3136c..d0edb9cc84 100644 --- a/internal/service/searchindex/resource_search_index_test.go +++ b/internal/service/searchindex/resource_search_index_test.go @@ -27,8 +27,8 @@ func TestAccSearchIndex_withSearchType(t *testing.T) { CheckDestroy: acc.CheckDestroySearchIndex, Steps: []resource.TestStep{ { - Config: configBasic(projectID, indexName, databaseName, clusterName, true), - Check: checkBasic(projectID, indexName, databaseName, clusterName, true), + Config: configBasic(projectID, clusterName, indexName, "search", databaseName), + Check: checkBasic(projectID, clusterName, indexName, "search", databaseName), }, }, }) @@ -158,11 +158,11 @@ func basicTestCase(tb testing.TB) *resource.TestCase { CheckDestroy: acc.CheckDestroySearchIndex, Steps: []resource.TestStep{ { - Config: configBasic(projectID, indexName, databaseName, clusterName, false), - Check: checkBasic(projectID, indexName, databaseName, clusterName, false), + 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, @@ -192,30 +192,27 @@ func basicVectorTestCase(tb testing.TB) *resource.TestCase { } } -func commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic string, explicitType bool) []resource.TestCheckFunc { +func checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic string, extra ...resource.TestCheckFunc) resource.TestCheckFunc { attributes := map[string]string{ "project_id": projectID, - "name": indexName, "cluster_name": clusterName, + "name": indexName, + "type": indexType, "database": databaseName, "collection_name": collectionName, } if indexType != "vectorSearch" { attributes["mappings_dynamic"] = mappingsDynamic } - indexTypeEffective := "" - if explicitType { - indexTypeEffective = indexType - } checks := []resource.TestCheckFunc{ checkExists(resourceName), - resource.TestCheckResourceAttr(resourceName, "type", indexTypeEffective), - resource.TestCheckResourceAttr(datasourceName, "type", indexTypeEffective), } checks = acc.AddAttrChecks(resourceName, checks, attributes) checks = acc.AddAttrChecks(datasourceName, checks, attributes) checks = acc.AddAttrSetChecks(resourceName, checks, "index_id") - return acc.AddAttrSetChecks(datasourceName, checks, "index_id") + checks = acc.AddAttrSetChecks(datasourceName, checks, "index_id") + checks = append(checks, extra...) + return resource.ComposeAggregateTestCheckFunc(checks...) } func checkExists(resourceName string) resource.TestCheckFunc { @@ -236,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" { @@ -258,14 +255,12 @@ func configBasic(projectID, indexName, databaseName, clusterName string, explici 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, indexName, databaseName, clusterName string, explicitType bool) resource.TestCheckFunc { - indexType := "search" +func checkBasic(projectID, clusterName, indexName, indexType, databaseName string) resource.TestCheckFunc { mappingsDynamic := "true" - checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, explicitType) - return resource.ComposeAggregateTestCheckFunc(checks...) + return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic) } func configWithMapping(projectID, indexName, databaseName, clusterName string) string { @@ -294,10 +289,9 @@ func checkWithMapping(projectID, indexName, databaseName, clusterName string) re indexType := "" mappingsDynamic := "false" attrNames := []string{"mappings_fields", "analyzers"} - checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) - checks = acc.AddAttrSetChecks(resourceName, checks, attrNames...) + checks := acc.AddAttrSetChecks(resourceName, nil, attrNames...) checks = acc.AddAttrSetChecks(datasourceName, checks, attrNames...) - return resource.ComposeAggregateTestCheckFunc(checks...) + return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, checks...) } func configWithSynonyms(projectID, indexName, databaseName, clusterName string, has bool) string { @@ -344,10 +338,9 @@ func checkWithSynonyms(projectID, indexName, databaseName, clusterName string, h "synonyms.0.source_collection": collectionName, } } - checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) - checks = acc.AddAttrChecks(resourceName, checks, attrs) + checks := acc.AddAttrChecks(resourceName, nil, attrs) checks = acc.AddAttrChecks(datasourceName, checks, attrs) - return resource.ComposeAggregateTestCheckFunc(checks...) + return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, checks...) } func configAdditional(projectID, indexName, databaseName, clusterName, additional string) string { @@ -374,25 +367,21 @@ func configAdditional(projectID, indexName, databaseName, clusterName, additiona func checkAdditionalAnalyzers(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { indexType := "" mappingsDynamic := "true" - checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) + check := resource.TestCheckResourceAttr(resourceName, "analyzers", "") if has { - checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON))) - } else { - checks = append(checks, resource.TestCheckResourceAttr(resourceName, "analyzers", "")) + check = resource.TestCheckResourceAttrWith(resourceName, "analyzers", acc.JSONEquals(analyzersJSON)) } - return resource.ComposeAggregateTestCheckFunc(checks...) + return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, check) } func checkAdditionalMappingsFields(projectID, indexName, databaseName, clusterName string, has bool) resource.TestCheckFunc { indexType := "" mappingsDynamic := "true" - checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, false) + check := resource.TestCheckResourceAttr(resourceName, "mappings_fields", "") if has { - checks = append(checks, resource.TestCheckResourceAttrWith(resourceName, "mappings_fields", acc.JSONEquals(mappingsFieldsJSON))) - } else { - checks = append(checks, resource.TestCheckResourceAttr(resourceName, "mappings_fields", "")) + check = resource.TestCheckResourceAttrWith(resourceName, "mappings_fields", acc.JSONEquals(mappingsFieldsJSON)) } - return resource.ComposeAggregateTestCheckFunc(checks...) + return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, check) } func configVector(projectID, indexName, databaseName, clusterName string) string { @@ -422,23 +411,9 @@ func configVector(projectID, indexName, databaseName, clusterName string) string func checkVector(projectID, indexName, databaseName, clusterName string) resource.TestCheckFunc { indexType := "vectorSearch" mappingsDynamic := "true" - attributes := map[string]string{ - "project_id": projectID, - "name": indexName, - "cluster_name": clusterName, - "database": databaseName, - "collection_name": collectionName, - "type": indexType, - } - checks := commonChecks(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, true) - 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, + return checkAggr(projectID, clusterName, indexName, indexType, databaseName, mappingsDynamic, resource.TestCheckResourceAttrWith(resourceName, "fields", acc.JSONEquals(fieldsJSON)), resource.TestCheckResourceAttrWith(datasourceName, "fields", acc.JSONEquals(fieldsJSON))) - return resource.ComposeAggregateTestCheckFunc(checks...) } func importStateIDFunc(resourceName string) resource.ImportStateIdFunc {