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

     1  package model
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/enbility/spine-go/util"
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestTimePeriodType(t *testing.T) {
    13  	tc := &TimePeriodType{}
    14  	duration, err := tc.GetDuration()
    15  	assert.NotNil(t, err)
    16  	assert.Equal(t, time.Duration(0), duration)
    17  
    18  	tc = &TimePeriodType{
    19  		EndTime: NewAbsoluteOrRelativeTimeTypeFromDuration(time.Minute * 1),
    20  	}
    21  	duration, err = tc.GetDuration()
    22  	assert.Nil(t, err)
    23  	assert.Equal(t, time.Minute*1, duration)
    24  
    25  	tc = NewTimePeriodTypeWithRelativeEndTime(time.Minute * 1)
    26  
    27  	duration, err = tc.GetDuration()
    28  	assert.Nil(t, err)
    29  	assert.Equal(t, time.Minute*1, duration)
    30  
    31  	data, err := json.Marshal(tc)
    32  	assert.Nil(t, err)
    33  	assert.NotNil(t, data)
    34  	assert.Equal(t, "{\"endTime\":\"PT1M\"}", string(data))
    35  
    36  	var tp1 TimePeriodType
    37  	err = json.Unmarshal(data, &tp1)
    38  	assert.Nil(t, err)
    39  	assert.Equal(t, *tc.EndTime, *tp1.EndTime)
    40  
    41  	time.Sleep(time.Second * 1)
    42  
    43  	duration, err = tc.GetDuration()
    44  	assert.Nil(t, err)
    45  	assert.Equal(t, time.Second*59, duration)
    46  
    47  	data, err = json.Marshal(tc)
    48  	assert.Nil(t, err)
    49  	assert.NotNil(t, data)
    50  	assert.Equal(t, "{\"endTime\":\"PT59S\"}", string(data))
    51  }
    52  
    53  func TestTimeType(t *testing.T) {
    54  	tc := []struct {
    55  		in    string
    56  		parse string
    57  	}{
    58  		{"21:32:52.12679", "15:04:05.999999999"},
    59  		{"21:32:52.12679Z", "15:04:05.999999999Z"},
    60  		{"21:32:52", "15:04:05"},
    61  		{"19:32:52Z", "15:04:05Z"},
    62  		{"19:32:52+07:00", "15:04:05+07:00"},
    63  		{"19:32:52-07:00", "15:04:05-07:00"},
    64  	}
    65  
    66  	for _, tc := range tc {
    67  		got := NewTimeType(tc.in)
    68  		expect, err := time.ParseInLocation(tc.parse, tc.in, time.UTC)
    69  		if err != nil {
    70  			t.Errorf("Parsing failure with %s and parser %s: %s", tc.in, tc.parse, err)
    71  			continue
    72  		}
    73  		value, err := got.GetTime()
    74  		if err != nil {
    75  			t.Errorf("Test Failure with %s and parser %s: %s", tc.in, tc.parse, err)
    76  			continue
    77  		}
    78  
    79  		if value.UTC() != expect.UTC() {
    80  			t.Errorf("Test failure for %s, expected %s and got %s", tc.in, value.String(), expect.String())
    81  		}
    82  	}
    83  }
    84  
    85  func TestDateType(t *testing.T) {
    86  	tc := []struct {
    87  		in    string
    88  		parse string
    89  	}{
    90  		{"2022-02-01", "2006-01-02"},
    91  		{"2022-02-01Z", "2006-01-02Z"},
    92  		{"2022-02-01+07:00", "2006-01-02+07:00"},
    93  	}
    94  
    95  	for _, tc := range tc {
    96  		got := NewDateType(tc.in)
    97  		expect, err := time.ParseInLocation(tc.parse, tc.in, time.UTC)
    98  		if err != nil {
    99  			t.Errorf("Parsing failure with %s and parser %s: %s", tc.in, tc.parse, err)
   100  			continue
   101  		}
   102  		value, err := got.GetTime()
   103  		if err != nil {
   104  			t.Errorf("Test Failure with %s and parser %s: %s", tc.in, tc.parse, err)
   105  			continue
   106  		}
   107  
   108  		if value.UTC() != expect.UTC() {
   109  			t.Errorf("Test failure for %s, expected %s and got %s", tc.in, value.String(), expect.String())
   110  		}
   111  	}
   112  }
   113  
   114  func TestDateTimeType(t *testing.T) {
   115  	tc := []struct {
   116  		in    string
   117  		parse string
   118  	}{
   119  		{"2022-02-01T21:32:52.12679", "2006-01-02T15:04:05.999999999"},
   120  		{"2022-02-01T21:32:52.12679Z", "2006-01-02T15:04:05.999999999Z"},
   121  		{"2022-02-01T21:32:52", "2006-01-02T15:04:05"},
   122  		{"2022-02-01T19:32:52Z", "2006-01-02T15:04:05Z"},
   123  	}
   124  
   125  	for _, tc := range tc {
   126  		got := NewDateTimeType(tc.in)
   127  		expect, err := time.Parse(tc.parse, tc.in)
   128  		if err != nil {
   129  			t.Errorf("Parsing failure with %s and parser %s: %s", tc.in, tc.parse, err)
   130  			continue
   131  		}
   132  		value, err := got.GetTime()
   133  		if err != nil {
   134  			t.Errorf("Test Failure with %s and parser %s: %s", tc.in, tc.parse, err)
   135  			continue
   136  		}
   137  		nDateTime := NewDateTimeTypeFromTime(value)
   138  		nValue, err := nDateTime.GetTime()
   139  		assert.Nil(t, err)
   140  		assert.Equal(t, nValue.Hour(), value.Hour())
   141  		assert.Equal(t, nValue.Minute(), value.Minute())
   142  		assert.Equal(t, nValue.Second(), value.Second())
   143  		if value.UTC() != expect.UTC() {
   144  			t.Errorf("Test failure for %s, expected %s and got %s", tc.in, value.String(), expect.String())
   145  		}
   146  	}
   147  }
   148  
   149  func TestDurationType(t *testing.T) {
   150  	tc := []struct {
   151  		in  time.Duration
   152  		out string
   153  	}{
   154  		{time.Duration(4) * time.Second, "PT4S"},
   155  	}
   156  
   157  	for _, tc := range tc {
   158  		duration := NewDurationType(tc.in)
   159  		got, err := duration.GetTimeDuration()
   160  		if err != nil {
   161  			t.Errorf("Test Failure with %s: %s", tc.in, err)
   162  			continue
   163  		}
   164  		if got != tc.in {
   165  			t.Errorf("Test failure for %d, got %d", tc.in, got)
   166  		}
   167  		if string(*duration) != tc.out {
   168  			t.Errorf("Test failure for %d, expected %s got %s", tc.in, tc.out, string(*duration))
   169  		}
   170  	}
   171  }
   172  
   173  func TestAbsoluteOrRelativeTimeTypeAbsolute(t *testing.T) {
   174  	tc := []struct {
   175  		in       string
   176  		dateTime time.Time
   177  	}{
   178  		{"2022-02-01T19:32:52Z", time.Date(2022, 02, 01, 19, 32, 52, 0, time.UTC)},
   179  	}
   180  
   181  	for _, tc := range tc {
   182  		a := NewAbsoluteOrRelativeTimeType(tc.in)
   183  		got, err := a.GetTime()
   184  		if err != nil {
   185  			t.Errorf("Test Failure with %s: %s", tc.in, err)
   186  			continue
   187  		}
   188  		if got != tc.dateTime {
   189  			t.Errorf("Test failure for %s, expected %s got %s", tc.in, tc.dateTime.String(), got.String())
   190  		}
   191  
   192  		d := a.GetDateTimeType()
   193  		got, err = d.GetTime()
   194  		if err != nil {
   195  			t.Errorf("Test Failure with %s: %s", tc.in, err)
   196  			continue
   197  		}
   198  		if got != tc.dateTime {
   199  			t.Errorf("Test failure for %s, expected %s got %s", tc.in, tc.dateTime.String(), got.String())
   200  		}
   201  	}
   202  }
   203  
   204  func TestAbsoluteOrRelativeTimeTypeDuration(t *testing.T) {
   205  	tc := []struct {
   206  		in  time.Duration
   207  		out string
   208  	}{
   209  		{time.Duration(4) * time.Second, "PT4S"},
   210  	}
   211  
   212  	for _, tc := range tc {
   213  		a := NewAbsoluteOrRelativeTimeTypeFromDuration(tc.in)
   214  		got, err := a.GetDurationType()
   215  		if err != nil {
   216  			t.Errorf("Test Failure with %d: %s", tc.in, err)
   217  			continue
   218  		}
   219  		if string(*got) != tc.out {
   220  			t.Errorf("Test failure for %d, expected %s got %s", tc.in, tc.out, string(*got))
   221  		}
   222  
   223  		d, err := a.GetTimeDuration()
   224  		if err != nil {
   225  			t.Errorf("Test Failure with %d: %s", tc.in, err)
   226  			continue
   227  		}
   228  		got = NewDurationType(d)
   229  		if string(*got) != tc.out {
   230  			t.Errorf("Test failure for %d, expected %s got %s", tc.in, tc.out, string(*got))
   231  		}
   232  	}
   233  }
   234  
   235  func TestAbsoluteOrRelativeTimeTypeRelative(t *testing.T) {
   236  	tc := []struct {
   237  		in  string
   238  		out time.Duration
   239  	}{
   240  		{"PT4S", time.Duration(4) * time.Second},
   241  	}
   242  
   243  	for _, tc := range tc {
   244  		a := NewAbsoluteOrRelativeTimeType(tc.in)
   245  		got, err := a.GetTimeDuration()
   246  		if err != nil {
   247  			t.Errorf("Test Failure with %s: %s", tc.in, err)
   248  			continue
   249  		}
   250  		if got != tc.out {
   251  			t.Errorf("Test failure for %s, expected %d got %d", tc.in, tc.out, got)
   252  		}
   253  
   254  		d, err := a.GetDurationType()
   255  		if err != nil {
   256  			t.Errorf("Test Failure with %s: %s", tc.in, err)
   257  			continue
   258  		}
   259  		got, err = d.GetTimeDuration()
   260  		if err != nil {
   261  			t.Errorf("Test Failure with %s: %s", tc.in, err)
   262  			continue
   263  		}
   264  		if got != tc.out {
   265  			t.Errorf("Test failure for %s, expected %d got %d", tc.in, tc.out, got)
   266  		}
   267  	}
   268  }
   269  
   270  func TestNewScaledNumberType(t *testing.T) {
   271  	tc := []struct {
   272  		in     float64
   273  		number int64
   274  		scale  int
   275  	}{
   276  		{0, 0, 0},
   277  		{0.1, 1, -1},
   278  		{1.0, 1, 0},
   279  		{6.25, 625, -2},
   280  		{10, 10, 0},
   281  		{12.5952, 125952, -4},
   282  		{13.1637, 131637, -4},
   283  	}
   284  
   285  	for _, tc := range tc {
   286  		got := NewScaledNumberType(tc.in)
   287  		if got.Scale == nil {
   288  			t.Errorf("NewScaledNumberType(%v): Scale may not be nil", tc.in)
   289  		}
   290  
   291  		number := int64(*got.Number)
   292  		scale := 0
   293  		if got.Scale != nil {
   294  			scale = int(*got.Scale)
   295  		}
   296  		if number != tc.number || scale != tc.scale {
   297  			t.Errorf("NewScaledNumberType(%v) = %d %d, want %d %d", tc.in, got.Number, got.Scale, tc.number, tc.scale)
   298  		}
   299  
   300  		val := got.GetValue()
   301  		if val != tc.in {
   302  			t.Errorf("GetValue(%d %d) = %f, want %f", tc.number, tc.scale, val, tc.in)
   303  		}
   304  	}
   305  }
   306  
   307  func TestDeviceAddressTypeString(t *testing.T) {
   308  	tc := []struct {
   309  		device AddressDeviceType
   310  		out    string
   311  	}{
   312  		{
   313  			"Device 1",
   314  			"Device 1",
   315  		},
   316  	}
   317  
   318  	for _, tc := range tc {
   319  		f := DeviceAddressType{
   320  			Device: util.Ptr(tc.device),
   321  		}
   322  
   323  		got := f.String()
   324  		if got != tc.out {
   325  			t.Errorf("TestDeviceAddressTypeString(), got %s, expects %s", got, tc.out)
   326  		}
   327  	}
   328  }
   329  
   330  func TestEntityAddressTypeString(t *testing.T) {
   331  	tc := []struct {
   332  		device AddressDeviceType
   333  		entity []AddressEntityType
   334  		out    string
   335  	}{
   336  		{
   337  			"Device",
   338  			[]AddressEntityType{1, 1},
   339  			"Device:[1,1]:",
   340  		},
   341  	}
   342  
   343  	for _, tc := range tc {
   344  		f := FeatureAddressType{
   345  			Device: util.Ptr(tc.device),
   346  			Entity: tc.entity,
   347  		}
   348  
   349  		got := f.String()
   350  		if got != tc.out {
   351  			t.Errorf("TestEntityAddressTypeString(), got %s, expects %s", got, tc.out)
   352  		}
   353  	}
   354  }
   355  
   356  func TestFeatureAddressTypeString(t *testing.T) {
   357  	tc := []struct {
   358  		device  AddressDeviceType
   359  		entity  []AddressEntityType
   360  		feature AddressFeatureType
   361  		out     string
   362  	}{
   363  		{
   364  			"Device",
   365  			[]AddressEntityType{1, 1},
   366  			0,
   367  			"Device:[1,1]:0",
   368  		},
   369  	}
   370  
   371  	for _, tc := range tc {
   372  		f := FeatureAddressType{
   373  			Device:  util.Ptr(tc.device),
   374  			Entity:  tc.entity,
   375  			Feature: util.Ptr(tc.feature),
   376  		}
   377  
   378  		got := f.String()
   379  		if got != tc.out {
   380  			t.Errorf("TestFeatureAddressTypeString(), got %s, expects %s", got, tc.out)
   381  		}
   382  	}
   383  }