github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/endpoints/put_item/put_item_test.go (about)

     1  // Tests JSON formats as described on the AWS docs site. For live tests, see ../../tests
     2  package put_item
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"testing"
     8  )
     9  
    10  func TestNil(t *testing.T) {
    11  	g := NewPutItem()
    12  	_,_,err := g.EndpointReqWithConf(nil)
    13  	if err == nil {
    14  		t.Errorf("nil conf should result in error")
    15  	}
    16  }
    17  
    18  func TestRequestMarshal(t *testing.T) {
    19  	s := []string{
    20  		`{"Expected":{"ForumName":{"AttributeValueList":null,"ComparisonOperator":"","Value":null,"Exists":false},"Subject":{"AttributeValueList":null,"ComparisonOperator":"","Value":null,"Exists":false},"Value":null},"Item":{"ForumName":{"S":"AmazonDynamoDB"},"LastPostDateTime":{"S":"201303190422"},"LastPostedBy":{"S":"fred@example.com"},"Message":{"S":"IwanttoupdatemultipleitemsinasingleAPIcall.What'sthebestwaytodothat?"},"Subject":{"S":"HowdoIupdatemultipleitems?"},"Tags":{"SS":["Update","MultipleItems","HelpMe"]}},"TableName":"Thread"}`,
    21  		`{"TableName":"Thread","Item":{"LastPostDateTime":{"S":"201303190422"},"Tags":{"SS":["Update","MultipleItems","HelpMe"]},"ForumName":{"S":"AmazonDynamoDB"},"Message":{"S":"IwanttoupdatemultipleitemsinasingleAPIcall.What'sthebestwaytodothat?"},"Subject":{"S":"HowdoIupdatemultipleitems?"},"LastPostedBy":{"S":"fred@example.com"}},"ConditionExpression":"ForumName<>:fandSubject<>:s","ExpressionAttributeValues":{":f":{"S":"AmazonDynamoDB"},":s":{"S":"HowdoIupdatemultipleitems?"}}}`}
    22  	for i, v := range s {
    23  		var p PutItem
    24  		um_err := json.Unmarshal([]byte(v), &p)
    25  		if um_err != nil {
    26  			t.Errorf("cannot unmarshal RequestItems:\n" + v + "\n")
    27  		}
    28  		json, jerr := json.Marshal(p)
    29  		if jerr != nil {
    30  			t.Errorf("cannot marshal\n")
    31  		}
    32  		if i == 0 {
    33  			if len(s[i]) != len(string(json)) {
    34  				e := fmt.Sprintf("\n%s\n%s\nshould be same",s[i],string(json))
    35  				t.Errorf(e)
    36  			}
    37  		}
    38  	}
    39  }
    40  
    41  func TestRequestJSONMarshal(t *testing.T) {
    42  	s := []string{`{"TableName":"Thread","Item":{"LastPostDateTime":"201303190422","Tags":["Update","MultipleItems","HelpMe"],"ForumName":"AmazonDynamoDB","Message":"IwanttoupdatemultipleitemsinasingleAPIcall.What'sthebestwaytodothat?","Subject":"HowdoIupdatemultipleitems?","LastPostedBy":"fred@example.com"},"Expected":{"ForumName":{"Exists":false},"Subject":{"Exists":false}}}`, `{"TableName":"Thread","Item":{"LastPostDateTime":"201303190422","Tags":["Update","MultipleItems","HelpMe"],"ForumName":"AmazonDynamoDB","Message":"IwanttoupdatemultipleitemsinasingleAPIcall.What'sthebestwaytodothat?","Subject":"HowdoIupdatemultipleitems?","LastPostedBy":"fred@example.com"},"ConditionExpression":"ForumName<>:fandSubject<>:s","ExpressionAttributeValues":{":f":{"S":"AmazonDynamoDB"},":s":{"S":"HowdoIupdatemultipleitems?"}}}`}
    43  	for _, v := range s {
    44  		var p PutItemJSON
    45  		um_err := json.Unmarshal([]byte(v), &p)
    46  		if um_err != nil {
    47  			t.Errorf("cannot unmarshal RequestItems:\n" + v + "\n")
    48  		}
    49  		json, jerr := json.Marshal(p)
    50  		if jerr != nil {
    51  			t.Errorf("cannot marshal\n")
    52  		}
    53  		// there is some unicode encoding here, so if you want, uncomment this line and
    54  		// do visual qa
    55  		_ = fmt.Sprintf("JSON IN:%v, OUT:%v\n", v, string(json))
    56  	}
    57  }
    58  
    59  func TestResponseMarshal(t *testing.T) {
    60  	s := []string{`{"Attributes":{"LastPostedBy":{"S":"alice@example.com"},"ForumName":{"S":"AmazonDynamoDB"},"LastPostDateTime":{"S":"20130320010350"},"Tags":{"SS":["Update","MultipleItems","HelpMe"]},"Subject":{"S":"Maximumnumberofitems?"},"Views":{"N":"5"},"Message":{"S":"Iwanttoput10milliondataitemstoanAmazonDynamoDBtable.Isthereanupperlimit?"}}}`}
    61  	for i, v := range s {
    62  		var p Response
    63  		um_err := json.Unmarshal([]byte(v), &p)
    64  		if um_err != nil {
    65  			t.Errorf("cannot unmarshal\n" + v + "\n")
    66  		}
    67  		json, jerr := json.Marshal(p)
    68  		if jerr != nil {
    69  			t.Errorf("cannot marshal\n")
    70  		}
    71  		if len(s[i]) != len(string(json)) {
    72  			e := fmt.Sprintf("\n%s\n%s\nshould be same",s[i],string(json))
    73  			t.Errorf(e)
    74  		}
    75  	}
    76  }