github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_json_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  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestJSON(t *testing.T) {
    18  	testCase := []*funcCase{
    19  		{
    20  			in: `{
    21  			  "name": {"first": "Tom", "last": "Anderson"},
    22  			  "age":37,
    23  			  "children": ["Sara","Alex","Jack"],
    24  			  "fav.movie": "Deer Hunter",
    25  			  "friends": [
    26  			    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    27  			    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    28  			    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
    29  			  ]
    30  			}`,
    31  			script: `json(_, name)
    32  			json(name, first)`,
    33  			expected: "Tom",
    34  			key:      "first",
    35  		},
    36  		{
    37  			in: `{
    38  			  "name": {"first": "Tom", "last": "Anderson"},
    39  			  "age":37,
    40  			  "children": ["Sara","Alex","Jack"],
    41  			  "fav.movie": "Deer Hunter",
    42  			  "friends": [
    43  			    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    44  			    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    45  			    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
    46  			  ]
    47  			}`,
    48  			script: `json(_, friends)
    49  			json(friends, .[1].first, f_first)`,
    50  			expected: "Roger",
    51  			key:      "f_first",
    52  		},
    53  		{
    54  			in: `[
    55  				    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    56  				    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    57  				    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
    58  				]`,
    59  			script:   `json(_, .[0].nets[-1])`,
    60  			expected: "tw",
    61  			key:      "[0].nets[-1]",
    62  		},
    63  		{
    64  			in: `[
    65  				    {"first": "Dale", "last": "Murphy", "age": 44, "nets": ["ig", "fb", "tw"]},
    66  				    {"first": "Roger", "last": "Craig", "age": 68, "nets": ["fb", "tw"]},
    67  				    {"first": "Jane", "last": "Murphy", "age": 47, "nets": ["ig", "tw"]}
    68  				]`,
    69  			script:   `json(_, .[1].age)`,
    70  			expected: float64(68),
    71  			key:      "[1].age",
    72  		},
    73  		{
    74  			name:     "trim_space auto",
    75  			in:       `{"item": " not_space "}`,
    76  			script:   `json(_, item, item)`,
    77  			key:      "item",
    78  			expected: "not_space",
    79  		},
    80  		{
    81  			name:     "trim_space disable",
    82  			in:       `{"item": " not_space "}`,
    83  			script:   `json(_, item, item, false)`,
    84  			key:      "item",
    85  			expected: " not_space ",
    86  		},
    87  		{
    88  			name:     "trim_space enable",
    89  			in:       `{"item": " not_space "}`,
    90  			script:   `json(_, item, item, true)`,
    91  			key:      "item",
    92  			expected: "not_space",
    93  		},
    94  		{
    95  			name:     "map_delete_after",
    96  			in:       `{"item": " not_space "}`,
    97  			script:   `json(_, item, item, true, true)`,
    98  			key:      "message",
    99  			expected: "{}",
   100  			fail:     false,
   101  		},
   102  		{
   103  			name:     "map_delete_after1",
   104  			in:       `{"item": " not_space ", "item2":{"item3": [123]}}`,
   105  			script:   `json(_, item2.item3, item, delete_after_extract = true)`,
   106  			key:      "message",
   107  			expected: `{"item":" not_space ","item2":{}}`,
   108  		},
   109  		{
   110  			name:     "list_delete_after1",
   111  			in:       `{"item": " not_space ", "item2": [[1,2,3,4,5],[6]]}`,
   112  			script:   `json(_, .[0].item2[0][2].a[0], item, true, true)`,
   113  			key:      "item",
   114  			expected: "1",
   115  			fail:     true,
   116  		},
   117  		{
   118  			name:     "list_delete_after2",
   119  			in:       `{"item": " not_space ", "item2": [[1,2,3,4,5],[6]]}`,
   120  			script:   `json(_, .[0], item, true, true)`,
   121  			key:      "item",
   122  			expected: "1",
   123  			fail:     true,
   124  		},
   125  		{
   126  			name:     "list_delete_after3",
   127  			in:       `{"item": " not_space ", "item2": [[1,2,3,4,5],[6]]}`,
   128  			script:   `json(_, a[0][1], item, true, true)`,
   129  			key:      "item",
   130  			expected: "1",
   131  			fail:     true,
   132  		},
   133  	}
   134  
   135  	for idx, tc := range testCase {
   136  		t.Run(tc.name, func(t *testing.T) {
   137  			runner, err := NewTestingRunner(tc.script)
   138  
   139  			if err != nil && tc.fail {
   140  				return
   141  			} else if err != nil || tc.fail {
   142  				assert.Equal(t, nil, err)
   143  				assert.Equal(t, tc.fail, err != nil)
   144  			}
   145  
   146  			pt := ptinput.NewPlPoint(
   147  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
   148  			errR := runScript(runner, pt)
   149  			if errR != nil {
   150  				t.Fatal(errR.Error())
   151  			}
   152  
   153  			r, _, e := pt.Get(tc.key)
   154  			assert.NoError(t, e)
   155  			if tc.key == "[2].age" {
   156  				t.Log(1)
   157  			}
   158  			assert.Equal(t, tc.expected, r)
   159  
   160  			t.Logf("[%d] PASS", idx)
   161  		})
   162  	}
   163  }