github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/refertable/table_sqlite_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  //go:build !(windows && 386)
     7  // +build !windows !386
     8  
     9  package refertable
    10  
    11  import (
    12  	"database/sql"
    13  	"reflect"
    14  	"testing"
    15  
    16  	_ "modernc.org/sqlite"
    17  )
    18  
    19  func Test_buildCreateTableStmt(t *testing.T) {
    20  	type args struct {
    21  		table referTable
    22  	}
    23  	tests := []struct {
    24  		name string
    25  		in   args
    26  		want string
    27  	}{
    28  		{
    29  			name: "normal",
    30  			in: args{referTable{
    31  				TableName:  "test_table",
    32  				ColumnName: []string{"c1", "c2"},
    33  				ColumnType: []string{columnTypeStr, columnTypeInt},
    34  			}},
    35  			want: "CREATE TABLE test_table (c1 TEXT, c2 INTEGER)",
    36  		},
    37  		{
    38  			name: "normal",
    39  			in: args{referTable{
    40  				TableName:  "employee",
    41  				ColumnName: []string{"c1", "c2"},
    42  				ColumnType: []string{columnTypeFloat, columnTypeBool},
    43  			}},
    44  			want: "CREATE TABLE employee (c1 REAL, c2 NUMERIC)",
    45  		},
    46  	}
    47  	for _, tt := range tests {
    48  		t.Run(tt.name, func(t *testing.T) {
    49  			if got := buildCreateTableStmt(&tt.in.table); !reflect.DeepEqual(got, tt.want) {
    50  				t.Errorf("buildCreateTableStmt() = %v, want %v", got, tt.want)
    51  			}
    52  		})
    53  	}
    54  }
    55  
    56  func Test_buildInsertIntoStmts(t *testing.T) {
    57  	type args struct {
    58  		table referTable
    59  	}
    60  	tests := []struct {
    61  		name string
    62  		in   args
    63  		want string
    64  	}{
    65  		{
    66  			name: "normal",
    67  			in: args{referTable{
    68  				TableName:  "test_table",
    69  				ColumnName: []string{"c1", "c2"},
    70  				ColumnType: []string{columnTypeStr, columnTypeInt},
    71  				RowData:    [][]any{{"fiona", 20}, {"michael", 25}},
    72  			}},
    73  			want: "INSERT INTO test_table (c1, c2) VALUES (?, ?)",
    74  		},
    75  	}
    76  	for _, tt := range tests {
    77  		t.Run(tt.name, func(t *testing.T) {
    78  			if got := buildInsertIntoStmts(&tt.in.table); !reflect.DeepEqual(got, tt.want) {
    79  				t.Errorf("buildInsertIntoStmts() = %v, want %v", got, tt.want)
    80  			}
    81  		})
    82  	}
    83  }
    84  
    85  func Test_buildSelectStmt(t *testing.T) {
    86  	type args struct {
    87  		tableName string
    88  		colName   []string
    89  		colValue  []any
    90  		kGet      []string
    91  	}
    92  	tests := []struct {
    93  		name string
    94  		in   args
    95  		want string
    96  	}{
    97  		{
    98  			name: "normal",
    99  			in: args{
   100  				tableName: "test_table",
   101  				colName:   []string{},
   102  				colValue:  []any{},
   103  				kGet:      []string{"id"},
   104  			},
   105  			want: "SELECT id FROM test_table",
   106  		},
   107  		{
   108  			name: "normal",
   109  			in: args{
   110  				tableName: "test_table",
   111  				colName:   []string{"weight"},
   112  				colValue:  []any{50.5},
   113  				kGet:      []string{"id"},
   114  			},
   115  			want: "SELECT id FROM test_table WHERE weight = ?",
   116  		},
   117  		{
   118  			name: "normal",
   119  			in: args{
   120  				tableName: "test_table",
   121  				colName:   []string{"name", "age"},
   122  				colValue:  []any{"fiona", 21},
   123  				kGet:      []string{"id", "name", "age"},
   124  			},
   125  			want: "SELECT id, name, age FROM test_table WHERE name = ? AND age = ?",
   126  		},
   127  	}
   128  
   129  	for _, tt := range tests {
   130  		t.Run(tt.name, func(t *testing.T) {
   131  			if got := buildSelectStmt(tt.in.tableName, tt.in.colName, tt.in.kGet); !reflect.DeepEqual(got, tt.want) {
   132  				t.Errorf("buildSelectStmt() = %v, want %v", got, tt.want)
   133  			}
   134  		})
   135  	}
   136  }
   137  
   138  func TestPlReferTablesSqlite_query(t *testing.T) {
   139  	p := &PlReferTablesSqlite{}
   140  	d, err := sql.Open("sqlite", ":memory:")
   141  	p.db = d
   142  	if err != nil {
   143  		t.Fatal(err)
   144  	}
   145  	if err := p.db.Ping(); err != nil {
   146  		t.Fatal(err)
   147  	}
   148  	r := p.db.QueryRow("select sqlite_version()")
   149  	var ver string
   150  	r.Scan(&ver)
   151  	t.Logf("sqlite version: %s", ver)
   152  	if _, err := p.db.Exec("CREATE TABLE test(id INTEGER, name TEXT, age INTEGER, alive NUMERIC, grade REAL)"); err != nil {
   153  		t.Fatal(err)
   154  	}
   155  	if _, err := p.db.Exec("INSERT INTO test(id, name, age, alive, grade) VALUES(23, \"Michael\", 25, true, 99.5)"); err != nil {
   156  		t.Fatal(err)
   157  	}
   158  	if _, err := p.db.Exec("INSERT INTO test(id, name) VALUES(23, \"Jimmy\")"); err != nil {
   159  		t.Fatal(err)
   160  	}
   161  	if _, err := p.db.Exec("INSERT INTO test(id, name, age, alive, grade) VALUES(8, \"Kobe\", 40, false, 99.0)"); err != nil {
   162  		t.Fatal(err)
   163  	}
   164  
   165  	type args struct {
   166  		tableName string
   167  		colName   []string
   168  		colValue  []any
   169  		kGet      []string
   170  	}
   171  	tests := []struct {
   172  		name  string
   173  		args  args
   174  		want  map[string]any
   175  		want1 bool
   176  	}{
   177  		{
   178  			name: "select by id",
   179  			args: args{
   180  				tableName: "test",
   181  				colName:   []string{"id"},
   182  				colValue:  []any{8},
   183  				kGet:      []string{"id", "name"},
   184  			},
   185  			want:  map[string]any{"id": int64(8), "name": "Kobe"},
   186  			want1: true,
   187  		},
   188  		{
   189  			name: "select by id and name",
   190  			args: args{
   191  				tableName: "test",
   192  				colName:   []string{"id", "name"},
   193  				colValue:  []any{23, "Michael"},
   194  				kGet:      []string{},
   195  			},
   196  			want:  map[string]any{"id": int64(23), "name": "Michael", "age": int64(25), "alive": int64(1), "grade": 99.5},
   197  			want1: true,
   198  		},
   199  	}
   200  	for _, tt := range tests {
   201  		t.Run(tt.name, func(t *testing.T) {
   202  			got, got1 := p.Query(tt.args.tableName, tt.args.colName, tt.args.colValue, tt.args.kGet)
   203  			if !reflect.DeepEqual(got, tt.want) {
   204  				t.Errorf("PlReferTablesSqlite.query() got = %v, want %v", got, tt.want)
   205  			}
   206  			if got1 != tt.want1 {
   207  				t.Errorf("PlReferTablesSqlite.query() got1 = %v, want %v", got1, tt.want1)
   208  			}
   209  		})
   210  	}
   211  }