statement_test.go 3.9 KB

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