github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/tests/scan-param-conf-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  	scan "github.com/smugmug/godynamo/endpoints/scan"
     9  	"github.com/smugmug/godynamo/types/attributevalue"
    10  	"github.com/smugmug/godynamo/types/condition"
    11  	"net/http"
    12  	"os"
    13  )
    14  
    15  func main() {
    16  
    17  	home := os.Getenv("HOME")
    18  	home_conf_file := home + string(os.PathSeparator) + "." + conf.CONF_NAME
    19  	home_conf, home_conf_err := conf_file.ReadConfFile(home_conf_file)
    20  	if home_conf_err != nil {
    21  		panic("cannot read conf from " + home_conf_file)
    22  	}
    23  	home_conf.ConfLock.RLock()
    24  	if home_conf.Initialized == false {
    25  		panic("conf struct has not been initialized")
    26  	}
    27  
    28  	s := scan.NewScan()
    29  	tn := "test-godynamo-livetest"
    30  	s.TableName = tn
    31  	k_v1 := fmt.Sprintf("AHashKey%d", 100)
    32  
    33  	kc := condition.NewCondition()
    34  	kc.AttributeValueList = make([]*attributevalue.AttributeValue, 1)
    35  	kc.AttributeValueList[0] = &attributevalue.AttributeValue{S: k_v1}
    36  	kc.ComparisonOperator = scan.OP_EQ
    37  
    38  	s.ScanFilter["TheHashKey"] = kc
    39  	jsonstr, _ := json.Marshal(s)
    40  	fmt.Printf("JSON:%s\n", string(jsonstr))
    41  	body, code, err := s.EndpointReqWithConf(home_conf)
    42  	if err != nil || code != http.StatusOK {
    43  		fmt.Printf("scan failed %d %v %s\n", code, err, body)
    44  	}
    45  	fmt.Printf("%v\n%v\n%v\n", string(body), code, err)
    46  
    47  	var r scan.Response
    48  	um_err := json.Unmarshal([]byte(body), &r)
    49  	if um_err != nil {
    50  		e := fmt.Sprintf("unmarshal Response: %v", um_err)
    51  		fmt.Printf("%s\n", e)
    52  	}
    53  
    54  	s = scan.NewScan()
    55  	s.TableName = tn
    56  	jsonstr, _ = json.Marshal(s)
    57  	fmt.Printf("JSON:%s\n", string(jsonstr))
    58  	body, code, err = s.EndpointReqWithConf(home_conf)
    59  	if err != nil || code != http.StatusOK {
    60  		fmt.Printf("scan failed %d %v %s\n", code, err, body)
    61  	}
    62  	fmt.Printf("%v\n%v\n%v\n", string(body), code, err)
    63  
    64  	um_err = json.Unmarshal([]byte(body), &r)
    65  	if um_err != nil {
    66  		e := fmt.Sprintf("unmarshal Response: %v", um_err)
    67  		fmt.Printf("%s\n", e)
    68  	}
    69  
    70  	kc = condition.NewCondition()
    71  	kc.AttributeValueList = make([]*attributevalue.AttributeValue, 2)
    72  	kc.AttributeValueList[0] = &attributevalue.AttributeValue{N: "270"}
    73  	kc.AttributeValueList[1] = &attributevalue.AttributeValue{N: "290"}
    74  	kc.ComparisonOperator = scan.OP_BETWEEN
    75  	s.ScanFilter["SomeValue"] = kc
    76  
    77  	jsonstr, _ = json.Marshal(s)
    78  	fmt.Printf("JSON:%s\n", string(jsonstr))
    79  	body, code, err = s.EndpointReqWithConf(home_conf)
    80  	fmt.Printf("%v\n%v\n%v\n", string(body), code, err)
    81  	um_err = json.Unmarshal([]byte(body), &r)
    82  	if um_err != nil {
    83  		e := fmt.Sprintf("unmarshal Response: %v", um_err)
    84  		fmt.Printf("%s\n", e)
    85  	}
    86  	k_v2 := fmt.Sprintf("AHashKey%d", 290)
    87  	r_v2 := fmt.Sprintf("%d", 290)
    88  	s.ExclusiveStartKey["TheHashKey"] = &attributevalue.AttributeValue{S: k_v2}
    89  	s.ExclusiveStartKey["TheRangeKey"] = &attributevalue.AttributeValue{N: r_v2}
    90  	jsonstr, _ = json.Marshal(s)
    91  	fmt.Printf("JSON:%s\n", string(jsonstr))
    92  	body, code, err = s.EndpointReqWithConf(home_conf)
    93  	if err != nil || code != http.StatusOK {
    94  		fmt.Printf("scan failed %d %v %s\n", code, err, body)
    95  	}
    96  	fmt.Printf("%v\n%v\n%v\n", string(body), code, err)
    97  
    98  	um_err = json.Unmarshal([]byte(body), &r)
    99  	if um_err != nil {
   100  		e := fmt.Sprintf("unmarshal Response: %v", um_err)
   101  		fmt.Printf("%s\n", e)
   102  	}
   103  }