From d779cfc0b311ac458209d18ed2c653596288379c Mon Sep 17 00:00:00 2001 From: Adrian Witas Date: Thu, 4 Aug 2016 15:00:24 -0700 Subject: [PATCH] Increased test coverage --- dialect_test.go | 56 ++++++++++++++++++++++++++++++++++ doc.go | 1 - manager_factory.go | 6 ++++ manager_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ test/datastore_init.json | 14 +++++++++ util_test.go | 23 ++++++++++++++ 6 files changed, 165 insertions(+), 1 deletion(-) create mode 100644 dialect_test.go create mode 100644 util_test.go diff --git a/dialect_test.go b/dialect_test.go new file mode 100644 index 0000000..6503698 --- /dev/null +++ b/dialect_test.go @@ -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) + } + +} diff --git a/doc.go b/doc.go index 3470a79..42fefab 100644 --- a/doc.go +++ b/doc.go @@ -21,4 +21,3 @@ import ( } */ - diff --git a/manager_factory.go b/manager_factory.go index f130801..89f5526 100644 --- a/manager_factory.go +++ b/manager_factory.go @@ -1,6 +1,7 @@ package bgc import ( + "encoding/json" "github.com/viant/dsc" "github.com/viant/toolbox" ) @@ -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) } diff --git a/manager_test.go b/manager_test.go index 0f63ef4..24a8d93 100644 --- a/manager_test.go +++ b/manager_test.go @@ -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 } @@ -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) +} diff --git a/test/datastore_init.json b/test/datastore_init.json index b4ba660..5045fb8 100644 --- a/test/datastore_init.json +++ b/test/datastore_init.json @@ -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" } ] } diff --git a/util_test.go b/util_test.go new file mode 100644 index 0000000..293495f --- /dev/null +++ b/util_test.go @@ -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) +}