Skip to content

Commit 8ac77ad

Browse files
committed
Tests: check that setter has been called
1 parent 1c315d5 commit 8ac77ad

10 files changed

+222
-242
lines changed

pkg/ottl/ottlfuncs/func_delete_key_test.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,47 +20,30 @@ func Test_deleteKey(t *testing.T) {
2020
input.PutInt("test2", 3)
2121
input.PutBool("test3", true)
2222

23-
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
24-
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
25-
return tCtx, nil
26-
},
27-
Setter: func(_ context.Context, tCtx pcommon.Map, val any) error {
28-
if v, ok := val.(pcommon.Map); ok {
29-
v.CopyTo(tCtx)
30-
return nil
31-
}
32-
return errors.New("expected pcommon.Map")
33-
},
34-
}
35-
3623
tests := []struct {
37-
name string
38-
target ottl.PMapGetSetter[pcommon.Map]
39-
key string
40-
want func(pcommon.Map)
24+
name string
25+
key string
26+
want func(pcommon.Map)
4127
}{
4228
{
43-
name: "delete test",
44-
target: target,
45-
key: "test",
29+
name: "delete test",
30+
key: "test",
4631
want: func(expectedMap pcommon.Map) {
4732
expectedMap.PutBool("test3", true)
4833
expectedMap.PutInt("test2", 3)
4934
},
5035
},
5136
{
52-
name: "delete test2",
53-
target: target,
54-
key: "test2",
37+
name: "delete test2",
38+
key: "test2",
5539
want: func(expectedMap pcommon.Map) {
5640
expectedMap.PutStr("test", "hello world")
5741
expectedMap.PutBool("test3", true)
5842
},
5943
},
6044
{
61-
name: "delete nothing",
62-
target: target,
63-
key: "not a valid key",
45+
name: "delete nothing",
46+
key: "not a valid key",
6447
want: func(expectedMap pcommon.Map) {
6548
expectedMap.PutStr("test", "hello world")
6649
expectedMap.PutInt("test2", 3)
@@ -73,10 +56,26 @@ func Test_deleteKey(t *testing.T) {
7356
scenarioMap := pcommon.NewMap()
7457
input.CopyTo(scenarioMap)
7558

76-
exprFunc := deleteKey(tt.target, tt.key)
59+
setterWasCalled := false
60+
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
61+
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
62+
return tCtx, nil
63+
},
64+
Setter: func(_ context.Context, tCtx pcommon.Map, val any) error {
65+
setterWasCalled = true
66+
if v, ok := val.(pcommon.Map); ok {
67+
v.CopyTo(tCtx)
68+
return nil
69+
}
70+
return errors.New("expected pcommon.Map")
71+
},
72+
}
73+
74+
exprFunc := deleteKey(target, tt.key)
7775

7876
_, err := exprFunc(nil, scenarioMap)
7977
assert.NoError(t, err)
78+
assert.True(t, setterWasCalled)
8079

8180
expected := pcommon.NewMap()
8281
tt.want(expected)

pkg/ottl/ottlfuncs/func_delete_matching_keys_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,44 +21,27 @@ func Test_deleteMatchingKeys(t *testing.T) {
2121
input.PutInt("test2", 3)
2222
input.PutBool("test3", true)
2323

24-
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
25-
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
26-
return tCtx, nil
27-
},
28-
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
29-
if v, ok := m.(pcommon.Map); ok {
30-
v.CopyTo(tCtx)
31-
return nil
32-
}
33-
return errors.New("expected pcommon.Map")
34-
},
35-
}
36-
3724
tests := []struct {
3825
name string
39-
target ottl.PMapGetSetter[pcommon.Map]
4026
pattern string
4127
want func(pcommon.Map)
4228
}{
4329
{
4430
name: "delete everything",
45-
target: target,
4631
pattern: "test.*",
4732
want: func(expectedMap pcommon.Map) {
4833
expectedMap.EnsureCapacity(3)
4934
},
5035
},
5136
{
5237
name: "delete attributes that end in a number",
53-
target: target,
5438
pattern: "\\d$",
5539
want: func(expectedMap pcommon.Map) {
5640
expectedMap.PutStr("test", "hello world")
5741
},
5842
},
5943
{
6044
name: "delete nothing",
61-
target: target,
6245
pattern: "not a matching pattern",
6346
want: func(expectedMap pcommon.Map) {
6447
expectedMap.PutStr("test", "hello world")
@@ -72,11 +55,27 @@ func Test_deleteMatchingKeys(t *testing.T) {
7255
scenarioMap := pcommon.NewMap()
7356
input.CopyTo(scenarioMap)
7457

75-
exprFunc, err := deleteMatchingKeys(tt.target, tt.pattern)
58+
setterWasCalled := false
59+
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
60+
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
61+
return tCtx, nil
62+
},
63+
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
64+
setterWasCalled = true
65+
if v, ok := m.(pcommon.Map); ok {
66+
v.CopyTo(tCtx)
67+
return nil
68+
}
69+
return errors.New("expected pcommon.Map")
70+
},
71+
}
72+
73+
exprFunc, err := deleteMatchingKeys(target, tt.pattern)
7674
assert.NoError(t, err)
7775

7876
_, err = exprFunc(nil, scenarioMap)
7977
assert.NoError(t, err)
78+
assert.True(t, setterWasCalled)
8079

8180
expected := pcommon.NewMap()
8281
tt.want(expected)

pkg/ottl/ottlfuncs/func_flatten_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,14 @@ func Test_flatten(t *testing.T) {
341341
m := pcommon.NewMap()
342342
err := m.FromRaw(tt.target)
343343
assert.NoError(t, err)
344+
345+
setterWasCalled := false
344346
target := ottl.StandardPMapGetSetter[any]{
345347
Getter: func(_ context.Context, _ any) (pcommon.Map, error) {
346348
return m, nil
347349
},
348350
Setter: func(_ context.Context, _ any, val any) error {
351+
setterWasCalled = true
349352
if v, ok := val.(pcommon.Map); ok {
350353
v.CopyTo(m)
351354
return nil
@@ -356,8 +359,10 @@ func Test_flatten(t *testing.T) {
356359

357360
exprFunc, err := flatten[any](target, tt.prefix, tt.depth, ottl.NewTestingOptional[bool](tt.conflict))
358361
assert.NoError(t, err)
362+
359363
_, err = exprFunc(nil, nil)
360364
assert.NoError(t, err)
365+
assert.True(t, setterWasCalled)
361366

362367
assert.Equal(t, tt.expected, m.AsRaw())
363368
})
@@ -498,11 +503,14 @@ func Test_flatten_undeterministic(t *testing.T) {
498503
m := pcommon.NewMap()
499504
err := m.FromRaw(tt.target)
500505
assert.NoError(t, err)
506+
507+
setterWasCalled := false
501508
target := ottl.StandardPMapGetSetter[any]{
502509
Getter: func(_ context.Context, _ any) (pcommon.Map, error) {
503510
return m, nil
504511
},
505512
Setter: func(_ context.Context, _ any, val any) error {
513+
setterWasCalled = true
506514
if v, ok := val.(pcommon.Map); ok {
507515
v.CopyTo(m)
508516
return nil
@@ -513,8 +521,10 @@ func Test_flatten_undeterministic(t *testing.T) {
513521

514522
exprFunc, err := flatten[any](target, tt.prefix, tt.depth, ottl.NewTestingOptional[bool](tt.conflict))
515523
assert.NoError(t, err)
524+
516525
_, err = exprFunc(nil, nil)
517526
assert.NoError(t, err)
527+
assert.True(t, setterWasCalled)
518528

519529
keys, val := extractKeysAndValues(m.AsRaw())
520530

pkg/ottl/ottlfuncs/func_keep_keys_test.go

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,64 +20,62 @@ func Test_keepKeys(t *testing.T) {
2020
input.PutInt("test2", 3)
2121
input.PutBool("test3", true)
2222

23-
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
24-
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
25-
return tCtx, nil
26-
},
27-
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
28-
if v, ok := m.(pcommon.Map); ok {
29-
v.CopyTo(tCtx)
30-
return nil
31-
}
32-
return errors.New("expected pcommon.Map")
33-
},
34-
}
35-
3623
tests := []struct {
37-
name string
38-
target ottl.PMapGetSetter[pcommon.Map]
39-
keys []string
40-
want func(pcommon.Map)
24+
name string
25+
keys []string
26+
want func(pcommon.Map)
4127
}{
4228
{
43-
name: "keep one",
44-
target: target,
45-
keys: []string{"test"},
29+
name: "keep one",
30+
keys: []string{"test"},
4631
want: func(expectedMap pcommon.Map) {
4732
expectedMap.PutStr("test", "hello world")
4833
},
4934
},
5035
{
51-
name: "keep two",
52-
target: target,
53-
keys: []string{"test", "test2"},
36+
name: "keep two",
37+
keys: []string{"test", "test2"},
5438
want: func(expectedMap pcommon.Map) {
5539
expectedMap.PutStr("test", "hello world")
5640
expectedMap.PutInt("test2", 3)
5741
},
5842
},
5943
{
60-
name: "keep none",
61-
target: target,
62-
keys: []string{},
63-
want: func(_ pcommon.Map) {},
44+
name: "keep none",
45+
keys: []string{},
46+
want: func(_ pcommon.Map) {},
6447
},
6548
{
66-
name: "no match",
67-
target: target,
68-
keys: []string{"no match"},
69-
want: func(_ pcommon.Map) {},
49+
name: "no match",
50+
keys: []string{"no match"},
51+
want: func(_ pcommon.Map) {},
7052
},
7153
}
7254
for _, tt := range tests {
7355
t.Run(tt.name, func(t *testing.T) {
7456
scenarioMap := pcommon.NewMap()
7557
input.CopyTo(scenarioMap)
7658

77-
exprFunc := keepKeys(tt.target, tt.keys)
59+
setterWasCalled := false
60+
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
61+
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
62+
return tCtx, nil
63+
},
64+
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
65+
setterWasCalled = true
66+
if v, ok := m.(pcommon.Map); ok {
67+
v.CopyTo(tCtx)
68+
return nil
69+
}
70+
return errors.New("expected pcommon.Map")
71+
},
72+
}
73+
74+
exprFunc := keepKeys(target, tt.keys)
7875

7976
_, err := exprFunc(nil, scenarioMap)
8077
assert.NoError(t, err)
78+
assert.True(t, setterWasCalled)
8179

8280
expected := pcommon.NewMap()
8381
tt.want(expected)

pkg/ottl/ottlfuncs/func_keep_matching_keys_test.go

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,14 @@ func Test_keepMatchingKeys(t *testing.T) {
2020
in.PutStr("foo1", "bar")
2121
in.PutInt("foo2", 3)
2222

23-
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
24-
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
25-
return tCtx, nil
26-
},
27-
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
28-
if v, ok := m.(pcommon.Map); ok {
29-
v.CopyTo(tCtx)
30-
return nil
31-
}
32-
return errors.New("expected pcommon.Map")
33-
},
34-
}
35-
3623
tests := []struct {
3724
name string
38-
target ottl.PMapGetSetter[pcommon.Map]
3925
pattern string
4026
want func() *pcommon.Map
4127
wantError bool
4228
}{
4329
{
4430
name: "keep everything that ends with a number",
45-
target: target,
4631
pattern: "\\d$",
4732
want: func() *pcommon.Map {
4833
m := pcommon.NewMap()
@@ -53,7 +38,6 @@ func Test_keepMatchingKeys(t *testing.T) {
5338
},
5439
{
5540
name: "keep nothing",
56-
target: target,
5741
pattern: "bar.*",
5842
want: func() *pcommon.Map {
5943
m := pcommon.NewMap()
@@ -65,7 +49,6 @@ func Test_keepMatchingKeys(t *testing.T) {
6549
},
6650
{
6751
name: "keep everything",
68-
target: target,
6952
pattern: "foo.*",
7053
want: func() *pcommon.Map {
7154
m := pcommon.NewMap()
@@ -77,7 +60,6 @@ func Test_keepMatchingKeys(t *testing.T) {
7760
},
7861
{
7962
name: "invalid pattern",
80-
target: target,
8163
pattern: "*",
8264
want: func() *pcommon.Map {
8365
return nil
@@ -90,7 +72,22 @@ func Test_keepMatchingKeys(t *testing.T) {
9072
scenarioMap := pcommon.NewMap()
9173
in.CopyTo(scenarioMap)
9274

93-
exprFunc, err := keepMatchingKeys(tt.target, tt.pattern)
75+
setterWasCalled := false
76+
target := &ottl.StandardPMapGetSetter[pcommon.Map]{
77+
Getter: func(_ context.Context, tCtx pcommon.Map) (pcommon.Map, error) {
78+
return tCtx, nil
79+
},
80+
Setter: func(_ context.Context, tCtx pcommon.Map, m any) error {
81+
setterWasCalled = true
82+
if v, ok := m.(pcommon.Map); ok {
83+
v.CopyTo(tCtx)
84+
return nil
85+
}
86+
return errors.New("expected pcommon.Map")
87+
},
88+
}
89+
90+
exprFunc, err := keepMatchingKeys(target, tt.pattern)
9491

9592
if tt.wantError {
9693
assert.Error(t, err)
@@ -100,6 +97,7 @@ func Test_keepMatchingKeys(t *testing.T) {
10097

10198
_, err = exprFunc(nil, scenarioMap)
10299
assert.NoError(t, err)
100+
assert.True(t, setterWasCalled)
103101

104102
assert.Equal(t, *tt.want(), scenarioMap)
105103
})

0 commit comments

Comments
 (0)