elastic

package
v0.28.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 21, 2023 License: MIT Imports: 12 Imported by: 0

README

Gnomock Elasticsearch

Gnomock Elasticsearch is a Gnomock preset for running tests against a real Elasticsearch container, without mocks.

package elastic_test

import (
	"encoding/json"
	"fmt"
	"testing"

	"github.com/elastic/go-elasticsearch/v8"
	"github.com/orlangure/gnomock"
	"github.com/orlangure/gnomock/preset/elastic"
	"github.com/stretchr/testify/require"
)

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

	p := elastic.Preset(
		elastic.WithInputFile("./testdata/titles"),
		elastic.WithInputFile("./testdata/names"),
	)

	c, err := gnomock.Start(p)
	require.NoError(t, err)

	defer func() { require.NoError(t, gnomock.Stop(c)) }()

	cfg := elasticsearch.Config{
		Addresses: []string{fmt.Sprintf("http://%s", c.DefaultAddress())},
	}

	client, err := elasticsearch.NewClient(cfg)
	require.NoError(t, err)

	res, err := client.Search(
		client.Search.WithIndex("titles"),
		client.Search.WithQuery("gnomock"),
	)
	require.NoError(t, err)
	require.False(t, res.IsError(), res.String())

	var out struct {
		Hits struct {
			Total struct {
				Value int `json:"value"`
			} `json:"total"`
		} `json:"hits"`
	}
	require.NoError(t, json.NewDecoder(res.Body).Decode(&out))
	require.NoError(t, res.Body.Close())
	require.Equal(t, 1, out.Hits.Total.Value)

	res, err = client.Search(
		client.Search.WithIndex("titles"),
		client.Search.WithQuery("unknown"),
	)
	require.NoError(t, err)
	require.False(t, res.IsError(), res.String())

	require.NoError(t, json.NewDecoder(res.Body).Decode(&out))
	require.NoError(t, res.Body.Close())
	require.Equal(t, 0, out.Hits.Total.Value)
}

Documentation

Overview

Package elastic provides a Gnomock Preset for Elasticsearch.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Preset

func Preset(opts ...Option) gnomock.Preset

Preset creates a new Gnomock Elasticsearch preset. This preset includes an Elasticsearch specific healthcheck function and default Elasticsearch image and port.

By default, version 8.7.0 is used.

Types

type Option

type Option func(*P)

Option is an optional configuration of this Gnomock preset. Use available Options to configure the container.

func WithInputFile

func WithInputFile(file string) Option

WithInputFile allows to load JSON data located in the provided file into Elasticsearch container once it is up. File name is used as ingestion index. Use multiple files to ingest JSON data into multiple indices.

Data should be provided as a sequence of separate JSON objects, where each object is ingested as a single entity. There is no need to make an array of objects; a regular JSON object sequence without any separators will work.

func WithVersion

func WithVersion(version string) Option

WithVersion sets image version.

type P

type P struct {
	Version string   `json:"version"`
	Inputs  []string `json:"input_files"`
}

P is a Gnomock Preset implementation of Elasticsearch.

func (*P) Image

func (p *P) Image() string

Image returns an image that should be pulled to create this container.

func (*P) Options

func (p *P) Options() []gnomock.Option

Options returns a list of options to configure this container.

func (*P) Ports

func (p *P) Ports() gnomock.NamedPorts

Ports returns ports that should be used to access this container.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL