...

Source file src/github.com/go-openapi/strfmt/duration_test.go

Documentation: github.com/go-openapi/strfmt

     1  // Copyright 2015 go-swagger maintainers
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //    http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package strfmt
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  	"go.mongodb.org/mongo-driver/bson"
    24  )
    25  
    26  func TestDuration(t *testing.T) {
    27  	pp := Duration(0)
    28  
    29  	err := pp.UnmarshalText([]byte("0ms"))
    30  	require.NoError(t, err)
    31  	err = pp.UnmarshalText([]byte("yada"))
    32  	require.Error(t, err)
    33  
    34  	orig := "2ms"
    35  	b := []byte(orig)
    36  	bj := []byte("\"" + orig + "\"")
    37  
    38  	err = pp.UnmarshalText(b)
    39  	require.NoError(t, err)
    40  
    41  	err = pp.UnmarshalText([]byte("three week"))
    42  	require.Error(t, err)
    43  
    44  	err = pp.UnmarshalText([]byte("9999999999999999999999999999999999999999999999999999999 weeks"))
    45  	require.Error(t, err)
    46  
    47  	txt, err := pp.MarshalText()
    48  	require.NoError(t, err)
    49  	assert.Equal(t, orig, string(txt))
    50  
    51  	err = pp.UnmarshalJSON(bj)
    52  	require.NoError(t, err)
    53  	assert.EqualValues(t, orig, pp.String())
    54  
    55  	err = pp.UnmarshalJSON([]byte("yada"))
    56  	require.Error(t, err)
    57  
    58  	err = pp.UnmarshalJSON([]byte(`"12 parsecs"`))
    59  	require.Error(t, err)
    60  
    61  	err = pp.UnmarshalJSON([]byte(`"12 y"`))
    62  	require.Error(t, err)
    63  
    64  	b, err = pp.MarshalJSON()
    65  	require.NoError(t, err)
    66  	assert.Equal(t, bj, b)
    67  
    68  	dur := Duration(42)
    69  	bsonData, err := bson.Marshal(&dur)
    70  	require.NoError(t, err)
    71  
    72  	var durCopy Duration
    73  	err = bson.Unmarshal(bsonData, &durCopy)
    74  	require.NoError(t, err)
    75  	assert.Equal(t, dur, durCopy)
    76  }
    77  
    78  func testDurationParser(t *testing.T, toParse string, expected time.Duration) {
    79  	t.Helper()
    80  
    81  	r, e := ParseDuration(toParse)
    82  	require.NoError(t, e)
    83  	assert.Equal(t, expected, r)
    84  }
    85  
    86  func TestDurationParser_Failed(t *testing.T) {
    87  	_, e := ParseDuration("45 wekk")
    88  	require.Error(t, e)
    89  }
    90  
    91  func TestIsDuration_Failed(t *testing.T) {
    92  	e := IsDuration("45 weeekks")
    93  	assert.False(t, e)
    94  }
    95  
    96  func testDurationSQLScanner(t *testing.T, dur time.Duration) {
    97  	t.Helper()
    98  
    99  	values := []interface{}{int64(dur), float64(dur)}
   100  	for _, value := range values {
   101  		var result Duration
   102  		err := result.Scan(value)
   103  		require.NoError(t, err)
   104  		assert.Equal(t, dur, time.Duration(result))
   105  
   106  		// And the other way around
   107  		resv, erv := result.Value()
   108  		require.NoError(t, erv)
   109  		assert.EqualValues(t, value, resv)
   110  
   111  	}
   112  }
   113  
   114  func TestDurationScanner_Nil(t *testing.T) {
   115  	var result Duration
   116  	err := result.Scan(nil)
   117  	require.NoError(t, err)
   118  	assert.EqualValues(t, 0, time.Duration(result))
   119  
   120  	err = result.Scan("1 ms")
   121  	require.Error(t, err)
   122  }
   123  
   124  func TestDurationParser(t *testing.T) {
   125  	testcases := map[string]time.Duration{
   126  
   127  		// parse the short forms without spaces
   128  		"1ns": 1 * time.Nanosecond,
   129  		"1us": 1 * time.Microsecond,
   130  		"1µs": 1 * time.Microsecond,
   131  		"1ms": 1 * time.Millisecond,
   132  		"1s":  1 * time.Second,
   133  		"1m":  1 * time.Minute,
   134  		"1h":  1 * time.Hour,
   135  		"1hr": 1 * time.Hour,
   136  		"1d":  24 * time.Hour,
   137  		"1w":  7 * 24 * time.Hour,
   138  		"1wk": 7 * 24 * time.Hour,
   139  
   140  		// parse the long forms without spaces
   141  		"1nanoseconds":  1 * time.Nanosecond,
   142  		"1nanos":        1 * time.Nanosecond,
   143  		"1microseconds": 1 * time.Microsecond,
   144  		"1micros":       1 * time.Microsecond,
   145  		"1millis":       1 * time.Millisecond,
   146  		"1milliseconds": 1 * time.Millisecond,
   147  		"1second":       1 * time.Second,
   148  		"1sec":          1 * time.Second,
   149  		"1min":          1 * time.Minute,
   150  		"1minute":       1 * time.Minute,
   151  		"1hour":         1 * time.Hour,
   152  		"1day":          24 * time.Hour,
   153  		"1week":         7 * 24 * time.Hour,
   154  
   155  		// parse the short forms with spaces
   156  		"1  ns": 1 * time.Nanosecond,
   157  		"1  us": 1 * time.Microsecond,
   158  		"1  µs": 1 * time.Microsecond,
   159  		"1  ms": 1 * time.Millisecond,
   160  		"1  s":  1 * time.Second,
   161  		"1  m":  1 * time.Minute,
   162  		"1  h":  1 * time.Hour,
   163  		"1  hr": 1 * time.Hour,
   164  		"1  d":  24 * time.Hour,
   165  		"1  w":  7 * 24 * time.Hour,
   166  		"1  wk": 7 * 24 * time.Hour,
   167  
   168  		// parse the long forms without spaces
   169  		"1  nanoseconds":  1 * time.Nanosecond,
   170  		"1  nanos":        1 * time.Nanosecond,
   171  		"1  microseconds": 1 * time.Microsecond,
   172  		"1  micros":       1 * time.Microsecond,
   173  		"1  millis":       1 * time.Millisecond,
   174  		"1  milliseconds": 1 * time.Millisecond,
   175  		"1  second":       1 * time.Second,
   176  		"1  sec":          1 * time.Second,
   177  		"1  min":          1 * time.Minute,
   178  		"1  minute":       1 * time.Minute,
   179  		"1  hour":         1 * time.Hour,
   180  		"1  day":          24 * time.Hour,
   181  		"1  week":         7 * 24 * time.Hour,
   182  	}
   183  
   184  	for str, dur := range testcases {
   185  		testDurationParser(t, str, dur)
   186  		testDurationSQLScanner(t, dur)
   187  	}
   188  }
   189  func TestIsDuration_Caveats(t *testing.T) {
   190  	// This works too
   191  	e := IsDuration("45 weeks")
   192  	assert.True(t, e)
   193  
   194  	// This works too
   195  	e = IsDuration("45 weekz")
   196  	assert.True(t, e)
   197  
   198  	// This works too
   199  	e = IsDuration("12 hours")
   200  	assert.True(t, e)
   201  
   202  	// This works too
   203  	e = IsDuration("12 minutes")
   204  	assert.True(t, e)
   205  
   206  	// This does not work
   207  	e = IsDuration("12 phours")
   208  	assert.False(t, e)
   209  
   210  }
   211  
   212  func TestDeepCopyDuration(t *testing.T) {
   213  	dur := Duration(42)
   214  	in := &dur
   215  
   216  	out := new(Duration)
   217  	in.DeepCopyInto(out)
   218  	assert.Equal(t, in, out)
   219  
   220  	out2 := in.DeepCopy()
   221  	assert.Equal(t, in, out2)
   222  
   223  	var inNil *Duration
   224  	out3 := inNil.DeepCopy()
   225  	assert.Nil(t, out3)
   226  }
   227  

View as plain text