mssql_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package mssql
  2. import (
  3. "testing"
  4. "time"
  5. "strings"
  6. _ "github.com/denisenkom/go-mssqldb"
  7. "github.com/go-xorm/xorm"
  8. "github.com/grafana/grafana/pkg/components/simplejson"
  9. "github.com/grafana/grafana/pkg/log"
  10. "github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
  11. "github.com/grafana/grafana/pkg/tsdb"
  12. . "github.com/smartystreets/goconvey/convey"
  13. )
  14. // To run this test, remove the Skip from SkipConvey
  15. // and set up a MSSQL db named grafana_tests and a user/password grafana/password
  16. // and set the variable below to the IP address of the database
  17. var serverIP string = "10.20.30.40"
  18. func TestMSSQL(t *testing.T) {
  19. //SkipConvey("MSSQL", t, func() {
  20. SkipConvey("MSSQL", t, func() {
  21. x := InitMSSQLTestDB(t)
  22. endpoint := &MssqlQueryEndpoint{
  23. sqlEngine: &tsdb.DefaultSqlEngine{
  24. MacroEngine: NewMssqlMacroEngine(),
  25. XormEngine: x,
  26. },
  27. log: log.New("tsdb.mssql"),
  28. }
  29. sess := x.NewSession()
  30. defer sess.Close()
  31. sql := "IF OBJECT_ID('dbo.[mssql_types]', 'U') IS NOT NULL"
  32. sql += " DROP TABLE dbo.[mssql_types];"
  33. sql += "CREATE TABLE [mssql_types] ( "
  34. sql += "abit bit, "
  35. sql += "atinyint tinyint, "
  36. sql += "asmallint smallint, "
  37. sql += "aint int, "
  38. sql += "abigint bigint, "
  39. sql += "avarchar varchar(3), "
  40. sql += "achar char(3), "
  41. sql += "anewvarchar varchar(14), "
  42. sql += "anewchar char(14), "
  43. sql += "areal real, "
  44. sql += "anewdecimal decimal(10,2), "
  45. sql += "afloat float, "
  46. sql += "adatetime datetime, "
  47. sql += "adate date, "
  48. sql += "atime time) "
  49. _, err := sess.Exec(sql)
  50. So(err, ShouldBeNil)
  51. sql = "INSERT INTO [mssql_types] "
  52. sql += "(abit, atinyint, asmallint, aint, abigint, "
  53. sql += "avarchar, achar, anewvarchar, anewchar, "
  54. sql += "areal, anewdecimal, afloat, "
  55. sql += "adatetime, adate, atime ) "
  56. sql += "VALUES(1, 5, 20020, 980300, 1420070400, "
  57. sql += "'abc', 'def', 'hi varchar', 'I am only char', "
  58. sql += "1.11, 2.22, 3.33, "
  59. sql += "GETUTCDATE(), CAST(GETUTCDATE() AS DATE), CAST(GETUTCDATE() AS TIME) );"
  60. _, err = sess.Exec(sql)
  61. So(err, ShouldBeNil)
  62. Convey("Query with Table format should map MSSQL column types to Go types", func() {
  63. query := &tsdb.TsdbQuery{
  64. Queries: []*tsdb.Query{
  65. {
  66. Model: simplejson.NewFromAny(map[string]interface{}{
  67. "rawSql": "SELECT * FROM mssql_types",
  68. "format": "table",
  69. }),
  70. RefId: "A",
  71. },
  72. },
  73. }
  74. resp, err := endpoint.Query(nil, nil, query)
  75. queryResult := resp.Results["A"]
  76. So(err, ShouldBeNil)
  77. column := queryResult.Tables[0].Rows[0]
  78. So(column[0].(bool), ShouldEqual, true)
  79. So(column[1].(int64), ShouldEqual, 5)
  80. So(column[2].(int64), ShouldEqual, 20020)
  81. So(column[3].(int64), ShouldEqual, 980300)
  82. So(column[4].(int64), ShouldEqual, 1420070400)
  83. So(column[5].(string), ShouldEqual, "abc")
  84. So(column[6].(string), ShouldEqual, "def")
  85. So(column[7].(string), ShouldEqual, "hi varchar")
  86. So(column[8].(string), ShouldEqual, "I am only char")
  87. So(column[9].(float64), ShouldEqual, 1.1100000143051147) // MSSQL dose not have precision for "real" datatype
  88. // fiix me: MSSQL driver puts the decimal inside an array of chars. and the test fails despite the values are correct.
  89. //So(column[10].([]uint8), ShouldEqual, []uint8{'2', '.', '2', '2'})
  90. So(column[11].(float64), ShouldEqual, 3.33)
  91. So(column[12].(time.Time), ShouldHappenWithin, time.Duration(15*time.Second), time.Now().UTC() )
  92. So(column[13].(time.Time), ShouldHappenWithin, time.Duration(15*time.Second), time.Now().UTC().Truncate(24*time.Hour) )
  93. So(column[14].(time.Time), ShouldHappenWithin, time.Duration(15*time.Second), time.Date( 1, time.January, 1, time.Now().UTC().Hour(), time.Now().UTC().Minute(), time.Now().UTC().Second(), 0, time.UTC) )
  94. })
  95. })
  96. }
  97. func InitMSSQLTestDB(t *testing.T) *xorm.Engine {
  98. x, err := xorm.NewEngine(sqlutil.TestDB_Mssql.DriverName, strings.Replace(sqlutil.TestDB_Mssql.ConnStr, "localhost", serverIP, 1) )
  99. // x.ShowSQL()
  100. if err != nil {
  101. t.Fatalf("Failed to init mssql db %v", err)
  102. }
  103. sqlutil.CleanDB(x)
  104. return x
  105. }