Skip to content

Commit f2e5bf4

Browse files
author
Andrea Spacca
authored
Unit test corpusgenerator (#1267)
* TDD number of events generated * refatoring for client mocking * add license
1 parent b33d0fa commit f2e5bf4

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

cmd/benchmark.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ func generateDataStreamCorpusCommandAction(cmd *cobra.Command, _ []string) error
269269
return cobraext.FlagParsingError(err, cobraext.GenerateCorpusRallyTrackOutputDirFlagName)
270270
}
271271

272-
generator, err := corpusgenerator.NewGenerator(packageName, dataSetName, commit, totSizeInBytes)
272+
genLibClient := corpusgenerator.NewClient(commit)
273+
generator, err := corpusgenerator.NewGenerator(genLibClient, packageName, dataSetName, totSizeInBytes)
273274
if err != nil {
274275
return errors.Wrap(err, "can't generate benchmarks data corpus for data stream")
275276
}

internal/corpusgenerator/client.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"net/url"
1212
"strings"
1313

14+
"github.com/elastic/elastic-integration-corpus-generator-tool/pkg/genlib"
1415
"github.com/pkg/errors"
1516

1617
"github.com/elastic/elastic-package/internal/logger"
@@ -27,8 +28,15 @@ type Client struct {
2728
commit string
2829
}
2930

31+
// GenLibClient is an interface for the genlib client
32+
type GenLibClient interface {
33+
GetGoTextTemplate(packageName, dataStreamName string) ([]byte, error)
34+
GetConf(packageName, dataStreamName string) (genlib.Config, error)
35+
GetFields(packageName, dataStreamName string) (genlib.Fields, error)
36+
}
37+
3038
// NewClient creates a new instance of the client.
31-
func NewClient(commit string) *Client {
39+
func NewClient(commit string) GenLibClient {
3240
return &Client{commit: commit}
3341
}
3442

internal/corpusgenerator/utils.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,7 @@ func RunGenerator(generator genlib.Generator, dataStream, rallyTrackOutputDir st
7373
return generator.Close()
7474
}
7575

76-
func NewGenerator(packageName, dataStreamName, commit string, totSizeInBytes uint64) (genlib.Generator, error) {
77-
78-
genLibClient := NewClient(commit)
76+
func NewGenerator(genLibClient GenLibClient, packageName, dataStreamName string, totSizeInBytes uint64) (genlib.Generator, error) {
7977

8078
config, err := genLibClient.GetConf(packageName, dataStreamName)
8179
if err != nil {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
2+
// or more contributor license agreements. Licensed under the Elastic License;
3+
// you may not use this file except in compliance with the Elastic License.
4+
5+
package corpusgenerator
6+
7+
import (
8+
"bytes"
9+
"io"
10+
"testing"
11+
12+
"github.com/elastic/elastic-integration-corpus-generator-tool/pkg/genlib"
13+
"github.com/stretchr/testify/assert"
14+
)
15+
16+
type mockClient struct {
17+
}
18+
19+
func (c mockClient) GetGoTextTemplate(packageName, dataStreamName string) ([]byte, error) {
20+
return []byte("7 bytes"), nil
21+
}
22+
func (c mockClient) GetConf(packageName, dataStreamName string) (genlib.Config, error) {
23+
return genlib.Config{}, nil
24+
}
25+
func (c mockClient) GetFields(packageName, dataStreamName string) (genlib.Fields, error) {
26+
return genlib.Fields{}, nil
27+
}
28+
29+
func TestGeneratorEmitTotEvents(t *testing.T) {
30+
generator, err := NewGenerator(mockClient{}, "packageName", "dataSetName", 7)
31+
assert.NoError(t, err)
32+
33+
state := genlib.NewGenState()
34+
35+
totEvents := 0
36+
buf := bytes.NewBufferString("")
37+
for {
38+
err := generator.Emit(state, buf)
39+
if err == io.EOF {
40+
break
41+
}
42+
43+
totEvents += 1
44+
}
45+
46+
assert.Equal(t, 1, totEvents, "expected 1 totEvents, got %d", totEvents)
47+
}

0 commit comments

Comments
 (0)