time_range_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tsdb
  15. import (
  16. "testing"
  17. "time"
  18. . "github.com/smartystreets/goconvey/convey"
  19. )
  20. func TestTimeRange(t *testing.T) {
  21. Convey("Time range", t, func() {
  22. now := time.Now()
  23. Convey("Can parse 5m, now", func() {
  24. tr := TimeRange{
  25. From: "5m",
  26. To: "now",
  27. now: now,
  28. }
  29. Convey("5m ago ", func() {
  30. fiveMinAgo, _ := time.ParseDuration("-5m")
  31. expected := now.Add(fiveMinAgo)
  32. res, err := tr.ParseFrom()
  33. So(err, ShouldBeNil)
  34. So(res.Unix(), ShouldEqual, expected.Unix())
  35. })
  36. Convey("now ", func() {
  37. res, err := tr.ParseTo()
  38. So(err, ShouldBeNil)
  39. So(res.Unix(), ShouldEqual, now.Unix())
  40. })
  41. })
  42. Convey("Can parse 5h, now-10m", func() {
  43. tr := TimeRange{
  44. From: "5h",
  45. To: "now-10m",
  46. now: now,
  47. }
  48. Convey("5h ago ", func() {
  49. fiveHourAgo, _ := time.ParseDuration("-5h")
  50. expected := now.Add(fiveHourAgo)
  51. res, err := tr.ParseFrom()
  52. So(err, ShouldBeNil)
  53. So(res.Unix(), ShouldEqual, expected.Unix())
  54. })
  55. Convey("now-10m ", func() {
  56. tenMinAgo, _ := time.ParseDuration("-10m")
  57. expected := now.Add(tenMinAgo)
  58. res, err := tr.ParseTo()
  59. So(err, ShouldBeNil)
  60. So(res.Unix(), ShouldEqual, expected.Unix())
  61. })
  62. })
  63. Convey("can parse unix epocs", func() {
  64. var err error
  65. tr := TimeRange{
  66. From: "1474973725473",
  67. To: "1474975757930",
  68. now: now,
  69. }
  70. res, err := tr.ParseFrom()
  71. So(err, ShouldBeNil)
  72. So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, int64(1474973725473))
  73. res, err = tr.ParseTo()
  74. So(err, ShouldBeNil)
  75. So(res.UnixNano()/int64(time.Millisecond), ShouldEqual, int64(1474975757930))
  76. })
  77. Convey("Cannot parse asdf", func() {
  78. var err error
  79. tr := TimeRange{
  80. From: "asdf",
  81. To: "asdf",
  82. now: now,
  83. }
  84. _, err = tr.ParseFrom()
  85. So(err, ShouldNotBeNil)
  86. _, err = tr.ParseTo()
  87. So(err, ShouldNotBeNil)
  88. })
  89. })
  90. }