sqlServerDialect_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2022 EMQ Technologies Co., Ltd.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package sqlgen
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestQueryGenerator_SqlQueryStatement(t *testing.T) {
  20. type fields struct {
  21. indexSlice []interface{}
  22. InternalSqlQueryCfg *InternalSqlQueryCfg
  23. }
  24. tests := []struct {
  25. name string
  26. fields fields
  27. want string
  28. wantErr bool
  29. }{
  30. {
  31. name: "int index",
  32. fields: fields{
  33. indexSlice: nil,
  34. InternalSqlQueryCfg: &InternalSqlQueryCfg{
  35. Table: "table",
  36. Limit: 2,
  37. IndexField: "responseTime",
  38. IndexValue: 10,
  39. IndexFieldType: "",
  40. DateTimeFormat: "",
  41. },
  42. },
  43. want: "select top 2 * from table where responseTime > '10' order by responseTime ASC",
  44. wantErr: false,
  45. },
  46. {
  47. name: "time string index",
  48. fields: fields{
  49. indexSlice: nil,
  50. InternalSqlQueryCfg: &InternalSqlQueryCfg{
  51. Table: "table",
  52. Limit: 2,
  53. IndexField: "responseTime",
  54. IndexValue: "2022-04-13 06:22:32.233",
  55. IndexFieldType: "DATETIME",
  56. DateTimeFormat: "YYYY-MM-dd HH:mm:ssSSS",
  57. },
  58. },
  59. want: "select top 2 * from table where responseTime > '2022-04-13 06:22:32.233' order by responseTime ASC",
  60. wantErr: false,
  61. },
  62. }
  63. for _, tt := range tests {
  64. t.Run(tt.name, func(t *testing.T) {
  65. q := &SqlServerQueryGenerator{
  66. InternalSqlQueryCfg: tt.fields.InternalSqlQueryCfg,
  67. }
  68. got, err := q.SqlQueryStatement()
  69. if (err != nil) != tt.wantErr {
  70. t.Errorf("SqlQueryStatement() error = %v, wantErr %v", err, tt.wantErr)
  71. return
  72. }
  73. if got != tt.want {
  74. t.Errorf("SqlQueryStatement() got = %v, want %v", got, tt.want)
  75. }
  76. })
  77. }
  78. }
  79. func TestInternalQuery(t *testing.T) {
  80. s := NewSqlServerQuery(&InternalSqlQueryCfg{
  81. Table: "table",
  82. Limit: 10,
  83. IndexField: "responseTime",
  84. IndexValue: 10,
  85. })
  86. s.UpdateMaxIndexValue(map[string]interface{}{
  87. "responseTime": 20,
  88. })
  89. s.UpdateMaxIndexValue(map[string]interface{}{
  90. "responseTime": 30,
  91. })
  92. s.UpdateMaxIndexValue(map[string]interface{}{
  93. "responseTime": 40,
  94. })
  95. s.UpdateMaxIndexValue(map[string]interface{}{
  96. "responseTime": 50,
  97. })
  98. nextSqlStr, _ := s.SqlQueryStatement()
  99. want := "select top 10 * from table where responseTime > '50' order by responseTime ASC"
  100. if !reflect.DeepEqual(nextSqlStr, want) {
  101. t.Errorf("SqlQueryStatement() = %v, want %v", nextSqlStr, want)
  102. }
  103. }