mirror of https://github.com/grafana/grafana
pull/1442/head
parent
27f07e9de2
commit
c7ed348ee8
@ -1,20 +1,47 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"errors" |
||||
"time" |
||||
|
||||
"github.com/torkelo/grafana-pro/pkg/bus" |
||||
m "github.com/torkelo/grafana-pro/pkg/models" |
||||
|
||||
"github.com/go-xorm/xorm" |
||||
) |
||||
|
||||
func init() { |
||||
bus.AddHandler("sql", GetDataSourcesQuery) |
||||
bus.AddHandler("sql", GetDataSources) |
||||
bus.AddHandler("sql", AddDataSource) |
||||
} |
||||
|
||||
func GetDataSourcesQuery(query *m.GetDataSourcesQuery) error { |
||||
return errors.New("Hello from query handler") |
||||
func GetDataSources(query *m.GetDataSourcesQuery) error { |
||||
sess := x.Limit(100, 0).Where("account_id=?", query.AccountId) |
||||
|
||||
query.Resp = make([]*m.DataSource, 0) |
||||
return sess.Find(&query.Resp) |
||||
} |
||||
|
||||
func AddDataSource(cmd *m.AddDataSourceCommand) error { |
||||
return errors.New("Hello from command handler") |
||||
|
||||
return inTransaction(func(sess *xorm.Session) error { |
||||
var err error |
||||
|
||||
ds := m.DataSource{ |
||||
AccountId: cmd.AccountId, |
||||
Name: cmd.Name, |
||||
Type: cmd.Type, |
||||
Access: cmd.Access, |
||||
Url: cmd.Url, |
||||
Created: time.Now(), |
||||
Updated: time.Now(), |
||||
} |
||||
|
||||
if ds.Id == 0 { |
||||
_, err = sess.Insert(ds) |
||||
} else { |
||||
_, err = sess.Id(ds.Id).Update(ds) |
||||
} |
||||
|
||||
return err |
||||
}) |
||||
} |
||||
|
@ -0,0 +1,54 @@ |
||||
package sqlstore |
||||
|
||||
import ( |
||||
"testing" |
||||
|
||||
"github.com/go-xorm/xorm" |
||||
|
||||
. "github.com/smartystreets/goconvey/convey" |
||||
|
||||
m "github.com/torkelo/grafana-pro/pkg/models" |
||||
) |
||||
|
||||
func InitTestDB(t *testing.T) { |
||||
x, err := xorm.NewEngine("sqlite3", ":memory:") |
||||
|
||||
if err != nil { |
||||
t.Fatalf("Failed to init in memory sqllite3 db %v", err) |
||||
} |
||||
|
||||
SetEngine(x, false) |
||||
} |
||||
|
||||
type Test struct { |
||||
Id int64 |
||||
Name string |
||||
} |
||||
|
||||
func TestDataAccess(t *testing.T) { |
||||
|
||||
Convey("Testing DB", t, func() { |
||||
InitTestDB(t) |
||||
|
||||
Convey("Can add datasource", func() { |
||||
|
||||
err := AddDataSource(&m.AddDataSourceCommand{ |
||||
AccountId: 10, |
||||
Type: m.DS_GRAPHITE, |
||||
Access: m.DS_ACCESS_DIRECT, |
||||
Url: "http://test", |
||||
}) |
||||
|
||||
So(err, ShouldBeNil) |
||||
|
||||
query := m.GetDataSourcesQuery{AccountId: 10} |
||||
err = GetDataSources(&query) |
||||
So(err, ShouldBeNil) |
||||
|
||||
So(len(query.Resp), ShouldEqual, 1) |
||||
|
||||
}) |
||||
|
||||
}) |
||||
|
||||
} |
Loading…
Reference in new issue