github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/scan-livetest.go (about) 1 package main 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "github.com/smugmug/godynamo/conf" 7 "github.com/smugmug/godynamo/conf_file" 8 conf_iam "github.com/smugmug/godynamo/conf_iam" 9 scan "github.com/smugmug/godynamo/endpoints/scan" 10 keepalive "github.com/smugmug/godynamo/keepalive" 11 "github.com/smugmug/godynamo/types/attributevalue" 12 "github.com/smugmug/godynamo/types/condition" 13 "log" 14 "net/http" 15 ) 16 17 func main() { 18 conf_file.Read() 19 conf.Vals.ConfLock.RLock() 20 if conf.Vals.Initialized == false { 21 panic("the conf.Vals global conf struct has not been initialized") 22 } 23 24 // launch a background poller to keep conns to aws alive 25 if conf.Vals.Network.DynamoDB.KeepAlive { 26 log.Printf("launching background keepalive") 27 go keepalive.KeepAlive([]string{}) 28 } 29 30 // deal with iam, or not 31 if conf.Vals.UseIAM { 32 iam_ready_chan := make(chan bool) 33 go conf_iam.GoIAM(iam_ready_chan) 34 _ = <-iam_ready_chan 35 } 36 conf.Vals.ConfLock.RUnlock() 37 38 s := scan.NewScan() 39 tn := "test-godynamo-livetest" 40 s.TableName = tn 41 k_v1 := fmt.Sprintf("AHashKey%d", 100) 42 43 kc := condition.NewCondition() 44 kc.AttributeValueList = make([]*attributevalue.AttributeValue, 1) 45 kc.AttributeValueList[0] = &attributevalue.AttributeValue{S: k_v1} 46 kc.ComparisonOperator = scan.OP_EQ 47 48 s.ScanFilter["TheHashKey"] = kc 49 jsonstr, _ := json.Marshal(s) 50 fmt.Printf("JSON:%s\n", string(jsonstr)) 51 body, code, err := s.EndpointReq() 52 if err != nil || code != http.StatusOK { 53 fmt.Printf("scan failed %d %v %s\n", code, err, body) 54 } 55 fmt.Printf("%v\n%v\n%v\n", string(body), code, err) 56 57 var r scan.Response 58 um_err := json.Unmarshal([]byte(body), &r) 59 if um_err != nil { 60 e := fmt.Sprintf("unmarshal Response: %v", um_err) 61 fmt.Printf("%s\n", e) 62 } 63 64 s = scan.NewScan() 65 s.TableName = tn 66 jsonstr, _ = json.Marshal(s) 67 fmt.Printf("JSON:%s\n", string(jsonstr)) 68 body, code, err = s.EndpointReq() 69 if err != nil || code != http.StatusOK { 70 fmt.Printf("scan failed %d %v %s\n", code, err, body) 71 } 72 fmt.Printf("%v\n%v\n%v\n", string(body), code, err) 73 74 um_err = json.Unmarshal([]byte(body), &r) 75 if um_err != nil { 76 e := fmt.Sprintf("unmarshal Response: %v", um_err) 77 fmt.Printf("%s\n", e) 78 } 79 80 kc = condition.NewCondition() 81 kc.AttributeValueList = make([]*attributevalue.AttributeValue, 2) 82 kc.AttributeValueList[0] = &attributevalue.AttributeValue{N: "270"} 83 kc.AttributeValueList[1] = &attributevalue.AttributeValue{N: "290"} 84 kc.ComparisonOperator = scan.OP_BETWEEN 85 s.ScanFilter["SomeValue"] = kc 86 87 jsonstr, _ = json.Marshal(s) 88 fmt.Printf("JSON:%s\n", string(jsonstr)) 89 body, code, err = s.EndpointReq() 90 fmt.Printf("%v\n%v\n%v\n", string(body), code, err) 91 um_err = json.Unmarshal([]byte(body), &r) 92 if um_err != nil { 93 e := fmt.Sprintf("unmarshal Response: %v", um_err) 94 fmt.Printf("%s\n", e) 95 } 96 k_v2 := fmt.Sprintf("AHashKey%d", 290) 97 r_v2 := fmt.Sprintf("%d", 290) 98 s.ExclusiveStartKey["TheHashKey"] = &attributevalue.AttributeValue{S: k_v2} 99 s.ExclusiveStartKey["TheRangeKey"] = &attributevalue.AttributeValue{N: r_v2} 100 jsonstr, _ = json.Marshal(s) 101 fmt.Printf("JSON:%s\n", string(jsonstr)) 102 body, code, err = s.EndpointReq() 103 if err != nil || code != http.StatusOK { 104 fmt.Printf("scan failed %d %v %s\n", code, err, body) 105 } 106 fmt.Printf("%v\n%v\n%v\n", string(body), code, err) 107 108 um_err = json.Unmarshal([]byte(body), &r) 109 if um_err != nil { 110 e := fmt.Sprintf("unmarshal Response: %v", um_err) 111 fmt.Printf("%s\n", e) 112 } 113 }