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

     1  package data
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestMergePoints(t *testing.T) {
     9  	out := testTypeData
    10  
    11  	modifiedDescription := "test type modified"
    12  
    13  	mods := []Point{
    14  		{Type: "description", Text: modifiedDescription},
    15  	}
    16  
    17  	err := MergePoints(out.ID, mods, &out)
    18  
    19  	if err != nil {
    20  		t.Fatal("Merge error: ", err)
    21  	}
    22  
    23  	if out.Description != modifiedDescription {
    24  		t.Errorf("Description not modified, exp: %v, got: %v", modifiedDescription,
    25  			out.Description)
    26  	}
    27  
    28  	// make sure other points did not get reset
    29  }
    30  
    31  func TestMergeEdgePoints(t *testing.T) {
    32  	out := testTypeData
    33  
    34  	modifiedRole := "user"
    35  
    36  	mods := []Point{
    37  		{Type: "role", Text: modifiedRole},
    38  	}
    39  
    40  	err := MergeEdgePoints(out.ID, out.Parent, mods, &out)
    41  
    42  	if err != nil {
    43  		t.Fatal("Merge error: ", err)
    44  	}
    45  
    46  	if out.Role != modifiedRole {
    47  		t.Errorf("role not modified, exp: %v, got: %v", modifiedRole,
    48  			out.Role)
    49  	}
    50  }
    51  
    52  func TestMergeChildPoints(t *testing.T) {
    53  	testData := testX{
    54  		ID:          "ID-testX",
    55  		Parent:      "ID-parent",
    56  		Description: "test X node",
    57  		TestYs: []testY{
    58  			{ID: "ID-testY",
    59  				Parent:      "ID-testX",
    60  				Description: "test Y node",
    61  				Count:       3,
    62  				Role:        "",
    63  				TestZs: []testZ{
    64  					{
    65  						ID:          "ID-testZ",
    66  						Parent:      "ID-testY",
    67  						Description: "test Z node",
    68  						Count:       23,
    69  						Role:        "peon",
    70  					},
    71  				},
    72  			},
    73  		},
    74  	}
    75  
    76  	modifiedDescription := "test type modified"
    77  
    78  	mods := []Point{
    79  		{Type: "description", Text: modifiedDescription},
    80  	}
    81  
    82  	err := MergePoints("ID-testY", mods, &testData)
    83  
    84  	if err != nil {
    85  		t.Fatal("Merge error: ", err)
    86  	}
    87  
    88  	if testData.TestYs[0].Description != modifiedDescription {
    89  		t.Errorf("Description not modified, exp: %v, got: %v", modifiedDescription,
    90  			testData.TestYs[0].Description)
    91  	}
    92  
    93  	// make sure other points did not get reset
    94  	if testData.TestYs[0].Count != 3 {
    95  		t.Errorf("Merge reset other data")
    96  	}
    97  
    98  	if testData.Description != "test X node" {
    99  		t.Errorf("Top level node description modified when it should not have")
   100  	}
   101  
   102  	// modify description of Z point
   103  	modifiedDescription = "test Z type modified"
   104  
   105  	mods = []Point{
   106  		{Type: "description", Text: modifiedDescription},
   107  	}
   108  
   109  	err = MergePoints("ID-testZ", mods, &testData)
   110  	if err != nil {
   111  		t.Fatal("Merge error: ", err)
   112  	}
   113  
   114  	if testData.TestYs[0].TestZs[0].Description != modifiedDescription {
   115  		t.Errorf("Description not modified, exp: %v, got: %v", modifiedDescription,
   116  			testData.TestYs[0].TestZs[0].Description)
   117  	}
   118  
   119  	// Test edge modifications
   120  	modifiedRole := "yrole"
   121  
   122  	mods = []Point{
   123  		{Type: "role", Text: modifiedRole},
   124  	}
   125  
   126  	err = MergeEdgePoints("ID-testZ", "ID-testY", mods, &testData)
   127  	if err != nil {
   128  		t.Fatal("Merge error: ", err)
   129  	}
   130  
   131  	if testData.TestYs[0].TestZs[0].Role != modifiedRole {
   132  		t.Errorf("Role not modified, exp: %v, got: %v", modifiedRole,
   133  			testData.TestYs[0].TestZs[0].Role)
   134  	}
   135  }
   136  
   137  func TestMergeComplex(t *testing.T) {
   138  	td := testTypeComplex{
   139  		ID:          "ID-TC",
   140  		Parent:      "456",
   141  		Description: "hi there",
   142  		IPAddresses: []string{"192.168.1.1", "127.0.0.1"},
   143  		Location: map[string]string{
   144  			"hello":   "world",
   145  			"goodbye": "cruel world",
   146  		},
   147  		Sensors: map[string]int{
   148  			"temp1": 23,
   149  			"temp2": 40,
   150  		},
   151  		Nested:     TestType{"789", "456", "nested test type"},
   152  		TestValues: []int32{314, 1024},
   153  		Tombstone:  false,
   154  	}
   155  
   156  	p := Points{{Type: "location", Key: "hello", Text: "Siot"}}
   157  
   158  	err := MergePoints("ID-TC", p, &td)
   159  
   160  	if err != nil {
   161  		t.Fatal("Error merging points to complex struct: ", err)
   162  	}
   163  
   164  	if td.Location["hello"] != "Siot" {
   165  		t.Fatal("Map not modified to Siot")
   166  	}
   167  
   168  	ep := Points{{Type: "testValue", Value: 123}}
   169  
   170  	err = MergeEdgePoints("ID-TC", "456", ep, &td)
   171  
   172  	if err != nil {
   173  		t.Fatal("Error merging points to complex struct: ", err)
   174  	}
   175  
   176  	if td.TestValues[0] != 123 {
   177  		t.Fatal("edge point array not modified")
   178  	}
   179  
   180  	// delete points in array
   181  	p = Points{
   182  		{Type: "ipAddress", Key: "0", Tombstone: 1},
   183  		{Type: "ipAddress", Key: "1", Tombstone: 1},
   184  	}
   185  
   186  	err = MergePoints("ID-TC", p, &td)
   187  	if err != nil {
   188  		t.Fatal("Error deleting array entries: ", err)
   189  	}
   190  
   191  	if len(td.IPAddresses) > 0 {
   192  		t.Fatal("Expected 0 IP addresses, got: ", len(td.IPAddresses))
   193  	}
   194  
   195  	// add IP addresses back
   196  	p = Points{
   197  		{Type: "ipAddress", Key: "0", Text: "192.168.1.1"},
   198  		{Type: "ipAddress", Key: "1", Text: "127.0.0.1"},
   199  		{Type: "ipAddress", Key: "3", Text: "127.0.0.3"},
   200  		{Type: "ipAddress", Key: "4", Text: "127.0.0.4"},
   201  	}
   202  
   203  	err = MergePoints("ID-TC", p, &td)
   204  	if err != nil {
   205  		t.Fatal("Error merging array entries: ", err)
   206  	}
   207  
   208  	if len(td.IPAddresses) != 5 {
   209  		t.Fatal("Expected 5 IP addresses, got: ", len(td.IPAddresses))
   210  	}
   211  
   212  	// delete point in array over several merges
   213  	p = Points{
   214  		{Type: "ipAddress", Key: "4", Tombstone: 1},
   215  	}
   216  
   217  	err = MergePoints("ID-TC", p, &td)
   218  	if err != nil {
   219  		t.Fatal("Error merging array entries: ", err)
   220  	}
   221  
   222  	// slice should be trimmed
   223  	if len(td.IPAddresses) != 4 {
   224  		t.Fatal("Expected 4 IP addresses, got: ", len(td.IPAddresses))
   225  	}
   226  
   227  	p = Points{
   228  		{Type: "ipAddress", Key: "1", Tombstone: 1},
   229  	}
   230  
   231  	err = MergePoints("ID-TC", p, &td)
   232  	if err != nil {
   233  		t.Fatal("Error merging array entries: ", err)
   234  	}
   235  
   236  	// index 1 is set to zero value
   237  	exp := []string{"192.168.1.1", "", "", "127.0.0.3"}
   238  	if !reflect.DeepEqual(exp, td.IPAddresses) {
   239  		t.Fatalf("Expected %v, got: %v", exp, td.IPAddresses)
   240  	}
   241  
   242  	p = Points{
   243  		{Type: "ipAddress", Key: "3", Tombstone: 1},
   244  	}
   245  
   246  	err = MergePoints("ID-TC", p, &td)
   247  	if err != nil {
   248  		t.Fatal("Error merging array entries: ", err)
   249  	}
   250  
   251  	// slice is trimmed but only index 3 is removed!
   252  	// MergePoints is unaware that index 1 was removed previously and that
   253  	// index 2 was never set.
   254  	exp = []string{"192.168.1.1", "", ""}
   255  	if !reflect.DeepEqual(exp, td.IPAddresses) {
   256  		t.Fatalf("Expected %v, got: %v", exp, td.IPAddresses)
   257  	}
   258  
   259  	// delete a map entry
   260  	p = Points{{Type: "sensor", Key: "temp1", Tombstone: 1}}
   261  
   262  	err = MergePoints("ID-TC", p, &td)
   263  	if err != nil {
   264  		t.Fatal("Error deleting key entry: ", err)
   265  	}
   266  
   267  	_, ok := td.Sensors["temp1"]
   268  
   269  	if ok {
   270  		t.Fatal("Expected temp key to be deleted, got: ", td.Sensors)
   271  	}
   272  }