github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/funcs_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  /*
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	tu "github.com/GuanceCloud/cliutils/testutil"
    14  	"github.com/GuanceCloud/cliutils/pipeline/parser"
    15  )
    16  
    17  func assertEqual(t *testing.T, a, b interface{}) {
    18  	t.Helper()
    19  	if a != b {
    20  		t.Errorf("Not Equal. %v %v", a, b)
    21  	}
    22  }
    23  
    24  func TestGrokFunc(t *testing.T) {
    25  	script := `
    26  add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)")
    27  add_pattern("_minute", "(?:[0-5][0-9])")
    28  add_pattern("_hour", "(?:2[0123]|[01]?[0-9])")
    29  add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)")
    30  grok(_, "%{time}")`
    31  
    32  	p, err := NewPipeline(script)
    33  
    34  	assertEqual(t, err, nil)
    35  
    36  	p.Run("12:13:14")
    37  	assertEqual(t, p.lastErr, nil)
    38  
    39  	hour, _ := p.getContentStr("hour")
    40  	assertEqual(t, hour, "12")
    41  
    42  	minute, _ := p.getContentStr("minute")
    43  	assertEqual(t, minute, "13")
    44  
    45  	second, _ := p.getContentStr("second")
    46  	assertEqual(t, second, "14")
    47  }
    48  
    49  func TestRenameFunc(t *testing.T) {
    50  	script := `
    51  		add_pattern("_second", "(?:(?:[0-5]?[0-9]|60)(?:[:.,][0-9]+)?)")
    52  		add_pattern("_minute", "(?:[0-5][0-9])")
    53  		add_pattern("_hour", "(?:2[0123]|[01]?[0-9])")
    54  		add_pattern("time", "([^0-9]?)%{_hour:hour}:%{_minute:minute}(?::%{_second:second})([^0-9]?)")
    55  		grok(_, "%{time}")
    56  		rename(newhour, hour)
    57  	`
    58  	p, err := NewPipeline(script)
    59  
    60  	assertEqual(t, err, nil)
    61  
    62  	p.Run("12:13:14")
    63  
    64  	assertEqual(t, p.lastErr, nil)
    65  
    66  	r, _ := p.getContentStr("newhour")
    67  
    68  	assertEqual(t, r, "12")
    69  }
    70  
    71  func TestExprFunc(t *testing.T) {
    72  	js := `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`
    73  	script := `json(_, a.second)
    74  		cast(a.second, "int")
    75  		expr(a.second*10+(2+3)*5, bb)
    76  	`
    77  	p, err := NewPipeline(script)
    78  	assertEqual(t, err, nil)
    79  
    80  	p.Run(js)
    81  	assertEqual(t, p.lastErr, nil)
    82  
    83  	if v, err := p.getContentStr("bb"); err != nil {
    84  		t.Error(err)
    85  	} else {
    86  		assertEqual(t, v, "45")
    87  	}
    88  }
    89  
    90  func TestCastFloat2IntFunc(t *testing.T) {
    91  	js := `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`
    92  	script := `json(_, a.first)
    93  cast(a.first, "int")
    94  `
    95  	p, err := NewPipeline(script)
    96  	assertEqual(t, err, nil)
    97  
    98  	p.Run(js)
    99  	v, _ := p.getContentStr("a.first")
   100  
   101  	assertEqual(t, v, "2")
   102  }
   103  
   104  func TestCastInt2FloatFunc(t *testing.T) {
   105  	js := `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`
   106  	script := `json(_, a.second)
   107  cast(a.second, "float")
   108  `
   109  	p, err := NewPipeline(script)
   110  	assertEqual(t, err, nil)
   111  
   112  	p.Run(js)
   113  
   114  	v, _ := p.getContentStr("a.second")
   115  	assertEqual(t, v, "2")
   116  }
   117  
   118  // a.second 为 float 类型
   119  func TestStringfFunc(t *testing.T) {
   120  	// js := `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`
   121  	js := `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`
   122  	script := `json(_, a.second)
   123  		json(_, a.third)
   124  		json(_, a.forth)
   125  		cast(a.second, "int")
   126  		strfmt(bb, "%d %s %v", a.second, a.third, a.forth)
   127  	`
   128  	p, err := NewPipeline(script)
   129  	tu.Assert(t, err == nil, "")
   130  
   131  	p.Run(js)
   132  
   133  	t.Logf("%+#v", p.Output)
   134  	v, _ := p.getContent("bb")
   135  	tu.Equals(t, "2 abc true", v)
   136  }
   137  
   138  func TestUppercaseFunc(t *testing.T) {
   139  	js := `{"a":{"first":2.3,"second":2,"third":"abc","forth":true},"age":47}`
   140  	script := `json(_, a.third)
   141  uppercase(a.third)
   142  `
   143  	p, err := NewPipeline(script)
   144  	assertEqual(t, err, nil)
   145  
   146  	p.Run(js)
   147  
   148  	v, _ := p.getContent("a.third")
   149  	assertEqual(t, v, "ABC")
   150  }
   151  
   152  func TestLowercaseFunc(t *testing.T) {
   153  	js := `{"a":{"first":2.3,"second":2,"third":"aBC","forth":true},"age":47}`
   154  	script := `json(_, a.third)
   155  lowercase(a.third)
   156  `
   157  	p, err := NewPipeline(script)
   158  	t.Log(err)
   159  	assertEqual(t, err, nil)
   160  
   161  	p.Run(js)
   162  	v, _ := p.getContentStr("a.third")
   163  	assertEqual(t, v, "abc")
   164  }
   165  
   166  func TestAddkeyFunc(t *testing.T) {
   167  	js := `{"a":{"first":2.3,"second":2,"third":"aBC","forth":true},"age":47}`
   168  	script := `add_key(aa, 3)
   169  `
   170  	p, err := NewPipeline(script)
   171  	assertEqual(t, err, nil)
   172  
   173  	p.Run(js)
   174  
   175  	t.Log(p.engine.Output)
   176  
   177  	// v, _ := p.getContentStr("aa")
   178  	// assertEqual(t, v, "3")
   179  }
   180  
   181  func TestDropkeyFunc(t *testing.T) {
   182  	js := `{"a":{"first":2.3,"second":2,"third":"aBC","forth":true},"age":47}`
   183  	script := `json(_, a.third)
   184  json(_, a.first)
   185  drop_key(a.third)
   186  `
   187  	p, err := NewPipeline(script)
   188  	assertEqual(t, err, nil)
   189  
   190  	p.Run(js)
   191  
   192  	v, _ := p.getContentStr("a.first")
   193  	assertEqual(t, v, "2.3")
   194  }
   195  
   196  func TestTimeParse(t *testing.T) {
   197  	cases := []*struct {
   198  		fmt    string
   199  		input  string
   200  		output int64
   201  		pass   bool
   202  	}{
   203  		{
   204  			fmt:    "2006-01-02 15:04:05",
   205  			input:  "2021-07-20 18:00:00",
   206  			output: 1626804000000000000,
   207  		},
   208  		{
   209  			fmt:    "01/02/06",
   210  			input:  "07/20/21",
   211  			output: 1626739200000000000,
   212  		},
   213  	}
   214  
   215  	tz, _ := time.LoadLocation("UTC")
   216  	for _, c := range cases {
   217  		if pt, err := time.ParseInLocation(c.fmt, c.input, tz); err == nil {
   218  			if pt.UnixNano() == c.output {
   219  				c.pass = true
   220  			} else {
   221  				t.Error("act:", pt.UnixNano(),
   222  					"exp:", c.output, "delta:", pt.UnixNano()-c.output)
   223  			}
   224  		} else {
   225  			t.Error(err)
   226  		}
   227  	}
   228  }
   229  */