github.com/octohelm/storage@v0.0.0-20240516030302-1ac2cc1ea347/pkg/sqlbuilder/utils__test.go (about)

     1  package sqlbuilder_test
     2  
     3  import (
     4  	"context"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/octohelm/storage/internal/testutil"
     9  	. "github.com/octohelm/storage/pkg/sqlbuilder"
    10  )
    11  
    12  func TestValueMap(t *testing.T) {
    13  	type User struct {
    14  		ID       uint64 `db:"f_id"`
    15  		Name     string `db:"f_name"`
    16  		Username string `db:"f_username"`
    17  	}
    18  
    19  	user := User{
    20  		ID: 123123213,
    21  	}
    22  
    23  	t.Run("#FieldValuesFromStructBy", func(t *testing.T) {
    24  		testutil.Expect(t, FieldValuesFromStructBy(user, []string{}), testutil.HaveLen[FieldValues](0))
    25  
    26  		values := FieldValuesFromStructBy(user, []string{"ID"})
    27  
    28  		testutil.Expect(t, values, testutil.Equal(FieldValues{
    29  			"ID": user.ID,
    30  		}))
    31  	})
    32  
    33  	t.Run("#FieldValuesFromStructBy", func(t *testing.T) {
    34  		testutil.Expect(t, FieldValuesFromStructByNonZero(user), testutil.Equal(FieldValues{
    35  			"ID": user.ID,
    36  		}))
    37  
    38  		testutil.Expect(t, FieldValuesFromStructByNonZero(user, "Username"), testutil.Equal(FieldValues{
    39  			"ID":       user.ID,
    40  			"Username": user.Username,
    41  		}))
    42  	})
    43  
    44  	t.Run("#GetColumnName", func(t *testing.T) {
    45  		testutil.Expect(t, GetColumnName("Text", ""), testutil.Equal("f_text"))
    46  		testutil.Expect(t, GetColumnName("Text", ",size=256"), testutil.Equal("f_text"))
    47  		testutil.Expect(t, GetColumnName("Text", "f_text2"), testutil.Equal("f_text2"))
    48  		testutil.Expect(t, GetColumnName("Text", "f_text2,default=''"), testutil.Equal("f_text2"))
    49  	})
    50  }
    51  
    52  func TestParseDef(t *testing.T) {
    53  	t.Run("index with Field Names", func(t *testing.T) {
    54  
    55  		i := ParseIndexDefine("index i_xxx/BTREE Name")
    56  
    57  		testutil.Expect(t, i, testutil.Equal(&IndexDefine{
    58  			Kind:              "index",
    59  			Name:              "i_xxx",
    60  			Method:            "BTREE",
    61  			ColNameAndOptions: []string{"Name"},
    62  		}))
    63  	})
    64  
    65  	t.Run("primary with Field Names", func(t *testing.T) {
    66  
    67  		i := ParseIndexDefine("primary ID Name")
    68  
    69  		testutil.Expect(t, i, testutil.Equal(&IndexDefine{
    70  			Kind:              "primary",
    71  			ColNameAndOptions: []string{"ID", "Name"},
    72  		}))
    73  	})
    74  
    75  	t.Run("index with expr", func(t *testing.T) {
    76  		i := ParseIndexDefine("index i_xxx/GIST Test/gist_trgm_ops")
    77  
    78  		testutil.Expect(t, i, testutil.Equal(&IndexDefine{
    79  			Kind:   "index",
    80  			Name:   "i_xxx",
    81  			Method: "GIST",
    82  			ColNameAndOptions: []string{
    83  				"Test/gist_trgm_ops",
    84  			},
    85  		}))
    86  	})
    87  }
    88  
    89  type UserBase struct {
    90  	ID       uint64 `db:"f_id"`
    91  	Name     string `db:"f_name"`
    92  	Username string `db:"f_username"`
    93  }
    94  
    95  func (UserBase) TableName() string {
    96  	return "t_user_base"
    97  }
    98  
    99  type User struct {
   100  	UserBase
   101  }
   102  
   103  func (User) TableName() string {
   104  	return "t_user"
   105  }
   106  
   107  type OrgUser struct {
   108  	OrgID  uint64 `db:"f_org_id"`
   109  	UserID uint64 `db:"f_user_id"`
   110  }
   111  
   112  func (OrgUser) TableName() string {
   113  	return "t_org_user"
   114  }
   115  
   116  type Org struct {
   117  	ID   uint64 `db:"f_id"`
   118  	Name string `db:"f_name"`
   119  }
   120  
   121  func (Org) TableName() string {
   122  	return "t_org"
   123  }
   124  
   125  type OrgUserAll struct {
   126  	OrgUser
   127  	User User `json:"user"`
   128  	Org  Org  `json:"org"`
   129  }
   130  
   131  func TestColumnsByStruct(t *testing.T) {
   132  	t.Run("simple", func(t *testing.T) {
   133  		q := ColumnsByStruct(&User{}).Ex(context.Background()).Query()
   134  		testutil.Expect(t, q, testutil.Equal("t_user.f_id AS t_user__f_id, t_user.f_name AS t_user__f_name, t_user.f_username AS t_user__f_username"))
   135  	})
   136  
   137  	t.Run("joined", func(t *testing.T) {
   138  		q := ColumnsByStruct(&OrgUserAll{}).Ex(context.Background()).Query()
   139  		for _, g := range strings.Split(q, ", ") {
   140  			t.Log(g)
   141  		}
   142  		testutil.Expect(t, q, testutil.Equal("t_org_user.f_org_id AS t_org_user__f_org_id, t_org_user.f_user_id AS t_org_user__f_user_id, t_user.f_id AS t_user__f_id, t_user.f_name AS t_user__f_name, t_user.f_username AS t_user__f_username, t_org.f_id AS t_org__f_id, t_org.f_name AS t_org__f_name"))
   143  	})
   144  }