github.com/blend/go-sdk@v1.20220411.3/validate/time_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package validate
     9  
    10  import (
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/blend/go-sdk/assert"
    15  	"github.com/blend/go-sdk/ex"
    16  )
    17  
    18  func tv(t time.Time) *time.Time { return &t }
    19  
    20  func TestTimeBefore(t *testing.T) {
    21  	assert := assert.New(t)
    22  
    23  	ts := time.Date(2019, 07, 18, 17, 24, 0, 0, time.UTC)
    24  
    25  	var verr error
    26  	verr = Time(tv(ts.Add(-time.Hour))).Before(ts)()
    27  	assert.Nil(verr)
    28  	verr = Time(tv(ts.Add(time.Hour))).Before(ts)()
    29  	assert.NotNil(verr)
    30  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    31  	assert.Equal(ErrTimeBefore, ErrCause(verr))
    32  }
    33  
    34  func TestTimeBeforeNowUTC(t *testing.T) {
    35  	assert := assert.New(t)
    36  
    37  	ts := time.Now().UTC()
    38  
    39  	var verr error
    40  	verr = Time(tv(ts.Add(-time.Hour))).BeforeNowUTC()()
    41  	assert.Nil(verr)
    42  	verr = Time(tv(ts.Add(time.Hour))).BeforeNowUTC()()
    43  	assert.NotNil(verr)
    44  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    45  	assert.Equal(ErrTimeBefore, ErrCause(verr))
    46  }
    47  
    48  func TestTimeAfter(t *testing.T) {
    49  	assert := assert.New(t)
    50  
    51  	ts := time.Date(2019, 07, 18, 17, 24, 0, 0, time.UTC)
    52  
    53  	var verr error
    54  	verr = Time(tv(ts.Add(time.Hour))).After(ts)()
    55  	assert.Nil(verr)
    56  	verr = Time(tv(ts.Add(-time.Hour))).After(ts)()
    57  	assert.NotNil(verr)
    58  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    59  	assert.Equal(ErrTimeAfter, ErrCause(verr))
    60  }
    61  
    62  func TestTimeAfterNowUTC(t *testing.T) {
    63  	assert := assert.New(t)
    64  
    65  	ts := time.Now().UTC()
    66  
    67  	var verr error
    68  	verr = Time(tv(ts.Add(time.Hour))).AfterNowUTC()()
    69  	assert.Nil(verr)
    70  	verr = Time(tv(ts.Add(-time.Hour))).AfterNowUTC()()
    71  	assert.NotNil(verr)
    72  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    73  	assert.Equal(ErrTimeAfter, ErrCause(verr))
    74  }
    75  
    76  func TestTimeBetween(t *testing.T) {
    77  	assert := assert.New(t)
    78  
    79  	a := time.Date(2019, 07, 18, 0, 0, 0, 0, time.UTC)
    80  	b := time.Date(2019, 07, 19, 0, 0, 0, 0, time.UTC)
    81  	c := time.Date(2019, 07, 20, 0, 0, 0, 0, time.UTC)
    82  
    83  	var verr error
    84  	verr = Time(&b).Between(a, c)()
    85  	assert.Nil(verr)
    86  
    87  	verr = Time(&c).Between(a, b)()
    88  	assert.NotNil(verr)
    89  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    90  	assert.Equal(ErrTimeBefore, ErrCause(verr))
    91  
    92  	verr = Time(&a).Between(b, c)()
    93  	assert.NotNil(verr)
    94  	assert.Equal(ErrValidation, ex.ErrClass(verr))
    95  	assert.Equal(ErrTimeAfter, ErrCause(verr))
    96  }