github.com/enbility/spine-go@v0.7.0/model/threshold_additions_test.go (about)

     1  package model
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/enbility/spine-go/util"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestThresholdListDataType_Update(t *testing.T) {
    11  	sut := ThresholdListDataType{
    12  		ThresholdData: []ThresholdDataType{
    13  			{
    14  				ThresholdId:    util.Ptr(ThresholdIdType(0)),
    15  				ThresholdValue: NewScaledNumberType(1),
    16  			},
    17  			{
    18  				ThresholdId:    util.Ptr(ThresholdIdType(1)),
    19  				ThresholdValue: NewScaledNumberType(1),
    20  			},
    21  		},
    22  	}
    23  
    24  	newData := ThresholdListDataType{
    25  		ThresholdData: []ThresholdDataType{
    26  			{
    27  				ThresholdId:    util.Ptr(ThresholdIdType(1)),
    28  				ThresholdValue: NewScaledNumberType(10),
    29  			},
    30  		},
    31  	}
    32  
    33  	// Act
    34  	_, success := sut.UpdateList(false, true, &newData, NewFilterTypePartial(), nil)
    35  	assert.True(t, success)
    36  
    37  	data := sut.ThresholdData
    38  	// check the non changing items
    39  	assert.Equal(t, 2, len(data))
    40  	item1 := data[0]
    41  	assert.Equal(t, 0, int(*item1.ThresholdId))
    42  	assert.Equal(t, 1.0, item1.ThresholdValue.GetValue())
    43  	// check properties of updated item
    44  	item2 := data[1]
    45  	assert.Equal(t, 1, int(*item2.ThresholdId))
    46  	assert.Equal(t, 10.0, item2.ThresholdValue.GetValue())
    47  }
    48  
    49  func TestThresholdConstraintsListDataType_Update(t *testing.T) {
    50  	sut := ThresholdConstraintsListDataType{
    51  		ThresholdConstraintsData: []ThresholdConstraintsDataType{
    52  			{
    53  				ThresholdId:       util.Ptr(ThresholdIdType(0)),
    54  				ThresholdRangeMin: NewScaledNumberType(1),
    55  			},
    56  			{
    57  				ThresholdId:       util.Ptr(ThresholdIdType(1)),
    58  				ThresholdRangeMin: NewScaledNumberType(1),
    59  			},
    60  		},
    61  	}
    62  
    63  	newData := ThresholdConstraintsListDataType{
    64  		ThresholdConstraintsData: []ThresholdConstraintsDataType{
    65  			{
    66  				ThresholdId:       util.Ptr(ThresholdIdType(1)),
    67  				ThresholdRangeMin: NewScaledNumberType(10),
    68  			},
    69  		},
    70  	}
    71  
    72  	// Act
    73  	_, success := sut.UpdateList(false, true, &newData, NewFilterTypePartial(), nil)
    74  	assert.True(t, success)
    75  
    76  	data := sut.ThresholdConstraintsData
    77  	// check the non changing items
    78  	assert.Equal(t, 2, len(data))
    79  	item1 := data[0]
    80  	assert.Equal(t, 0, int(*item1.ThresholdId))
    81  	assert.Equal(t, 1.0, item1.ThresholdRangeMin.GetValue())
    82  	// check properties of updated item
    83  	item2 := data[1]
    84  	assert.Equal(t, 1, int(*item2.ThresholdId))
    85  	assert.Equal(t, 10.0, item2.ThresholdRangeMin.GetValue())
    86  }
    87  
    88  func TestThresholdDescriptionListDataType_Update(t *testing.T) {
    89  	sut := ThresholdDescriptionListDataType{
    90  		ThresholdDescriptionData: []ThresholdDescriptionDataType{
    91  			{
    92  				ThresholdId: util.Ptr(ThresholdIdType(0)),
    93  				Description: util.Ptr(DescriptionType("old")),
    94  			},
    95  			{
    96  				ThresholdId: util.Ptr(ThresholdIdType(1)),
    97  				Description: util.Ptr(DescriptionType("old")),
    98  			},
    99  		},
   100  	}
   101  
   102  	newData := ThresholdDescriptionListDataType{
   103  		ThresholdDescriptionData: []ThresholdDescriptionDataType{
   104  			{
   105  				ThresholdId: util.Ptr(ThresholdIdType(1)),
   106  				Description: util.Ptr(DescriptionType("new")),
   107  			},
   108  		},
   109  	}
   110  
   111  	// Act
   112  	_, success := sut.UpdateList(false, true, &newData, NewFilterTypePartial(), nil)
   113  	assert.True(t, success)
   114  
   115  	data := sut.ThresholdDescriptionData
   116  	// check the non changing items
   117  	assert.Equal(t, 2, len(data))
   118  	item1 := data[0]
   119  	assert.Equal(t, 0, int(*item1.ThresholdId))
   120  	assert.Equal(t, "old", string(*item1.Description))
   121  	// check properties of updated item
   122  	item2 := data[1]
   123  	assert.Equal(t, 1, int(*item2.ThresholdId))
   124  	assert.Equal(t, "new", string(*item2.Description))
   125  }