This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
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 #65 from vjeantet/tests
new tests bugfix filter-change
- Loading branch information
Showing
9 changed files
with
1,005 additions
and
324 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package processors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/vjeantet/bitfan/codecs" | ||
"github.com/vjeantet/bitfan/processors/doc" | ||
"gopkg.in/go-playground/validator.v8" | ||
) | ||
|
||
func TestBaseNew(t *testing.T) { | ||
p := &Base{} | ||
p.SetPipelineUUID("123456") | ||
assert.Implements(t, (*Processor)(nil), p) | ||
assert.Equal(t, "123456", p.PipelineUUID) | ||
assert.IsType(t, (*doc.Processor)(nil), p.Doc()) | ||
assert.ObjectsAreEqual(p, p.B()) | ||
} | ||
|
||
func TestBaseMaxConcurentIsZero(t *testing.T) { | ||
p := &Base{} | ||
assert.Equal(t, 0, p.MaxConcurent()) | ||
} | ||
|
||
func TestBaseMethods(t *testing.T) { | ||
p := &Base{} | ||
ctx := newProcessorContext() | ||
conf := map[string]interface{}{} | ||
|
||
assert.NoError(t, p.Configure(ctx, conf)) | ||
assert.NoError(t, p.Receive((IPacket)(nil))) | ||
assert.NoError(t, p.Tick((IPacket)(nil))) | ||
assert.NoError(t, p.Start((IPacket)(nil))) | ||
assert.NoError(t, p.Stop((IPacket)(nil))) | ||
} | ||
|
||
func TestBaseConfigureAndValidate(t *testing.T) { | ||
p := &Base{} | ||
ctx := newProcessorContext() | ||
|
||
rawVal := &struct { | ||
Numbeurre int | ||
Stringue string `mapstructure:"driver"` | ||
Boule bool | ||
Flaut float64 | ||
}{ | ||
1, | ||
"hello", | ||
true, | ||
1.45, | ||
} | ||
|
||
conf := map[string]interface{}{ | ||
"driver": "world", | ||
} | ||
|
||
err := p.ConfigureAndValidate(ctx, conf, rawVal) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 1, rawVal.Numbeurre) | ||
assert.Equal(t, true, rawVal.Boule) | ||
assert.Equal(t, "world", rawVal.Stringue) | ||
} | ||
|
||
func TestBaseConfigureAndValidateValidationError(t *testing.T) { | ||
p := &Base{} | ||
ctx := newProcessorContext() | ||
|
||
rawVal := &struct { | ||
Numbeurre int | ||
Stringue string `validate:"required"` | ||
Boule bool | ||
Flaut float64 | ||
}{ | ||
Boule: false, | ||
} | ||
|
||
conf := map[string]interface{}{ | ||
"flaut": 1.67, | ||
} | ||
|
||
err := p.ConfigureAndValidate(ctx, conf, rawVal) | ||
|
||
assert.Error(t, err) | ||
assert.IsType(t, (validator.ValidationErrors)(nil), err) | ||
assert.Contains(t, err.(validator.ValidationErrors), ".Stringue") | ||
} | ||
|
||
func TestBaseConfigureAndValidateRawConfDecodingError(t *testing.T) { | ||
p := &Base{} | ||
ctx := newProcessorContext() | ||
|
||
rawVal := &struct { | ||
Numbeurre int | ||
Stringue string `mapstructure:"driver"` | ||
Boule bool | ||
Flaut float64 | ||
}{ | ||
Boule: false, | ||
} | ||
|
||
conf := map[string]interface{}{ | ||
"flaut": "1.67", | ||
"driver": ctx, | ||
} | ||
|
||
err := p.ConfigureAndValidate(ctx, conf, rawVal) | ||
assert.Error(t, err) | ||
assert.IsType(t, (*mapstructure.Error)(nil), err) | ||
} | ||
|
||
func TestBaseConfigureAndValidateCodecs(t *testing.T) { | ||
p := &Base{} | ||
ctx := newProcessorContext() | ||
|
||
rawVal := &struct { | ||
Numbeurre int | ||
Stringue string `mapstructure:"driver"` | ||
Boule bool | ||
Flaut float64 | ||
Codec codecs.CodecCollection | ||
}{ | ||
Boule: false, | ||
} | ||
|
||
conf := map[string]interface{}{ | ||
"flaut": "1.67", | ||
"codecs": map[int]interface{}{ | ||
0: &dummyCodec{name: "codecType1"}, | ||
1: &dummyCodec{name: "csv", role: "encoder"}, | ||
2: &dummyCodec{name: "json", role: "decoder"}, | ||
}, | ||
} | ||
|
||
err := p.ConfigureAndValidate(ctx, conf, rawVal) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "codecType1", rawVal.Codec.Default.Name) | ||
assert.Equal(t, "csv", rawVal.Codec.Enc.Name) | ||
assert.Equal(t, "json", rawVal.Codec.Dec.Name) | ||
|
||
} | ||
|
||
type dummyCodec struct { | ||
name string | ||
role string | ||
} | ||
|
||
func (d *dummyCodec) GetName() string { | ||
return d.name | ||
} | ||
func (d *dummyCodec) GetRole() string { | ||
return d.role | ||
} | ||
func (d *dummyCodec) GetOptions() map[string]interface{} { | ||
return map[string]interface{}{} | ||
} | ||
|
||
type dummyProcessorContext struct { | ||
logger Logger | ||
packetSender PacketSender | ||
packetBuilder PacketBuilder | ||
sentPackets map[int][]IPacket | ||
builtPackets []IPacket | ||
memory Memory | ||
store IStore | ||
mock.Mock | ||
} | ||
|
||
func newProcessorContext() *dummyProcessorContext { | ||
dp := &dummyProcessorContext{} | ||
dp.logger = logrus.New() | ||
dp.sentPackets = map[int][]IPacket{} | ||
dp.packetSender = nil | ||
dp.packetBuilder = nil | ||
dp.memory = nil | ||
dp.store = nil | ||
return dp | ||
} | ||
|
||
func (p dummyProcessorContext) Log() Logger { | ||
return p.logger | ||
} | ||
func (p dummyProcessorContext) PacketSender() PacketSender { | ||
return p.packetSender | ||
} | ||
func (p dummyProcessorContext) PacketBuilder() PacketBuilder { | ||
return p.packetBuilder | ||
} | ||
func (d *dummyProcessorContext) SentPackets(portNumber int) []IPacket { | ||
return d.sentPackets[portNumber] | ||
} | ||
func (d *dummyProcessorContext) BuiltPackets() []IPacket { | ||
return d.builtPackets | ||
} | ||
func (d *dummyProcessorContext) BuiltPacketsCount() int { | ||
return 0 | ||
} | ||
func (d *dummyProcessorContext) SentPacketsCount(portNumber int) int { | ||
return 0 | ||
} | ||
func (d *dummyProcessorContext) ConfigWorkingLocation() string { | ||
return "" | ||
} | ||
func (d *dummyProcessorContext) DataLocation() string { | ||
return "" | ||
} | ||
func (d *dummyProcessorContext) Memory() Memory { | ||
return d.memory | ||
} | ||
func (d *dummyProcessorContext) Store() IStore { | ||
return d.store | ||
} | ||
func (d *dummyProcessorContext) WebHook() WebHook { | ||
return nil | ||
} |
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,30 @@ | ||
package processors | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProcessCommonOptions(t *testing.T) { | ||
data := getTestFields() | ||
AddTags([]string{"foo", "bar"}, &data) | ||
|
||
co := &CommonOptions{ | ||
AddField: map[string]interface{}{"hello": "world"}, | ||
AddTag: []string{"toto", "bar"}, | ||
RemoveField: []string{"name"}, | ||
RemoveTag: []string{"foo"}, | ||
Type: "type1", | ||
} | ||
co.ProcessCommonOptions(&data) | ||
|
||
assert.Equal(t, "world", data.ValueOrEmptyForPathString("hello")) | ||
assert.False(t, data.Exists("name")) | ||
assert.Equal(t, "type1", data.ValueOrEmptyForPathString("type")) | ||
|
||
tags, _ := data.ValueForPath("tags") | ||
assert.Equal(t, 2, len(tags.([]string))) | ||
assert.Contains(t, tags, "toto") | ||
assert.NotContains(t, tags, "foo") | ||
} |
Oops, something went wrong.