github.com/CiscoM31/godata@v1.0.10/response_model_test.go (about)

     1  package godata
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  )
     7  
     8  type testResponseJson struct {
     9  	ODataContext string              `json:"@odata.context"`
    10  	ODataCount   int                 `json:"@odata.count"`
    11  	Value        []testResponseValue `json:"value"`
    12  }
    13  
    14  type testResponseValue struct {
    15  	Name   string
    16  	Age    float64
    17  	Gender string
    18  }
    19  
    20  func TestResponseWriter(t *testing.T) {
    21  
    22  	test := &GoDataResponse{
    23  		Fields: map[string]*GoDataResponseField{
    24  			"@odata.context": {
    25  				Value: "http://service.example",
    26  			},
    27  			"@odata.count": {
    28  				Value: 8,
    29  			},
    30  			"value": {
    31  				Value: []*GoDataResponseField{
    32  					{
    33  						Value: map[string]*GoDataResponseField{
    34  							"Name": {Value: "John Doe"},
    35  							"Age":  {11.01},
    36  							"Male": {Value: "Female"},
    37  						},
    38  					},
    39  					{
    40  						Value: map[string]*GoDataResponseField{
    41  							"Name":   {Value: "Jane \"Cool\" Doe"},
    42  							"Age":    {12.1},
    43  							"Gender": {Value: "Female"},
    44  						},
    45  					},
    46  				},
    47  			},
    48  		},
    49  	}
    50  
    51  	written, err := test.Json()
    52  
    53  	if err != nil {
    54  		t.Error(err)
    55  		return
    56  	}
    57  
    58  	var result testResponseJson
    59  	err = json.Unmarshal(written, &result)
    60  
    61  	if err != nil {
    62  		t.Error(err)
    63  		return
    64  	}
    65  
    66  	if result.ODataContext != "http://service.example" {
    67  		t.Error("@odata.context is", result.ODataContext)
    68  		return
    69  	}
    70  
    71  	if result.ODataCount != 8 {
    72  		t.Error("@odata.count is", result.ODataCount)
    73  		return
    74  	}
    75  
    76  	if len(result.Value) != 2 {
    77  		t.Error("Result value is not length 2")
    78  		return
    79  	}
    80  
    81  	if result.Value[0].Name != "John Doe" {
    82  		t.Error("First value name is", result.Value[0].Name)
    83  		return
    84  	}
    85  
    86  	if result.Value[1].Name != "Jane \"Cool\" Doe" {
    87  		t.Error("Second value name is", result.Value[1].Name)
    88  		return
    89  	}
    90  
    91  }