Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
6f3a62d2
Commit
6f3a62d2
authored
Dec 03, 2017
by
linux chips
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add _tests for mssql data source
parent
c75f9a19
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
195 additions
and
0 deletions
+195
-0
pkg/services/sqlstore/sqlutil/sqlutil.go
+1
-0
pkg/tsdb/mssql/macros_test.go
+72
-0
pkg/tsdb/mssql/mssql_test.go
+122
-0
No files found.
pkg/services/sqlstore/sqlutil/sqlutil.go
View file @
6f3a62d2
...
@@ -14,6 +14,7 @@ type TestDB struct {
...
@@ -14,6 +14,7 @@ type TestDB struct {
var
TestDB_Sqlite3
=
TestDB
{
DriverName
:
"sqlite3"
,
ConnStr
:
":memory:?_loc=Local"
}
var
TestDB_Sqlite3
=
TestDB
{
DriverName
:
"sqlite3"
,
ConnStr
:
":memory:?_loc=Local"
}
var
TestDB_Mysql
=
TestDB
{
DriverName
:
"mysql"
,
ConnStr
:
"grafana:password@tcp(localhost:3306)/grafana_tests?collation=utf8mb4_unicode_ci"
}
var
TestDB_Mysql
=
TestDB
{
DriverName
:
"mysql"
,
ConnStr
:
"grafana:password@tcp(localhost:3306)/grafana_tests?collation=utf8mb4_unicode_ci"
}
var
TestDB_Postgres
=
TestDB
{
DriverName
:
"postgres"
,
ConnStr
:
"user=grafanatest password=grafanatest host=localhost port=5432 dbname=grafanatest sslmode=disable"
}
var
TestDB_Postgres
=
TestDB
{
DriverName
:
"postgres"
,
ConnStr
:
"user=grafanatest password=grafanatest host=localhost port=5432 dbname=grafanatest sslmode=disable"
}
var
TestDB_Mssql
=
TestDB
{
DriverName
:
"mssql"
,
ConnStr
:
"server=localhost;port=1433;database=grafana_tests;user id=grafana;password=password"
}
func
CleanDB
(
x
*
xorm
.
Engine
)
{
func
CleanDB
(
x
*
xorm
.
Engine
)
{
if
x
.
DriverName
()
==
"postgres"
{
if
x
.
DriverName
()
==
"postgres"
{
...
...
pkg/tsdb/mssql/macros_test.go
0 → 100644
View file @
6f3a62d2
package
mssql
import
(
"testing"
"github.com/grafana/grafana/pkg/tsdb"
.
"github.com/smartystreets/goconvey/convey"
)
func
TestMacroEngine
(
t
*
testing
.
T
)
{
Convey
(
"MacroEngine"
,
t
,
func
()
{
engine
:=
&
MsSqlMacroEngine
{}
timeRange
:=
&
tsdb
.
TimeRange
{
From
:
"5m"
,
To
:
"now"
}
Convey
(
"interpolate __time function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
nil
,
"select $__time(time_column)"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time_column) ) as time_sec"
)
})
Convey
(
"interpolate __time function wrapped in aggregation"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
nil
,
"select min($__time(time_column))"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select min(DATEDIFF(second, {d '1970-01-01'}, DATEADD(second, DATEDIFF(second,GETDATE(),GETUTCDATE()), time_column) ) as time_sec)"
)
})
Convey
(
"interpolate __timeFilter function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
timeRange
,
"WHERE $__timeFilter(time_column)"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"WHERE time_column >= DATEADD(s, 18446744066914186738+DATEDIFF(second,GETUTCDATE(),GETDATE()), '1970-01-01') AND time_column <= DATEADD(s, 18446744066914187038+DATEDIFF(second,GETUTCDATE(),GETDATE()), '1970-01-01')"
)
})
Convey
(
"interpolate __timeFrom function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
timeRange
,
"select $__timeFrom(time_column)"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select DATEADD(second, 18446744066914186738+DATEDIFF(second,GETUTCDATE(),GETDATE()), '1970-01-01')"
)
})
Convey
(
"interpolate __timeTo function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
timeRange
,
"select $__timeTo(time_column)"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select DATEADD(second, 18446744066914187038+DATEDIFF(second,GETUTCDATE(),GETDATE()), '1970-01-01')"
)
})
Convey
(
"interpolate __unixEpochFilter function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
timeRange
,
"select $__unixEpochFilter(18446744066914186738)"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select 18446744066914186738 >= 18446744066914186738 AND 18446744066914186738 <= 18446744066914187038"
)
})
Convey
(
"interpolate __unixEpochFrom function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
timeRange
,
"select $__unixEpochFrom()"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select 18446744066914186738"
)
})
Convey
(
"interpolate __unixEpochTo function"
,
func
()
{
sql
,
err
:=
engine
.
Interpolate
(
timeRange
,
"select $__unixEpochTo()"
)
So
(
err
,
ShouldBeNil
)
So
(
sql
,
ShouldEqual
,
"select 18446744066914187038"
)
})
})
}
pkg/tsdb/mssql/mssql_test.go
0 → 100644
View file @
6f3a62d2
package
mssql
import
(
"testing"
"time"
"strings"
_
"github.com/denisenkom/go-mssqldb"
"github.com/go-xorm/xorm"
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/services/sqlstore/sqlutil"
"github.com/grafana/grafana/pkg/tsdb"
.
"github.com/smartystreets/goconvey/convey"
)
// To run this test, remove the Skip from SkipConvey
// and set up a MSSQL db named grafana_tests and a user/password grafana/password
// and set the variable below to the IP address of the database
var
serverIP
string
=
"10.20.30.40"
func
TestMSSQL
(
t
*
testing
.
T
)
{
//SkipConvey("MSSQL", t, func() {
SkipConvey
(
"MSSQL"
,
t
,
func
()
{
x
:=
InitMSSQLTestDB
(
t
)
endpoint
:=
&
MssqlQueryEndpoint
{
sqlEngine
:
&
tsdb
.
DefaultSqlEngine
{
MacroEngine
:
NewMssqlMacroEngine
(),
XormEngine
:
x
,
},
log
:
log
.
New
(
"tsdb.mssql"
),
}
sess
:=
x
.
NewSession
()
defer
sess
.
Close
()
sql
:=
"IF OBJECT_ID('dbo.[mssql_types]', 'U') IS NOT NULL"
sql
+=
" DROP TABLE dbo.[mssql_types];"
sql
+=
"CREATE TABLE [mssql_types] ( "
sql
+=
"abit bit, "
sql
+=
"atinyint tinyint, "
sql
+=
"asmallint smallint, "
sql
+=
"aint int, "
sql
+=
"abigint bigint, "
sql
+=
"avarchar varchar(3), "
sql
+=
"achar char(3), "
sql
+=
"anewvarchar varchar(14), "
sql
+=
"anewchar char(14), "
sql
+=
"areal real, "
sql
+=
"anewdecimal decimal(10,2), "
sql
+=
"afloat float, "
sql
+=
"adatetime datetime, "
sql
+=
"adate date, "
sql
+=
"atime time) "
_
,
err
:=
sess
.
Exec
(
sql
)
So
(
err
,
ShouldBeNil
)
sql
=
"INSERT INTO [mssql_types] "
sql
+=
"(abit, atinyint, asmallint, aint, abigint, "
sql
+=
"avarchar, achar, anewvarchar, anewchar, "
sql
+=
"areal, anewdecimal, afloat, "
sql
+=
"adatetime, adate, atime ) "
sql
+=
"VALUES(1, 5, 20020, 980300, 1420070400, "
sql
+=
"'abc', 'def', 'hi varchar', 'I am only char', "
sql
+=
"1.11, 2.22, 3.33, "
sql
+=
"GETUTCDATE(), CAST(GETUTCDATE() AS DATE), CAST(GETUTCDATE() AS TIME) );"
_
,
err
=
sess
.
Exec
(
sql
)
So
(
err
,
ShouldBeNil
)
Convey
(
"Query with Table format should map MSSQL column types to Go types"
,
func
()
{
query
:=
&
tsdb
.
TsdbQuery
{
Queries
:
[]
*
tsdb
.
Query
{
{
Model
:
simplejson
.
NewFromAny
(
map
[
string
]
interface
{}{
"rawSql"
:
"SELECT * FROM mssql_types"
,
"format"
:
"table"
,
}),
RefId
:
"A"
,
},
},
}
resp
,
err
:=
endpoint
.
Query
(
nil
,
nil
,
query
)
queryResult
:=
resp
.
Results
[
"A"
]
So
(
err
,
ShouldBeNil
)
column
:=
queryResult
.
Tables
[
0
]
.
Rows
[
0
]
So
(
column
[
0
]
.
(
bool
),
ShouldEqual
,
true
)
So
(
column
[
1
]
.
(
int64
),
ShouldEqual
,
5
)
So
(
column
[
2
]
.
(
int64
),
ShouldEqual
,
20020
)
So
(
column
[
3
]
.
(
int64
),
ShouldEqual
,
980300
)
So
(
column
[
4
]
.
(
int64
),
ShouldEqual
,
1420070400
)
So
(
column
[
5
]
.
(
string
),
ShouldEqual
,
"abc"
)
So
(
column
[
6
]
.
(
string
),
ShouldEqual
,
"def"
)
So
(
column
[
7
]
.
(
string
),
ShouldEqual
,
"hi varchar"
)
So
(
column
[
8
]
.
(
string
),
ShouldEqual
,
"I am only char"
)
So
(
column
[
9
]
.
(
float64
),
ShouldEqual
,
1.1100000143051147
)
// MSSQL dose not have precision for "real" datatype
// fiix me: MSSQL driver puts the decimal inside an array of chars. and the test fails despite the values are correct.
//So(column[10].([]uint8), ShouldEqual, []uint8{'2', '.', '2', '2'})
So
(
column
[
11
]
.
(
float64
),
ShouldEqual
,
3.33
)
So
(
column
[
12
]
.
(
time
.
Time
),
ShouldHappenWithin
,
time
.
Duration
(
15
*
time
.
Second
),
time
.
Now
()
.
UTC
()
)
So
(
column
[
13
]
.
(
time
.
Time
),
ShouldHappenWithin
,
time
.
Duration
(
15
*
time
.
Second
),
time
.
Now
()
.
UTC
()
.
Truncate
(
24
*
time
.
Hour
)
)
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
)
)
})
})
}
func
InitMSSQLTestDB
(
t
*
testing
.
T
)
*
xorm
.
Engine
{
x
,
err
:=
xorm
.
NewEngine
(
sqlutil
.
TestDB_Mssql
.
DriverName
,
strings
.
Replace
(
sqlutil
.
TestDB_Mssql
.
ConnStr
,
"localhost"
,
serverIP
,
1
)
)
// x.ShowSQL()
if
err
!=
nil
{
t
.
Fatalf
(
"Failed to init mssql db %v"
,
err
)
}
sqlutil
.
CleanDB
(
x
)
return
x
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment