THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.3KB

  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. "xorm.io/builder"
  9. "xorm.io/core"
  10. )
  11. func TestSetExpr(t *testing.T) {
  12. assert.NoError(t, prepareEngine())
  13. type UserExprIssue struct {
  14. Id int64
  15. Title string
  16. }
  17. assert.NoError(t, testEngine.Sync2(new(UserExprIssue)))
  18. var issue = UserExprIssue{
  19. Title: "my issue",
  20. }
  21. cnt, err := testEngine.Insert(&issue)
  22. assert.NoError(t, err)
  23. assert.EqualValues(t, 1, cnt)
  24. assert.EqualValues(t, 1, issue.Id)
  25. type UserExpr struct {
  26. Id int64
  27. IssueId int64 `xorm:"index"`
  28. Show bool
  29. }
  30. assert.NoError(t, testEngine.Sync2(new(UserExpr)))
  31. cnt, err = testEngine.Insert(&UserExpr{
  32. Show: true,
  33. })
  34. assert.NoError(t, err)
  35. assert.EqualValues(t, 1, cnt)
  36. var not = "NOT"
  37. if testEngine.Dialect().DBType() == core.MSSQL {
  38. not = "~"
  39. }
  40. cnt, err = testEngine.SetExpr("show", not+" `show`").ID(1).Update(new(UserExpr))
  41. assert.NoError(t, err)
  42. assert.EqualValues(t, 1, cnt)
  43. tableName := testEngine.TableName(new(UserExprIssue), true)
  44. cnt, err = testEngine.SetExpr("issue_id",
  45. builder.Select("id").
  46. From(tableName).
  47. Where(builder.Eq{"id": issue.Id})).
  48. ID(1).
  49. Update(new(UserExpr))
  50. assert.NoError(t, err)
  51. assert.EqualValues(t, 1, cnt)
  52. }
  53. func TestCols(t *testing.T) {
  54. assert.NoError(t, prepareEngine())
  55. type ColsTable struct {
  56. Id int64
  57. Col1 string
  58. Col2 string
  59. }
  60. assertSync(t, new(ColsTable))
  61. _, err := testEngine.Insert(&ColsTable{
  62. Col1: "1",
  63. Col2: "2",
  64. })
  65. assert.NoError(t, err)
  66. sess := testEngine.ID(1)
  67. _, err = sess.Cols("col1").Cols("col2").Update(&ColsTable{
  68. Col1: "",
  69. Col2: "",
  70. })
  71. assert.NoError(t, err)
  72. var tb ColsTable
  73. has, err := testEngine.ID(1).Get(&tb)
  74. assert.NoError(t, err)
  75. assert.True(t, has)
  76. assert.EqualValues(t, "", tb.Col1)
  77. assert.EqualValues(t, "", tb.Col2)
  78. }
  79. func TestMustCol(t *testing.T) {
  80. assert.NoError(t, prepareEngine())
  81. type CustomerUpdate struct {
  82. Id int64 `form:"id" json:"id"`
  83. Username string `form:"username" json:"username" binding:"required"`
  84. Email string `form:"email" json:"email"`
  85. Sex int `form:"sex" json:"sex"`
  86. Name string `form:"name" json:"name" binding:"required"`
  87. Telephone string `form:"telephone" json:"telephone"`
  88. Type int `form:"type" json:"type" binding:"required"`
  89. ParentId int64 `form:"parent_id" json:"parent_id" xorm:"int null"`
  90. Remark string `form:"remark" json:"remark"`
  91. Status int `form:"status" json:"status" binding:"required"`
  92. Age int `form:"age" json:"age"`
  93. CreatedAt int64 `xorm:"created" form:"created_at" json:"created_at"`
  94. UpdatedAt int64 `xorm:"updated" form:"updated_at" json:"updated_at"`
  95. BirthDate int64 `form:"birth_date" json:"birth_date"`
  96. Password string `xorm:"varchar(200)" form:"password" json:"password"`
  97. }
  98. assertSync(t, new(CustomerUpdate))
  99. var customer = CustomerUpdate{
  100. ParentId: 1,
  101. }
  102. cnt, err := testEngine.Insert(&customer)
  103. assert.NoError(t, err)
  104. assert.EqualValues(t, 1, cnt)
  105. type CustomerOnlyId struct {
  106. Id int64
  107. }
  108. customer.ParentId = 0
  109. affected, err := testEngine.MustCols("parent_id").Update(&customer, &CustomerOnlyId{Id: customer.Id})
  110. assert.NoError(t, err)
  111. assert.EqualValues(t, 1, affected)
  112. }