github.com/m3db/m3@v1.5.0/src/query/models/bounds_test.go (about) 1 // Copyright (c) 2018 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package models 22 23 import ( 24 "testing" 25 "time" 26 27 "github.com/stretchr/testify/assert" 28 29 xtime "github.com/m3db/m3/src/x/time" 30 ) 31 32 func TestBounds(t *testing.T) { 33 now := xtime.Now() 34 bounds := Bounds{ 35 Start: now, 36 Duration: 0, 37 StepSize: time.Minute, 38 } 39 40 assert.Equal(t, bounds.Steps(), 0) 41 _, err := bounds.TimeForIndex(0) 42 assert.Error(t, err, "no valid index in this block") 43 44 bounds = Bounds{ 45 Start: now, 46 Duration: time.Second, 47 StepSize: time.Minute, 48 } 49 50 assert.Equal(t, bounds.Steps(), 0) 51 _, err = bounds.TimeForIndex(0) 52 assert.Error(t, err, "no valid index in this block") 53 54 bounds = Bounds{ 55 Start: now, 56 Duration: time.Minute, 57 StepSize: time.Minute, 58 } 59 assert.Equal(t, bounds.Steps(), 1) 60 it, err := bounds.TimeForIndex(0) 61 assert.NoError(t, err) 62 assert.Equal(t, it, now) 63 _, err = bounds.TimeForIndex(1) 64 assert.Error(t, err) 65 66 b := bounds.Next(1) 67 assert.Equal(t, b.Start, bounds.End()) 68 assert.Equal(t, b.StepSize, bounds.StepSize) 69 70 bounds = Bounds{ 71 Start: now, 72 Duration: 10 * time.Minute, 73 StepSize: time.Minute, 74 } 75 assert.Equal(t, bounds.Steps(), 10) 76 it, err = bounds.TimeForIndex(9) 77 assert.NoError(t, err) 78 assert.Equal(t, it, now.Add(9*bounds.StepSize)) 79 b = bounds.Next(9) 80 assert.Equal(t, b.Start, now.Add(9*bounds.Duration)) 81 assert.Equal(t, b.StepSize, bounds.StepSize) 82 b = b.Previous(9) 83 assert.Equal(t, b.Start, bounds.Start) 84 assert.Equal(t, b.Duration, bounds.Duration) 85 }