github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_load_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  	tu "github.com/GuanceCloud/cliutils/testutil"
    15  )
    16  
    17  func TestLoadJson(t *testing.T) {
    18  	cases := []struct {
    19  		name, pl, in string
    20  		expected     interface{}
    21  		fail         bool
    22  		outkey       string
    23  	}{
    24  		{
    25  			name: "normal",
    26  			pl: `abc = load_json(_)
    27  			add_key(abc, abc["a"]["first"])`,
    28  			in:       `{"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47}`,
    29  			expected: float64(2.3),
    30  			outkey:   "abc",
    31  		},
    32  		{
    33  			name: "normal",
    34  			pl: `abc = load_json(_)
    35  			add_key(abc, abc["a"]["first"][-1])
    36  			add_key(len_abc, len(load_json(abc["a"]["ff"])))`,
    37  			in:       `{"a":{"first": [2.2, 1.1], "ff": "[2.2, 1.1]","second":2,"third":"aBC","forth":true},"age":47}`,
    38  			expected: int64(2),
    39  			outkey:   "len_abc",
    40  		},
    41  		{
    42  			name: "normal",
    43  			pl: `abc = load_json(_)
    44  			add_key(abc, abc[-1])`,
    45  			in:       `[2.2, 1.1]`,
    46  			expected: float64(1.1),
    47  			outkey:   "abc",
    48  		},
    49  		{
    50  			name: "normal",
    51  			pl: `abc = load_json(_)
    52  			add_key(abc, len(abc))`,
    53  			in:       `[]`,
    54  			expected: int64(0),
    55  			outkey:   "abc",
    56  		},
    57  		{
    58  			name: "normal",
    59  			pl: `abc = load_json("1")
    60  			add_key(abc)`,
    61  			in:       `{"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47}`,
    62  			expected: float64(1),
    63  			outkey:   "abc",
    64  		},
    65  		{
    66  			name: "normal",
    67  			pl: `abc = load_json("true")
    68  			add_key(abc)`,
    69  			in:       `{"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47}`,
    70  			expected: true,
    71  			outkey:   "abc",
    72  		},
    73  
    74  		{
    75  			name: "normal",
    76  			pl: `abc = load_json("null")
    77  			add_key(abc)`,
    78  			in:       `{"a":{"first": 2.3, "second":2,"third":"aBC","forth":true},"age":47}`,
    79  			expected: nil,
    80  			outkey:   "abc",
    81  		},
    82  	}
    83  
    84  	for idx, tc := range cases {
    85  		t.Run(tc.name, func(t *testing.T) {
    86  			runner, err := NewTestingRunner(tc.pl)
    87  			if err != nil {
    88  				if tc.fail {
    89  					t.Logf("[%d]expect error: %s", idx, err)
    90  				} else {
    91  					t.Errorf("[%d] failed: %s", idx, err)
    92  				}
    93  				return
    94  			}
    95  			pt := ptinput.NewPlPoint(
    96  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
    97  			errR := runScript(runner, pt)
    98  
    99  			if errR != nil {
   100  				t.Fatal(errR.Error())
   101  			}
   102  
   103  			v, _, _ := pt.Get(tc.outkey)
   104  			// tu.Equals(t, nil, err)
   105  			tu.Equals(t, tc.expected, v)
   106  
   107  			t.Logf("[%d] PASS", idx)
   108  		})
   109  	}
   110  }