statement_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/xormplus/core"
  10. )
  11. var colStrTests = []struct {
  12. omitColumn string
  13. onlyToDBColumnNdx int
  14. expected string
  15. }{
  16. {"", -1, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`, `Longitude`"},
  17. {"Code2", -1, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code3`, `ParentID`, `Latitude`, `Longitude`"},
  18. {"", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`, `Longitude`"},
  19. {"Code3", 1, "`ID`, `Caption`, `Code1`, `Code2`, `ParentID`, `Latitude`, `Longitude`"},
  20. {"Longitude", 1, "`ID`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"},
  21. {"", 8, "`ID`, `IsDeleted`, `Caption`, `Code1`, `Code2`, `Code3`, `ParentID`, `Latitude`"},
  22. }
  23. func TestColumnsStringGeneration(t *testing.T) {
  24. if *db == "postgres" {
  25. return
  26. }
  27. var statement *Statement
  28. for ndx, testCase := range colStrTests {
  29. statement = createTestStatement()
  30. if testCase.omitColumn != "" {
  31. statement.Omit(testCase.omitColumn)
  32. }
  33. columns := statement.RefTable.Columns()
  34. if testCase.onlyToDBColumnNdx >= 0 {
  35. columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB
  36. }
  37. actual := statement.genColumnStr()
  38. if actual != testCase.expected {
  39. t.Errorf("[test #%d] Unexpected columns string:\nwant:\t%s\nhave:\t%s", ndx, testCase.expected, actual)
  40. }
  41. if testCase.onlyToDBColumnNdx >= 0 {
  42. columns[testCase.onlyToDBColumnNdx].MapType = core.TWOSIDES
  43. }
  44. }
  45. }
  46. func BenchmarkColumnsStringGeneration(b *testing.B) {
  47. b.StopTimer()
  48. statement := createTestStatement()
  49. testCase := colStrTests[0]
  50. if testCase.omitColumn != "" {
  51. statement.Omit(testCase.omitColumn) // !nemec784! Column must be skipped
  52. }
  53. if testCase.onlyToDBColumnNdx >= 0 {
  54. columns := statement.RefTable.Columns()
  55. columns[testCase.onlyToDBColumnNdx].MapType = core.ONLYTODB // !nemec784! Column must be skipped
  56. }
  57. b.StartTimer()
  58. for i := 0; i < b.N; i++ {
  59. actual := statement.genColumnStr()
  60. if actual != testCase.expected {
  61. b.Errorf("Unexpected columns string:\nwant:\t%s\nhave:\t%s", testCase.expected, actual)
  62. }
  63. }
  64. }
  65. func BenchmarkGetFlagForColumnWithICKey_ContainsKey(b *testing.B) {
  66. b.StopTimer()
  67. mapCols := make(map[string]bool)
  68. cols := []*core.Column{
  69. {Name: `ID`},
  70. {Name: `IsDeleted`},
  71. {Name: `Caption`},
  72. {Name: `Code1`},
  73. {Name: `Code2`},
  74. {Name: `Code3`},
  75. {Name: `ParentID`},
  76. {Name: `Latitude`},
  77. {Name: `Longitude`},
  78. }
  79. for _, col := range cols {
  80. mapCols[strings.ToLower(col.Name)] = true
  81. }
  82. b.StartTimer()
  83. for i := 0; i < b.N; i++ {
  84. for _, col := range cols {
  85. if _, ok := getFlagForColumn(mapCols, col); !ok {
  86. b.Fatal("Unexpected result")
  87. }
  88. }
  89. }
  90. }
  91. func BenchmarkGetFlagForColumnWithICKey_EmptyMap(b *testing.B) {
  92. b.StopTimer()
  93. mapCols := make(map[string]bool)
  94. cols := []*core.Column{
  95. {Name: `ID`},
  96. {Name: `IsDeleted`},
  97. {Name: `Caption`},
  98. {Name: `Code1`},
  99. {Name: `Code2`},
  100. {Name: `Code3`},
  101. {Name: `ParentID`},
  102. {Name: `Latitude`},
  103. {Name: `Longitude`},
  104. }
  105. b.StartTimer()
  106. for i := 0; i < b.N; i++ {
  107. for _, col := range cols {
  108. if _, ok := getFlagForColumn(mapCols, col); ok {
  109. b.Fatal("Unexpected result")
  110. }
  111. }
  112. }
  113. }
  114. type TestType struct {
  115. ID int64 `xorm:"ID PK"`
  116. IsDeleted bool `xorm:"IsDeleted"`
  117. Caption string `xorm:"Caption"`
  118. Code1 string `xorm:"Code1"`
  119. Code2 string `xorm:"Code2"`
  120. Code3 string `xorm:"Code3"`
  121. ParentID int64 `xorm:"ParentID"`
  122. Latitude float64 `xorm:"Latitude"`
  123. Longitude float64 `xorm:"Longitude"`
  124. }
  125. func (TestType) TableName() string {
  126. return "TestTable"
  127. }
  128. func createTestStatement() *Statement {
  129. statement := &Statement{}
  130. statement.Init()
  131. statement.Engine = testEngine
  132. statement.setRefValue(reflect.ValueOf(TestType{}))
  133. return statement
  134. }