github.com/Foodji/aws-lambda-go@v1.20.2/events/test/assert.go (about)

     1  package test
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func AssertJsonFile(t *testing.T, file string, o interface{}) {
    12  	inputJSON, err := ioutil.ReadFile(file)
    13  	if err != nil {
    14  		t.Errorf("could not open test file. details: %v", err)
    15  	}
    16  	AssertJsonBytes(t, inputJSON, o)
    17  }
    18  
    19  func AssertJsonBytes(t *testing.T, inputJSON []byte, o interface{}) {
    20  	// de-serialize
    21  	if err := json.Unmarshal(inputJSON, o); err != nil {
    22  		t.Errorf("could not unmarshal event. details: %v", err)
    23  	}
    24  
    25  	// serialize to json
    26  	outputJSON, err := json.Marshal(o)
    27  	if err != nil {
    28  		t.Errorf("could not marshal event. details: %v", err)
    29  	}
    30  
    31  	assert.JSONEq(t, string(inputJSON), string(outputJSON))
    32  }