helpers_test.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 TestSplitTag(t *testing.T) {
  10. var cases = []struct {
  11. tag string
  12. tags []string
  13. }{
  14. {"not null default '2000-01-01 00:00:00' TIMESTAMP", []string{"not", "null", "default", "'2000-01-01 00:00:00'", "TIMESTAMP"}},
  15. {"TEXT", []string{"TEXT"}},
  16. {"default('2000-01-01 00:00:00')", []string{"default('2000-01-01 00:00:00')"}},
  17. {"json binary", []string{"json", "binary"}},
  18. }
  19. for _, kase := range cases {
  20. tags := splitTag(kase.tag)
  21. if !sliceEq(tags, kase.tags) {
  22. t.Fatalf("[%d]%v is not equal [%d]%v", len(tags), tags, len(kase.tags), kase.tags)
  23. }
  24. }
  25. }
  26. func TestEraseAny(t *testing.T) {
  27. raw := "SELECT * FROM `table`.[table_name]"
  28. assert.EqualValues(t, raw, eraseAny(raw))
  29. assert.EqualValues(t, "SELECT * FROM table.[table_name]", eraseAny(raw, "`"))
  30. assert.EqualValues(t, "SELECT * FROM table.table_name", eraseAny(raw, "`", "[", "]"))
  31. }
  32. func TestQuoteColumns(t *testing.T) {
  33. cols := []string{"f1", "f2", "f3"}
  34. quoteFunc := func(value string) string {
  35. return "[" + value + "]"
  36. }
  37. assert.EqualValues(t, "[f1], [f2], [f3]", quoteColumns(cols, quoteFunc, ","))
  38. }