github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_default_time_with_fmt_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package funcs
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    13  	"github.com/GuanceCloud/cliutils/point"
    14  	tu "github.com/GuanceCloud/cliutils/testutil"
    15  )
    16  
    17  func TestDefaultTimeWithFmt(t *testing.T) {
    18  	// local timezone: utc+0800
    19  	cst := time.FixedZone("CST", 8*3600)
    20  	time.Local = cst
    21  
    22  	cases := []struct {
    23  		name, pl string
    24  		in       []string
    25  		outkey   string
    26  		expect   []interface{}
    27  		fail     bool
    28  	}{
    29  		{
    30  			name: "02/Jan/2006:15:04:05 -0700",
    31  			in: []string{
    32  				`{"time":"02/Dec/2021:12:55:34 +0900"}`,
    33  				`{"time":"02/Dec/2021:11:55:34 +0800"}`,
    34  			},
    35  			pl: `
    36  			json(_, time)
    37  			default_time_with_fmt(time, "02/Jan/2006:15:04:05 -0700","Asia/Tokyo")
    38  		`,
    39  			outkey: "time",
    40  			expect: []interface{}{
    41  				int64(1638417334000000000),
    42  				int64(1638417334000000000),
    43  			},
    44  			fail: false,
    45  		},
    46  		{
    47  			name: "02/Jan/2006:15:04:05 (Shanghai)",
    48  			in: []string{
    49  				`{"time":"02/Dec/2021:11:55:34"}`,
    50  			},
    51  			pl: `
    52  			json(_, time)
    53  			default_time_with_fmt(time, "02/Jan/2006:15:04:05","Asia/Shanghai")
    54  		`,
    55  			outkey: "time",
    56  			expect: []interface{}{
    57  				int64(1638417334000000000),
    58  			},
    59  			fail: false,
    60  		},
    61  		{
    62  			name: "02/Jan/2006:15:04:05 (Local Shanghai)",
    63  			in: []string{
    64  				`{"time":"02/Dec/2021:11:55:34"}`,
    65  			},
    66  			pl: `
    67  			json(_, time)
    68  			default_time_with_fmt(time, "02/Jan/2006:15:04:05")
    69  		`,
    70  			outkey: "time",
    71  			expect: []interface{}{
    72  				int64(1638417334000000000),
    73  			},
    74  			fail: false,
    75  		},
    76  		{
    77  			name: "02/Jan/2006:15:04:05 (Tokyo)",
    78  			in: []string{
    79  				`{"time":"02/Dec/2021:12:55:34"}`,
    80  			},
    81  			pl: `
    82  			json(_, time)
    83  			default_time_with_fmt(time, "02/Jan/2006:15:04:05","Asia/Tokyo")
    84  		`,
    85  			outkey: "time",
    86  			expect: []interface{}{
    87  				int64(1638417334000000000),
    88  			},
    89  			fail: false,
    90  		},
    91  	}
    92  	for idx, tc := range cases {
    93  		t.Run(tc.name, func(t *testing.T) {
    94  			runner, err := NewTestingRunner(tc.pl)
    95  			if err != nil {
    96  				if tc.fail {
    97  					t.Logf("[%d]expect error: %s", idx, err)
    98  				} else {
    99  					t.Errorf("[%d] failed: %s", idx, err)
   100  				}
   101  				return
   102  			}
   103  			for idxIn := 0; idxIn < len(tc.in); idxIn++ {
   104  				pt := ptinput.NewPlPoint(
   105  					point.Logging, "test", nil, map[string]any{"message": tc.in[idxIn]}, time.Now())
   106  				errR := runScript(runner, pt)
   107  				if errR != nil {
   108  					t.Fatal(errR)
   109  				}
   110  
   111  				pt.KeyTime2Time()
   112  
   113  				var v interface{}
   114  				if tc.outkey != "time" && tc.outkey != "" {
   115  					v, _, _ = pt.Get(tc.outkey)
   116  				} else {
   117  					v = pt.PtTime().UnixNano()
   118  				}
   119  				tu.Equals(t, tc.expect[idxIn], v)
   120  				t.Logf("[%d] PASS", idx)
   121  			}
   122  		})
   123  	}
   124  }