...

Source file src/github.com/doug-martin/goqu/v9/exp/insert_test.go

Documentation: github.com/doug-martin/goqu/v9/exp

     1  package exp_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/doug-martin/goqu/v9/exp"
     7  	"github.com/stretchr/testify/suite"
     8  )
     9  
    10  type testAppendableExpression struct {
    11  	exp.AppendableExpression
    12  	sql     string
    13  	args    []interface{}
    14  	clauses exp.SelectClauses
    15  }
    16  
    17  func newTestAppendableExpression(sql string, args []interface{}) exp.AppendableExpression {
    18  	return &testAppendableExpression{sql: sql, args: args}
    19  }
    20  
    21  func (tae *testAppendableExpression) Expression() exp.Expression {
    22  	return tae
    23  }
    24  
    25  func (tae *testAppendableExpression) GetClauses() exp.SelectClauses {
    26  	return tae.clauses
    27  }
    28  
    29  func (tae *testAppendableExpression) Clone() exp.Expression {
    30  	return tae
    31  }
    32  
    33  type insertExpressionTestSuite struct {
    34  	suite.Suite
    35  }
    36  
    37  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withDifferentRecordTypes() {
    38  	type testRecord struct {
    39  		C string `db:"c"`
    40  	}
    41  	type testRecord2 struct {
    42  		C string `db:"c"`
    43  	}
    44  	_, err := exp.NewInsertExpression(
    45  		testRecord{C: "v1"},
    46  		exp.Record{"c": "v2"},
    47  	)
    48  	iets.EqualError(err, "goqu: rows must be all the same type expected exp_test.testRecord got exp.Record")
    49  	_, err = exp.NewInsertExpression(
    50  		testRecord{C: "v1"},
    51  		testRecord2{C: "v2"},
    52  	)
    53  	iets.EqualError(err, "goqu: rows must be all the same type expected exp_test.testRecord got exp_test.testRecord2")
    54  }
    55  
    56  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withInvalidValue() {
    57  	_, err := exp.NewInsertExpression(true)
    58  	iets.EqualError(err, "goqu: unsupported insert must be map, goqu.Record, or struct type got: bool")
    59  }
    60  
    61  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withDifferentTypes() {
    62  	_, err := exp.NewInsertExpression(exp.Record{"a": "a1"}, true)
    63  	iets.EqualError(err, "goqu: rows must be all the same type expected exp.Record got bool")
    64  }
    65  
    66  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withNoValues() {
    67  	ie, err := exp.NewInsertExpression()
    68  	iets.NoError(err)
    69  	iets.Nil(ie.Cols())
    70  	iets.Nil(ie.Vals())
    71  	iets.True(ie.IsEmpty())
    72  	iets.False(ie.IsInsertFrom())
    73  }
    74  
    75  func (iets *insertExpressionTestSuite) TestNewInsertExpression_Vals() {
    76  	ie, err := exp.NewInsertExpression()
    77  	iets.NoError(err)
    78  	vals := [][]interface{}{
    79  		{"a", "b"},
    80  	}
    81  	ie = ie.SetCols(exp.NewColumnListExpression("a", "b")).SetVals(vals)
    82  	iets.False(ie.IsEmpty())
    83  	iets.False(ie.IsInsertFrom())
    84  	iets.Equal(vals, ie.Vals())
    85  }
    86  
    87  func (iets *insertExpressionTestSuite) TestNewInsertExpression_Cols() {
    88  	ie, err := exp.NewInsertExpression()
    89  	iets.NoError(err)
    90  	vals := [][]interface{}{
    91  		{"a", "b"},
    92  	}
    93  	ce := exp.NewColumnListExpression("a", "b")
    94  	ie = ie.SetCols(ce).SetVals(vals)
    95  	iets.False(ie.IsEmpty())
    96  	iets.False(ie.IsInsertFrom())
    97  	iets.Equal(vals, ie.Vals())
    98  	iets.Equal(ce, ie.Cols())
    99  }
   100  
   101  func (iets *insertExpressionTestSuite) TestNewInsertExpression_From() {
   102  	ae := newTestAppendableExpression("select * from test", []interface{}{})
   103  	ie, err := exp.NewInsertExpression(ae)
   104  	iets.NoError(err)
   105  	iets.False(ie.IsEmpty())
   106  	iets.True(ie.IsInsertFrom())
   107  	iets.Equal(ae, ie.From())
   108  }
   109  
   110  func (iets *insertExpressionTestSuite) TestNewInsertExpression_appendableExpression() {
   111  	ae := newTestAppendableExpression("test ae", nil)
   112  
   113  	ie, err := exp.NewInsertExpression(ae)
   114  	iets.NoError(err)
   115  	iets.False(ie.IsEmpty())
   116  	iets.True(ie.IsInsertFrom())
   117  }
   118  
   119  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withRecords() {
   120  	ie, err := exp.NewInsertExpression(exp.Record{"c": "a"}, exp.Record{"c": "b"})
   121  	iets.NoError(err)
   122  	iets.Equal(exp.NewColumnListExpression("c"), ie.Cols())
   123  	iets.Equal([][]interface{}{{"a"}, {"b"}}, ie.Vals())
   124  	iets.False(ie.IsEmpty())
   125  	iets.False(ie.IsInsertFrom())
   126  }
   127  
   128  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withRecordsSlice() {
   129  	ie, err := exp.NewInsertExpression([]exp.Record{{"c": "a"}, {"c": "b"}})
   130  	iets.NoError(err)
   131  	iets.Equal(exp.NewColumnListExpression("c"), ie.Cols())
   132  	iets.Equal([][]interface{}{{"a"}, {"b"}}, ie.Vals())
   133  	iets.False(ie.IsEmpty())
   134  	iets.False(ie.IsInsertFrom())
   135  }
   136  
   137  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withRecordOfDifferentLength() {
   138  	_, err := exp.NewInsertExpression(exp.Record{"c": "a"}, exp.Record{"c": "b", "c2": "d"})
   139  	iets.EqualError(err, "goqu: rows with different value length expected 1 got 2")
   140  }
   141  
   142  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withRecordWithDifferentkeys() {
   143  	_, err := exp.NewInsertExpression(exp.Record{"c1": "a"}, exp.Record{"c2": "b"})
   144  	iets.EqualError(err, `goqu: rows with different keys expected ["c1"] got ["c2"]`)
   145  }
   146  
   147  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withMap() {
   148  	ie, err := exp.NewInsertExpression(
   149  		map[string]interface{}{"c": "a"},
   150  		map[string]interface{}{"c": "b"},
   151  	)
   152  	iets.NoError(err)
   153  	iets.Equal(exp.NewColumnListExpression("c"), ie.Cols())
   154  	iets.Equal([][]interface{}{{"a"}, {"b"}}, ie.Vals())
   155  	iets.False(ie.IsEmpty())
   156  	iets.False(ie.IsInsertFrom())
   157  }
   158  
   159  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructs() {
   160  	type testRecord struct {
   161  		C string `db:"c"`
   162  	}
   163  	ie, err := exp.NewInsertExpression(
   164  		testRecord{C: "a"},
   165  		testRecord{C: "b"},
   166  	)
   167  	iets.NoError(err)
   168  	iets.Equal(exp.NewColumnListExpression("c"), ie.Cols())
   169  	iets.Equal([][]interface{}{{"a"}, {"b"}}, ie.Vals())
   170  	iets.False(ie.IsEmpty())
   171  	iets.False(ie.IsInsertFrom())
   172  }
   173  
   174  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructSlice() {
   175  	type testRecord struct {
   176  		C string `db:"c"`
   177  	}
   178  	ie, err := exp.NewInsertExpression([]testRecord{
   179  		{C: "a"},
   180  		{C: "b"},
   181  	})
   182  	iets.NoError(err)
   183  	iets.Equal(exp.NewColumnListExpression("c"), ie.Cols())
   184  	iets.Equal([][]interface{}{{"a"}, {"b"}}, ie.Vals())
   185  	iets.False(ie.IsEmpty())
   186  	iets.False(ie.IsInsertFrom())
   187  }
   188  
   189  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructsWithoutTags() {
   190  	type testRecord struct {
   191  		FieldA int64
   192  		FieldB bool
   193  		FieldC string
   194  	}
   195  	ie, err := exp.NewInsertExpression(
   196  		testRecord{FieldA: 1, FieldB: true, FieldC: "a"},
   197  		testRecord{FieldA: 2, FieldB: false, FieldC: "b"},
   198  	)
   199  	iets.NoError(err)
   200  	iets.Equal(exp.NewColumnListExpression("fielda", "fieldb", "fieldc"), ie.Cols())
   201  	iets.Equal([][]interface{}{{int64(1), true, "a"}, {int64(2), false, "b"}}, ie.Vals())
   202  	iets.False(ie.IsEmpty())
   203  	iets.False(ie.IsInsertFrom())
   204  }
   205  
   206  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructsIgnoredDbTag() {
   207  	type testRecord struct {
   208  		FieldA int64 `db:"-"`
   209  		FieldB bool
   210  		FieldC string
   211  	}
   212  	ie, err := exp.NewInsertExpression(
   213  		testRecord{FieldA: 1, FieldB: true, FieldC: "a"},
   214  		testRecord{FieldA: 2, FieldB: false, FieldC: "b"},
   215  	)
   216  	iets.NoError(err)
   217  	iets.Equal(exp.NewColumnListExpression("fieldb", "fieldc"), ie.Cols())
   218  	iets.Equal([][]interface{}{{true, "a"}, {false, "b"}}, ie.Vals())
   219  	iets.False(ie.IsEmpty())
   220  	iets.False(ie.IsInsertFrom())
   221  }
   222  
   223  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructsWithGoquSkipInsert() {
   224  	type testRecord struct {
   225  		FieldA int64
   226  		FieldB bool   `goqu:"skipupdate"`
   227  		FieldC string `goqu:"skipinsert"`
   228  	}
   229  	ie, err := exp.NewInsertExpression(
   230  		testRecord{FieldA: 1, FieldB: true, FieldC: "a"},
   231  		testRecord{FieldA: 2, FieldB: false, FieldC: "b"},
   232  	)
   233  	iets.NoError(err)
   234  	iets.Equal(exp.NewColumnListExpression("fielda", "fieldb"), ie.Cols())
   235  	iets.Equal([][]interface{}{{int64(1), true}, {int64(2), false}}, ie.Vals())
   236  	iets.False(ie.IsEmpty())
   237  	iets.False(ie.IsInsertFrom())
   238  }
   239  
   240  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructPointers() {
   241  	type testRecord struct {
   242  		C string `db:"c"`
   243  	}
   244  	ie, err := exp.NewInsertExpression(
   245  		&testRecord{C: "a"},
   246  		&testRecord{C: "b"},
   247  	)
   248  	iets.NoError(err)
   249  	iets.Equal(exp.NewColumnListExpression("c"), ie.Cols())
   250  	iets.Equal([][]interface{}{{"a"}, {"b"}}, ie.Vals())
   251  	iets.False(ie.IsEmpty())
   252  	iets.False(ie.IsInsertFrom())
   253  }
   254  
   255  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructsWithEmbeddedStructs() {
   256  	type Phone struct {
   257  		Primary string `db:"primary_phone"`
   258  		Home    string `db:"home_phone"`
   259  	}
   260  	type item struct {
   261  		Phone
   262  		Address string `db:"address"`
   263  		Name    string `db:"name"`
   264  	}
   265  	ie, err := exp.NewInsertExpression(
   266  		item{Address: "111 Test Addr", Name: "Test1", Phone: Phone{Home: "123123", Primary: "456456"}},
   267  		item{Address: "211 Test Addr", Name: "Test2", Phone: Phone{Home: "123123", Primary: "456456"}},
   268  		item{Address: "311 Test Addr", Name: "Test3", Phone: Phone{Home: "123123", Primary: "456456"}},
   269  		item{Address: "411 Test Addr", Name: "Test4", Phone: Phone{Home: "123123", Primary: "456456"}},
   270  	)
   271  	iets.NoError(err)
   272  	iets.Equal(exp.NewColumnListExpression("address", "home_phone", "name", "primary_phone"), ie.Cols())
   273  	iets.Equal([][]interface{}{
   274  		{"111 Test Addr", "123123", "Test1", "456456"},
   275  		{"211 Test Addr", "123123", "Test2", "456456"},
   276  		{"311 Test Addr", "123123", "Test3", "456456"},
   277  		{"411 Test Addr", "123123", "Test4", "456456"},
   278  	}, ie.Vals())
   279  	iets.False(ie.IsEmpty())
   280  	iets.False(ie.IsInsertFrom())
   281  }
   282  
   283  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withStructsWithEmbeddedStructPointers() {
   284  	type Phone struct {
   285  		Primary string `db:"primary_phone"`
   286  		Home    string `db:"home_phone"`
   287  	}
   288  	type item struct {
   289  		*Phone
   290  		Address string `db:"address"`
   291  		Name    string `db:"name"`
   292  	}
   293  	ie, err := exp.NewInsertExpression(
   294  		item{Address: "111 Test Addr", Name: "Test1", Phone: &Phone{Home: "123123", Primary: "456456"}},
   295  		item{Address: "211 Test Addr", Name: "Test2", Phone: &Phone{Home: "123123", Primary: "456456"}},
   296  		item{Address: "311 Test Addr", Name: "Test3", Phone: &Phone{Home: "123123", Primary: "456456"}},
   297  		item{Address: "411 Test Addr", Name: "Test4", Phone: &Phone{Home: "123123", Primary: "456456"}},
   298  	)
   299  	iets.NoError(err)
   300  	iets.Equal(exp.NewColumnListExpression("address", "home_phone", "name", "primary_phone"), ie.Cols())
   301  	iets.Equal([][]interface{}{
   302  		{"111 Test Addr", "123123", "Test1", "456456"},
   303  		{"211 Test Addr", "123123", "Test2", "456456"},
   304  		{"311 Test Addr", "123123", "Test3", "456456"},
   305  		{"411 Test Addr", "123123", "Test4", "456456"},
   306  	}, ie.Vals())
   307  	iets.False(ie.IsEmpty())
   308  	iets.False(ie.IsInsertFrom())
   309  }
   310  
   311  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withNilEmbeddedStructPointers() {
   312  	type Phone struct {
   313  		Primary string `db:"primary_phone"`
   314  		Home    string `db:"home_phone"`
   315  	}
   316  	type item struct {
   317  		*Phone
   318  		Address string `db:"address"`
   319  		Name    string `db:"name"`
   320  	}
   321  	ie, err := exp.NewInsertExpression(
   322  		item{Address: "111 Test Addr", Name: "Test1"},
   323  		item{Address: "211 Test Addr", Name: "Test2"},
   324  		item{Address: "311 Test Addr", Name: "Test3"},
   325  		item{Address: "411 Test Addr", Name: "Test4"},
   326  	)
   327  	iets.NoError(err)
   328  	iets.Equal(exp.NewColumnListExpression("address", "name"), ie.Cols())
   329  	iets.Equal([][]interface{}{
   330  		{"111 Test Addr", "Test1"},
   331  		{"211 Test Addr", "Test2"},
   332  		{"311 Test Addr", "Test3"},
   333  		{"411 Test Addr", "Test4"},
   334  	}, ie.Vals())
   335  	iets.False(ie.IsEmpty())
   336  	iets.False(ie.IsInsertFrom())
   337  }
   338  
   339  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withDifferentStructTypes() {
   340  	type Phone struct {
   341  		Primary string `db:"primary_phone"`
   342  		Home    string `db:"home_phone"`
   343  	}
   344  	type item struct {
   345  		*Phone
   346  		Address string `db:"address"`
   347  		Name    string `db:"name"`
   348  	}
   349  	_, err := exp.NewInsertExpression(
   350  		item{Address: "111 Test Addr", Name: "Test1"},
   351  		Phone{Home: "123123", Primary: "456456"},
   352  		item{Address: "311 Test Addr", Name: "Test3"},
   353  		Phone{Home: "123123", Primary: "456456"},
   354  	)
   355  	iets.EqualError(err, "goqu: rows must be all the same type expected exp_test.item got exp_test.Phone")
   356  }
   357  
   358  func (iets *insertExpressionTestSuite) TestNewInsertExpression_withDifferentColumnLengths() {
   359  	type Phone struct {
   360  		Primary string `db:"primary_phone"`
   361  		Home    string `db:"home_phone"`
   362  	}
   363  	type Phone2 struct {
   364  		Primary string `db:"primary_phone2"`
   365  		Home    string `db:"home_phone2"`
   366  	}
   367  	type item struct {
   368  		*Phone
   369  		*Phone2
   370  		Address string `db:"address"`
   371  		Name    string `db:"name"`
   372  	}
   373  	_, err := exp.NewInsertExpression(
   374  		item{Address: "111 Test Addr", Name: "Test1", Phone2: &Phone2{Home: "123123", Primary: "456456"}},
   375  		item{Address: "311 Test Addr", Name: "Test3", Phone: &Phone{Home: "123123", Primary: "456456"}},
   376  	)
   377  	iets.EqualError(err, `goqu: rows with different keys expected `+
   378  		`["address","home_phone2","name","primary_phone2"] got ["address","home_phone","name","primary_phone"]`)
   379  }
   380  
   381  func TestInsertExpressionSuite(t *testing.T) {
   382  	suite.Run(t, new(insertExpressionTestSuite))
   383  }
   384  

View as plain text