github.com/simpleiot/simpleiot@v0.18.3/data/config_test.go (about)

     1  package data
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/goccy/go-yaml"
     9  )
    10  
    11  var configTestNode = NodeEdgeChildren{
    12  	NodeEdge: NodeEdge{
    13  		ID:     "123",
    14  		Parent: "456",
    15  		Type:   "testType",
    16  		Points: []Point{
    17  			{Type: "description", Text: "test type"},
    18  			{Type: "count", Value: 120},
    19  			{Type: "value", Value: 15.43},
    20  			{Type: "value2", Value: 10},
    21  		},
    22  		EdgePoints: []Point{
    23  			{Type: "role", Text: "admin"},
    24  			{Type: "tombstone", Value: 1},
    25  		},
    26  	},
    27  }
    28  
    29  var configTestNodeYAML = `
    30  nodes:
    31    - type: testType
    32      parent: 456
    33      id: 123
    34      points:
    35        - {type: description, text: "test type"}
    36        - {type: count, value: 120}
    37        - {type: value, value: 15.43}
    38        - {type: value2, value: 10}
    39      edgePoints:
    40        - {type: role, text: admin}
    41        - {type: tombstone, value: 1}
    42  `
    43  
    44  type configImport struct {
    45  	Nodes []NodeEdgeChildren
    46  }
    47  
    48  func TestConfigImport(t *testing.T) {
    49  	var res configImport
    50  
    51  	err := yaml.Unmarshal([]byte(configTestNodeYAML), &res)
    52  	if err != nil {
    53  		t.Fatal("unmarshal failed: ", err)
    54  	}
    55  
    56  	if !reflect.DeepEqual(res.Nodes[0], configTestNode) {
    57  		fmt.Printf("res: %+v\n", res)
    58  		t.Fatal("Did not get expected result")
    59  	}
    60  }
    61  
    62  var configTestNodeChildren = NodeEdgeChildren{
    63  	NodeEdge{
    64  		ID:     "123",
    65  		Parent: "456",
    66  		Type:   "testType",
    67  	},
    68  	[]NodeEdgeChildren{
    69  		{NodeEdge{
    70  			ID:     "abc",
    71  			Parent: "123",
    72  			Type:   "testY",
    73  			Points: []Point{
    74  				{Type: "description", Text: "test Y1", Key: "2"},
    75  			},
    76  			EdgePoints: []Point{
    77  				{Type: "role", Text: "user"},
    78  			},
    79  		},
    80  			[]NodeEdgeChildren{
    81  				{NodeEdge{
    82  					ID:     "jkl",
    83  					Parent: "abc",
    84  					Type:   "testY",
    85  					Points: []Point{
    86  						{Type: "description", Text: "test Y2"},
    87  					},
    88  				}, nil},
    89  			},
    90  		},
    91  	},
    92  }
    93  
    94  var configTestNodeChildrenYAML = `
    95  nodes:
    96    - type: testType
    97      id: 123
    98      parent: 456
    99      children:
   100        - id: "abc"
   101          parent: "123"
   102          type: "testY"
   103          points:
   104            - {type: description, text: "test Y1", key: "2"}
   105          edgePoints:
   106            - {type: role, text: user}
   107          children:
   108            - id: "jkl"
   109              parent: "abc"
   110              type: "testY"
   111              points:
   112                - {type: description, text: "test Y2"}
   113  `
   114  
   115  func TestConfigChildrenImport(t *testing.T) {
   116  	var res configImport
   117  
   118  	err := yaml.Unmarshal([]byte(configTestNodeChildrenYAML), &res)
   119  	if err != nil {
   120  		t.Fatal("unmarshal failed: ", err)
   121  	}
   122  
   123  	if !reflect.DeepEqual(res.Nodes[0], configTestNodeChildren) {
   124  		fmt.Printf("res: %+v\n", res)
   125  		t.Fatal("Did not get expected result")
   126  	}
   127  }