github.com/hashicorp/terraform-plugin-sdk@v1.17.2/helper/validation/time_test.go (about)

     1  package validation
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestValidationIsRFC3339Time(t *testing.T) {
     8  	cases := map[string]struct {
     9  		Value interface{}
    10  		Error bool
    11  	}{
    12  		"NotString": {
    13  			Value: 7,
    14  			Error: true,
    15  		},
    16  		"ValidDate": {
    17  			Value: "2018-03-01T00:00:00Z",
    18  			Error: false,
    19  		},
    20  		"ValidDateTime": {
    21  			Value: "2018-03-01T00:00:00-05:00",
    22  			Error: false,
    23  		},
    24  		"ValidDateTime2": {
    25  			Value: "2018-03-01T00:00:00+05:00",
    26  			Error: false,
    27  		},
    28  		"InvalidDateWithSlashes": {
    29  			Value: "03/01/2018",
    30  			Error: true,
    31  		},
    32  		"InvalidDateWithDashes": {
    33  			Value: "03-01-2018",
    34  			Error: true,
    35  		},
    36  		"InvalidDateWithDashes2": {
    37  			Value: "2018-03-01",
    38  			Error: true,
    39  		},
    40  		"InvalidDateWithT": {
    41  			Value: "2018-03-01T",
    42  			Error: true,
    43  		},
    44  		"DateTimeWithoutZone": {
    45  			Value: "2018-03-01T00:00:00",
    46  			Error: true,
    47  		},
    48  		"DateTimeWithZZone": {
    49  			Value: "2018-03-01T00:00:00Z05:00",
    50  			Error: true,
    51  		},
    52  		"DateTimeWithZZoneNeg": {
    53  			Value: "2018-03-01T00:00:00Z-05:00",
    54  			Error: true,
    55  		},
    56  	}
    57  
    58  	for tn, tc := range cases {
    59  		t.Run(tn, func(t *testing.T) {
    60  			_, errors := IsRFC3339Time(tc.Value, tn)
    61  
    62  			if len(errors) > 0 && !tc.Error {
    63  				t.Errorf("IsRFC3339Time(%s) produced an unexpected error", tc.Value)
    64  			} else if len(errors) == 0 && tc.Error {
    65  				t.Errorf("IsRFC3339Time(%s) did not error", tc.Value)
    66  			}
    67  		})
    68  	}
    69  }