forked from tav/dynamodb
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdynamodb_test.go
73 lines (68 loc) · 1.74 KB
/
dynamodb_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
73
package dynamodb
import (
"log"
"testing"
)
type MyItem struct {
Name string `ddb:"MyItem2,HASH"`
Weight int
Height int
}
func TestDynamo(t *testing.T) {
auth := Auth("your-access-key", "your-secret-key")
endpoint := EndPoint("DynamoDB Local", "home", "localhost:8000", false)
dbClient := Dial(endpoint, auth, nil)
dbClient.DeleteTable("Test")
_, err := dbClient.CreateTable("Test", &MyItem{}, 10, 10, nil, nil)
if err != nil {
log.Printf("%v", err)
}
testT, _ := dbClient.DescribeTable("Test")
if testT.TableStatus != "ACTIVE" {
log.Fatal("Error creating table")
}
table := dbClient.Table("Test")
item := MyItem{Name: "Tom", Weight: 80, Height: 179}
table.Put(&item)
newI := MyItem{Name: "Tom"}
table.Get(&newI, true)
if newI.Weight != 80 {
log.Fatal("Failed to put or get the item")
}
delI := MyItem{Name: "Tom"}
err = table.Delete(&delI)
if err != nil {
log.Fatal("Failed to delete the item")
}
newItem := MyItem{Name: "Tom"}
err = table.Get(&newItem, true)
log.Printf("Item: %v", newItem)
log.Printf("Error: %v", err)
if err == nil {
log.Fatal("Item still present")
}
err = table.Add(&item)
if err != nil {
log.Fatal("Can't add the item")
}
err = table.Add(&item)
if err == nil {
log.Fatal("Shouldn't be able to add two items with the same primary key")
}
err = table.PutIf(&item, &item)
if err != nil {
log.Fatal("putif failed although nothing changed")
}
item2 := item
item2.Height = 180
err = table.PutIf(&item2, &item2)
if err == nil {
// should throw an error because height is currently 179
err = table.PutIf(&item2, &item2)
}
err = table.PutIf(&item2, &item)
if err != nil {
// should not throw an error because we expect height to be 179
log.Fatal("putif should not have thrown an error")
}
}