forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_test.go
72 lines (58 loc) · 1.35 KB
/
db_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2016 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package bookshelf
import (
"fmt"
"testing"
"time"
"golang.org/x/net/context"
"google.golang.org/cloud/datastore"
"github.com/GoogleCloudPlatform/golang-samples/internal/testutil"
)
func testDB(t *testing.T, db BookDatabase) {
b := &Book{
Author: "testy mc testface",
Title: fmt.Sprintf("t-%d", time.Now().Unix()),
Description: "desc",
}
id, err := db.AddBook(b)
if err != nil {
t.Fatal(err)
}
b.ID = id
b.Description = "newdesc"
if err := db.UpdateBook(b); err != nil {
t.Error(err)
}
gotBook, err := db.GetBook(id)
if err != nil {
t.Error(err)
}
if got, want := gotBook.Description, b.Description; got != want {
t.Errorf("Update description: got %q, want %q", got, want)
}
if err := db.DeleteBook(id); err != nil {
t.Error(err)
}
if _, err := db.GetBook(id); err == nil {
t.Error("want non-nil err")
}
}
func TestMemoryDB(t *testing.T) {
testDB(t, newMemoryDB())
}
func TestDatastoreDB(t *testing.T) {
tc := testutil.SystemTest(t)
ctx := context.Background()
client, err := datastore.NewClient(ctx, tc.ProjectID)
if err != nil {
t.Fatal(err)
}
defer client.Close()
db, err := newDatastoreDB(client)
if err != nil {
t.Fatal(err)
}
testDB(t, db)
}