|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package ottlfuncs |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 14 | +) |
| 15 | + |
| 16 | +func Test_Nanosecond(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + time ottl.TimeGetter[any] |
| 20 | + expected int64 |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "some time", |
| 24 | + time: &ottl.StandardTimeGetter[any]{ |
| 25 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 26 | + return time.Date(2006, time.January, 2, 15, 4, 5, 197382465, time.UTC), nil |
| 27 | + }, |
| 28 | + }, |
| 29 | + expected: 197382465, |
| 30 | + }, |
| 31 | + } |
| 32 | + for _, tt := range tests { |
| 33 | + t.Run(tt.name, func(t *testing.T) { |
| 34 | + exprFunc, err := Nanosecond(tt.time) |
| 35 | + assert.NoError(t, err) |
| 36 | + result, err := exprFunc(nil, nil) |
| 37 | + assert.NoError(t, err) |
| 38 | + assert.Equal(t, tt.expected, result) |
| 39 | + }) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func Test_Nanosecond_Error(t *testing.T) { |
| 44 | + var getter ottl.TimeGetter[any] = &ottl.StandardTimeGetter[any]{ |
| 45 | + Getter: func(_ context.Context, _ any) (any, error) { |
| 46 | + return "not a time", nil |
| 47 | + }, |
| 48 | + } |
| 49 | + exprFunc, err := Nanosecond(getter) |
| 50 | + assert.NoError(t, err) |
| 51 | + result, err := exprFunc(context.Background(), nil) |
| 52 | + assert.Nil(t, result) |
| 53 | + assert.Error(t, err) |
| 54 | +} |
0 commit comments