Skip to content

Commit

Permalink
Google Datastore sql driver tests (#43)
Browse files Browse the repository at this point in the history
Google Datastore sql driver tests
  • Loading branch information
araddon authored Mar 15, 2017
1 parent 89dde5f commit 5bf8dcc
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backends/datastore/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var (
)

func init() {
// We need to register our Source into Datastource provider here
// We need to register our Source into Datasource provider here
datasource.Register(SourceLabel, &Source{})
}

Expand Down
6 changes: 5 additions & 1 deletion backends/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ type Article struct {
*tu.Article
}

func NewArticle() Article {
return Article{&tu.Article{}}
}

func (m *Article) Load(props []datastore.Property) error {
for _, p := range props {
switch p.Name {
Expand Down Expand Up @@ -270,7 +274,7 @@ func TestDataSourceInterface(t *testing.T) {

// Now make sure that the datastore source has been registered
// and meets api for qlbridge.DataSource
ds, err := datasource.OpenConn(gds.DataSourceLabel, ArticleKind)
ds, err := datasource.OpenConn(gds.SourceLabel, ArticleKind)
assert.Tf(t, err == nil, "no error on conn: %v", err)
assert.Tf(t, ds != nil, "Found datastore")
}
Expand Down
57 changes: 57 additions & 0 deletions backends/datastore/sql_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package datastore_test

import (
"database/sql"
"testing"
"time"

u "github.com/araddon/gou"
"github.com/bmizerany/assert"

"github.com/araddon/qlbridge/exec"
"github.com/araddon/qlbridge/testutil"
"github.com/dataux/dataux/backends/datastore"
)

func init() {
testutil.Setup()
time.Sleep(time.Second * 1)
exec.RegisterSqlDriver()
exec.DisableRecover()
}

func TestDatastoreSelectSqlDriver(t *testing.T) {

sqlText := `select title, count, deleted, author from DataUxTestArticle WHERE author = "aaron" LIMIT 1`
db, err := sql.Open("qlbridge", datastore.SourceLabel)
assert.Equalf(t, nil, err, "no error: %v", err)
assert.NotEqual(t, nil, db, "has conn: ", db)

defer func() {
if err := db.Close(); err != nil {
t.Fatalf("Should not error on close: %v", err)
}
}()

rows, err := db.Query(sqlText)
assert.Tf(t, err == nil, "no error: %v", err)
defer rows.Close()
assert.Tf(t, rows != nil, "has results: %v", rows)
cols, err := rows.Columns()
assert.Tf(t, err == nil, "no error: %v", err)
assert.Tf(t, len(cols) == 4, "4 cols: %v", cols)
articles := make([]Article, 0)
for rows.Next() {
a := NewArticle()
err = rows.Scan(&a.Title, &a.Count, &a.Deleted, &a.Author)
assert.Tf(t, err == nil, "no error: %v", err)
u.Debugf("article=%+v", a)
articles = append(articles, a)
}
assert.Tf(t, rows.Err() == nil, "no error: %v", err)
assert.Tf(t, len(articles) == 1, "has 1 articles row: %+v", articles)

a1 := articles[0]
assert.Equal(t, "article1", a1.Title)
assert.T(t, a1.Author == "aaron")
}

0 comments on commit 5bf8dcc

Please sign in to comment.