github.com/mantzas/incata@v0.3.0/marshal/json_marshaller_test.go (about)

     1  package marshal_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	. "github.com/mantzas/incata/marshal"
     8  	. "github.com/mantzas/incata/mocks"
     9  	. "github.com/onsi/ginkgo"
    10  	. "github.com/onsi/gomega"
    11  )
    12  
    13  var _ = Describe("Marshal", func() {
    14  
    15  	It("serialize test data to json and match", func() {
    16  		expectedString := `{"version":1,"name":"Joe","balance":12.99,"birth_date":"2015-12-13T23:59:59+02:00"}`
    17  
    18  		location, _ := time.LoadLocation("Europe/Athens")
    19  
    20  		testData := TestData{
    21  			Version:   1,
    22  			Name:      "Joe",
    23  			Balance:   12.99,
    24  			BirthDate: time.Date(2015, 12, 13, 23, 59, 59, 0, location),
    25  		}
    26  
    27  		serializedString, err := NewJSONMarshaller().Serialize(testData)
    28  
    29  		Expect(serializedString).To(Equal(expectedString))
    30  		Expect(err).NotTo(HaveOccurred())
    31  	})
    32  
    33  	It("serialize unsupported data type fails", func() {
    34  		var m = make(map[int]int, 0)
    35  		_, err := NewJSONMarshaller().Serialize(m)
    36  
    37  		Expect(err).To(HaveOccurred())
    38  	})
    39  
    40  	It("deserialize json to test data and match", func() {
    41  		location, _ := time.LoadLocation("Europe/Athens")
    42  
    43  		expected := TestData{
    44  			Version:   1,
    45  			Name:      "Joe",
    46  			Balance:   12.99,
    47  			BirthDate: time.Date(2015, 12, 13, 23, 59, 59, 0, location),
    48  		}
    49  
    50  		actualData := `{"version":1,"name":"Joe","balance":12.99,"birth_date":"2015-12-13T23:59:59+02:00"}`
    51  		var actual TestData
    52  
    53  		err := NewJSONMarshaller().Deserialize(actualData, &actual)
    54  
    55  		Expect(actual.Balance).To(Equal(expected.Balance))
    56  		Expect(actual.BirthDate.Equal(expected.BirthDate)).To(BeTrue())
    57  		Expect(actual.Name).To(Equal(expected.Name))
    58  		Expect(actual.Version).To(Equal(expected.Version))
    59  		Expect(err).NotTo(HaveOccurred())
    60  	})
    61  
    62  	It("deserialize fails due to invalid json", func() {
    63  
    64  		var actual TestData
    65  		err := NewJSONMarshaller().Deserialize(`{"version":1,"name":"Joe","balance":12.99,"birth_date":"2015-12-13T23:59:59+02:00------"}`, &actual)
    66  		Expect(err).To(HaveOccurred())
    67  	})
    68  
    69  	It("deserialize wrong to the struct", func() {
    70  
    71  		var actual TestData
    72  		err := NewJSONMarshaller().Deserialize(123, &actual)
    73  		Expect(err).To(HaveOccurred())
    74  	})
    75  })
    76  
    77  func BenchmarkJSONSerializer(b *testing.B) {
    78  
    79  	var sert = NewJSONMarshaller()
    80  
    81  	location, err := time.LoadLocation("Europe/Athens")
    82  
    83  	if err != nil {
    84  		b.Fatalf("Error getting location!")
    85  	}
    86  
    87  	testData := TestData{
    88  		Version:   1,
    89  		Name:      "Joe",
    90  		Balance:   12.99,
    91  		BirthDate: time.Date(2015, 12, 13, 23, 59, 59, 0, location),
    92  	}
    93  
    94  	for n := 0; n < b.N; n++ {
    95  		sert.Serialize(testData)
    96  	}
    97  }