session_exist_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. "github.com/stretchr/testify/assert"
  8. )
  9. func TestExistStruct(t *testing.T) {
  10. assert.NoError(t, prepareEngine())
  11. type RecordExist struct {
  12. Id int64
  13. Name string
  14. }
  15. assertSync(t, new(RecordExist))
  16. has, err := testEngine.Exist(new(RecordExist))
  17. assert.NoError(t, err)
  18. assert.False(t, has)
  19. cnt, err := testEngine.Insert(&RecordExist{
  20. Name: "test1",
  21. })
  22. assert.NoError(t, err)
  23. assert.EqualValues(t, 1, cnt)
  24. has, err = testEngine.Exist(new(RecordExist))
  25. assert.NoError(t, err)
  26. assert.True(t, has)
  27. has, err = testEngine.Exist(&RecordExist{
  28. Name: "test1",
  29. })
  30. assert.NoError(t, err)
  31. assert.True(t, has)
  32. has, err = testEngine.Exist(&RecordExist{
  33. Name: "test2",
  34. })
  35. assert.NoError(t, err)
  36. assert.False(t, has)
  37. has, err = testEngine.Where("name = ?", "test1").Exist(&RecordExist{})
  38. assert.NoError(t, err)
  39. assert.True(t, has)
  40. has, err = testEngine.Where("name = ?", "test2").Exist(&RecordExist{})
  41. assert.NoError(t, err)
  42. assert.False(t, has)
  43. has, err = testEngine.SQL("select * from "+testEngine.TableName("record_exist", true)+" where name = ?", "test1").Exist()
  44. assert.NoError(t, err)
  45. assert.True(t, has)
  46. has, err = testEngine.SQL("select * from "+testEngine.TableName("record_exist", true)+" where name = ?", "test2").Exist()
  47. assert.NoError(t, err)
  48. assert.False(t, has)
  49. has, err = testEngine.Table("record_exist").Exist()
  50. assert.NoError(t, err)
  51. assert.True(t, has)
  52. has, err = testEngine.Table("record_exist").Where("name = ?", "test1").Exist()
  53. assert.NoError(t, err)
  54. assert.True(t, has)
  55. has, err = testEngine.Table("record_exist").Where("name = ?", "test2").Exist()
  56. assert.NoError(t, err)
  57. assert.False(t, has)
  58. }