-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
143 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package tigris | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/tigrisdata/tigris-client-go/config" | ||
"github.com/tigrisdata/tigris-client-go/driver" | ||
"github.com/tigrisdata/tigris-client-go/schema" | ||
) | ||
|
||
// Client responsible for connecting to the server and opening a database | ||
type Client struct { | ||
driver driver.Driver | ||
config *config.Database | ||
} | ||
|
||
// NewClient creates a connection to the Tigris server | ||
func NewClient(ctx context.Context, cfg *config.Database) (*Client, error) { | ||
d, err := driver.NewDriver(ctx, &cfg.Driver) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Client{driver: d, config: cfg}, nil | ||
} | ||
|
||
// Close terminates client connections and release resources | ||
func (c *Client) Close() error { | ||
return c.driver.Close() | ||
} | ||
|
||
// OpenDatabase initializes Database from given collection models. | ||
// It creates Database if necessary. | ||
// Creates and migrates schemas of the collections which constitutes the Database | ||
func (c *Client) OpenDatabase(ctx context.Context, dbName string, model schema.Model, models ...schema.Model) (*Database, error) { | ||
if getTxCtx(ctx) != nil { | ||
return nil, ErrNotTransactional | ||
} | ||
|
||
return openDatabaseFromModels(ctx, c.driver, c.config, dbName, model, models...) | ||
} | ||
|
||
// DropDatabase deletes the database and all collections in it | ||
func (c *Client) DropDatabase(ctx context.Context, dbName string) error { | ||
if getTxCtx(ctx) != nil { | ||
return ErrNotTransactional | ||
} | ||
|
||
return c.driver.DropDatabase(ctx, dbName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package tigris | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/golang/mock/gomock" | ||
"github.com/stretchr/testify/require" | ||
api "github.com/tigrisdata/tigris-client-go/api/server/v1" | ||
"github.com/tigrisdata/tigris-client-go/config" | ||
"github.com/tigrisdata/tigris-client-go/test" | ||
) | ||
|
||
func TestClient(t *testing.T) { | ||
mc, cancel := test.SetupTests(t, 8) | ||
defer cancel() | ||
|
||
ctx, cancel1 := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel1() | ||
|
||
cfg := &config.Database{Driver: config.Driver{URL: test.GRPCURL(8)}} | ||
cfg.TLS = test.SetupTLS(t) | ||
|
||
type Coll1 struct { | ||
Key1 string `tigris:"primary_key"` | ||
} | ||
|
||
txCtx := &api.TransactionCtx{Id: "tx_id1", Origin: "origin_id1"} | ||
|
||
mc.EXPECT().CreateDatabase(gomock.Any(), | ||
pm(&api.CreateDatabaseRequest{ | ||
Db: "db1", | ||
Options: &api.DatabaseOptions{}, | ||
})).Return(&api.CreateDatabaseResponse{}, nil) | ||
|
||
mc.EXPECT().BeginTransaction(gomock.Any(), | ||
pm(&api.BeginTransactionRequest{ | ||
Db: "db1", | ||
Options: &api.TransactionOptions{}, | ||
})).Return(&api.BeginTransactionResponse{TxCtx: txCtx}, nil) | ||
|
||
mc.EXPECT().CreateOrUpdateCollection(gomock.Any(), | ||
pm(&api.CreateOrUpdateCollectionRequest{ | ||
Db: "db1", Collection: "coll_1", | ||
Schema: []byte(`{"title":"coll_1","properties":{"Key1":{"type":"string"}},"primary_key":["Key1"]}`), | ||
Options: &api.CollectionOptions{}, | ||
})).Do(func(ctx context.Context, r *api.CreateOrUpdateCollectionRequest) { | ||
}).Return(&api.CreateOrUpdateCollectionResponse{}, nil) | ||
|
||
mc.EXPECT().CommitTransaction(gomock.Any(), | ||
pm(&api.CommitTransactionRequest{ | ||
Db: "db1", | ||
})).Return(&api.CommitTransactionResponse{}, nil) | ||
|
||
c, err := NewClient(ctx, cfg) | ||
require.NoError(t, err) | ||
|
||
db, err := c.OpenDatabase(ctx, "db1", &Coll1{}) | ||
require.NoError(t, err) | ||
require.NotNil(t, db) | ||
|
||
_, err = c.OpenDatabase(setTxCtx(ctx, &Tx{}), "db1", &Coll1{}) | ||
require.Error(t, err) | ||
|
||
mc.EXPECT().DropDatabase(gomock.Any(), | ||
pm(&api.DropDatabaseRequest{ | ||
Db: "db1", | ||
Options: &api.DatabaseOptions{}, | ||
})).Return(&api.DropDatabaseResponse{}, nil) | ||
|
||
err = c.DropDatabase(ctx, "db1") | ||
require.NoError(t, err) | ||
|
||
err = c.DropDatabase(setTxCtx(ctx, &Tx{}), "db1") | ||
require.Error(t, err) | ||
|
||
err = c.Close() | ||
require.NoError(t, err) | ||
|
||
cfg.URL = "http://invalid" | ||
_, err = NewClient(ctx, cfg) | ||
require.Error(t, err) | ||
} |