window_test.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. package kubecost
  2. import (
  3. "fmt"
  4. "github.com/opencost/opencost/pkg/util/timeutil"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/opencost/opencost/pkg/env"
  9. )
  10. func TestRoundBack(t *testing.T) {
  11. boulder := time.FixedZone("Boulder", -7*60*60)
  12. beijing := time.FixedZone("Beijing", 8*60*60)
  13. to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
  14. tb := RoundBack(to, 24*time.Hour)
  15. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  16. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  17. }
  18. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
  19. tb = RoundBack(to, 24*time.Hour)
  20. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  21. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  22. }
  23. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
  24. tb = RoundBack(to, 24*time.Hour)
  25. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  26. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  27. }
  28. to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
  29. tb = RoundBack(to, 24*time.Hour)
  30. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  31. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  32. }
  33. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
  34. tb = RoundBack(to, 24*time.Hour)
  35. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  36. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  37. }
  38. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
  39. tb = RoundBack(to, 24*time.Hour)
  40. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  41. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  42. }
  43. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
  44. tb = RoundBack(to, 24*time.Hour)
  45. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  46. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  47. }
  48. to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
  49. tb = RoundBack(to, 24*time.Hour)
  50. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  51. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  52. }
  53. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  54. tb = RoundBack(to, 24*time.Hour)
  55. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  56. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  57. }
  58. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
  59. tb = RoundBack(to, 24*time.Hour)
  60. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  61. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  62. }
  63. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
  64. tb = RoundBack(to, 24*time.Hour)
  65. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  66. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  67. }
  68. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  69. tb = RoundBack(to, 24*time.Hour)
  70. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  71. t.Fatalf("RoundBack: expected 2020-01-01T00:00:00Z; actual %s", tb)
  72. }
  73. }
  74. func TestRoundForward(t *testing.T) {
  75. boulder := time.FixedZone("Boulder", -7*60*60)
  76. beijing := time.FixedZone("Beijing", 8*60*60)
  77. to := time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)
  78. tb := RoundForward(to, 24*time.Hour)
  79. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, boulder)) {
  80. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00-07:00; actual %s", tb)
  81. }
  82. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, boulder)
  83. tb = RoundForward(to, 24*time.Hour)
  84. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  85. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  86. }
  87. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, boulder)
  88. tb = RoundForward(to, 24*time.Hour)
  89. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  90. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  91. }
  92. to = time.Date(2020, time.January, 1, 23, 37, 48, 0, boulder)
  93. tb = RoundForward(to, 24*time.Hour)
  94. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, boulder)) {
  95. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00-07:00; actual %s", tb)
  96. }
  97. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)
  98. tb = RoundForward(to, 24*time.Hour)
  99. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, beijing)) {
  100. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00+08:00; actual %s", tb)
  101. }
  102. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, beijing)
  103. tb = RoundForward(to, 24*time.Hour)
  104. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  105. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  106. }
  107. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, beijing)
  108. tb = RoundForward(to, 24*time.Hour)
  109. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  110. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  111. }
  112. to = time.Date(2020, time.January, 1, 23, 59, 59, 0, beijing)
  113. tb = RoundForward(to, 24*time.Hour)
  114. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, beijing)) {
  115. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00+08:00; actual %s", tb)
  116. }
  117. to = time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)
  118. tb = RoundForward(to, 24*time.Hour)
  119. if !tb.Equal(time.Date(2020, time.January, 1, 0, 0, 0, 0, time.UTC)) {
  120. t.Fatalf("RoundForward: expected 2020-01-01T00:00:00Z; actual %s", tb)
  121. }
  122. to = time.Date(2020, time.January, 1, 0, 0, 1, 0, time.UTC)
  123. tb = RoundForward(to, 24*time.Hour)
  124. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  125. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  126. }
  127. to = time.Date(2020, time.January, 1, 12, 37, 48, 0, time.UTC)
  128. tb = RoundForward(to, 24*time.Hour)
  129. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  130. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  131. }
  132. to = time.Date(2020, time.January, 1, 23, 59, 0, 0, time.UTC)
  133. tb = RoundForward(to, 24*time.Hour)
  134. if !tb.Equal(time.Date(2020, time.January, 2, 0, 0, 0, 0, time.UTC)) {
  135. t.Fatalf("RoundForward: expected 2020-01-02T00:00:00Z; actual %s", tb)
  136. }
  137. }
  138. func TestParseWindowUTC(t *testing.T) {
  139. now := time.Now().UTC()
  140. // "today" should span Now() and not produce an error
  141. today, err := ParseWindowUTC("today")
  142. if err != nil {
  143. t.Fatalf(`unexpected error parsing "today": %s`, err)
  144. }
  145. if today.Duration().Hours() != 24 {
  146. t.Fatalf(`expect: window "today" to have duration 24 hour; actual: %f hours`, today.Duration().Hours())
  147. }
  148. if !today.Contains(time.Now().UTC()) {
  149. t.Fatalf(`expect: window "today" to contain now; actual: %s`, today)
  150. }
  151. // "yesterday" should span Now() and not produce an error
  152. yesterday, err := ParseWindowUTC("yesterday")
  153. if err != nil {
  154. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  155. }
  156. if yesterday.Duration().Hours() != 24 {
  157. t.Fatalf(`expect: window "yesterday" to have duration 24 hour; actual: %f hours`, yesterday.Duration().Hours())
  158. }
  159. if !yesterday.End().Before(time.Now().UTC()) {
  160. t.Fatalf(`expect: window "yesterday" to end before now; actual: %s ends after %s`, yesterday, time.Now().UTC())
  161. }
  162. week, err := ParseWindowUTC("week")
  163. hoursThisWeek := float64(time.Now().UTC().Weekday()) * 24.0
  164. if err != nil {
  165. t.Fatalf(`unexpected error parsing "week": %s`, err)
  166. }
  167. if week.Duration().Hours() < hoursThisWeek {
  168. t.Fatalf(`expect: window "week" to have at least %f hours; actual: %f hours`, hoursThisWeek, week.Duration().Hours())
  169. }
  170. if week.End().After(time.Now().UTC()) {
  171. t.Fatalf(`expect: window "week" to end before now; actual: %s ends after %s`, week, time.Now().UTC())
  172. }
  173. month, err := ParseWindowUTC("month")
  174. hoursThisMonth := float64(time.Now().UTC().Day()) * 24.0
  175. if err != nil {
  176. t.Fatalf(`unexpected error parsing "month": %s`, err)
  177. }
  178. if month.Duration().Hours() > hoursThisMonth || month.Duration().Hours() < (hoursThisMonth-24.0) {
  179. t.Fatalf(`expect: window "month" to have approximately %f hours; actual: %f hours`, hoursThisMonth, month.Duration().Hours())
  180. }
  181. if !month.End().Before(time.Now().UTC()) {
  182. t.Fatalf(`expect: window "month" to end before now; actual: %s ends after %s`, month, time.Now().UTC())
  183. }
  184. // TODO lastweek
  185. lastmonth, err := ParseWindowUTC("lastmonth")
  186. monthMinHours := float64(24 * 28)
  187. monthMaxHours := float64(24 * 31)
  188. firstOfMonth := now.Truncate(time.Hour * 24).Add(-24 * time.Hour * time.Duration(now.Day()-1))
  189. if err != nil {
  190. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  191. }
  192. if lastmonth.Duration().Hours() > monthMaxHours || lastmonth.Duration().Hours() < monthMinHours {
  193. t.Fatalf(`expect: window "lastmonth" to have approximately %f hours; actual: %f hours`, hoursThisMonth, lastmonth.Duration().Hours())
  194. }
  195. if !lastmonth.End().Equal(firstOfMonth) {
  196. t.Fatalf(`expect: window "lastmonth" to end on the first of the current month; actual: %s doesn't end on %s`, lastmonth, firstOfMonth)
  197. }
  198. ago12h := time.Now().UTC().Add(-12 * time.Hour)
  199. ago36h := time.Now().UTC().Add(-36 * time.Hour)
  200. ago60h := time.Now().UTC().Add(-60 * time.Hour)
  201. // "24h" should have 24 hour duration and not produce an error
  202. dur24h, err := ParseWindowUTC("24h")
  203. if err != nil {
  204. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  205. }
  206. if dur24h.Duration().Hours() != 24 {
  207. t.Fatalf(`expect: window "24h" to have duration 24 hour; actual: %f hours`, dur24h.Duration().Hours())
  208. }
  209. if !dur24h.Contains(ago12h) {
  210. t.Fatalf(`expect: window "24h" to contain 12 hours ago; actual: %s doesn't contain %s`, dur24h, ago12h)
  211. }
  212. if dur24h.Contains(ago36h) {
  213. t.Fatalf(`expect: window "24h" to not contain 36 hours ago; actual: %s contains %s`, dur24h, ago36h)
  214. }
  215. // "2d" should have 2 day duration and not produce an error
  216. dur2d, err := ParseWindowUTC("2d")
  217. if err != nil {
  218. t.Fatalf(`unexpected error parsing "2d": %s`, err)
  219. }
  220. if dur2d.Duration().Hours() != 48 {
  221. t.Fatalf(`expect: window "2d" to have duration 48 hour; actual: %f hours`, dur2d.Duration().Hours())
  222. }
  223. if !dur2d.Contains(ago36h) {
  224. t.Fatalf(`expect: window "2d" to contain 36 hours ago; actual: %s doesn't contain %s`, dur2d, ago36h)
  225. }
  226. if dur2d.Contains(ago60h) {
  227. t.Fatalf(`expect: window "2d" to not contain 60 hours ago; actual: %s contains %s`, dur2d, ago60h)
  228. }
  229. // "24h offset 14h" should have 24 hour duration and not produce an error
  230. dur24hOff14h, err := ParseWindowUTC("24h offset 14h")
  231. if err != nil {
  232. t.Fatalf(`unexpected error parsing "24h offset 14h": %s`, err)
  233. }
  234. if dur24hOff14h.Duration().Hours() != 24 {
  235. t.Fatalf(`expect: window "24h offset 14h" to have duration 24 hour; actual: %f hours`, dur24hOff14h.Duration().Hours())
  236. }
  237. if dur24hOff14h.Contains(ago12h) {
  238. t.Fatalf(`expect: window "24h offset 14h" not to contain 12 hours ago; actual: %s contains %s`, dur24hOff14h, ago12h)
  239. }
  240. if !dur24hOff14h.Contains(ago36h) {
  241. t.Fatalf(`expect: window "24h offset 14h" to contain 36 hours ago; actual: %s does not contain %s`, dur24hOff14h, ago36h)
  242. }
  243. april152020, _ := time.Parse(time.RFC3339, "2020-04-15T00:00:00Z")
  244. april102020, _ := time.Parse(time.RFC3339, "2020-04-10T00:00:00Z")
  245. april052020, _ := time.Parse(time.RFC3339, "2020-04-05T00:00:00Z")
  246. // "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z" should have 96 hour duration and not produce an error
  247. april8to12, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
  248. if err != nil {
  249. t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
  250. }
  251. if april8to12.Duration().Hours() != 96 {
  252. t.Fatalf(`expect: window %s to have duration 96 hour; actual: %f hours`, april8to12, april8to12.Duration().Hours())
  253. }
  254. if !april8to12.Contains(april102020) {
  255. t.Fatalf(`expect: window April 8-12 to contain April 10; actual: %s doesn't contain %s`, april8to12, april102020)
  256. }
  257. if april8to12.Contains(april052020) {
  258. t.Fatalf(`expect: window April 8-12 to not contain April 5; actual: %s contains %s`, april8to12, april052020)
  259. }
  260. if april8to12.Contains(april152020) {
  261. t.Fatalf(`expect: window April 8-12 to not contain April 15; actual: %s contains %s`, april8to12, april152020)
  262. }
  263. march152020, _ := time.Parse(time.RFC3339, "2020-03-15T00:00:00Z")
  264. march102020, _ := time.Parse(time.RFC3339, "2020-03-10T00:00:00Z")
  265. march052020, _ := time.Parse(time.RFC3339, "2020-03-05T00:00:00Z")
  266. // "1583712000,1583884800" should have 48 hour duration and not produce an error
  267. march9to11, err := ParseWindowUTC("1583712000,1583884800")
  268. if err != nil {
  269. t.Fatalf(`unexpected error parsing "2020-04-08T00:00:00Z,2020-04-12T00:00:00Z": %s`, err)
  270. }
  271. if march9to11.Duration().Hours() != 48 {
  272. t.Fatalf(`expect: window %s to have duration 48 hour; actual: %f hours`, march9to11, march9to11.Duration().Hours())
  273. }
  274. if !march9to11.Contains(march102020) {
  275. t.Fatalf(`expect: window March 9-11 to contain March 10; actual: %s doesn't contain %s`, march9to11, march102020)
  276. }
  277. if march9to11.Contains(march052020) {
  278. t.Fatalf(`expect: window March 9-11 to not contain March 5; actual: %s contains %s`, march9to11, march052020)
  279. }
  280. if march9to11.Contains(march152020) {
  281. t.Fatalf(`expect: window March 9-11 to not contain March 15; actual: %s contains %s`, march9to11, march152020)
  282. }
  283. }
  284. func BenchmarkParseWindowUTC(b *testing.B) {
  285. for n := 0; n < b.N; n++ {
  286. _, err := ParseWindowUTC("2020-04-08T00:00:00Z,2020-04-12T00:00:00Z")
  287. if err != nil {
  288. b.Fatalf("error running benchmark: %s", err.Error())
  289. }
  290. }
  291. }
  292. func TestParseWindowWithOffsetString(t *testing.T) {
  293. // ParseWindowWithOffsetString should equal ParseWindowUTC when location == "UTC"
  294. // for all window string formats
  295. todayUTC, err := ParseWindowUTC("today")
  296. if err != nil {
  297. t.Fatalf(`unexpected error parsing "today": %s`, err)
  298. }
  299. todayTZ, err := ParseWindowWithOffsetString("today", "UTC")
  300. if err != nil {
  301. t.Fatalf(`unexpected error parsing "today": %s`, err)
  302. }
  303. if !todayUTC.ApproximatelyEqual(todayTZ, time.Millisecond) {
  304. t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "UTC"; actual: %s not equal %s`, todayUTC, todayTZ)
  305. }
  306. yesterdayUTC, err := ParseWindowUTC("yesterday")
  307. if err != nil {
  308. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  309. }
  310. yesterdayTZ, err := ParseWindowWithOffsetString("yesterday", "UTC")
  311. if err != nil {
  312. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  313. }
  314. if !yesterdayUTC.ApproximatelyEqual(yesterdayTZ, time.Millisecond) {
  315. t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "UTC"; actual: %s not equal %s`, yesterdayUTC, yesterdayTZ)
  316. }
  317. weekUTC, err := ParseWindowUTC("week")
  318. if err != nil {
  319. t.Fatalf(`unexpected error parsing "week": %s`, err)
  320. }
  321. weekTZ, err := ParseWindowWithOffsetString("week", "UTC")
  322. if err != nil {
  323. t.Fatalf(`unexpected error parsing "week": %s`, err)
  324. }
  325. if !weekUTC.ApproximatelyEqual(weekTZ, time.Millisecond) {
  326. t.Fatalf(`expect: window "week" UTC to equal "week" with timezone "UTC"; actual: %s not equal %s`, weekUTC, weekTZ)
  327. }
  328. monthUTC, err := ParseWindowUTC("month")
  329. if err != nil {
  330. t.Fatalf(`unexpected error parsing "month": %s`, err)
  331. }
  332. monthTZ, err := ParseWindowWithOffsetString("month", "UTC")
  333. if err != nil {
  334. t.Fatalf(`unexpected error parsing "month": %s`, err)
  335. }
  336. if !monthUTC.ApproximatelyEqual(monthTZ, time.Millisecond) {
  337. t.Fatalf(`expect: window "month" UTC to equal "month" with timezone "UTC"; actual: %s not equal %s`, monthUTC, monthTZ)
  338. }
  339. lastweekUTC, err := ParseWindowUTC("lastweek")
  340. if err != nil {
  341. t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
  342. }
  343. lastweekTZ, err := ParseWindowWithOffsetString("lastweek", "UTC")
  344. if err != nil {
  345. t.Fatalf(`unexpected error parsing "lastweek": %s`, err)
  346. }
  347. if !lastweekUTC.ApproximatelyEqual(lastweekTZ, time.Millisecond) {
  348. t.Fatalf(`expect: window "lastweek" UTC to equal "lastweek" with timezone "UTC"; actual: %s not equal %s`, lastweekUTC, lastweekTZ)
  349. }
  350. lastmonthUTC, err := ParseWindowUTC("lastmonth")
  351. if err != nil {
  352. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  353. }
  354. lastmonthTZ, err := ParseWindowWithOffsetString("lastmonth", "UTC")
  355. if err != nil {
  356. t.Fatalf(`unexpected error parsing "lastmonth": %s`, err)
  357. }
  358. if !lastmonthUTC.ApproximatelyEqual(lastmonthTZ, time.Millisecond) {
  359. t.Fatalf(`expect: window "lastmonth" UTC to equal "lastmonth" with timezone "UTC"; actual: %s not equal %s`, lastmonthUTC, lastmonthTZ)
  360. }
  361. dur10mUTC, err := ParseWindowUTC("10m")
  362. if err != nil {
  363. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  364. }
  365. dur10mTZ, err := ParseWindowWithOffsetString("10m", "UTC")
  366. if err != nil {
  367. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  368. }
  369. if !dur10mUTC.ApproximatelyEqual(dur10mTZ, time.Millisecond) {
  370. t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "UTC"; actual: %s not equal %s`, dur10mUTC, dur10mTZ)
  371. }
  372. dur24hUTC, err := ParseWindowUTC("24h")
  373. if err != nil {
  374. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  375. }
  376. dur24hTZ, err := ParseWindowWithOffsetString("24h", "UTC")
  377. if err != nil {
  378. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  379. }
  380. if !dur24hUTC.ApproximatelyEqual(dur24hTZ, time.Millisecond) {
  381. t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "UTC"; actual: %s not equal %s`, dur24hUTC, dur24hTZ)
  382. }
  383. dur37dUTC, err := ParseWindowUTC("37d")
  384. if err != nil {
  385. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  386. }
  387. dur37dTZ, err := ParseWindowWithOffsetString("37d", "UTC")
  388. if err != nil {
  389. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  390. }
  391. if !dur37dUTC.ApproximatelyEqual(dur37dTZ, time.Millisecond) {
  392. t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "UTC"; actual: %s not equal %s`, dur37dUTC, dur37dTZ)
  393. }
  394. // ParseWindowWithOffsetString should be the correct relative to ParseWindowUTC; i.e.
  395. // - for durations, the times should match, but the representations should differ
  396. // by the number of hours offset
  397. // - for words like "today" and "yesterday", the times may not match, in which
  398. // case, for example, "today" UTC-08:00 might equal "yesterday" UTC
  399. // fmtWindow only compares date and time to the minute, not second or
  400. // timezone. Helper for comparing timezone shifted windows.
  401. fmtWindow := func(w Window) string {
  402. s := "nil"
  403. if w.start != nil {
  404. s = w.start.Format("2006-01-02T15:04")
  405. }
  406. e := "nil"
  407. if w.end != nil {
  408. e = w.end.Format("2006-01-02T15:04")
  409. }
  410. return fmt.Sprintf("[%s, %s]", s, e)
  411. }
  412. // Test UTC-08:00 (California), UTC+03:00 (Moscow), UTC+12:00 (New Zealand), and UTC itself
  413. for _, offsetHrs := range []int{-8, 3, 12, 0} {
  414. offStr := fmt.Sprintf("+%02d:00", offsetHrs)
  415. if offsetHrs < 0 {
  416. offStr = fmt.Sprintf("-%02d:00", -offsetHrs)
  417. }
  418. off := time.Duration(offsetHrs) * time.Hour
  419. dur10mTZ, err = ParseWindowWithOffsetString("10m", offStr)
  420. if err != nil {
  421. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  422. }
  423. if !dur10mTZ.ApproximatelyEqual(dur10mUTC, time.Second) {
  424. t.Fatalf(`expect: window "10m" UTC to equal "10m" with timezone "%s"; actual: %s not equal %s`, offStr, dur10mUTC, dur10mTZ)
  425. }
  426. if fmtWindow(dur10mTZ.Shift(-off)) != fmtWindow(dur10mUTC) {
  427. t.Fatalf(`expect: date, hour, and minute of window "10m" UTC to equal that of "10m" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur10mUTC), fmtWindow(dur10mTZ.Shift(-off)))
  428. }
  429. dur24hTZ, err = ParseWindowWithOffsetString("24h", offStr)
  430. if err != nil {
  431. t.Fatalf(`unexpected error parsing "24h": %s`, err)
  432. }
  433. if !dur24hTZ.ApproximatelyEqual(dur24hUTC, time.Second) {
  434. t.Fatalf(`expect: window "24h" UTC to equal "24h" with timezone "%s"; actual: %s not equal %s`, offStr, dur24hUTC, dur24hTZ)
  435. }
  436. if fmtWindow(dur24hTZ.Shift(-off)) != fmtWindow(dur24hUTC) {
  437. t.Fatalf(`expect: date, hour, and minute of window "24h" UTC to equal that of "24h" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur24hUTC), fmtWindow(dur24hTZ.Shift(-off)))
  438. }
  439. dur37dTZ, err = ParseWindowWithOffsetString("37d", offStr)
  440. if err != nil {
  441. t.Fatalf(`unexpected error parsing "37d": %s`, err)
  442. }
  443. if !dur37dTZ.ApproximatelyEqual(dur37dUTC, time.Second) {
  444. t.Fatalf(`expect: window "37d" UTC to equal "37d" with timezone "%s"; actual: %s not equal %s`, offStr, dur37dUTC, dur37dTZ)
  445. }
  446. if fmtWindow(dur37dTZ.Shift(-off)) != fmtWindow(dur37dUTC) {
  447. t.Fatalf(`expect: date, hour, and minute of window "37d" UTC to equal that of "37d" %s shifted by %s; actual: %s not equal %s`, offStr, off, fmtWindow(dur37dUTC), fmtWindow(dur37dTZ.Shift(-off)))
  448. }
  449. // "today" and "yesterday" should comply with the current day in each
  450. // respective timezone, depending on if it is ahead of, equal to, or
  451. // behind UTC at the given moment.
  452. todayTZ, err = ParseWindowWithOffsetString("today", offStr)
  453. if err != nil {
  454. t.Fatalf(`unexpected error parsing "today": %s`, err)
  455. }
  456. yesterdayTZ, err = ParseWindowWithOffsetString("yesterday", offStr)
  457. if err != nil {
  458. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  459. }
  460. hoursSinceYesterdayUTC := time.Now().UTC().Sub(time.Now().UTC().Truncate(24.0 * time.Hour)).Hours()
  461. hoursUntilTomorrowUTC := 24.0 - hoursSinceYesterdayUTC
  462. aheadOfUTC := float64(offsetHrs)-hoursUntilTomorrowUTC > 0
  463. behindUTC := float64(offsetHrs)+hoursSinceYesterdayUTC < 0
  464. // yesterday in this timezone should equal today UTC
  465. if aheadOfUTC {
  466. if fmtWindow(yesterdayTZ) != fmtWindow(todayUTC) {
  467. t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, todayUTC)
  468. }
  469. }
  470. // today in this timezone should equal yesterday UTC
  471. if behindUTC {
  472. if fmtWindow(todayTZ) != fmtWindow(yesterdayUTC) {
  473. t.Fatalf(`expect: window "today" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, yesterdayUTC)
  474. }
  475. }
  476. // today in this timezone should equal today UTC, likewise for yesterday
  477. if !aheadOfUTC && !behindUTC {
  478. if fmtWindow(todayTZ) != fmtWindow(todayUTC) {
  479. t.Fatalf(`expect: window "today" UTC to equal "today" with timezone "%s"; actual: %s not equal %s`, offStr, todayTZ, todayUTC)
  480. }
  481. // yesterday in this timezone should equal yesterday UTC
  482. if fmtWindow(yesterdayTZ) != fmtWindow(yesterdayUTC) {
  483. t.Fatalf(`expect: window "yesterday" UTC to equal "yesterday" with timezone "%s"; actual: %s not equal %s`, offStr, yesterdayTZ, yesterdayUTC)
  484. }
  485. }
  486. }
  487. }
  488. func TestWindow_DurationOffsetStrings(t *testing.T) {
  489. w, err := ParseWindowUTC("1d")
  490. if err != nil {
  491. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  492. }
  493. dur, off := w.DurationOffsetStrings()
  494. if dur != "1d" {
  495. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  496. }
  497. if off != "" {
  498. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  499. }
  500. w, err = ParseWindowUTC("3h")
  501. if err != nil {
  502. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  503. }
  504. dur, off = w.DurationOffsetStrings()
  505. if dur != "3h" {
  506. t.Fatalf(`expect: window to be "3h"; actual: "%s"`, dur)
  507. }
  508. if off != "" {
  509. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  510. }
  511. w, err = ParseWindowUTC("10m")
  512. if err != nil {
  513. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  514. }
  515. dur, off = w.DurationOffsetStrings()
  516. if dur != "10m" {
  517. t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
  518. }
  519. if off != "" {
  520. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  521. }
  522. w, err = ParseWindowUTC("1589448338,1589534798")
  523. if err != nil {
  524. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  525. }
  526. dur, off = w.DurationOffsetStrings()
  527. if dur != "1441m" {
  528. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  529. }
  530. if off == "" {
  531. t.Fatalf(`expect: offset to not be ""; actual: "%s"`, off)
  532. }
  533. w, err = ParseWindowUTC("yesterday")
  534. if err != nil {
  535. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  536. }
  537. dur, _ = w.DurationOffsetStrings()
  538. if dur != "1d" {
  539. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  540. }
  541. }
  542. func TestWindow_DurationOffsetForPrometheus(t *testing.T) {
  543. // Set-up and tear-down
  544. thanosEnabled := env.GetBool(env.ThanosEnabledEnvVar, false)
  545. defer env.SetBool(env.ThanosEnabledEnvVar, thanosEnabled)
  546. // Test for Prometheus (env.IsThanosEnabled() == false)
  547. env.SetBool(env.ThanosEnabledEnvVar, false)
  548. if env.IsThanosEnabled() {
  549. t.Fatalf("expected env.IsThanosEnabled() == false")
  550. }
  551. w, err := ParseWindowUTC("1d")
  552. if err != nil {
  553. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  554. }
  555. dur, off, err := w.DurationOffsetForPrometheus()
  556. if err != nil {
  557. t.Fatalf("unexpected error: %s", err)
  558. }
  559. if dur != "1d" {
  560. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  561. }
  562. if off != "" {
  563. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  564. }
  565. w, err = ParseWindowUTC("2h")
  566. if err != nil {
  567. t.Fatalf(`unexpected error parsing "2h": %s`, err)
  568. }
  569. dur, off, err = w.DurationOffsetForPrometheus()
  570. if err != nil {
  571. t.Fatalf("unexpected error: %s", err)
  572. }
  573. if dur != "2h" {
  574. t.Fatalf(`expect: window to be "2h"; actual: "%s"`, dur)
  575. }
  576. if off != "" {
  577. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  578. }
  579. w, err = ParseWindowUTC("10m")
  580. if err != nil {
  581. t.Fatalf(`unexpected error parsing "10m": %s`, err)
  582. }
  583. dur, off, err = w.DurationOffsetForPrometheus()
  584. if err != nil {
  585. t.Fatalf("unexpected error: %s", err)
  586. }
  587. if dur != "10m" {
  588. t.Fatalf(`expect: window to be "10m"; actual: "%s"`, dur)
  589. }
  590. if off != "" {
  591. t.Fatalf(`expect: offset to be ""; actual: "%s"`, off)
  592. }
  593. w, err = ParseWindowUTC("1589448338,1589534798")
  594. if err != nil {
  595. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  596. }
  597. dur, off, err = w.DurationOffsetForPrometheus()
  598. if err != nil {
  599. t.Fatalf("unexpected error: %s", err)
  600. }
  601. if dur != "1441m" {
  602. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  603. }
  604. if !strings.HasPrefix(off, " offset ") {
  605. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  606. }
  607. w, err = ParseWindowUTC("yesterday")
  608. if err != nil {
  609. t.Fatalf(`unexpected error parsing "yesterday": %s`, err)
  610. }
  611. dur, off, err = w.DurationOffsetForPrometheus()
  612. if err != nil {
  613. t.Fatalf("unexpected error: %s", err)
  614. }
  615. if dur != "1d" {
  616. t.Fatalf(`expect: window to be "1d"; actual: "%s"`, dur)
  617. }
  618. if !strings.HasPrefix(off, " offset ") {
  619. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  620. }
  621. // Test for Thanos (env.IsThanosEnabled() == true)
  622. env.SetBool(env.ThanosEnabledEnvVar, true)
  623. if !env.IsThanosEnabled() {
  624. t.Fatalf("expected env.IsThanosEnabled() == true")
  625. }
  626. w, err = ParseWindowUTC("1d")
  627. if err != nil {
  628. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  629. }
  630. dur, off, err = w.DurationOffsetForPrometheus()
  631. if err != nil {
  632. t.Fatalf("unexpected error: %s", err)
  633. }
  634. if dur != "21h" {
  635. t.Fatalf(`expect: window to be "21d"; actual: "%s"`, dur)
  636. }
  637. if off != " offset 3h" {
  638. t.Fatalf(`expect: offset to be " offset 3h"; actual: "%s"`, off)
  639. }
  640. w, err = ParseWindowUTC("2h")
  641. if err != nil {
  642. t.Fatalf(`unexpected error parsing "2h": %s`, err)
  643. }
  644. dur, off, err = w.DurationOffsetForPrometheus()
  645. if err == nil {
  646. t.Fatalf(`expected error (negative duration); got ("%s", "%s")`, dur, off)
  647. }
  648. w, err = ParseWindowUTC("10m")
  649. if err != nil {
  650. t.Fatalf(`unexpected error parsing "1d": %s`, err)
  651. }
  652. dur, off, err = w.DurationOffsetForPrometheus()
  653. if err == nil {
  654. t.Fatalf(`expected error (negative duration); got ("%s", "%s")`, dur, off)
  655. }
  656. w, err = ParseWindowUTC("1589448338,1589534798")
  657. if err != nil {
  658. t.Fatalf(`unexpected error parsing "1589448338,1589534798": %s`, err)
  659. }
  660. dur, off, err = w.DurationOffsetForPrometheus()
  661. if err != nil {
  662. t.Fatalf("unexpected error: %s", err)
  663. }
  664. if dur != "1441m" {
  665. t.Fatalf(`expect: window to be "1441m"; actual: "%s"`, dur)
  666. }
  667. if !strings.HasPrefix(off, " offset ") {
  668. t.Fatalf(`expect: offset to start with " offset "; actual: "%s"`, off)
  669. }
  670. }
  671. // TODO
  672. // func TestWindow_Overlaps(t *testing.T) {}
  673. // TODO
  674. // func TestWindow_Contains(t *testing.T) {}
  675. // TODO
  676. // func TestWindow_Duration(t *testing.T) {}
  677. // TODO
  678. // func TestWindow_End(t *testing.T) {}
  679. // TODO
  680. // func TestWindow_Equal(t *testing.T) {}
  681. // TODO
  682. // func TestWindow_ExpandStart(t *testing.T) {}
  683. // TODO
  684. // func TestWindow_ExpandEnd(t *testing.T) {}
  685. func TestWindow_Expand(t *testing.T) {
  686. t1 := time.Now().Round(time.Hour)
  687. t2 := t1.Add(34 * time.Minute)
  688. t3 := t1.Add(50 * time.Minute)
  689. t4 := t1.Add(84 * time.Minute)
  690. cases := []struct {
  691. windowToExpand Window
  692. windowArgument Window
  693. expected Window
  694. }{
  695. {
  696. windowToExpand: NewClosedWindow(t1, t2),
  697. windowArgument: NewClosedWindow(t3, t4),
  698. expected: NewClosedWindow(t1, t4),
  699. },
  700. {
  701. windowToExpand: NewClosedWindow(t3, t4),
  702. windowArgument: NewClosedWindow(t1, t2),
  703. expected: NewClosedWindow(t1, t4),
  704. },
  705. {
  706. windowToExpand: NewClosedWindow(t1, t3),
  707. windowArgument: NewClosedWindow(t2, t4),
  708. expected: NewClosedWindow(t1, t4),
  709. },
  710. {
  711. windowToExpand: NewClosedWindow(t2, t4),
  712. windowArgument: NewClosedWindow(t1, t3),
  713. expected: NewClosedWindow(t1, t4),
  714. },
  715. {
  716. windowToExpand: Window{},
  717. windowArgument: NewClosedWindow(t1, t2),
  718. expected: NewClosedWindow(t1, t2),
  719. },
  720. {
  721. windowToExpand: NewWindow(nil, &t2),
  722. windowArgument: NewWindow(nil, &t3),
  723. expected: NewWindow(nil, &t3),
  724. },
  725. {
  726. windowToExpand: NewWindow(&t2, nil),
  727. windowArgument: NewWindow(&t1, nil),
  728. expected: NewWindow(&t1, nil),
  729. },
  730. }
  731. for _, c := range cases {
  732. result := c.windowToExpand.Expand(c.windowArgument)
  733. if !result.Equal(c.expected) {
  734. t.Errorf("Expand %s with %s, expected %s but got %s", c.windowToExpand, c.windowArgument, c.expected, result)
  735. }
  736. }
  737. }
  738. // TODO
  739. // func TestWindow_Start(t *testing.T) {}
  740. // TODO
  741. // func TestWindow_String(t *testing.T) {}
  742. func TestWindow_GetPercentInWindow(t *testing.T) {
  743. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  744. dayEnd := dayStart.Add(timeutil.Day)
  745. window := NewClosedWindow(dayStart, dayEnd)
  746. testcases := map[string]struct {
  747. window Window
  748. itemStart time.Time
  749. itemEnd time.Time
  750. expected float64
  751. }{
  752. "matching start/matching end": {
  753. window: window,
  754. itemStart: dayStart,
  755. itemEnd: dayEnd,
  756. expected: 1.0,
  757. },
  758. "matching start/contained end": {
  759. window: window,
  760. itemStart: dayStart,
  761. itemEnd: dayEnd.Add(-time.Hour * 6),
  762. expected: 1.0,
  763. },
  764. "contained start/matching end": {
  765. window: window,
  766. itemStart: dayStart.Add(time.Hour * 6),
  767. itemEnd: dayEnd,
  768. expected: 1.0,
  769. },
  770. "contained start/contained end": {
  771. window: window,
  772. itemStart: dayStart.Add(time.Hour * 6),
  773. itemEnd: dayEnd.Add(-time.Hour * 6),
  774. expected: 1.0,
  775. },
  776. "before start/contained end": {
  777. window: window,
  778. itemStart: dayStart.Add(-time.Hour * 12),
  779. itemEnd: dayEnd.Add(-time.Hour * 12),
  780. expected: 0.5,
  781. },
  782. "before start/before end": {
  783. window: window,
  784. itemStart: dayStart.Add(-time.Hour * 24),
  785. itemEnd: dayEnd.Add(-time.Hour * 24),
  786. expected: 0.0,
  787. },
  788. "contained start/after end": {
  789. window: window,
  790. itemStart: dayStart.Add(time.Hour * 12),
  791. itemEnd: dayEnd.Add(time.Hour * 12),
  792. expected: 0.5,
  793. },
  794. "after start/after end": {
  795. window: window,
  796. itemStart: dayStart.Add(time.Hour * 24),
  797. itemEnd: dayEnd.Add(time.Hour * 24),
  798. expected: 0.0,
  799. },
  800. "before start/after end": {
  801. window: window,
  802. itemStart: dayStart.Add(-time.Hour * 12),
  803. itemEnd: dayEnd.Add(time.Hour * 12),
  804. expected: 0.5,
  805. },
  806. }
  807. for name, tc := range testcases {
  808. t.Run(name, func(t *testing.T) {
  809. if actual := tc.window.GetPercentInWindow(tc.itemStart, tc.itemEnd); actual != tc.expected {
  810. t.Errorf("GetPercentInWindow() = %v, want %v", actual, tc.expected)
  811. }
  812. })
  813. }
  814. }
  815. func TestWindow_GetWindows(t *testing.T) {
  816. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  817. dayEnd := dayStart.Add(timeutil.Day)
  818. loc, _ := time.LoadLocation("America/Vancouver")
  819. testCases := map[string]struct {
  820. start time.Time
  821. end time.Time
  822. windowSize time.Duration
  823. expected []Window
  824. expectedErr bool
  825. }{
  826. "mismatching tz": {
  827. start: dayStart,
  828. end: dayEnd.In(loc),
  829. windowSize: time.Hour,
  830. expected: nil,
  831. expectedErr: true,
  832. },
  833. "hour windows over 1 hours": {
  834. start: dayStart,
  835. end: dayStart.Add(time.Hour),
  836. windowSize: time.Hour,
  837. expected: []Window{
  838. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  839. },
  840. expectedErr: false,
  841. },
  842. "hour windows over 3 hours": {
  843. start: dayStart,
  844. end: dayStart.Add(time.Hour * 3),
  845. windowSize: time.Hour,
  846. expected: []Window{
  847. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  848. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
  849. NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
  850. },
  851. expectedErr: false,
  852. },
  853. "hour windows off hour grid": {
  854. start: dayStart.Add(time.Minute),
  855. end: dayEnd.Add(time.Minute),
  856. windowSize: time.Hour,
  857. expected: nil,
  858. expectedErr: true,
  859. },
  860. "hour windows range not divisible by hour": {
  861. start: dayStart,
  862. end: dayStart.Add(time.Minute * 90),
  863. windowSize: time.Hour,
  864. expected: nil,
  865. expectedErr: true,
  866. },
  867. "day windows over 1 day": {
  868. start: dayStart,
  869. end: dayEnd,
  870. windowSize: timeutil.Day,
  871. expected: []Window{
  872. NewClosedWindow(dayStart, dayEnd),
  873. },
  874. expectedErr: false,
  875. },
  876. "day windows over 3 days": {
  877. start: dayStart,
  878. end: dayStart.Add(timeutil.Day * 3),
  879. windowSize: timeutil.Day,
  880. expected: []Window{
  881. NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
  882. NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
  883. NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
  884. },
  885. expectedErr: false,
  886. },
  887. "day windows off day grid": {
  888. start: dayStart.Add(time.Hour),
  889. end: dayEnd.Add(time.Hour),
  890. windowSize: timeutil.Day,
  891. expected: nil,
  892. expectedErr: true,
  893. },
  894. "day windows range not divisible by day": {
  895. start: dayStart,
  896. end: dayEnd.Add(time.Hour),
  897. windowSize: timeutil.Day,
  898. expected: nil,
  899. expectedErr: true,
  900. },
  901. }
  902. for name, tc := range testCases {
  903. t.Run(name, func(t *testing.T) {
  904. actual, err := GetWindows(tc.start, tc.end, tc.windowSize)
  905. if (err != nil) != tc.expectedErr {
  906. t.Errorf("GetWindows() error = %v, expectedErr %v", err, tc.expectedErr)
  907. return
  908. }
  909. if len(tc.expected) != len(actual) {
  910. t.Errorf("GetWindows() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
  911. }
  912. for i, actualWindow := range actual {
  913. expectedWindow := tc.expected[i]
  914. if !actualWindow.Equal(expectedWindow) {
  915. t.Errorf("GetWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
  916. }
  917. }
  918. })
  919. }
  920. }
  921. func TestWindow_GetWindowsForQueryWindow(t *testing.T) {
  922. dayStart := time.Date(2022, 12, 6, 0, 0, 0, 0, time.UTC)
  923. dayEnd := dayStart.Add(timeutil.Day)
  924. loc, _ := time.LoadLocation("America/Vancouver")
  925. testCases := map[string]struct {
  926. start time.Time
  927. end time.Time
  928. windowSize time.Duration
  929. expected []Window
  930. expectedErr bool
  931. }{
  932. "mismatching tz": {
  933. start: dayStart,
  934. end: dayEnd.In(loc),
  935. windowSize: time.Hour,
  936. expected: nil,
  937. expectedErr: true,
  938. },
  939. "hour windows over 1 hours": {
  940. start: dayStart,
  941. end: dayStart.Add(time.Hour),
  942. windowSize: time.Hour,
  943. expected: []Window{
  944. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  945. },
  946. expectedErr: false,
  947. },
  948. "hour windows over 3 hours": {
  949. start: dayStart,
  950. end: dayStart.Add(time.Hour * 3),
  951. windowSize: time.Hour,
  952. expected: []Window{
  953. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  954. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Hour*2)),
  955. NewClosedWindow(dayStart.Add(time.Hour*2), dayStart.Add(time.Hour*3)),
  956. },
  957. expectedErr: false,
  958. },
  959. "hour windows off hour grid": {
  960. start: dayStart.Add(time.Minute),
  961. end: dayStart.Add(time.Minute * 61),
  962. windowSize: time.Hour,
  963. expected: []Window{
  964. NewClosedWindow(dayStart.Add(time.Minute), dayStart.Add(time.Minute*61)),
  965. },
  966. expectedErr: false,
  967. },
  968. "hour windows range not divisible by hour": {
  969. start: dayStart,
  970. end: dayStart.Add(time.Minute * 90),
  971. windowSize: time.Hour,
  972. expected: []Window{
  973. NewClosedWindow(dayStart, dayStart.Add(time.Hour)),
  974. NewClosedWindow(dayStart.Add(time.Hour), dayStart.Add(time.Minute*90)),
  975. },
  976. expectedErr: false,
  977. },
  978. "day windows over 1 day": {
  979. start: dayStart,
  980. end: dayEnd,
  981. windowSize: timeutil.Day,
  982. expected: []Window{
  983. NewClosedWindow(dayStart, dayEnd),
  984. },
  985. expectedErr: false,
  986. },
  987. "day windows over 3 days": {
  988. start: dayStart,
  989. end: dayStart.Add(timeutil.Day * 3),
  990. windowSize: timeutil.Day,
  991. expected: []Window{
  992. NewClosedWindow(dayStart, dayStart.Add(timeutil.Day)),
  993. NewClosedWindow(dayStart.Add(timeutil.Day), dayStart.Add(timeutil.Day*2)),
  994. NewClosedWindow(dayStart.Add(timeutil.Day*2), dayStart.Add(timeutil.Day*3)),
  995. },
  996. expectedErr: false,
  997. },
  998. "day windows off day grid": {
  999. start: dayStart.Add(time.Hour),
  1000. end: dayEnd.Add(time.Hour),
  1001. windowSize: timeutil.Day,
  1002. expected: []Window{
  1003. NewClosedWindow(dayStart.Add(time.Hour), dayEnd.Add(time.Hour)),
  1004. },
  1005. expectedErr: false,
  1006. },
  1007. "day windows range not divisible by day": {
  1008. start: dayStart,
  1009. end: dayEnd.Add(time.Hour),
  1010. windowSize: timeutil.Day,
  1011. expected: []Window{
  1012. NewClosedWindow(dayStart, dayEnd),
  1013. NewClosedWindow(dayEnd, dayEnd.Add(time.Hour)),
  1014. },
  1015. expectedErr: false,
  1016. },
  1017. }
  1018. for name, tc := range testCases {
  1019. t.Run(name, func(t *testing.T) {
  1020. actual, err := GetWindowsForQueryWindow(tc.start, tc.end, tc.windowSize)
  1021. if (err != nil) != tc.expectedErr {
  1022. t.Errorf("GetWindowsForQueryWindow() error = %v, expectedErr %v", err, tc.expectedErr)
  1023. return
  1024. }
  1025. if len(tc.expected) != len(actual) {
  1026. t.Errorf("GetWindowsForQueryWindow() []window has incorrect length expected: %d, actual: %d", len(tc.expected), len(actual))
  1027. }
  1028. for i, actualWindow := range actual {
  1029. expectedWindow := tc.expected[i]
  1030. if !actualWindow.Equal(expectedWindow) {
  1031. t.Errorf("GetWindowsForQueryWindow() window at index %d were not equal expected: %s, actual %s", i, expectedWindow.String(), actualWindow)
  1032. }
  1033. }
  1034. })
  1035. }
  1036. }