Skip to content

Commit

Permalink
Merge pull request #96 from k-yomo/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
k-yomo authored May 21, 2022
2 parents 90f86d8 + 72e5f00 commit 0a71b5e
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_API_KEY }}

run: |
go test -v -coverprofile=coverage.out ./internal/provider/
go test -v -coverprofile=coverage.out ./internal/...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
Expand Down
5 changes: 3 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ Then commit the changes to `go.mod` and `go.sum`.

## Developing the Provider

If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Prerequisites](#requirements) above).
If you wish to work on the provider, you'll first need [Go](http://www.golang.org) installed on your machine (see [Prerequisites](#Prerequisites) above).

When you changed schema or `examples` directory, you need to regenerate documents.
To generate or update documentation, run `make generate`.

### Test
Expand All @@ -57,5 +58,5 @@ Run `make testacc` to run the full suite of Acceptance tests.
```sh
$ make testacc
```
*Note:* Acceptance tests create real resources, and often cost money to run.
*Note:* Acceptance tests create real resources, and might cost money to run.

44 changes: 44 additions & 0 deletions internal/algoliautil/region_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package algoliautil

import "testing"

func TestIsValidRegion(t *testing.T) {
t.Parallel()

type args struct {
r string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "returns true if 'us' region",
args: args{r: "us"},
want: true,
},
{
name: "returns true if 'eu' region",
args: args{r: "eu"},
want: true,
},
{
name: "returns true if 'de' region",
args: args{r: "de"},
want: true,
},
{
name: "returns false if invalid region",
args: args{r: "invalid"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := IsValidRegion(tt.args.r); got != tt.want {
t.Errorf("IsValidRegion() = %v, want %v", got, tt.want)
}
})
}
}
32 changes: 27 additions & 5 deletions internal/provider/resource_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

// TODO: Cover all params
// TODO: Cover all fields
func TestAccResourceIndex(t *testing.T) {
indexName := randStringStartWithAlpha(100)
resourceName := fmt.Sprintf("algolia_index.%s", indexName)
Expand Down Expand Up @@ -63,6 +63,14 @@ func TestAccResourceIndex(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.highlight_post_tag", "</b>"),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.snippet_ellipsis_text", "..."),
resource.TestCheckResourceAttr(resourceName, "highlight_and_snippet_config.0.restrict_highlight_and_snippet_arrays", "true"),
resource.TestCheckResourceAttr(resourceName, "pagination_config.0.hits_per_page", "100"),
resource.TestCheckResourceAttr(resourceName, "pagination_config.0.pagination_limited_to", "500"),
resource.TestCheckResourceAttr(resourceName, "typos_config.0.min_word_size_for_1_typo", "3"),
resource.TestCheckResourceAttr(resourceName, "typos_config.0.min_word_size_for_2_typos", "6"),
resource.TestCheckResourceAttr(resourceName, "typos_config.0.typo_tolerance", "strict"),
resource.TestCheckResourceAttr(resourceName, "typos_config.0.allow_typos_on_numeric_tokens", "false"),
testCheckResourceListAttr(resourceName, "typos_config.0.disable_typo_tolerance_on_attributes", []string{"model"}),
testCheckResourceListAttr(resourceName, "typos_config.0.disable_typo_tolerance_on_words", []string{"test"}),
resource.TestCheckResourceAttr(resourceName, "deletion_protection", "false"),
),
},
Expand Down Expand Up @@ -139,9 +147,9 @@ resource "algolia_index" "%s" {
}

func testAccResourceIndexUpdate(name string) string {
return fmt.Sprintf(`
resource "algolia_index" "%s" {
name = "%s"
return `
resource "algolia_index" "` + name + `" {
name = "` + name + `"
attributes_config {
searchable_attributes = [
Expand Down Expand Up @@ -185,13 +193,27 @@ resource "algolia_index" "%s" {
restrict_highlight_and_snippet_arrays = true
}
pagination_config {
hits_per_page = 100
pagination_limited_to = 500
}
typos_config {
min_word_size_for_1_typo = 3
min_word_size_for_2_typos = 6
typo_tolerance = "strict"
allow_typos_on_numeric_tokens = false
disable_typo_tolerance_on_attributes = ["model"]
disable_typo_tolerance_on_words = ["test"]
}
languages_config {
remove_stop_words_for = ["en"]
}
deletion_protection = false
}
`, name, name)
`
}

func testAccResourceVirtualIndex(name string, virtualIndexName string) string {
Expand Down
2 changes: 1 addition & 1 deletion scripts/teardown.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func main() {
eg.Go(func() error {
res, err := algoliaClient.InitIndex(index.Name).Delete()
if err != nil {
return fmt.Errorf("failed to delete %s", index.Name)
return fmt.Errorf("failed to delete %s: %w", index.Name, err)
}
if err := res.Wait(); err != nil {
return fmt.Errorf("failed to delete %s: %w", index.Name, err)
Expand Down

0 comments on commit 0a71b5e

Please sign in to comment.