github.com/smugmug/godynamo@v0.0.0-20151122084750-7913028f6623/types/item/item_test.go (about)

     1  package item
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"testing"
     7  )
     8  
     9  // Roundtrip some examples
    10  func TestItemMarshal(t *testing.T) {
    11  	s := []string{
    12  		`{"ItemName":{"S":"a string"}}`,
    13  		`{"ItemName":{"B":"aGkgdGhlcmUK"}}`,
    14  		`{"ItemName":{"N":"5"}}`,
    15  		`{"ItemName":{"BOOL":true}}`,
    16  		`{"ItemName":{"NULL":true}}`,
    17  		`{"ItemName":{"SS":["a","b","c"]}}`,
    18  		`{"ItemName":{"BS":["aGkgdGhlcmUK"]}}`,
    19  		`{"ItemName":{"NS":["42","1","0"]}}`,
    20  		`{"ItemName":{"L":[{"S":"a string"},{"L":[{"S":"another string"}]}]}}`,
    21  		`{"ItemName":{"M":{"key1":{"S":"a string"},"key2":{"L":[{"S":"a string"},{"L":[{"S":"another string"}]}]}}}}`,
    22  	}
    23  	for i, v := range s {
    24  		var a Item
    25  		um_err := json.Unmarshal([]byte(v), &a)
    26  		if um_err != nil {
    27  			t.Errorf("cannot unmarshal\n")
    28  		}
    29  
    30  		json, jerr := json.Marshal(a)
    31  		if jerr != nil {
    32  			t.Errorf("cannot marshal\n")
    33  		}
    34  		if len(s[i]) != len(string(json)) {
    35  			e := fmt.Sprintf("\n%s\n%s\nshould be same",s[i],string(json))
    36  			t.Errorf(e)
    37  		}
    38  		l := len(a)
    39  		ac := NewItem()
    40  		a.Copy(ac)
    41  		lc := len(ac)
    42  		if l != lc {
    43  			e := fmt.Sprintf("lengths differ: %d %d",l,lc)
    44  			t.Errorf(e)
    45  		}
    46  		fmt.Printf("%d %d\n",l,lc)
    47  		delete(a,"ItemName")
    48  
    49  		lc = len(ac)
    50  		if l != lc {
    51  			e := fmt.Sprintf("lengths differ: %d %d",l,lc)
    52  			t.Errorf(e)
    53  		}
    54  
    55  	}
    56  }