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
09e71e00
Commit
09e71e00
authored
Jun 14, 2018
by
bergquist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
sql: adds tests for InTransaction
parent
03dae10e
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
69 additions
and
3 deletions
+69
-3
pkg/services/sqlstore/apikey.go
+4
-3
pkg/services/sqlstore/sqlstore.go
+1
-0
pkg/services/sqlstore/transactions_test.go
+64
-0
No files found.
pkg/services/sqlstore/apikey.go
View file @
09e71e00
package
sqlstore
import
(
"context"
"time"
"github.com/grafana/grafana/pkg/bus"
...
...
@@ -11,7 +12,7 @@ func init() {
bus
.
AddHandler
(
"sql"
,
GetApiKeys
)
bus
.
AddHandler
(
"sql"
,
GetApiKeyById
)
bus
.
AddHandler
(
"sql"
,
GetApiKeyByName
)
bus
.
AddHandler
(
"sql"
,
DeleteApiKey
)
bus
.
AddHandler
Ctx
(
"sql"
,
DeleteApiKeyCtx
)
bus
.
AddHandler
(
"sql"
,
AddApiKey
)
}
...
...
@@ -22,8 +23,8 @@ func GetApiKeys(query *m.GetApiKeysQuery) error {
return
sess
.
Find
(
&
query
.
Result
)
}
func
DeleteApiKey
(
cmd
*
m
.
DeleteApiKeyCommand
)
error
{
return
inTransaction
(
func
(
sess
*
DBSession
)
error
{
func
DeleteApiKey
Ctx
(
ctx
context
.
Context
,
cmd
*
m
.
DeleteApiKeyCommand
)
error
{
return
withDbSession
(
ctx
,
func
(
sess
*
DBSession
)
error
{
var
rawSql
=
"DELETE FROM api_key WHERE id=? and org_id=?"
_
,
err
:=
sess
.
Exec
(
rawSql
,
cmd
.
Id
,
cmd
.
OrgId
)
return
err
...
...
pkg/services/sqlstore/sqlstore.go
View file @
09e71e00
...
...
@@ -250,6 +250,7 @@ func (ss *SqlStore) readConfig() {
}
func
InitTestDB
(
t
*
testing
.
T
)
*
SqlStore
{
t
.
Helper
()
sqlstore
:=
&
SqlStore
{}
sqlstore
.
skipEnsureAdmin
=
true
sqlstore
.
Bus
=
bus
.
New
()
...
...
pkg/services/sqlstore/transactions_test.go
0 → 100644
View file @
09e71e00
package
sqlstore
import
(
"context"
"errors"
"testing"
"github.com/grafana/grafana/pkg/log"
"github.com/grafana/grafana/pkg/models"
.
"github.com/smartystreets/goconvey/convey"
)
type
testQuery
struct
{
result
bool
}
var
ProvokedError
=
errors
.
New
(
"testing error."
)
func
TestTransaction
(
t
*
testing
.
T
)
{
InitTestDB
(
t
)
Convey
(
"InTransaction asdf asdf"
,
t
,
func
()
{
ss
:=
SqlStore
{
log
:
log
.
New
(
"test-logger"
)}
cmd
:=
&
models
.
AddApiKeyCommand
{
Key
:
"secret-key"
,
Name
:
"key"
,
OrgId
:
1
}
err
:=
AddApiKey
(
cmd
)
So
(
err
,
ShouldBeNil
)
deleteApiKeyCmd
:=
&
models
.
DeleteApiKeyCommand
{
Id
:
cmd
.
Result
.
Id
,
OrgId
:
1
}
Convey
(
"can update key"
,
func
()
{
err
:=
ss
.
InTransaction
(
context
.
Background
(),
func
(
ctx
context
.
Context
)
error
{
return
DeleteApiKeyCtx
(
ctx
,
deleteApiKeyCmd
)
})
So
(
err
,
ShouldBeNil
)
query
:=
&
models
.
GetApiKeyByIdQuery
{
ApiKeyId
:
cmd
.
Result
.
Id
}
err
=
GetApiKeyById
(
query
)
So
(
err
,
ShouldEqual
,
models
.
ErrInvalidApiKey
)
})
Convey
(
"wont update if one handler fails"
,
func
()
{
err
:=
ss
.
InTransaction
(
context
.
Background
(),
func
(
ctx
context
.
Context
)
error
{
err
:=
DeleteApiKeyCtx
(
ctx
,
deleteApiKeyCmd
)
if
err
!=
nil
{
return
err
}
return
ProvokedError
})
So
(
err
,
ShouldEqual
,
ProvokedError
)
query
:=
&
models
.
GetApiKeyByIdQuery
{
ApiKeyId
:
cmd
.
Result
.
Id
}
err
=
GetApiKeyById
(
query
)
So
(
err
,
ShouldBeNil
)
So
(
query
.
Result
.
Id
,
ShouldEqual
,
cmd
.
Result
.
Id
)
})
})
}
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