-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add memory connector The memory connector can be used for testing, or when you just need an in-memory datastore. This implementation is fairly efficient, mimicking Cassandra's handling of ordering. This change also modifies the contract of the connectors: they now may return more data than originally requested, and expect the callers not to mutate the results. This is safe since really the only caller should be the Client interface implementation (or tests). TODO: Range and Scan always return all the rows, ignoring the specified limit As a result, you don't get any continuation tokens
- Loading branch information
Showing
17 changed files
with
1,340 additions
and
118 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 |
---|---|---|
|
@@ -31,8 +31,11 @@ import ( | |
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"fmt" | ||
|
||
dosaRenamed "github.com/uber-go/dosa" | ||
"github.com/uber-go/dosa/connectors/devnull" | ||
_ "github.com/uber-go/dosa/connectors/devnull" | ||
_ "github.com/uber-go/dosa/connectors/memory" | ||
"github.com/uber-go/dosa/mocks" | ||
) | ||
|
||
|
@@ -57,27 +60,88 @@ var ( | |
ctx = context.TODO() | ||
scope = "test" | ||
namePrefix = "team.service" | ||
nullConnector = &devnull.Connector{} | ||
nullConnector dosaRenamed.Connector | ||
) | ||
|
||
func init() { | ||
nullConnector, _ = dosaRenamed.GetConnector("devnull", nil) | ||
} | ||
|
||
// ExampleNewClient initializes a client using the devnull connector, which discards all | ||
// the data you send it and always returns no rows. It's only useful for testing dosa. | ||
func ExampleNewClient() { | ||
// initialize registrar | ||
reg, err := dosaRenamed.NewRegistrar("test", "myteam.myservice", cte1) | ||
if err != nil { | ||
// registration will fail if the object is tagged incorrectly | ||
panic("dosaRenamed.NewRegister returned an error") | ||
fmt.Printf("NewRegistrar error: %s", err) | ||
return | ||
} | ||
|
||
// use a devnull connector for example purposes | ||
conn := &devnull.Connector{} | ||
conn, err := dosaRenamed.GetConnector("devnull", nil) | ||
if err != nil { | ||
fmt.Printf("GetConnector error: %s", err) | ||
return | ||
} | ||
|
||
// create the client using the registry and connector | ||
client := dosaRenamed.NewClient(reg, conn) | ||
|
||
err = client.Initialize(context.Background()) | ||
if err != nil { | ||
errors.Wrap(err, "client.Initialize returned an error") | ||
fmt.Printf("Initialize error: %s", err) | ||
return | ||
} | ||
} | ||
|
||
// ExampleGetConnector gets an in-memory connector that can be used for testing your code. | ||
// The in-memory connector always starts off with no rows, so you'll need to add rows to | ||
// your "database" before reading them | ||
func ExampleGetConnector() { | ||
// register your entities so the engine can separate your data based on table names. | ||
// Scopes and prefixes are not used by the in-memory connector, and are ignored, but | ||
// your list of entities is important. In this case, we only have one, our ClientTestEntity1 | ||
reg, err := dosaRenamed.NewRegistrar("test", "myteam.myservice", &ClientTestEntity1{}) | ||
if err != nil { | ||
fmt.Printf("NewRegistrar error: %s", err) | ||
return | ||
} | ||
|
||
// Find the memory connector. There is no configuration information so pass a nil | ||
// For this to work, you must force the init method of memory to run first, which happens | ||
// when we imported memory in the import list, with an underscore to just get the side effects | ||
conn, _ := dosaRenamed.GetConnector("memory", nil) | ||
|
||
// now construct a client from the registry and the connector | ||
client := dosaRenamed.NewClient(reg, conn) | ||
|
||
// initialize the client; this should always work for the in-memory connector | ||
if err = client.Initialize(context.Background()); err != nil { | ||
fmt.Printf("Initialize error: %s", err) | ||
return | ||
} | ||
|
||
// now populate an entity and insert it into the memory store | ||
if err := client.CreateIfNotExists(context.Background(), &ClientTestEntity1{ | ||
ID: int64(1), | ||
Name: "rkuris", | ||
Email: "[email protected]"}); err != nil { | ||
fmt.Printf("CreateIfNotExists error: %s", err) | ||
return | ||
} | ||
|
||
// create an entity to hold the read result, just populate the key | ||
e := ClientTestEntity1{ID: int64(1)} | ||
// now read the data from the "database", all columns | ||
err = client.Read(context.Background(), dosaRenamed.All(), &e) | ||
if err != nil { | ||
fmt.Printf("Read error: %s", err) | ||
return | ||
} | ||
// great! It worked, so display the information we stored earlier | ||
fmt.Printf("id:%d Name:%q Email:%q\n", e.ID, e.Name, e.Email) | ||
// Output: id:1 Name:"rkuris" Email:"[email protected]" | ||
} | ||
|
||
func TestNewClient(t *testing.T) { | ||
|
@@ -151,7 +215,7 @@ func TestClient_Read(t *testing.T) { | |
assert.NoError(t, c3.Initialize(ctx)) | ||
assert.NoError(t, c3.Read(ctx, fieldsToRead, cte1)) | ||
assert.Equal(t, cte1.ID, results["id"]) | ||
assert.Equal(t, cte1.Name, results["name"]) | ||
assert.NotEqual(t, cte1.Name, results["name"]) | ||
assert.Equal(t, cte1.Email, results["email"]) | ||
} | ||
|
||
|
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
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
Oops, something went wrong.