|
| 1 | +package flux_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/google/go-cmp/cmp" |
| 9 | + "github.com/influxdata/flux" |
| 10 | +) |
| 11 | + |
| 12 | +func TestTime_MarshalText(t *testing.T) { |
| 13 | + for _, tt := range []struct { |
| 14 | + ts flux.Time |
| 15 | + want string |
| 16 | + }{ |
| 17 | + { |
| 18 | + ts: flux.Time{ |
| 19 | + IsRelative: true, |
| 20 | + }, |
| 21 | + want: "now", |
| 22 | + }, |
| 23 | + { |
| 24 | + ts: flux.Time{ |
| 25 | + Relative: -time.Minute, |
| 26 | + IsRelative: true, |
| 27 | + }, |
| 28 | + want: "-1m0s", |
| 29 | + }, |
| 30 | + { |
| 31 | + ts: flux.Time{ |
| 32 | + Relative: time.Minute, |
| 33 | + IsRelative: true, |
| 34 | + }, |
| 35 | + want: "1m0s", |
| 36 | + }, |
| 37 | + { |
| 38 | + ts: flux.Time{ |
| 39 | + Absolute: time.Unix(0, 0).UTC(), |
| 40 | + }, |
| 41 | + want: "1970-01-01T00:00:00Z", |
| 42 | + }, |
| 43 | + { |
| 44 | + ts: flux.Time{ |
| 45 | + // Minimum time in influxql. |
| 46 | + Absolute: time.Unix(0, math.MinInt64+2).UTC(), |
| 47 | + }, |
| 48 | + want: "1677-09-21T00:12:43.145224194Z", |
| 49 | + }, |
| 50 | + { |
| 51 | + ts: flux.Time{ |
| 52 | + // Maximum time in influxql. |
| 53 | + Absolute: time.Unix(0, math.MaxInt64-1).UTC(), |
| 54 | + }, |
| 55 | + want: "2262-04-11T23:47:16.854775806Z", |
| 56 | + }, |
| 57 | + } { |
| 58 | + t.Run(tt.want, func(t *testing.T) { |
| 59 | + data, err := tt.ts.MarshalText() |
| 60 | + if err != nil { |
| 61 | + t.Fatalf("unexpected error: %s", err) |
| 62 | + } |
| 63 | + |
| 64 | + if want, got := tt.want, string(data); want != got { |
| 65 | + t.Fatalf("unexpected text -want/+got\n\t- %s\n\t+ %s", want, got) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestTime_UnmarshalText(t *testing.T) { |
| 72 | + for _, tt := range []struct { |
| 73 | + s string |
| 74 | + want flux.Time |
| 75 | + }{ |
| 76 | + { |
| 77 | + s: "now", |
| 78 | + want: flux.Time{ |
| 79 | + IsRelative: true, |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + s: "-1m0s", |
| 84 | + want: flux.Time{ |
| 85 | + Relative: -time.Minute, |
| 86 | + IsRelative: true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + { |
| 90 | + s: "1m0s", |
| 91 | + want: flux.Time{ |
| 92 | + Relative: time.Minute, |
| 93 | + IsRelative: true, |
| 94 | + }, |
| 95 | + }, |
| 96 | + { |
| 97 | + s: "1970-01-01T00:00:00Z", |
| 98 | + want: flux.Time{ |
| 99 | + Absolute: time.Unix(0, 0).UTC(), |
| 100 | + }, |
| 101 | + }, |
| 102 | + { |
| 103 | + s: "1677-09-21T00:12:43.145224194Z", |
| 104 | + want: flux.Time{ |
| 105 | + // Minimum time in influxql. |
| 106 | + Absolute: time.Unix(0, math.MinInt64+2).UTC(), |
| 107 | + }, |
| 108 | + }, |
| 109 | + { |
| 110 | + s: "2262-04-11T23:47:16.854775806Z", |
| 111 | + want: flux.Time{ |
| 112 | + // Maximum time in influxql. |
| 113 | + Absolute: time.Unix(0, math.MaxInt64-1).UTC(), |
| 114 | + }, |
| 115 | + }, |
| 116 | + } { |
| 117 | + t.Run(tt.s, func(t *testing.T) { |
| 118 | + var ts flux.Time |
| 119 | + if err := ts.UnmarshalText([]byte(tt.s)); err != nil { |
| 120 | + t.Fatalf("unexpected error: %s", err) |
| 121 | + } |
| 122 | + |
| 123 | + if want, got := tt.want, ts; !cmp.Equal(want, got) { |
| 124 | + t.Fatalf("unexpected text -want/+got\n%s", cmp.Diff(want, got)) |
| 125 | + } |
| 126 | + }) |
| 127 | + } |
| 128 | +} |
0 commit comments