github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/diff_test.go (about)

     1  package datetime_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/MontFerret/ferret/pkg/runtime/values"
     8  
     9  	"github.com/MontFerret/ferret/pkg/runtime/core"
    10  	"github.com/MontFerret/ferret/pkg/stdlib/datetime"
    11  )
    12  
    13  var (
    14  	isFloat        = values.NewBoolean(true)
    15  	beginningEpoch = values.NewDateTime(time.Time{})
    16  )
    17  
    18  func TestDiff(t *testing.T) {
    19  	tcs := []*testCase{
    20  		&testCase{
    21  			Name:      "when less then 3 arguments",
    22  			Expected:  values.NewInt(1),
    23  			Args:      []core.Value{beginningEpoch},
    24  			ShouldErr: true,
    25  		},
    26  		&testCase{
    27  			Name:      "when more then 4 arguments",
    28  			Expected:  values.NewInt(1),
    29  			Args:      []core.Value{beginningEpoch, beginningEpoch, beginningEpoch, beginningEpoch, beginningEpoch},
    30  			ShouldErr: true,
    31  		},
    32  		&testCase{
    33  			Name:      "when wrong type argument",
    34  			Expected:  values.NewInt(1),
    35  			Args:      []core.Value{beginningEpoch, beginningEpoch, beginningEpoch},
    36  			ShouldErr: true,
    37  		},
    38  		&testCase{
    39  			Name:     "when the difference is 1 year and 1 month (int)",
    40  			Expected: values.NewInt(1),
    41  			Args: []core.Value{
    42  				beginningEpoch,
    43  				values.NewDateTime(
    44  					beginningEpoch.AddDate(1, 1, 0),
    45  				),
    46  				values.NewString("y"),
    47  			},
    48  		},
    49  		&testCase{
    50  			Name:     "when the difference is 1 year and 1 month (float)",
    51  			Expected: values.NewFloat(1.084931506849315),
    52  			Args: []core.Value{
    53  				beginningEpoch,
    54  				values.NewDateTime(
    55  					beginningEpoch.AddDate(1, 1, 0),
    56  				),
    57  				values.NewString("year"),
    58  				isFloat,
    59  			},
    60  		},
    61  		&testCase{
    62  			Name:     "when date1 after date2 (int)",
    63  			Expected: values.NewInt(2),
    64  			Args: []core.Value{
    65  				beginningEpoch,
    66  				values.NewDateTime(
    67  					beginningEpoch.Add(-time.Hour * 48),
    68  				),
    69  				values.NewString("d"),
    70  			},
    71  		},
    72  		&testCase{
    73  			Name:     "when date1 after date2 (float)",
    74  			Expected: values.NewFloat(2),
    75  			Args: []core.Value{
    76  				beginningEpoch,
    77  				values.NewDateTime(
    78  					beginningEpoch.Add(-time.Hour * 48),
    79  				),
    80  				values.NewString("d"),
    81  				isFloat,
    82  			},
    83  		},
    84  		&testCase{
    85  			Name:     "when dates are equal (int)",
    86  			Expected: values.NewInt(0),
    87  			Args: []core.Value{
    88  				beginningEpoch,
    89  				beginningEpoch,
    90  				values.NewString("i"),
    91  			},
    92  		},
    93  		&testCase{
    94  			Name:     "when dates are equal (float)",
    95  			Expected: values.NewFloat(0),
    96  			Args: []core.Value{
    97  				beginningEpoch,
    98  				beginningEpoch,
    99  				values.NewString("y"),
   100  				isFloat,
   101  			},
   102  		},
   103  	}
   104  
   105  	bigUnits := map[string][3]int{
   106  		"y": [3]int{1, 0, 0}, "year": [3]int{1, 0, 0}, "years": [3]int{1, 0, 0},
   107  		"m": [3]int{0, 1, 0}, "month": [3]int{0, 1, 0}, "months": [3]int{0, 1, 0},
   108  		"w": [3]int{0, 0, 7}, "week": [3]int{0, 0, 7}, "weeks": [3]int{0, 0, 7},
   109  		"d": [3]int{0, 0, 1}, "day": [3]int{0, 0, 1}, "days": [3]int{0, 0, 1},
   110  	}
   111  
   112  	for unit, dates := range bigUnits {
   113  		tcs = append(tcs,
   114  			&testCase{
   115  				Name:     "When difference is 1 " + unit + " (int)",
   116  				Expected: values.NewInt(1),
   117  				Args: []core.Value{
   118  					beginningEpoch,
   119  					values.NewDateTime(
   120  						beginningEpoch.AddDate(dates[0], dates[1], dates[2]),
   121  					),
   122  					values.NewString(unit),
   123  				},
   124  			},
   125  			&testCase{
   126  				Name:     "When difference is 1 " + unit + " (float)",
   127  				Expected: values.NewFloat(1),
   128  				Args: []core.Value{
   129  					beginningEpoch,
   130  					values.NewDateTime(
   131  						beginningEpoch.AddDate(dates[0], dates[1], dates[2]),
   132  					),
   133  					values.NewString(unit),
   134  					isFloat,
   135  				},
   136  			},
   137  		)
   138  	}
   139  
   140  	units := map[string]time.Duration{
   141  		"h": time.Hour, "hour": time.Hour, "hours": time.Hour,
   142  		"i": time.Minute, "minute": time.Minute, "minutes": time.Minute,
   143  		"s": time.Second, "second": time.Second, "seconds": time.Second,
   144  		"f": time.Millisecond, "millisecond": time.Millisecond, "milliseconds": time.Millisecond,
   145  	}
   146  
   147  	for unit, durn := range units {
   148  		tcs = append(tcs,
   149  			&testCase{
   150  				Name:     "When difference is 1 " + unit + " (int)",
   151  				Expected: values.NewInt(1),
   152  				Args: []core.Value{
   153  					beginningEpoch,
   154  					values.NewDateTime(
   155  						beginningEpoch.Add(durn),
   156  					),
   157  					values.NewString(unit),
   158  				},
   159  			},
   160  			&testCase{
   161  				Name:     "When difference is 1 " + unit + " (int)",
   162  				Expected: values.NewFloat(1),
   163  				Args: []core.Value{
   164  					beginningEpoch,
   165  					values.NewDateTime(
   166  						beginningEpoch.Add(durn),
   167  					),
   168  					values.NewString(unit),
   169  					isFloat,
   170  				},
   171  			},
   172  		)
   173  	}
   174  
   175  	for _, tc := range tcs {
   176  		tc.Do(t, datetime.DateDiff)
   177  	}
   178  }