-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from shashank-boyapally/main
Unit Testing Indexers
- Loading branch information
Showing
8 changed files
with
489 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package indexers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("Tests for elastic.go", func() { | ||
Context("Tests for new()", func() { | ||
var testcase newMethodTestcase | ||
var indexer Elastic | ||
BeforeEach(func() { | ||
testcase = newMethodTestcase{ | ||
indexerConfig: IndexerConfig{Type: "elastic", | ||
Servers: []string{}, | ||
Index: "go-commons-test", | ||
InsecureSkipVerify: true, | ||
}, | ||
mockServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(payload) | ||
})), | ||
} | ||
|
||
indexer.index = "go-commons-test" | ||
}) | ||
|
||
It("Returns error status bad request", func() { | ||
testcase.mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusBadRequest) | ||
})) | ||
defer testcase.mockServer.Close() | ||
testcase.indexerConfig.Servers = []string{testcase.mockServer.URL} | ||
err := indexer.new(testcase.indexerConfig) | ||
Expect(err).To(BeEquivalentTo(errors.New("unexpected ES status code: 400"))) | ||
}) | ||
|
||
It("when no url is passed", func() { | ||
err := indexer.new(testcase.indexerConfig) | ||
testcase.mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusGatewayTimeout) | ||
})) | ||
//using .Error() to convert to string as the error which is generated contains port and is dynamic | ||
Expect(err.Error()).To(ContainSubstring("connect: connection refused")) | ||
}) | ||
|
||
It("Returns err not passing a valid URL in env variable", func() { | ||
testcase.indexerConfig.Servers = []string{} | ||
os.Setenv("ELASTICSEARCH_URL", "not a valid url:port") | ||
defer os.Unsetenv("ELASTICSEARCH_URL") | ||
defer testcase.mockServer.Close() | ||
err := indexer.new(testcase.indexerConfig) | ||
Expect(err).To(BeEquivalentTo(errors.New("error creating the ES client: cannot create client: cannot parse url: parse \"not a valid url:port\": first path segment in URL cannot contain colon"))) | ||
}) | ||
|
||
It("Returns err no index name", func() { | ||
defer testcase.mockServer.Close() | ||
testcase.indexerConfig.Servers = []string{testcase.mockServer.URL} | ||
testcase.indexerConfig.Index = "" | ||
err := indexer.new(testcase.indexerConfig) | ||
|
||
Expect(err).To(BeEquivalentTo(errors.New("index name not specified"))) | ||
}) | ||
|
||
}) | ||
|
||
Context("Tests for Index()", func() { | ||
var testcase indexMethodTestcase | ||
var indexer Elastic | ||
BeforeEach(func() { | ||
testcase = indexMethodTestcase{ | ||
documents: []interface{}{ | ||
"example document", | ||
42, | ||
3.14, | ||
false, | ||
struct { | ||
Name string | ||
Age int | ||
}{ | ||
Name: "John Doe", | ||
Age: 25, | ||
}, | ||
map[string]interface{}{ | ||
"key1": "value1", | ||
"key2": 123, | ||
"key3": true, | ||
}}, | ||
opts: IndexingOpts{ | ||
MetricName: "placeholder", | ||
JobName: "placeholder", | ||
}, | ||
} | ||
}) | ||
|
||
It("No err returned", func() { | ||
_, err := indexer.Index(testcase.documents, testcase.opts) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("err returned docs not processed", func() { | ||
testcase.documents = append(testcase.documents, make(chan string)) | ||
_, err := indexer.Index(testcase.documents, testcase.opts) | ||
Expect(err.Error()).To(ContainSubstring("Cannot encode document")) | ||
}) | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package indexers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
// Testing factory.go | ||
var _ = Describe("Factory.go Unit Tests: NewIndexer()", func() { | ||
var testcase newMethodTestcase | ||
BeforeEach(func() { | ||
testcase = newMethodTestcase{ | ||
indexerConfig: IndexerConfig{Type: "elastic", | ||
Servers: []string{""}, | ||
Index: "go-commons-test", | ||
InsecureSkipVerify: true, | ||
}, | ||
mockServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(payload) | ||
})), | ||
} | ||
}) | ||
|
||
Context("Default behaviour of NewIndexer()", func() { | ||
It("returns indexer and nil", func() { | ||
defer testcase.mockServer.Close() | ||
testcase.indexerConfig.Servers = []string{testcase.mockServer.URL} | ||
_, err := NewIndexer(testcase.indexerConfig) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
It("returns indexer and err status bad gateway", func() { | ||
testcase.mockServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusBadGateway) | ||
})) | ||
defer testcase.mockServer.Close() | ||
testcase.indexerConfig.Servers = []string{testcase.mockServer.URL} | ||
_, err := NewIndexer(testcase.indexerConfig) | ||
|
||
Expect(err).To(BeEquivalentTo(errors.New("unexpected ES status code: 502"))) | ||
}) | ||
|
||
It("returns indexer and err unknown indexer", func() { | ||
defer testcase.mockServer.Close() | ||
testcase.indexerConfig.Servers = []string{testcase.mockServer.URL} | ||
testcase.indexerConfig.Type = "Unknown" | ||
_, err := NewIndexer(testcase.indexerConfig) | ||
Expect(err).To(BeEquivalentTo(errors.New("Indexer not found: Unknown"))) | ||
}) | ||
|
||
}) | ||
}) | ||
|
||
// Unit Test to call opensearch.new() | ||
var _ = Describe("Factory.go Unit Tests: NewIndexer()", func() { | ||
var testcase newMethodTestcase | ||
BeforeEach(func() { | ||
testcase = newMethodTestcase{ | ||
indexerConfig: IndexerConfig{Type: "opensearch", | ||
Servers: []string{""}, | ||
Index: "go-commons-test", | ||
InsecureSkipVerify: true, | ||
}, | ||
mockServer: httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write(payload) | ||
})), | ||
} | ||
}) | ||
|
||
Context("Default behaviour of NewIndexer()", func() { | ||
It("returns indexer and nil", func() { | ||
defer testcase.mockServer.Close() | ||
testcase.indexerConfig.Servers = []string{testcase.mockServer.URL} | ||
_, err := NewIndexer(testcase.indexerConfig) | ||
Expect(err).To(BeNil()) | ||
}) | ||
|
||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package indexers_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestIndexers(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Indexers Suite") | ||
|
||
} |
Oops, something went wrong.