github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/item_operations-livetest.go (about) 1 package main 2 3 import ( 4 "encoding/base64" 5 "encoding/json" 6 "fmt" 7 "github.com/smugmug/godynamo/conf" 8 "github.com/smugmug/godynamo/conf_file" 9 conf_iam "github.com/smugmug/godynamo/conf_iam" 10 delete_item "github.com/smugmug/godynamo/endpoints/delete_item" 11 get_item "github.com/smugmug/godynamo/endpoints/get_item" 12 put_item "github.com/smugmug/godynamo/endpoints/put_item" 13 update_item "github.com/smugmug/godynamo/endpoints/update_item" 14 keepalive "github.com/smugmug/godynamo/keepalive" 15 "github.com/smugmug/godynamo/types/attributevalue" 16 "log" 17 "net/http" 18 "os" 19 ) 20 21 // this test program runs a bunch of item-oriented operations. 22 23 func main() { 24 conf_file.Read() 25 conf.Vals.ConfLock.RLock() 26 if conf.Vals.Initialized == false { 27 panic("the conf.Vals global conf struct has not been initialized") 28 } 29 30 // launch a background poller to keep conns to aws alive 31 if conf.Vals.Network.DynamoDB.KeepAlive { 32 log.Printf("launching background keepalive") 33 go keepalive.KeepAlive([]string{}) 34 } 35 36 // deal with iam, or not 37 if conf.Vals.UseIAM { 38 iam_ready_chan := make(chan bool) 39 go conf_iam.GoIAM(iam_ready_chan) 40 _ = <-iam_ready_chan 41 } 42 conf.Vals.ConfLock.RUnlock() 43 44 tn := "test-godynamo-livetest" 45 tablename1 := tn 46 fmt.Printf("tablename1: %s\n", tablename1) 47 48 var code int 49 var err error 50 var body []byte 51 52 // INSERT SINGLE ITEM 53 hk := "a-hash-key" 54 rk := "1" 55 put1 := put_item.NewPutItem() 56 put1.TableName = tablename1 57 av1 := attributevalue.NewAttributeValue() 58 av2 := attributevalue.NewAttributeValue() 59 av3 := attributevalue.NewAttributeValue() 60 av4 := attributevalue.NewAttributeValue() 61 av5 := attributevalue.NewAttributeValue() 62 av6 := attributevalue.NewAttributeValue() 63 av7 := attributevalue.NewAttributeValue() 64 av1.S = hk 65 av2.N = rk 66 av3.InsertSS("pk1_a") 67 av3.InsertSS("pk1_c") 68 av4.InsertNS("1") 69 av4.InsertNS_float64(2) 70 av4.InsertNS("3") 71 av4.InsertNS("-7.2432342") 72 av5.N = "1" 73 av6.B = base64.StdEncoding.EncodeToString([]byte("hello")) 74 av7.InsertBS(base64.StdEncoding.EncodeToString([]byte("hello"))) 75 av7.InsertBS(base64.StdEncoding.EncodeToString([]byte("there"))) 76 put1.Item["TheHashKey"] = av1 77 put1.Item["TheRangeKey"] = av2 78 put1.Item["stringlist"] = av3 79 put1.Item["numlist"] = av4 80 put1.Item["num"] = av5 81 put1.Item["byte"] = av6 82 put1.Item["bytelist"] = av7 83 84 body, code, err = put1.EndpointReq() 85 if err != nil || code != http.StatusOK { 86 fmt.Printf("put1 failed %d %v %s\n", code, err, string(body)) 87 os.Exit(1) 88 } 89 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 90 91 // GET THAT ITEM 92 get1 := get_item.NewGetItem() 93 get1.TableName = tablename1 94 get1.Key["TheHashKey"] = av1 95 get1.Key["TheRangeKey"] = av2 96 97 get_json, get_json_err := json.Marshal(get1) 98 if get_json_err != nil { 99 fmt.Printf("%v\n", get_json_err) 100 os.Exit(1) 101 } 102 fmt.Printf("%s\n", string(get_json)) 103 104 body, code, err = get1.EndpointReq() 105 if err != nil || code != http.StatusOK { 106 fmt.Printf("get failed %d %v %s\n", code, err, string(body)) 107 os.Exit(1) 108 } 109 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 110 111 var gr get_item.Response 112 um_err := json.Unmarshal([]byte(body), &gr) 113 if um_err != nil { 114 fmt.Printf("get resp unmarshal failed %s\n", um_err.Error()) 115 os.Exit(1) 116 } 117 fmt.Printf("%v\n", string(body)) 118 119 // UPDATE THAT ITEM 120 up1 := update_item.NewUpdateItem() 121 new_attr_val := "new string here" 122 up1.TableName = tablename1 123 up1.Key["TheHashKey"] = av1 124 up1.Key["TheRangeKey"] = av2 125 126 up1.AttributeUpdates = attributevalue.NewAttributeValueUpdateMap() 127 up_avu := attributevalue.NewAttributeValueUpdate() 128 up_avu.Action = update_item.ACTION_PUT 129 up_avu.Value = &attributevalue.AttributeValue{S: new_attr_val} 130 up1.AttributeUpdates["new_string"] = up_avu 131 132 del_avu := attributevalue.NewAttributeValueUpdate() 133 del_avu.Action = update_item.ACTION_DEL 134 del_avu.Value = attributevalue.NewAttributeValue() 135 del_avu.Value.InsertSS("pk1_a") 136 up1.AttributeUpdates["stringlist"] = del_avu 137 138 del2_avu := attributevalue.NewAttributeValueUpdate() 139 del2_avu.Action = update_item.ACTION_DEL 140 del2_avu.Value = &attributevalue.AttributeValue{} 141 up1.AttributeUpdates["byte"] = del2_avu 142 143 add_avu := attributevalue.NewAttributeValueUpdate() 144 add_avu.Action = update_item.ACTION_ADD 145 add_avu.Value = &attributevalue.AttributeValue{N: "4"} 146 up1.AttributeUpdates["num"] = add_avu 147 148 up1.ReturnValues = update_item.RETVAL_ALL_NEW 149 150 update_item_json, update_item_json_err := json.Marshal(up1) 151 if update_item_json_err != nil { 152 fmt.Printf("%v\n", update_item_json_err) 153 os.Exit(1) 154 } 155 fmt.Printf("%s\n", string(update_item_json)) 156 157 body, code, err = up1.EndpointReq() 158 if err != nil || code != http.StatusOK { 159 fmt.Printf("update item failed %d %v %s\n", code, err, string(body)) 160 os.Exit(1) 161 } 162 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 163 164 var ur update_item.Response 165 um_err = json.Unmarshal([]byte(body), &ur) 166 if um_err != nil { 167 fmt.Printf("update resp unmarshal failed %s\n", um_err.Error()) 168 os.Exit(1) 169 } 170 171 // GET IT AGAIN 172 body, code, err = get1.EndpointReq() 173 if err != nil || code != http.StatusOK { 174 fmt.Printf("get failed %d %v %s\n", code, err, string(body)) 175 os.Exit(1) 176 } 177 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 178 179 // DELETE THE ITEM 180 del1 := delete_item.NewDeleteItem() 181 del1.TableName = tablename1 182 del1.Key["TheHashKey"] = av1 183 del1.Key["TheRangeKey"] = av2 184 185 del1.ReturnValues = delete_item.RETVAL_ALL_OLD 186 body, code, err = del1.EndpointReq() 187 if err != nil || code != http.StatusOK { 188 fmt.Printf("delete item failed %d %v %s\n", code, err, string(body)) 189 os.Exit(1) 190 } 191 fmt.Printf("%v\n%v\n,%v\n", string(body), code, err) 192 193 fmt.Printf("PASSED\n") 194 }