github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/sem/tree/time_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package tree
    12  
    13  import (
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/util/leaktest"
    18  	"github.com/stretchr/testify/assert"
    19  )
    20  
    21  func TestTimeFamilyPrecisionToRoundDuration(t *testing.T) {
    22  	defer leaktest.AfterTest(t)()
    23  	testCases := []struct {
    24  		precision     int32
    25  		expected      time.Duration
    26  		expectedPanic bool
    27  	}{
    28  		{precision: 0, expected: time.Duration(1000000000)},
    29  		{precision: 1, expected: time.Duration(100000000)},
    30  		{precision: 2, expected: time.Duration(10000000)},
    31  		{precision: 3, expected: time.Duration(1000000)},
    32  		{precision: 4, expected: time.Duration(100000)},
    33  		{precision: 5, expected: time.Duration(10000)},
    34  		{precision: 6, expected: time.Duration(1000)},
    35  
    36  		{precision: -2, expectedPanic: true},
    37  		{precision: 7, expectedPanic: true},
    38  	}
    39  
    40  	for _, tc := range testCases {
    41  		if tc.expectedPanic {
    42  			assert.Panics(t, func() { TimeFamilyPrecisionToRoundDuration(tc.precision) })
    43  		} else {
    44  			assert.Equal(t, tc.expected, TimeFamilyPrecisionToRoundDuration(tc.precision))
    45  		}
    46  	}
    47  }