github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/func_query_refer_table_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  	"context"
    10  	"net/http"
    11  	"net/http/httptest"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    16  	"github.com/GuanceCloud/cliutils/pipeline/ptinput/refertable"
    17  	"github.com/GuanceCloud/cliutils/point"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestQueryReferTable(t *testing.T) {
    22  	files := map[string]string{
    23  		"a.json": testTableData,
    24  	}
    25  	server := newJSONDataServer(files)
    26  	defer server.Close()
    27  	url := server.URL
    28  
    29  	ctx := context.Background()
    30  	ctx, cancel := context.WithCancel(ctx)
    31  	defer cancel()
    32  	refTable, err := refertable.NewReferTable(refertable.RefTbCfg{
    33  		URL:      url + "?name=a.json",
    34  		Interval: time.Second * 5,
    35  	})
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	go refTable.PullWorker(ctx)
    41  
    42  	ok := refTable.InitFinished(time.Second * 5)
    43  	if !ok {
    44  		t.Fatal("init refer table timeout")
    45  	}
    46  
    47  	testCase := []struct {
    48  		name string
    49  		in   string
    50  
    51  		script string
    52  
    53  		key      []string
    54  		expected []any
    55  		fail     bool
    56  	}{
    57  		{
    58  			name: "test query",
    59  			in:   `test query"`,
    60  			script: `add_key(f1, 123)
    61  			query_refer_table("table1", key = "f1", value= f1)`,
    62  			key:      []string{"key1", "key2", "f1", "f2"},
    63  			expected: []any{"a", float64(123), int64(123), false},
    64  		},
    65  		{
    66  			name: "test query multi",
    67  			in:   `test query multi"`,
    68  			script: `add_key(f1, 123)
    69  			mquery_refer_table("table1", ["f1"], values= [f1])`,
    70  			key:      []string{"key1", "key2", "f1", "f2"},
    71  			expected: []any{"a", float64(123), int64(123), false},
    72  		},
    73  		{
    74  			name: "test int",
    75  			in:   `test int `,
    76  			script: `# add_key(f1, 123)
    77  			query_refer_table("table1", "f1", 123)`,
    78  			key:      []string{"key1", "key2", "f1", "f2"},
    79  			expected: []any{"a", float64(123), int64(123), false},
    80  		},
    81  		{
    82  			name: "test string",
    83  			in:   `test string`,
    84  			script: `
    85  			query_refer_table("table1", "key1", "a")`,
    86  			key:      []string{"key1", "key2", "f1", "f2"},
    87  			expected: []any{"a", float64(123), int64(123), false},
    88  		},
    89  		{
    90  			name: "test string 2",
    91  			in:   `test string`,
    92  			script: `
    93  			t = "table1"
    94  			k = "key1"
    95  			v = "a"
    96  			query_refer_table(t, k, v)`,
    97  			key:      []string{"key1", "key2", "f1", "f2"},
    98  			expected: []any{"a", float64(123), int64(123), false},
    99  		},
   100  		{
   101  			name: "test float",
   102  			in:   `test float`,
   103  			script: `
   104  			# add_key(f1, 123)
   105  			query_refer_table("table1", "key2", 123.)`,
   106  			key:      []string{"key1", "key2", "f1", "f2"},
   107  			expected: []any{"a", float64(123), int64(123), false},
   108  		},
   109  		{
   110  			name: "test bool",
   111  			in:   `test bool`,
   112  			script: `
   113  			# add_key(f1, 123)
   114  			query_refer_table("table1", "f2", false)`,
   115  			key:      []string{"key1", "key2", "f1", "f2"},
   116  			expected: []any{"a", float64(123), int64(123), false},
   117  		},
   118  		{
   119  			name: "test float but int",
   120  			in:   `test float but int`,
   121  			script: `
   122  			# add_key(f1, 123)
   123  			query_refer_table("table1", "key2", 123)`,
   124  			key:      []string{"key1", "key2", "f1", "f2"},
   125  			expected: []any{nil, nil, nil, nil},
   126  		},
   127  		{
   128  			name: "test query, key not find",
   129  			in:   `test query, key not find"`,
   130  			script: `#add_key(f1, 123)
   131  			query_refer_table("table1", "f1", f1)`,
   132  			key:      []string{"key1", "key2", "f1", "f2"},
   133  			expected: []any{nil, nil, nil, nil},
   134  		},
   135  		{
   136  			name: "test query, positional keyword",
   137  			in:   `test query, positional keyword`,
   138  			script: `#add_key(f1, 123)
   139  			query_refer_table("table1", "f1", value=f1)`,
   140  			key:      []string{"key1", "key2", "f1", "f2"},
   141  			expected: []any{nil, nil, nil, nil},
   142  			fail:     true,
   143  		},
   144  		{
   145  			name: "test-multi",
   146  			in:   `test-multi"`,
   147  			script: `
   148  			key = "f2"
   149  			value = "ab"
   150  			mquery_refer_table("table1", ["key1", key], [value, false])`,
   151  			key:      []string{"key1", "key2", "f1", "f2"},
   152  			expected: []any{"ab", float64(1234), int64(123), false},
   153  		},
   154  	}
   155  
   156  	for _, tc := range testCase {
   157  		t.Run(tc.name, func(t *testing.T) {
   158  			runner, err := NewTestingRunner(tc.script)
   159  			if err != nil {
   160  				t.Error(err)
   161  				return
   162  			}
   163  
   164  			pt := ptinput.NewPlPoint(
   165  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
   166  
   167  			pt.SetPlReferTables(refTable.Tables())
   168  
   169  			errR := runScript(runner, pt)
   170  
   171  			if errR != nil {
   172  				t.Fatal(errR.Error())
   173  			}
   174  
   175  			for idxK, key := range tc.key {
   176  				v, _, err := pt.Get(key)
   177  				if err != nil {
   178  					if len(tc.expected) != 0 {
   179  						t.Logf("key: %s, value exp: %v  act: nil",
   180  							key, tc.expected[idxK])
   181  					}
   182  				}
   183  				assert.Equal(t, tc.expected[idxK], v)
   184  			}
   185  		})
   186  	}
   187  }
   188  
   189  func newJSONDataServer(files map[string]string) *httptest.Server {
   190  	server := httptest.NewServer(http.HandlerFunc(
   191  		func(w http.ResponseWriter, r *http.Request) {
   192  			switch r.Method {
   193  			case http.MethodGet:
   194  			default:
   195  				w.WriteHeader(http.StatusBadRequest)
   196  				return
   197  			}
   198  			name := r.FormValue("name")
   199  			data := files[name]
   200  			w.Write([]byte(data))
   201  			w.WriteHeader(http.StatusOK)
   202  		},
   203  	))
   204  	return server
   205  }
   206  
   207  var testTableData = `
   208  [
   209  	{
   210  		"table_name": "table1",
   211  		"column_name": [
   212  			 "key1",
   213  			 "key2",
   214  			 "f1",
   215  			 "f2"
   216  		],
   217  		"column_type": [
   218  			 "string",
   219  			 "float",
   220  			 "int",
   221  			 "bool"
   222  		],
   223  		"row_data": [
   224  			 [
   225  				  "a",
   226  				  123,
   227  				  "123",
   228  				  "false"
   229  			 ],
   230  			 [
   231  				  "ab",
   232  				  "1234",
   233  				  "123",
   234  				  "true"
   235  			 ],
   236  			 [
   237  				"ab",
   238  				"1234",
   239  				"123",
   240  				"false"
   241  		   ]
   242  		]
   243  	},
   244  	{
   245  		"table_name": "table2",
   246  		"primary_key": [
   247  			 "key1",
   248  			 "key2"
   249  		],
   250  		"column_name": [
   251  			 "key1",
   252  			 "key2",
   253  			 "f1",
   254  			 "f2"
   255  		],
   256  		"column_type": [
   257  			 "string",
   258  			 "float",
   259  			 "int",
   260  			 "bool"
   261  		],
   262  		"row_data": [
   263  			 [
   264  				  "a",
   265  				  123,
   266  				  "123",
   267  				  "true"
   268  			 ],
   269  			 [
   270  				  "a",
   271  				  "1234",
   272  				  "123",
   273  				  "true"
   274  			 ]
   275  		]
   276  	}
   277  ]
   278  
   279  `