github.com/simpleiot/simpleiot@v0.18.3/node/node_test.go (about)

     1  package node
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/simpleiot/simpleiot/data"
     8  )
     9  
    10  func TestNotifyTemplate(t *testing.T) {
    11  	device := data.Node{
    12  		ID: "1234",
    13  		Points: []data.Point{
    14  			{
    15  				Type: data.PointTypeDescription,
    16  				Text: "My Node",
    17  				Key:  "0",
    18  			},
    19  			{
    20  				Type:  "tankLevel",
    21  				Key:   "0",
    22  				Value: 12.523423423,
    23  			},
    24  			{
    25  				Type:  "current",
    26  				Key:   "c0",
    27  				Value: 1.52323,
    28  			},
    29  		},
    30  	}
    31  
    32  	res, err := renderNotifyTemplate(&device, `Alarm from {{.Description}}, tank level is {{printf "%.2f" (index .Ios "tankLevel")}}.`)
    33  
    34  	if err != nil {
    35  		t.Error("render failed: ", err)
    36  	}
    37  
    38  	if res != "Alarm from My Node, tank level is 12.52." {
    39  		t.Error("rendered text is not correct: ", res)
    40  	}
    41  
    42  	fmt.Println("render result: ", res)
    43  }