postgres_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package postgres
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/go-xorm/xorm"
  6. "github.com/grafana/grafana/pkg/components/simplejson"
  7. "github.com/grafana/grafana/pkg/log"
  8. "github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
  9. "github.com/grafana/grafana/pkg/tsdb"
  10. _ "github.com/lib/pq"
  11. . "github.com/smartystreets/goconvey/convey"
  12. )
  13. // To run this test, remove the Skip from SkipConvey
  14. // and set up a PostgreSQL db named grafanatest and a user/password grafanatest/grafanatest
  15. func TestPostgres(t *testing.T) {
  16. SkipConvey("PostgreSQL", t, func() {
  17. x := InitPostgresTestDB(t)
  18. endpoint := &PostgresQueryEndpoint{
  19. sqlEngine: &tsdb.DefaultSqlEngine{
  20. MacroEngine: NewPostgresMacroEngine(),
  21. XormEngine: x,
  22. },
  23. log: log.New("tsdb.postgres"),
  24. }
  25. sess := x.NewSession()
  26. defer sess.Close()
  27. sql := `
  28. CREATE TABLE postgres_types(
  29. c00_smallint smallint,
  30. c01_integer integer,
  31. c02_bigint bigint,
  32. c03_real real,
  33. c04_double double precision,
  34. c05_decimal decimal(10,2),
  35. c06_numeric numeric(10,2),
  36. c07_char char(10),
  37. c08_varchar varchar(10),
  38. c09_text text,
  39. c10_timestamp timestamp without time zone,
  40. c11_timestamptz timestamp with time zone,
  41. c12_date date,
  42. c13_time time without time zone,
  43. c14_timetz time with time zone,
  44. c15_interval interval
  45. );
  46. `
  47. _, err := sess.Exec(sql)
  48. So(err, ShouldBeNil)
  49. sql = `
  50. INSERT INTO postgres_types VALUES(
  51. 1,2,3,
  52. 4.5,6.7,1.1,1.2,
  53. 'char10','varchar10','text',
  54. now(),now(),now(),now(),now(),'15m'::interval
  55. );
  56. `
  57. _, err = sess.Exec(sql)
  58. So(err, ShouldBeNil)
  59. Convey("Query with Table format should map PostgreSQL column types to Go types", func() {
  60. query := &tsdb.TsdbQuery{
  61. Queries: []*tsdb.Query{
  62. {
  63. Model: simplejson.NewFromAny(map[string]interface{}{
  64. "rawSql": "SELECT * FROM postgres_types",
  65. "format": "table",
  66. }),
  67. RefId: "A",
  68. },
  69. },
  70. }
  71. resp, err := endpoint.Query(nil, nil, query)
  72. queryResult := resp.Results["A"]
  73. So(err, ShouldBeNil)
  74. column := queryResult.Tables[0].Rows[0]
  75. So(column[0].(int64), ShouldEqual, 1)
  76. So(column[1].(int64), ShouldEqual, 2)
  77. So(column[2].(int64), ShouldEqual, 3)
  78. So(column[3].(float64), ShouldEqual, 4.5)
  79. So(column[4].(float64), ShouldEqual, 6.7)
  80. // libpq doesnt properly convert decimal, numeric and char to go types but returns []uint8 instead
  81. // So(column[5].(float64), ShouldEqual, 1.1)
  82. // So(column[6].(float64), ShouldEqual, 1.2)
  83. // So(column[7].(string), ShouldEqual, "char")
  84. So(column[8].(string), ShouldEqual, "varchar10")
  85. So(column[9].(string), ShouldEqual, "text")
  86. So(column[10].(time.Time), ShouldHaveSameTypeAs, time.Now())
  87. So(column[11].(time.Time), ShouldHaveSameTypeAs, time.Now())
  88. So(column[12].(time.Time), ShouldHaveSameTypeAs, time.Now())
  89. So(column[13].(time.Time), ShouldHaveSameTypeAs, time.Now())
  90. So(column[14].(time.Time), ShouldHaveSameTypeAs, time.Now())
  91. // libpq doesnt properly convert interval to go types but returns []uint8 instead
  92. // So(column[15].(time.Time), ShouldHaveSameTypeAs, time.Now())
  93. })
  94. })
  95. }
  96. func InitPostgresTestDB(t *testing.T) *xorm.Engine {
  97. x, err := xorm.NewEngine(sqlutil.TestDB_Postgres.DriverName, sqlutil.TestDB_Postgres.ConnStr)
  98. // x.ShowSQL()
  99. if err != nil {
  100. t.Fatalf("Failed to init postgres db %v", err)
  101. }
  102. sqlutil.CleanDB(x)
  103. return x
  104. }