table_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2019 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 core
  5. import (
  6. "strings"
  7. "testing"
  8. )
  9. var testsGetColumn = []struct {
  10. name string
  11. idx int
  12. }{
  13. {"Id", 0},
  14. {"Deleted", 0},
  15. {"Caption", 0},
  16. {"Code_1", 0},
  17. {"Code_2", 0},
  18. {"Code_3", 0},
  19. {"Parent_Id", 0},
  20. {"Latitude", 0},
  21. {"Longitude", 0},
  22. }
  23. var table *Table
  24. func init() {
  25. table = NewEmptyTable()
  26. var name string
  27. for i := 0; i < len(testsGetColumn); i++ {
  28. // as in Table.AddColumn func
  29. name = strings.ToLower(testsGetColumn[i].name)
  30. table.columnsMap[name] = append(table.columnsMap[name], &Column{})
  31. }
  32. }
  33. func TestGetColumn(t *testing.T) {
  34. for _, test := range testsGetColumn {
  35. if table.GetColumn(test.name) == nil {
  36. t.Error("Column not found!")
  37. }
  38. }
  39. }
  40. func TestGetColumnIdx(t *testing.T) {
  41. for _, test := range testsGetColumn {
  42. if table.GetColumnIdx(test.name, test.idx) == nil {
  43. t.Errorf("Column %s with idx %d not found!", test.name, test.idx)
  44. }
  45. }
  46. }
  47. func BenchmarkGetColumnWithToLower(b *testing.B) {
  48. for i := 0; i < b.N; i++ {
  49. for _, test := range testsGetColumn {
  50. if _, ok := table.columnsMap[strings.ToLower(test.name)]; !ok {
  51. b.Errorf("Column not found:%s", test.name)
  52. }
  53. }
  54. }
  55. }
  56. func BenchmarkGetColumnIdxWithToLower(b *testing.B) {
  57. for i := 0; i < b.N; i++ {
  58. for _, test := range testsGetColumn {
  59. if c, ok := table.columnsMap[strings.ToLower(test.name)]; ok {
  60. if test.idx < len(c) {
  61. continue
  62. } else {
  63. b.Errorf("Bad idx in: %s, %d", test.name, test.idx)
  64. }
  65. } else {
  66. b.Errorf("Column not found: %s, %d", test.name, test.idx)
  67. }
  68. }
  69. }
  70. }
  71. func BenchmarkGetColumn(b *testing.B) {
  72. for i := 0; i < b.N; i++ {
  73. for _, test := range testsGetColumn {
  74. if table.GetColumn(test.name) == nil {
  75. b.Errorf("Column not found:%s", test.name)
  76. }
  77. }
  78. }
  79. }
  80. func BenchmarkGetColumnIdx(b *testing.B) {
  81. for i := 0; i < b.N; i++ {
  82. for _, test := range testsGetColumn {
  83. if table.GetColumnIdx(test.name, test.idx) == nil {
  84. b.Errorf("Column not found:%s, %d", test.name, test.idx)
  85. }
  86. }
  87. }
  88. }