github.com/xmidt-org/webpa-common@v1.11.9/xhttp/converter/duration_test.go (about)

     1  package converter
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func testDurationValid(t *testing.T) {
    12  	testData := []struct {
    13  		value    string
    14  		expected reflect.Value
    15  	}{
    16  		{"1s", reflect.ValueOf(time.Second)},
    17  		{"20m", reflect.ValueOf(20 * time.Minute)},
    18  		{"3h", reflect.ValueOf(3 * time.Hour)},
    19  	}
    20  
    21  	for _, record := range testData {
    22  		t.Run(record.value, func(t *testing.T) {
    23  			assert.Equal(t, record.expected.Interface(), Duration(record.value).Interface())
    24  		})
    25  	}
    26  }
    27  
    28  func testDurationInvalid(t *testing.T) {
    29  	testData := []struct {
    30  		value string
    31  	}{
    32  		{""},
    33  		{"asdf"},
    34  	}
    35  
    36  	for _, record := range testData {
    37  		t.Run(record.value, func(t *testing.T) {
    38  			assert.False(t, Duration(record.value).IsValid())
    39  		})
    40  	}
    41  }
    42  
    43  func TestDuration(t *testing.T) {
    44  	t.Run("Valid", testDurationValid)
    45  	t.Run("Invalid", testDurationInvalid)
    46  }