session_insert_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. "time"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestInsertOne(t *testing.T) {
  11. assert.NoError(t, prepareEngine())
  12. type Test struct {
  13. Id int64 `xorm:"autoincr pk"`
  14. Msg string `xorm:"varchar(255)"`
  15. Created time.Time `xorm:"created"`
  16. }
  17. assert.NoError(t, testEngine.Sync2(new(Test)))
  18. data := Test{Msg: "hi"}
  19. _, err := testEngine.InsertOne(data)
  20. assert.NoError(t, err)
  21. }
  22. func TestInsertOne2(t *testing.T) {
  23. assert.NoError(t, prepareEngine())
  24. type Test2 struct {
  25. Id int64 `xorm:"autoincr pk"`
  26. Msg string `xorm:"varchar(255)"`
  27. Created time.Time `xorm:"datetime created"`
  28. }
  29. assert.NoError(t, testEngine.Sync2(new(Test2)))
  30. data := Test2{Msg: "hi"}
  31. _, err := testEngine.InsertOne(data)
  32. assert.NoError(t, err)
  33. }
  34. func TestInsertOneIfPkIsPoint(t *testing.T) {
  35. assert.NoError(t, prepareEngine())
  36. type TestPoint struct {
  37. Id *int64 `xorm:"autoincr pk notnull 'id'"`
  38. Msg *string `xorm:"varchar(255)"`
  39. Created *time.Time `xorm:"created"`
  40. }
  41. assert.NoError(t, testEngine.Sync2(new(TestPoint)))
  42. msg := "hi"
  43. data := TestPoint{Msg: &msg}
  44. _, err := testEngine.InsertOne(&data)
  45. assert.NoError(t, err)
  46. }
  47. func TestInsertOneIfPkIsPointRename(t *testing.T) {
  48. assert.NoError(t, prepareEngine())
  49. type ID *int64
  50. type TestPoint struct {
  51. Id ID `xorm:"autoincr pk notnull 'id'"`
  52. Msg *string `xorm:"varchar(255)"`
  53. Created *time.Time `xorm:"created"`
  54. }
  55. assert.NoError(t, testEngine.Sync2(new(TestPoint)))
  56. msg := "hi"
  57. data := TestPoint{Msg: &msg}
  58. _, err := testEngine.InsertOne(&data)
  59. assert.NoError(t, err)
  60. }