github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/refertable/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 refertable
     7  
     8  import (
     9  	"testing"
    10  )
    11  
    12  func TestTable(t *testing.T) {
    13  	tables, err := decodeJSONData([]byte(testTableData))
    14  	if err != nil {
    15  		t.Fatal(err)
    16  	}
    17  
    18  	plRefTable := PlReferTablesInMemory{}
    19  	plRefTable.updateAll(tables)
    20  
    21  	if v, ok := plRefTable.Query("table1", []string{"key1"}, []any{"ab"}, nil); ok {
    22  		t.Log(v)
    23  	} else {
    24  		t.Error(ok)
    25  	}
    26  
    27  	if _, ok := plRefTable.Query("table1", []string{"key1", "key2"}, []any{"ab"}, nil); ok {
    28  		t.Errorf("exp: false, act: %v", ok)
    29  	}
    30  }
    31  
    32  func BenchmarkTableQueyr(b *testing.B) {
    33  	tables, err := decodeJSONData([]byte(testTableData))
    34  	if err != nil {
    35  		b.Fatal(err)
    36  	}
    37  
    38  	plRefTable := PlReferTablesInMemory{}
    39  	plRefTable.updateAll(tables)
    40  
    41  	for i := 0; i < b.N; i++ {
    42  		if v, ok := plRefTable.Query("table1", []string{"key1"}, []any{"ab"}, nil); ok {
    43  			b.Log(v)
    44  		} else {
    45  			b.Error(ok)
    46  		}
    47  	}
    48  }
    49  
    50  var testTableData = `
    51  [
    52  	{
    53  		"table_name": "table1",
    54  		"column_name": [
    55  			 "key1",
    56  			 "key2",
    57  			 "f1",
    58  			 "f2"
    59  		],
    60  		"column_type": [
    61  			 "string",
    62  			 "float",
    63  			 "int",
    64  			 "bool"
    65  		],
    66  		"row_data": [
    67  			 [
    68  				  "a",
    69  				  123,
    70  				  "123",
    71  				  "true"
    72  			 ],
    73  			 [
    74  				  "ab",
    75  				  "1234",
    76  				  "123",
    77  				  "true"
    78  			 ],
    79  			 [
    80  				"ab",
    81  				"1234",
    82  				"123",
    83  				"true"
    84  		   ]
    85  		]
    86  	},
    87  	{
    88  		"table_name": "table2",
    89  		"primary_key": [
    90  			 "key1",
    91  			 "key2"
    92  		],
    93  		"column_name": [
    94  			 "key1",
    95  			 "key2",
    96  			 "f1",
    97  			 "f2"
    98  		],
    99  		"column_type": [
   100  			 "string",
   101  			 "float",
   102  			 "int",
   103  			 "bool"
   104  		],
   105  		"row_data": [
   106  			 [
   107  				  "a",
   108  				  123,
   109  				  "123",
   110  				  "true"
   111  			 ],
   112  			 [
   113  				  "a",
   114  				  "1234",
   115  				  "123",
   116  				  "true"
   117  			 ]
   118  		]
   119  	}
   120  ]
   121  
   122  `