session_cols_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2017 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/xormplus/core"
  9. )
  10. func TestSetExpr(t *testing.T) {
  11. assert.NoError(t, prepareEngine())
  12. type UserExpr struct {
  13. Id int64
  14. Show bool
  15. }
  16. assert.NoError(t, testEngine.Sync2(new(UserExpr)))
  17. cnt, err := testEngine.Insert(&UserExpr{
  18. Show: true,
  19. })
  20. assert.NoError(t, err)
  21. assert.EqualValues(t, 1, cnt)
  22. var not = "NOT"
  23. if testEngine.Dialect().DBType() == core.MSSQL {
  24. not = "~"
  25. }
  26. cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))
  27. assert.NoError(t, err)
  28. assert.EqualValues(t, 1, cnt)
  29. }
  30. func TestCols(t *testing.T) {
  31. assert.NoError(t, prepareEngine())
  32. type ColsTable struct {
  33. Id int64
  34. Col1 string
  35. Col2 string
  36. }
  37. assertSync(t, new(ColsTable))
  38. _, err := testEngine.Insert(&ColsTable{
  39. Col1: "1",
  40. Col2: "2",
  41. })
  42. assert.NoError(t, err)
  43. sess := testEngine.ID(1)
  44. _, err = sess.Cols("col1").Cols("col2").Update(&ColsTable{
  45. Col1: "",
  46. Col2: "",
  47. })
  48. assert.NoError(t, err)
  49. var tb ColsTable
  50. has, err := testEngine.ID(1).Get(&tb)
  51. assert.NoError(t, err)
  52. assert.True(t, has)
  53. assert.EqualValues(t, "", tb.Col1)
  54. assert.EqualValues(t, "", tb.Col2)
  55. }
  56. func TestMustCol(t *testing.T) {
  57. assert.NoError(t, prepareEngine())
  58. type CustomerUpdate struct {
  59. Id int64 `form:"id" json:"id"`
  60. Username string `form:"username" json:"username" binding:"required"`
  61. Email string `form:"email" json:"email"`
  62. Sex int `form:"sex" json:"sex"`
  63. Name string `form:"name" json:"name" binding:"required"`
  64. Telephone string `form:"telephone" json:"telephone"`
  65. Type int `form:"type" json:"type" binding:"required"`
  66. ParentId int64 `form:"parent_id" json:"parent_id" xorm:"int null"`
  67. Remark string `form:"remark" json:"remark"`
  68. Status int `form:"status" json:"status" binding:"required"`
  69. Age int `form:"age" json:"age"`
  70. CreatedAt int64 `xorm:"created" form:"created_at" json:"created_at"`
  71. UpdatedAt int64 `xorm:"updated" form:"updated_at" json:"updated_at"`
  72. BirthDate int64 `form:"birth_date" json:"birth_date"`
  73. Password string `xorm:"varchar(200)" form:"password" json:"password"`
  74. }
  75. assertSync(t, new(CustomerUpdate))
  76. var customer = CustomerUpdate{
  77. ParentId: 1,
  78. }
  79. cnt, err := testEngine.Insert(&customer)
  80. assert.NoError(t, err)
  81. assert.EqualValues(t, 1, cnt)
  82. type CustomerOnlyId struct {
  83. Id int64
  84. }
  85. customer.ParentId = 0
  86. affected, err := testEngine.MustCols("parent_id").Update(&customer, &CustomerOnlyId{Id: customer.Id})
  87. assert.NoError(t, err)
  88. assert.EqualValues(t, 1, affected)
  89. }