Skip to content

Commit

Permalink
Increased test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Witas committed Aug 4, 2016
1 parent 1659eb1 commit d779cfc
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 1 deletion.
56 changes: 56 additions & 0 deletions dialect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package bgc_test

import (
"github.com/stretchr/testify/assert"
"github.com/viant/dsc"
"github.com/viant/dsunit"
"testing"
)

func Manager(t *testing.T) dsc.Manager {
keyPath := dsunit.ExpandTestProtocolAsPathIfNeeded("test:///test/test_service.pem")
config := dsc.NewConfig("bigquery", "",
"serviceAccountId:565950306583-hu98foqgnunu6a1a043plvl03ip60j5g@developer.gserviceaccount.com,privateKeyPath:"+keyPath+",projectId:spheric-arcadia-98015,datasetId:MyDataset,dateFormat:yyyy-MM-dd hh:mm:ss z")
factory := dsc.NewManagerFactory()
manager, err := factory.Create(config)
assert.Nil(t, err)
if err != nil {
assert.FailNow(t, err.Error())
}
return manager
}

func TestGetDatastores(t *testing.T) {
manager := Manager(t)
dialect := dsc.GetDatastoreDialect("bigquery")
datastores, err := dialect.GetDatastores(manager)
assert.Nil(t, err)
assert.True(t, len(datastores) > 0)

}

func TestGetDatastore(t *testing.T) {
manager := Manager(t)
dialect := dsc.GetDatastoreDialect("bigquery")
datastore, err := dialect.GetCurrentDatastore(manager)
assert.Nil(t, err)
assert.Equal(t, "MyDataset", datastore)

}

func TestGetTables(t *testing.T) {
{
manager := Manager(t)
dialect := dsc.GetDatastoreDialect("bigquery")
_, err := dialect.GetTables(manager, "MyDataset")
assert.Nil(t, err)
}

{
manager := Manager(t)
dialect := dsc.GetDatastoreDialect("bigquery")
_, err := dialect.GetTables(manager, "Fake")
assert.NotNil(t, err)
}

}
1 change: 0 additions & 1 deletion doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ import (
}
*/

6 changes: 6 additions & 0 deletions manager_factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package bgc

import (
"encoding/json"
"github.com/viant/dsc"
"github.com/viant/toolbox"
)
Expand All @@ -24,6 +25,11 @@ func (f managerFactory) CreateFromURL(url string) (dsc.Manager, error) {
}
defer reader.Close()
config := &dsc.Config{}
err = json.NewDecoder(reader).Decode(config)
if err != nil {
return nil, err
}
config.Init()
return f.Create(config)
}

Expand Down
66 changes: 66 additions & 0 deletions manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func GetManager(t *testing.T) dsc.Manager {
t.Fatalf("Failed to create manager %v", err)
}
manager.TableDescriptorRegistry().Register(&dsc.TableDescriptor{Table: "travelers3", PkColumns: []string{"id"}, SchemaURL: "some_url"})
manager.TableDescriptorRegistry().Register(&dsc.TableDescriptor{Table: "travelers4", PkColumns: []string{"id"}, SchemaURL: "some_url"})
manager.TableDescriptorRegistry().Register(&dsc.TableDescriptor{Table: "travelers5", PkColumns: []string{"id"}, SchemaURL: "some_url"})
manager.TableDescriptorRegistry().Register(&dsc.TableDescriptor{Table: "abc", PkColumns: []string{"id"}, SchemaURL: "some_url"})
return manager
}

Expand Down Expand Up @@ -113,3 +116,66 @@ func TestPersistAll(t *testing.T) {

dsunit.ExpectDatasetFor(t, "MyDataset", dsunit.FullTableDatasetCheckPolicy, "test://test/", "PersistAll")
}

func TestExecuteOnConnection(t *testing.T) {
manager := GetManager(t)
connetion, err := manager.ConnectionProvider().Get()
assert.Nil(t, err)
defer connetion.Close()
result, err := manager.ExecuteOnConnection(connetion, "INSERT INTO travelers4(id, name) VALUES(?, ?)", []interface{}{20, "Traveler20"})
assert.Nil(t, err)
rowsAdded, err := result.RowsAffected()
assert.Nil(t, err)
assert.EqualValues(t, 1, rowsAdded)

//Test error due to unknown table
_, err = manager.ExecuteOnConnection(connetion, "INSERT INTO abc(id, name) VALUES(?, ?)", []interface{}{20, "Traveler20"})
assert.NotNil(t, err)
_, err = manager.ExecuteOnConnection(connetion, "UPDATE abc SET name = ? WHERE id = ?", []interface{}{"Traveler20", 20})
assert.NotNil(t, err)
}

func TestPersistAllOnConnection(t *testing.T) {
manager := GetManager(t)
connetion, err := manager.ConnectionProvider().Get()
assert.Nil(t, err)
defer connetion.Close()

var travelers = make([]Traveler, 2)

travelers[0] = Traveler{
Id: 10,
Name: "Cook",
LastVisitTime: time.Now(),
Achievements: []string{"abc", "jhi"},
MostLikedCity: MostLikedCity{City: "Cracow", Visits: 4},
}

travelers[1] = Traveler{
Id: 20,
Name: "Robin",
LastVisitTime: time.Now(),
Achievements: []string{"w", "a"},
MostLikedCity: MostLikedCity{"Moscow", 3, []string{"s3", "sN"}},
}

inserted, _, err := manager.PersistAllOnConnection(connetion, &travelers, "travelers5", nil)
assert.Nil(t, err)
assert.Equal(t, 2, inserted)

//updated not supported
_, _, err = manager.PersistAllOnConnection(connetion, &travelers, "travelers5", nil)
assert.NotNil(t, err)

//Test error due to unknown table
_, _, err = manager.PersistAllOnConnection(connetion, &travelers, "travelers5", nil)
assert.NotNil(t, err)
}

func TestCreateFromURL(t *testing.T) {
factory := dsc.NewManagerFactory()
url := dsunit.ExpandTestProtocolAsURLIfNeeded("test:///test/config/store.json")
manager, err := factory.CreateFromURL(url)
assert.Nil(t, err)
assert.NotNil(t, manager)
}
14 changes: 14 additions & 0 deletions test/datastore_init.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@
"id"
],
"SchemaUrl": "test:///test/traveler_schema.json"
},
{
"Table": "travelers4",
"PkColumns": [
"id"
],
"SchemaUrl": "test:///test/traveler_schema.json"
},
{
"Table": "travelers5",
"PkColumns": [
"id"
],
"SchemaUrl": "test:///test/traveler_schema.json"
}
]
}
Expand Down
23 changes: 23 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bgc_test

import (
"github.com/stretchr/testify/assert"
"github.com/viant/bgc"
"github.com/viant/dsc"
"github.com/viant/dsunit"
"testing"
)

func TestGetServiceAndContextForManager(t *testing.T) {
manager1 := Manager(t)
service, context, err := bgc.GetServiceAndContextForManager(manager1)
assert.Nil(t, err)
assert.NotNil(t, service)
assert.NotNil(t, context)

config := dsc.NewConfig("ndjson", "[url]", "dateFormat:yyyy-MM-dd hh:mm:ss,ext:json,url:"+dsunit.ExpandTestProtocolAsURLIfNeeded("test:///test/"))
manager2, err := dsc.NewManagerFactory().Create(config)
assert.Nil(t, err)
_, _, err = bgc.GetServiceAndContextForManager(manager2)
assert.NotNil(t, err)
}

0 comments on commit d779cfc

Please sign in to comment.