github.com/blend/go-sdk@v1.20220411.3/db/column_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package db
     9  
    10  import (
    11  	"database/sql"
    12  	"testing"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  	"github.com/blend/go-sdk/ref"
    16  )
    17  
    18  type jsonFieldValue struct {
    19  	Label string `json:"label"`
    20  	Value string `json:"value"`
    21  }
    22  
    23  type setValueTest struct {
    24  	PrimaryKey   string `db:"primary_key,pk"`
    25  	UniqueKey    string `db:"unique_key,uk"`
    26  	InferredName string
    27  	NullFloat64  sql.NullFloat64        `db:"null_float64"`
    28  	NullInt64    sql.NullInt64          `db:"null_int64"`
    29  	NullString   sql.NullString         `db:"null_string"`
    30  	JSON         map[string]interface{} `db:"json_col,json"`
    31  	JSONPtr      *jsonFieldValue        `db:"json_ptr,json"`
    32  	Int64        int64                  `db:"int64"`
    33  	Int64Ptr     *int64                 `db:"int64_ptr"`
    34  	StringPtr    *string                `db:"string_ptr"`
    35  }
    36  
    37  func Test_Column_SetValue(t *testing.T) {
    38  	its := assert.New(t)
    39  
    40  	var obj setValueTest
    41  	meta := Columns(obj)
    42  	col := meta.Lookup()["int64"]
    43  	its.NotNil(col)
    44  
    45  	var value int64 = 10
    46  	its.Zero(obj.Int64)
    47  	its.Nil(col.SetValue(&obj, value))
    48  	its.Equal(10, obj.Int64)
    49  }
    50  
    51  func Test_Column_SetValueConverted(t *testing.T) {
    52  	its := assert.New(t)
    53  
    54  	obj := setValueTest{InferredName: "Hello."}
    55  	meta := Columns(obj)
    56  	col := meta.Lookup()["int64"]
    57  	its.NotNil(col)
    58  
    59  	value := int(21)
    60  	err := col.SetValue(&obj, value)
    61  	its.Nil(err)
    62  	its.Equal(21, obj.Int64)
    63  }
    64  
    65  func Test_Column_SetValuePtrAddr(t *testing.T) {
    66  	its := assert.New(t)
    67  
    68  	var obj setValueTest
    69  	meta := Columns(obj)
    70  	col := meta.Lookup()["string_ptr"]
    71  	its.NotNil(col)
    72  
    73  	value := "foobar"
    74  	err := col.SetValue(&obj, value)
    75  	its.NotNil(err)
    76  	its.Nil(obj.StringPtr)
    77  }
    78  
    79  func Test_Column_SetValueStringPtr(t *testing.T) {
    80  	its := assert.New(t)
    81  
    82  	var obj setValueTest
    83  	meta := Columns(obj)
    84  	col := meta.Lookup()["string_ptr"]
    85  	its.NotNil(col)
    86  
    87  	value := "foobar"
    88  	err := col.SetValue(&obj, &value)
    89  	its.Nil(err)
    90  	its.NotNil(obj.StringPtr)
    91  	its.Equal("foobar", *obj.StringPtr)
    92  }
    93  
    94  func Test_Column_SetValueInt64Ptr(t *testing.T) {
    95  	its := assert.New(t)
    96  
    97  	var obj setValueTest
    98  	meta := Columns(obj)
    99  
   100  	col := meta.Lookup()["int64_ptr"]
   101  	its.NotNil(col)
   102  	myValue := int64(21)
   103  	err := col.SetValue(&obj, &myValue)
   104  	its.Nil(err)
   105  
   106  	its.NotNil(obj.Int64Ptr)
   107  	its.Equal(21, *obj.Int64Ptr)
   108  }
   109  
   110  func Test_Column_SetValueJSONNullString(t *testing.T) {
   111  	its := assert.New(t)
   112  
   113  	var obj setValueTest
   114  	meta := Columns(obj)
   115  
   116  	col := meta.Lookup()["json_col"]
   117  	its.NotNil(col)
   118  	err := col.SetValue(&obj,
   119  		sql.NullString{String: `{"foo":"bar"}`, Valid: true},
   120  	)
   121  	its.Nil(err)
   122  	its.Equal("bar", obj.JSON["foo"])
   123  }
   124  
   125  func Test_Column_SetValueJSONNullStringUnset(t *testing.T) {
   126  	its := assert.New(t)
   127  
   128  	var obj setValueTest
   129  	meta := Columns(obj)
   130  
   131  	col := meta.Lookup()["json_col"]
   132  	its.NotNil(col)
   133  	err := col.SetValue(&obj,
   134  		sql.NullString{},
   135  	)
   136  	its.Nil(err)
   137  	its.Nil(obj.JSON)
   138  }
   139  
   140  func Test_Column_SetValueJSONNullStringPtr(t *testing.T) {
   141  	its := assert.New(t)
   142  
   143  	var obj setValueTest
   144  	meta := Columns(obj)
   145  
   146  	col := meta.Lookup()["json_col"]
   147  	its.NotNil(col)
   148  	err := col.SetValue(&obj,
   149  		&sql.NullString{String: `{"foo":"bar"}`, Valid: true},
   150  	)
   151  	its.Nil(err)
   152  	its.Equal("bar", obj.JSON["foo"])
   153  }
   154  
   155  func Test_Column_SetValueJSONNullStringPtrObjectPtr(t *testing.T) {
   156  	its := assert.New(t)
   157  
   158  	var obj setValueTest
   159  	meta := Columns(obj)
   160  
   161  	col := meta.Lookup()["json_ptr"]
   162  	its.NotNil(col)
   163  	err := col.SetValue(&obj,
   164  		&sql.NullString{String: `{"label":"foo", "value":"bar"}`, Valid: true},
   165  	)
   166  	its.Nil(err)
   167  	its.Equal("foo", obj.JSONPtr.Label)
   168  	its.Equal("bar", obj.JSONPtr.Value)
   169  }
   170  
   171  func Test_Column_SetValueJSONNullStringPtrUnset(t *testing.T) {
   172  	its := assert.New(t)
   173  
   174  	var obj setValueTest
   175  	meta := Columns(obj)
   176  
   177  	col := meta.Lookup()["json_col"]
   178  	its.NotNil(col)
   179  	err := col.SetValue(&obj,
   180  		&sql.NullString{},
   181  	)
   182  	its.Nil(err)
   183  	its.Nil(obj.JSON)
   184  }
   185  
   186  func Test_Column_SetValueJSONString(t *testing.T) {
   187  	its := assert.New(t)
   188  
   189  	var obj setValueTest
   190  	meta := Columns(obj)
   191  
   192  	col := meta.Lookup()["json_col"]
   193  	its.NotNil(col)
   194  	err := col.SetValue(&obj,
   195  		`{"foo":"bar"}`,
   196  	)
   197  	its.Nil(err)
   198  	its.Equal("bar", obj.JSON["foo"])
   199  }
   200  
   201  func Test_Column_SetValueJSONStringPtr(t *testing.T) {
   202  	its := assert.New(t)
   203  
   204  	var obj setValueTest
   205  	meta := Columns(obj)
   206  
   207  	col := meta.Lookup()["json_col"]
   208  	its.NotNil(col)
   209  	value := `{"foo":"bar"}`
   210  	err := col.SetValue(&obj,
   211  		&value,
   212  	)
   213  	its.Nil(err)
   214  	its.Equal("bar", obj.JSON["foo"])
   215  }
   216  
   217  func Test_Column_SetValueJSONBytes(t *testing.T) {
   218  	its := assert.New(t)
   219  
   220  	var obj setValueTest
   221  	meta := Columns(obj)
   222  
   223  	col := meta.Lookup()["json_col"]
   224  	its.NotNil(col)
   225  	value := []byte(`{"foo":"bar"}`)
   226  	err := col.SetValue(&obj,
   227  		value,
   228  	)
   229  	its.Nil(err)
   230  	its.Equal("bar", obj.JSON["foo"])
   231  }
   232  
   233  func Test_Column_SetValueJSONBytesPtr(t *testing.T) {
   234  	its := assert.New(t)
   235  
   236  	var obj setValueTest
   237  	meta := Columns(obj)
   238  
   239  	col := meta.Lookup()["json_col"]
   240  	its.NotNil(col)
   241  	value := []byte(`{"foo":"bar"}`)
   242  	err := col.SetValue(&obj,
   243  		&value,
   244  	)
   245  	its.Nil(err)
   246  	its.Equal("bar", obj.JSON["foo"])
   247  }
   248  
   249  func Test_Column_SetValueResetsNil(t *testing.T) {
   250  	its := assert.New(t)
   251  
   252  	obj := setValueTest{Int64Ptr: ref.Int64(1234)}
   253  	meta := Columns(obj)
   254  
   255  	col := meta.Lookup()["int64_ptr"]
   256  	its.NotNil(col)
   257  	err := col.SetValue(&obj, nil)
   258  	its.Nil(err)
   259  	its.Nil(obj.Int64Ptr)
   260  }
   261  
   262  func Test_Column_SetValueResetsNilUnset(t *testing.T) {
   263  	its := assert.New(t)
   264  
   265  	obj := setValueTest{Int64Ptr: ref.Int64(1234)}
   266  	meta := Columns(obj)
   267  
   268  	col := meta.Lookup()["int64_ptr"]
   269  	its.NotNil(col)
   270  
   271  	var myValue *int
   272  	err := col.SetValue(&obj, myValue)
   273  	its.Nil(err)
   274  	its.Nil(obj.Int64Ptr)
   275  }
   276  
   277  func Test_Column_GetValue(t *testing.T) {
   278  	its := assert.New(t)
   279  	obj := myStruct{EmbeddedMeta: EmbeddedMeta{PrimaryKeyCol: 5}, InferredName: "Hello."}
   280  	meta := Columns(obj)
   281  	pk := meta.PrimaryKeys().FirstOrDefault()
   282  	its.NotNil(pk)
   283  	value := pk.GetValue(&obj)
   284  	its.NotNil(value)
   285  	its.Equal(5, value)
   286  }