Documentation
¶
Overview ¶
Package ostesting provides a way to conveniently test against a real openSearch instance. It is in the same fashion of https://github.com/peterldowns/pgtestdb You need to have an OpenSearch instance running that the tests can connect to.
Index ¶
- Constants
- func GetDocuments[T any](t *testing.T, tester *Tester, index string) []T
- func GetDocumentsReturningError[T any](tester *Tester, index string) ([]T, error)
- func RunNotParallelOption(tst *Tester)
- func ToAnySlice[T any](input []T) []any
- type ClientConfig
- type TestType
- type Tester
- func (tst Tester) Config() ClientConfig
- func (tst Tester) CreateDocuments(t *testing.T, index string, docs []any, ids []string)
- func (tst Tester) CreateDocumentsReturningError(index string, docs []any, ids []string) error
- func (tst Tester) DeleteIndex(t *testing.T, index string)
- func (tst Tester) GetTestTypeDocuments(t *testing.T, index string) []TestType
- func (tst Tester) NewIndex(t *testing.T, prefix string, mapping *string) string
- func (tst Tester) NewIndexAlias(t *testing.T, prefix string, mapping *string) (index, alias string)
- func (tst Tester) NewNamedIndexAlias(t *testing.T, name string, mapping *string) string
- func (tst Tester) NewTestTypeIndex(t *testing.T, prefix string) string
- func (tst Tester) NewTestTypeIndexAlias(t *testing.T, prefix string) (index, alias string)
- func (tst Tester) OSClient() *opensearchapi.Client
- func (tst Tester) RefreshIndex(t *testing.T, index string)
- func (tst Tester) T() *testing.T
- type TesterOption
Constants ¶
const KeepFailedEnv = "TEST_KEEP_FAILED"
KeepFailedEnv when set keeps the environment and test data after test execution failed. This is useful for debugging failed tests.
Variables ¶
This section is empty.
Functions ¶
func GetDocuments ¶
GetDocuments returns all documents added to test index
func GetDocumentsReturningError ¶
GetDocumentsReturningError returns all documents added to test index and returns error (instead of failing the tests like GetDocuments or [GetTestTypeDocuments])
func RunNotParallelOption ¶
func RunNotParallelOption(tst *Tester)
RunNotParallelOption signifies that tests associated with Tester should not be run in parallel (by default they are)
func ToAnySlice ¶
ToAnySlice converts a slice of any type to a slice of []any. Useful when passing documents to Tester.CreateDocuments.
Types ¶
type ClientConfig ¶
type TestType ¶
type TestType struct {
ID string `json:"id"` // for easier identification in tests
Text string `json:"text"`
Keyword string `json:"keyword"`
TextAndKeyword string `json:"textAndKeyword"`
Integer int `json:"integer"`
Float float32 `json:"float"`
Boolean bool `json:"boolean"`
DateTimeStr string `json:"dateTimeStr,omitempty"`
DateTime time.Time `json:"dateTime"`
KeywordOmitEmpty string `json:"keywordOmitEmpty,omitempty"`
}
TestType can be used as generic document object for testing
type Tester ¶
type Tester struct {
// contains filtered or unexported fields
}
Tester manages connecting with testing OpenSearch instance and implements helper methods
func NewTester ¶
func NewTester(t *testing.T, opts ...TesterOption) *Tester
NewTester initializes new Tester It runs tests associated with [t] as parallel by default, unless runNotParallel option is provided.
func (Tester) Config ¶
func (tst Tester) Config() ClientConfig
Config returns opensearch config that can be used to initialize client using test OpenSearch instance
func (Tester) CreateDocuments ¶
CreateDocuments creates documents [docs] on index [index] with IDs [ids]. If provided [ids] is nil the IDs for created documents will be generated.
func (Tester) CreateDocumentsReturningError ¶
CreateDocumentsReturningError creates documents [docs] on index [index] with IDs [ids]. If provided [ids] is nil the IDs for created documents will be generated. Instead of using *testing.T to fail on errors (like CreateDocuments) it returns error. Can be used when we expect error on creating documents in index and do not want to fail a test on it.
func (Tester) DeleteIndex ¶
DeleteIndex deletes indices with given name (or pattern, eg. "index-name*")
func (Tester) GetTestTypeDocuments ¶
GetTestTypeDocuments returns all documents of type TestType from [index]
func (Tester) NewIndex ¶
NewIndex creates new index with unique name generated based on provided [prefix]. It returns the generated index name. Note: in most cases the _IndexAlias functions should be used that create both index and add it to alias as this represents the typical usage of indices (accessing them through alias) in the code.
func (Tester) NewIndexAlias ¶
NewIndexAlias creates new uniquely named index together with associated alias using mapping if not nil (otherwise with dynamic mapping). It also registers [t].Cleanup function that deletes the index after test.
[prefix] is the prefix name of the index that would be used to generate new unique index and alias name. Generation of unique index name is there to allow tests running in parallel and not interfere with each other.
Internally opensearchapi.Client is called directly rather than opensearch.Client, to avoid using tested object in testing setup and to not have to adjust opensearch.Client methods just for the sake of being used by Tester (as tester use-case of generating unique index/alias with custom mapping differs from production use-cases).
Upon successful creation the function returns index and associated alias names, in case of error [t] is used to mark test as failed. Note: usually the index should be accessed through alias name. In some cases the method needs to receive the concrete index instead.
func (Tester) NewNamedIndexAlias ¶
NewNamedIndexAlias works like Tester.NewIndexAlias, except the name of the newly created index alias will be exactly as provided with [name] instead of being generated based on provided prefix. On successful creation it returns the concrete underlying index name It can be used in testing functionalities that rely on defined index alias name (eg. IndexVTS) and for which generated index alias name would not work. It goes with a drawback of having to make sure that other test run in parallel will not use the same index alias and interfere - while internal indices names are still unique, they would refer to the same alias.
func (Tester) NewTestTypeIndex ¶
NewTestTypeIndex creates new index appropriate to use with [testType] documents with given [prefix] of index name. Internally it calls Tester.NewIndex.
func (Tester) NewTestTypeIndexAlias ¶
NewTestTypeIndexAlias creates new index appropriate to use with [testType] documents with given index and alias names [prefix] and returns new index and alias names. Internally it calls Tester.NewIndexAlias.
func (Tester) OSClient ¶
func (tst Tester) OSClient() *opensearchapi.Client
OSClient returns *opensearchapi.Client associated with test OpenSearch instance
func (Tester) RefreshIndex ¶
RefreshIndex waits for index to refresh, so the updated documents can be obtained
type TesterOption ¶
type TesterOption func(tst *Tester)
func WithAddress ¶
func WithAddress(address string) TesterOption
WithAddress sets the custom address of testing opensearch instance to which the tester should point to
func WithConfig ¶
func WithConfig(conf ClientConfig) TesterOption
WithConfig is an option to use custom ClientConfig for tester.