Skip to content

Commit

Permalink
Merge pull request #15 from acoshift/random-kind-test
Browse files Browse the repository at this point in the history
random test kind
  • Loading branch information
acoshift committed Apr 9, 2017
2 parents 1d7173c + 3dc2e3b commit ec16f6e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 32 deletions.
15 changes: 13 additions & 2 deletions ds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import (
"encoding/base64"
"os"
"testing"
"time"

"math/rand"
"strconv"

"cloud.google.com/go/datastore"
"golang.org/x/oauth2/google"
Expand All @@ -18,8 +22,15 @@ type ExampleModel struct {
Value int
}

var kind string

func init() {
rand.Seed(time.Now().Unix())
kind = "Test_" + strconv.Itoa(rand.Int())
}

func (x *ExampleModel) NewKey() {
x.NewIncomplateKey("Test", nil)
x.NewIncomplateKey(kind, nil)
}

type ExampleNotModel struct {
Expand Down Expand Up @@ -81,6 +92,6 @@ func prepareData(client *Client) []*datastore.Key {
}

func removeData(client *Client) {
keys, _ := client.QueryKeys(ctx, "Test")
keys, _ := client.QueryKeys(ctx, kind)
client.DeleteMulti(ctx, keys)
}
9 changes: 3 additions & 6 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func TestParseID(t *testing.T) {
}

func TestBuildIDKeys(t *testing.T) {
kind := "Test"
ids := []int64{1, 2, 3, 4, 5}
keys := BuildIDKeys(kind, ids)
for i, key := range keys {
Expand All @@ -38,7 +37,6 @@ func TestBuildIDKeys(t *testing.T) {
}

func TestBuildStringIDKeys(t *testing.T) {
kind := "Test"
ids := []string{"aa", "bb", "cccc", "123", "456"}
out := []int64{123, 456}
keys := BuildStringIDKeys(kind, ids)
Expand All @@ -56,7 +54,6 @@ func TestBuildStringIDKeys(t *testing.T) {
}

func TestBuildNameIDKeys(t *testing.T) {
kind := "Test"
ids := []string{"aa", "bb", "cccc", "123", "456"}
keys := BuildNameKeys(kind, ids)
if len(keys) != len(ids) {
Expand All @@ -74,7 +71,7 @@ func TestBuildNameIDKeys(t *testing.T) {

func TestExtractKey(t *testing.T) {
x := &ExampleModel{}
x.SetID("Test", 10)
x.SetID(kind, 10)
key := ExtractKey(x)
if key == nil {
t.Fatalf("expected key not nil")
Expand All @@ -85,7 +82,7 @@ func TestExtractKeys(t *testing.T) {
xs := make([]*ExampleModel, 10)
for i := range xs {
xs[i] = &ExampleModel{}
xs[i].SetID("Test", int64(i))
xs[i].SetID(kind, int64(i))
}
keys := ExtractKeys(xs)
for i, key := range keys {
Expand All @@ -102,7 +99,7 @@ func TestExtractKeysPtr(t *testing.T) {
xs := make([]*ExampleModel, 10)
for i := range xs {
xs[i] = &ExampleModel{}
xs[i].SetID("Test", int64(i))
xs[i].SetID(kind, int64(i))
}
keys := ExtractKeys(&xs)
for i, key := range keys {
Expand Down
44 changes: 22 additions & 22 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func TestModel(t *testing.T) {

// Should not panic
x.SetKey(nil)
x.SetKey(datastore.IDKey("Test", int64(10), nil))
x.SetID("Test", 10)
x.NewIncomplateKey("Test", nil)
x.SetKey(datastore.IDKey(kind, int64(10), nil))
x.SetID(kind, 10)
x.NewIncomplateKey(kind, nil)
if x.ID() != 0 {
t.Fatalf("expected id to be 0")
}

x = &Model{}
x.NewIncomplateKey("Test", nil)
x.NewIncomplateKey(kind, nil)
if x.GetKey() == nil {
t.Fatalf("expected key not nil")
}
Expand All @@ -37,7 +37,7 @@ func TestModel(t *testing.T) {
t.Fatalf("expected id to be 0")
}

x.SetID("Test", 10)
x.SetID(kind, 10)
if x.GetKey() == nil {
t.Fatalf("expected key not nil")
}
Expand All @@ -54,17 +54,17 @@ func TestStringIDModel(t *testing.T) {

// Should not panic
x.SetKey(nil)
x.SetKey(datastore.IDKey("Test", int64(10), nil))
x.SetID("Test", 10)
x.SetStringID("Test", "aaa")
x.SetNameID("Test", "bbb")
x.NewIncomplateKey("Test", nil)
x.SetKey(datastore.IDKey(kind, int64(10), nil))
x.SetID(kind, 10)
x.SetStringID(kind, "aaa")
x.SetNameID(kind, "bbb")
x.NewIncomplateKey(kind, nil)
if x.ID() != "" {
t.Fatalf("expected id to be empty")
}

x = &StringIDModel{}
x.NewIncomplateKey("Test", nil)
x.NewIncomplateKey(kind, nil)
if x.GetKey() == nil {
t.Fatalf("expected key not nil")
}
Expand All @@ -77,23 +77,23 @@ func TestStringIDModel(t *testing.T) {
t.Fatalf("expected id to be empty")
}

x.SetID("Test", 10)
x.SetID(kind, 10)
if x.GetKey() == nil {
t.Fatalf("expected key not nil")
}
if x.ID() == "" {
t.Fatalf("expected id not empty")
}

x.SetStringID("Test", "10")
x.SetStringID(kind, "10")
if x.GetKey() == nil {
t.Fatalf("expected key not nil")
}
if x.ID() != "10" {
t.Fatalf("expected id to be %s; got %s", "10", x.ID())
}

x.SetNameID("Test", "aaa")
x.SetNameID(kind, "aaa")
if x.GetKey() == nil {
t.Fatalf("expected key not nil")
}
Expand All @@ -104,7 +104,7 @@ func TestStringIDModel(t *testing.T) {

func TestSetKey(t *testing.T) {
x := &Model{}
key := datastore.IDKey("Test", 1, nil)
key := datastore.IDKey(kind, 1, nil)
SetKey(nil, nil)
SetKey(nil, x)
SetKey(key, nil)
Expand Down Expand Up @@ -132,7 +132,7 @@ func TestSetKeys(t *testing.T) {
}
keys := make([]*datastore.Key, len(xs))
for i := range xs {
keys[i] = datastore.IDKey("Test", int64(i), nil)
keys[i] = datastore.IDKey(kind, int64(i), nil)
}
SetKeys(nil, nil)
SetKeys(keys, nil)
Expand Down Expand Up @@ -174,7 +174,7 @@ func TestSetIDs(t *testing.T) {
}
}
}
SetIDs("Test", ids, xs)
SetIDs(kind, ids, xs)
validate()

xs = []interface{}{
Expand All @@ -186,7 +186,7 @@ func TestSetIDs(t *testing.T) {
ExampleNotModel{},
&ExampleNotModel{},
}
SetIDs("Test", ids, &xs)
SetIDs(kind, ids, &xs)
validate()
}

Expand All @@ -213,10 +213,10 @@ func TestSetStringIDs(t *testing.T) {
}
}
}
SetStringIDs("Test", ids, xs)
SetStringIDs(kind, ids, xs)
validate()

SetStringIDs("Test", ids, &xs)
SetStringIDs(kind, ids, &xs)
validate()
}

Expand All @@ -243,9 +243,9 @@ func TestSetNameIDs(t *testing.T) {
}
}
}
SetNameIDs("Test", ids, xs)
SetNameIDs(kind, ids, xs)
validate()

SetNameIDs("Test", ids, &xs)
SetNameIDs(kind, ids, &xs)
validate()
}
4 changes: 2 additions & 2 deletions put_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestPutModel(t *testing.T) {
if err != datastore.ErrInvalidKey {
t.Fatalf("expected error to be %v; got %v", datastore.ErrInvalidKey, err)
}
x.SetID("Test", 99)
x.SetID(kind, 99)
err = client.PutModel(ctx, x)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestPutModels(t *testing.T) {
t.Fatalf("expected error not nil")
}
for i, x := range xs {
x.SetID("Test", int64(i+100))
x.SetID(kind, int64(i+100))
}
err = client.PutModels(ctx, xs)
if err != nil {
Expand Down

0 comments on commit ec16f6e

Please sign in to comment.