mssql: adds test for time should be ms in table mode
This commit is contained in:
+159
-132
@@ -41,40 +41,40 @@ func TestMSSQL(t *testing.T) {
|
|||||||
|
|
||||||
Convey("Given a table with different native data types", func() {
|
Convey("Given a table with different native data types", func() {
|
||||||
sql := `
|
sql := `
|
||||||
IF OBJECT_ID('dbo.[mssql_types]', 'U') IS NOT NULL
|
IF OBJECT_ID('dbo.[mssql_types]', 'U') IS NOT NULL
|
||||||
DROP TABLE dbo.[mssql_types]
|
DROP TABLE dbo.[mssql_types]
|
||||||
|
|
||||||
CREATE TABLE [mssql_types] (
|
CREATE TABLE [mssql_types] (
|
||||||
c_bit bit,
|
c_bit bit,
|
||||||
|
|
||||||
c_tinyint tinyint,
|
c_tinyint tinyint,
|
||||||
c_smallint smallint,
|
c_smallint smallint,
|
||||||
c_int int,
|
c_int int,
|
||||||
c_bigint bigint,
|
c_bigint bigint,
|
||||||
|
|
||||||
c_money money,
|
c_money money,
|
||||||
c_smallmoney smallmoney,
|
c_smallmoney smallmoney,
|
||||||
c_numeric numeric(10,5),
|
c_numeric numeric(10,5),
|
||||||
c_real real,
|
c_real real,
|
||||||
c_decimal decimal(10,2),
|
c_decimal decimal(10,2),
|
||||||
c_float float,
|
c_float float,
|
||||||
|
|
||||||
c_char char(10),
|
c_char char(10),
|
||||||
c_varchar varchar(10),
|
c_varchar varchar(10),
|
||||||
c_text text,
|
c_text text,
|
||||||
|
|
||||||
c_nchar nchar(12),
|
c_nchar nchar(12),
|
||||||
c_nvarchar nvarchar(12),
|
c_nvarchar nvarchar(12),
|
||||||
c_ntext ntext,
|
c_ntext ntext,
|
||||||
|
|
||||||
c_datetime datetime,
|
c_datetime datetime,
|
||||||
c_datetime2 datetime2,
|
c_datetime2 datetime2,
|
||||||
c_smalldatetime smalldatetime,
|
c_smalldatetime smalldatetime,
|
||||||
c_date date,
|
c_date date,
|
||||||
c_time time,
|
c_time time,
|
||||||
c_datetimeoffset datetimeoffset
|
c_datetimeoffset datetimeoffset
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err := sess.Exec(sql)
|
_, err := sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -87,14 +87,14 @@ func TestMSSQL(t *testing.T) {
|
|||||||
d2 := dt2.Format(dt2Format)
|
d2 := dt2.Format(dt2Format)
|
||||||
|
|
||||||
sql = fmt.Sprintf(`
|
sql = fmt.Sprintf(`
|
||||||
INSERT INTO [mssql_types]
|
INSERT INTO [mssql_types]
|
||||||
SELECT
|
SELECT
|
||||||
1, 5, 20020, 980300, 1420070400, '$20000.15', '£2.15', 12345.12,
|
1, 5, 20020, 980300, 1420070400, '$20000.15', '£2.15', 12345.12,
|
||||||
1.11, 2.22, 3.33,
|
1.11, 2.22, 3.33,
|
||||||
'char10', 'varchar10', 'text',
|
'char10', 'varchar10', 'text',
|
||||||
N'☺nchar12☺', N'☺nvarchar12☺', N'☺text☺',
|
N'☺nchar12☺', N'☺nvarchar12☺', N'☺text☺',
|
||||||
CAST('%s' AS DATETIME), CAST('%s' AS DATETIME2), CAST('%s' AS SMALLDATETIME), CAST('%s' AS DATE), CAST('%s' AS TIME), SWITCHOFFSET(CAST('%s' AS DATETIMEOFFSET), '-07:00')
|
CAST('%s' AS DATETIME), CAST('%s' AS DATETIME2), CAST('%s' AS SMALLDATETIME), CAST('%s' AS DATE), CAST('%s' AS TIME), SWITCHOFFSET(CAST('%s' AS DATETIMEOFFSET), '-07:00')
|
||||||
`, d, d2, d, d, d, d2)
|
`, d, d2, d, d, d, d2)
|
||||||
|
|
||||||
_, err = sess.Exec(sql)
|
_, err = sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -151,14 +151,14 @@ func TestMSSQL(t *testing.T) {
|
|||||||
|
|
||||||
Convey("Given a table with metrics that lacks data for some series ", func() {
|
Convey("Given a table with metrics that lacks data for some series ", func() {
|
||||||
sql := `
|
sql := `
|
||||||
IF OBJECT_ID('dbo.[metric]', 'U') IS NOT NULL
|
IF OBJECT_ID('dbo.[metric]', 'U') IS NOT NULL
|
||||||
DROP TABLE dbo.[metric]
|
DROP TABLE dbo.[metric]
|
||||||
|
|
||||||
CREATE TABLE [metric] (
|
CREATE TABLE [metric] (
|
||||||
time datetime,
|
time datetime,
|
||||||
value int
|
value int
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err := sess.Exec(sql)
|
_, err := sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -189,9 +189,9 @@ func TestMSSQL(t *testing.T) {
|
|||||||
dtFormat := "2006-01-02 15:04:05.999999999"
|
dtFormat := "2006-01-02 15:04:05.999999999"
|
||||||
for _, s := range series {
|
for _, s := range series {
|
||||||
sql = fmt.Sprintf(`
|
sql = fmt.Sprintf(`
|
||||||
INSERT INTO metric (time, value)
|
INSERT INTO metric (time, value)
|
||||||
VALUES(CAST('%s' AS DATETIME), %d)
|
VALUES(CAST('%s' AS DATETIME), %d)
|
||||||
`, s.Time.Format(dtFormat), s.Value)
|
`, s.Time.Format(dtFormat), s.Value)
|
||||||
|
|
||||||
_, err = sess.Exec(sql)
|
_, err = sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -306,16 +306,16 @@ func TestMSSQL(t *testing.T) {
|
|||||||
|
|
||||||
Convey("Given a table with metrics having multiple values and measurements", func() {
|
Convey("Given a table with metrics having multiple values and measurements", func() {
|
||||||
sql := `
|
sql := `
|
||||||
IF OBJECT_ID('dbo.[metric_values]', 'U') IS NOT NULL
|
IF OBJECT_ID('dbo.[metric_values]', 'U') IS NOT NULL
|
||||||
DROP TABLE dbo.[metric_values]
|
DROP TABLE dbo.[metric_values]
|
||||||
|
|
||||||
CREATE TABLE [metric_values] (
|
CREATE TABLE [metric_values] (
|
||||||
time datetime,
|
time datetime,
|
||||||
measurement nvarchar(100),
|
measurement nvarchar(100),
|
||||||
valueOne int,
|
valueOne int,
|
||||||
valueTwo int,
|
valueTwo int,
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err := sess.Exec(sql)
|
_, err := sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -351,9 +351,9 @@ func TestMSSQL(t *testing.T) {
|
|||||||
dtFormat := "2006-01-02 15:04:05"
|
dtFormat := "2006-01-02 15:04:05"
|
||||||
for _, s := range series {
|
for _, s := range series {
|
||||||
sql = fmt.Sprintf(`
|
sql = fmt.Sprintf(`
|
||||||
INSERT metric_values (time, measurement, valueOne, valueTwo)
|
INSERT metric_values (time, measurement, valueOne, valueTwo)
|
||||||
VALUES(CAST('%s' AS DATETIME), '%s', %d, %d)
|
VALUES(CAST('%s' AS DATETIME), '%s', %d, %d)
|
||||||
`, s.Time.Format(dtFormat), s.Measurement, s.ValueOne, s.ValueTwo)
|
`, s.Time.Format(dtFormat), s.Measurement, s.ValueOne, s.ValueTwo)
|
||||||
|
|
||||||
_, err = sess.Exec(sql)
|
_, err = sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -407,45 +407,45 @@ func TestMSSQL(t *testing.T) {
|
|||||||
|
|
||||||
Convey("Given a stored procedure that takes @from and @to in epoch time", func() {
|
Convey("Given a stored procedure that takes @from and @to in epoch time", func() {
|
||||||
sql := `
|
sql := `
|
||||||
IF object_id('sp_test_epoch') IS NOT NULL
|
IF object_id('sp_test_epoch') IS NOT NULL
|
||||||
DROP PROCEDURE sp_test_epoch
|
DROP PROCEDURE sp_test_epoch
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err := sess.Exec(sql)
|
_, err := sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
sql = `
|
sql = `
|
||||||
CREATE PROCEDURE sp_test_epoch(
|
CREATE PROCEDURE sp_test_epoch(
|
||||||
@from int,
|
@from int,
|
||||||
@to int
|
@to int
|
||||||
) AS
|
) AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT
|
SELECT
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
|
||||||
measurement + ' - value one' as metric,
|
measurement + ' - value one' as metric,
|
||||||
avg(valueOne) as value
|
avg(valueOne) as value
|
||||||
FROM
|
FROM
|
||||||
metric_values
|
metric_values
|
||||||
WHERE
|
WHERE
|
||||||
time >= DATEADD(s, @from, '1970-01-01') AND time <= DATEADD(s, @to, '1970-01-01')
|
time >= DATEADD(s, @from, '1970-01-01') AND time <= DATEADD(s, @to, '1970-01-01')
|
||||||
GROUP BY
|
GROUP BY
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
|
||||||
measurement
|
measurement
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT
|
SELECT
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int) as time,
|
||||||
measurement + ' - value two' as metric,
|
measurement + ' - value two' as metric,
|
||||||
avg(valueTwo) as value
|
avg(valueTwo) as value
|
||||||
FROM
|
FROM
|
||||||
metric_values
|
metric_values
|
||||||
WHERE
|
WHERE
|
||||||
time >= DATEADD(s, @from, '1970-01-01') AND time <= DATEADD(s, @to, '1970-01-01')
|
time >= DATEADD(s, @from, '1970-01-01') AND time <= DATEADD(s, @to, '1970-01-01')
|
||||||
GROUP BY
|
GROUP BY
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time))/600 as int)*600 as int),
|
||||||
measurement
|
measurement
|
||||||
ORDER BY 1
|
ORDER BY 1
|
||||||
END
|
END
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err = sess.Exec(sql)
|
_, err = sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -456,10 +456,10 @@ func TestMSSQL(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Model: simplejson.NewFromAny(map[string]interface{}{
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"rawSql": `DECLARE
|
"rawSql": `DECLARE
|
||||||
@from int = $__unixEpochFrom(),
|
@from int = $__unixEpochFrom(),
|
||||||
@to int = $__unixEpochTo()
|
@to int = $__unixEpochTo()
|
||||||
|
|
||||||
EXEC dbo.sp_test_epoch @from, @to`,
|
EXEC dbo.sp_test_epoch @from, @to`,
|
||||||
"format": "time_series",
|
"format": "time_series",
|
||||||
}),
|
}),
|
||||||
RefId: "A",
|
RefId: "A",
|
||||||
@@ -486,45 +486,45 @@ func TestMSSQL(t *testing.T) {
|
|||||||
|
|
||||||
Convey("Given a stored procedure that takes @from and @to in datetime", func() {
|
Convey("Given a stored procedure that takes @from and @to in datetime", func() {
|
||||||
sql := `
|
sql := `
|
||||||
IF object_id('sp_test_datetime') IS NOT NULL
|
IF object_id('sp_test_datetime') IS NOT NULL
|
||||||
DROP PROCEDURE sp_test_datetime
|
DROP PROCEDURE sp_test_datetime
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err := sess.Exec(sql)
|
_, err := sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
sql = `
|
sql = `
|
||||||
CREATE PROCEDURE sp_test_datetime(
|
CREATE PROCEDURE sp_test_datetime(
|
||||||
@from datetime,
|
@from datetime,
|
||||||
@to datetime
|
@to datetime
|
||||||
) AS
|
) AS
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT
|
SELECT
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int) as time,
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int) as time,
|
||||||
measurement + ' - value one' as metric,
|
measurement + ' - value one' as metric,
|
||||||
avg(valueOne) as value
|
avg(valueOne) as value
|
||||||
FROM
|
FROM
|
||||||
metric_values
|
metric_values
|
||||||
WHERE
|
WHERE
|
||||||
time >= @from AND time <= @to
|
time >= @from AND time <= @to
|
||||||
GROUP BY
|
GROUP BY
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int),
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int),
|
||||||
measurement
|
measurement
|
||||||
UNION ALL
|
UNION ALL
|
||||||
SELECT
|
SELECT
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int) as time,
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int) as time,
|
||||||
measurement + ' - value two' as metric,
|
measurement + ' - value two' as metric,
|
||||||
avg(valueTwo) as value
|
avg(valueTwo) as value
|
||||||
FROM
|
FROM
|
||||||
metric_values
|
metric_values
|
||||||
WHERE
|
WHERE
|
||||||
time >= @from AND time <= @to
|
time >= @from AND time <= @to
|
||||||
GROUP BY
|
GROUP BY
|
||||||
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int),
|
cast(cast(DATEDIFF(second, {d '1970-01-01'}, time)/600 as int)*600 as int),
|
||||||
measurement
|
measurement
|
||||||
ORDER BY 1
|
ORDER BY 1
|
||||||
END
|
END
|
||||||
`
|
`
|
||||||
|
|
||||||
_, err = sess.Exec(sql)
|
_, err = sess.Exec(sql)
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
@@ -535,10 +535,10 @@ func TestMSSQL(t *testing.T) {
|
|||||||
{
|
{
|
||||||
Model: simplejson.NewFromAny(map[string]interface{}{
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
||||||
"rawSql": `DECLARE
|
"rawSql": `DECLARE
|
||||||
@from int = $__unixEpochFrom(),
|
@from int = $__unixEpochFrom(),
|
||||||
@to int = $__unixEpochTo()
|
@to int = $__unixEpochTo()
|
||||||
|
|
||||||
EXEC dbo.sp_test_epoch @from, @to`,
|
EXEC dbo.sp_test_epoch @from, @to`,
|
||||||
"format": "time_series",
|
"format": "time_series",
|
||||||
}),
|
}),
|
||||||
RefId: "A",
|
RefId: "A",
|
||||||
@@ -654,6 +654,33 @@ func TestMSSQL(t *testing.T) {
|
|||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
So(len(queryResult.Tables[0].Rows), ShouldEqual, 3)
|
So(len(queryResult.Tables[0].Rows), ShouldEqual, 3)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Convey("When doing an annotation query with a time column in datetime format", func() {
|
||||||
|
query := &tsdb.TsdbQuery{
|
||||||
|
Queries: []*tsdb.Query{
|
||||||
|
{
|
||||||
|
Model: simplejson.NewFromAny(map[string]interface{}{
|
||||||
|
"rawSql": "SELECT DATEADD(s, time_sec, {d '1970-01-01'}) AS time, description as [text], tags FROM [event] WHERE $__unixEpochFilter(time_sec) AND tags='ticket' ORDER BY 1 ASC",
|
||||||
|
"format": "table",
|
||||||
|
}),
|
||||||
|
RefId: "Tickets",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
TimeRange: &tsdb.TimeRange{
|
||||||
|
From: fmt.Sprintf("%v", fromStart.Add(-20*time.Minute).Unix()*1000),
|
||||||
|
To: fmt.Sprintf("%v", fromStart.Add(40*time.Minute).Unix()*1000),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := endpoint.Query(nil, nil, query)
|
||||||
|
queryResult := resp.Results["Tickets"]
|
||||||
|
So(err, ShouldBeNil)
|
||||||
|
So(len(queryResult.Tables[0].Rows), ShouldEqual, 3)
|
||||||
|
columns := queryResult.Tables[0].Rows[0]
|
||||||
|
|
||||||
|
//Should be in milliseconds
|
||||||
|
So(columns[0].(float64), ShouldBeGreaterThan, 1000000000000)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user