github.com/blong14/gache@v0.0.0-20240124023949-89416fd8bbfa/sql/sql_test.go (about)

     1  package sql
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	gdb "github.com/blong14/gache/internal/db"
     8  )
     9  
    10  func TestParse(t *testing.T) {
    11  	tests := map[string]*gdb.Query{
    12  		"select * from default;": {
    13  			Header: gdb.QueryHeader{
    14  				Inst:      gdb.GetRange,
    15  				TableName: []byte("default"),
    16  			},
    17  		},
    18  		"select * from default limit 10;": {
    19  			Header: gdb.QueryHeader{
    20  				Inst:      gdb.GetRange,
    21  				TableName: []byte("default"),
    22  			},
    23  			KeyRange: gdb.KeyRange{Limit: 10},
    24  		},
    25  
    26  		"select * from default where key = __key__;": {
    27  			Header: gdb.QueryHeader{
    28  				Inst:      gdb.GetValue,
    29  				TableName: []byte("default")},
    30  			Key: []byte("__key__"),
    31  		},
    32  		"select * from default where key between aaa and ddd;": {
    33  			Header: gdb.QueryHeader{
    34  				Inst:      gdb.GetRange,
    35  				TableName: []byte("default"),
    36  			},
    37  			KeyRange: gdb.KeyRange{Start: []byte("aaa"), End: []byte("ddd")},
    38  		},
    39  		"select count from default;": {
    40  			Header: gdb.QueryHeader{
    41  				Inst:      gdb.Count,
    42  				TableName: []byte("default"),
    43  			},
    44  		},
    45  
    46  		"insert into default set key = _key, value = _value;": {
    47  			Header: gdb.QueryHeader{
    48  				Inst:      gdb.SetValue,
    49  				TableName: []byte("default"),
    50  			},
    51  			Key:   []byte("_key"),
    52  			Value: []byte("_value"),
    53  		},
    54  
    55  		"copy default from ./persons.csv;": {
    56  			Header: gdb.QueryHeader{
    57  				Inst:      gdb.Load,
    58  				TableName: []byte("default"),
    59  				FileName:  []byte("./persons.csv"),
    60  			},
    61  		},
    62  
    63  		"create table default;": {
    64  			Header: gdb.QueryHeader{
    65  				Inst:      gdb.AddTable,
    66  				TableName: []byte("default"),
    67  			},
    68  		},
    69  	}
    70  	for test, expected := range tests {
    71  		t.Run(test, func(t *testing.T) {
    72  			reader := strings.NewReader(test)
    73  			query, err := parse(reader)
    74  			if err != nil {
    75  				t.Error(err)
    76  			}
    77  			if query.String() != expected.String() {
    78  				t.Errorf("e %s g %s", expected, query)
    79  			}
    80  			t.Log(query.String())
    81  		})
    82  	}
    83  }