Back
>> (clj)
(source)function
(>> t n-or-d)
shift Temporal forward
Examples
tick
(ns tick.api-test
(:require
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
[tick.core :as t]
[tick.locale-en-us]
[tick.protocols :as p]
[cljc.java-time.clock]
[cljc.java-time.instant]
[cljc.java-time.day-of-week]
[cljc.java-time.month]
[cljc.java-time.year]
#?@(:cljs [[java.time :refer [Instant]]]))
#?(:clj
(:import [java.time Instant])))
(let [now (t/instant)
from (t/<< now (t/new-duration 10 :seconds))
to (t/>> now (t/new-duration 10 :seconds))]
(is (= (t/new-duration 20 :seconds) (t/between from to) ))
(is (= 20 (t/between from to :seconds))))
(is
(= (t/new-duration 48 :hours)
(t/between (t/beginning (t/today)) (t/end (t/tomorrow)))))
(let [start (t/date-time "2020-01-01T12:00")
end (t/date-time "2020-01-01T12:02")]
(is
(=
(t/new-duration 2 :minutes)
(t/between start end))
(= 2 (t/between start end :minutes))))
(is
(=
(t/new-duration 30 :minutes)
(t/between (t/new-time 11 0 0) (t/new-time 11 30 0))))
(testing "LocalDate"
(let [start (t/date "2020-01-01")
end (t/date "2020-01-02")]
(is (= (t/new-period 1 :days)
(t/between start end)))
(is (= 1 (t/between start end :days))))))
(deftest comparison-test
(let [point (t/truncate (t/instant) :millis)
later (t/>> point (t/new-duration 1 :millis))]
(testing "shifting inst"
(let [i (t/inst)]
(is (= i (-> i
(t/>> (t/new-duration 10 :seconds))
(t/<< (t/new-duration 10 :seconds)))))))
(testing "max-min"
(is (= later (t/max point later point later)))
(is (= point (t/min point later point later))))
(testing "max-min key"
(is (= {:foo later} (t/max-key :foo {:foo point} {:foo later} {:foo point} {:foo later})))
(is (= {:foo point} (t/min-key :foo {:foo point} {:foo later} {:foo point} {:foo later}))))
(testing "comparables not="
(doseq [point (point-in-time-comparable point)]
(testing "comparables ="
(is (apply t/= point (point-in-time-comparable point)))
(is (apply t/>= point (point-in-time-comparable point))))
(is (apply t/<= point (point-in-time-comparable later))))
(doseq [later (point-in-time-comparable later)]
(is (apply t/>= later (point-in-time-comparable point))))
(testing "Instants and ZonedDateTimes should be equals if represents the same point in time"
(is (t/=
(t/instant (t/clock (t/instant "2017-10-31T16:00:00Z")))
(t/zoned-date-time "2017-10-31T16:00:00Z[UTC]"))))
(is
(t/<
(t/now)
(t/>> (t/now) (t/new-duration 10 :seconds))
(t/>> (t/now) (t/new-duration 20 :seconds))))
(is
(t/>
(t/>> (t/now) (t/new-duration 20 :seconds))
(t/>> (t/now) (t/new-duration 10 :seconds))
(t/now)))
(is (not
(t/<
(t/now)
(t/>> (t/now) (t/new-duration 20 :seconds))
(t/>> (t/now) (t/new-duration 10 :seconds)))))
(let [at (t/now)]
(is (t/<= at at (t/>> at (t/new-duration 1 :seconds))))
(is (t/>= at at (t/<< at (t/new-duration 10 :seconds)))))
(deftest coincidence-test
(let [int-beginning (t/instant "2020-02-02T00:00:00Z")
int-end (t/>> int-beginning (t/of-hours 2))
interval {:tick/beginning int-beginning
:tick/end int-end}]
(is (t/coincident? interval (t/>> int-beginning (t/of-hours 1))))
(is (not (t/coincident? interval (t/<< int-beginning (t/of-hours 1)))))
(is (t/coincident? interval int-beginning))
(is (t/coincident? interval int-end))
(is (t/coincident? interval interval))
(is (not (t/coincident? interval (-> interval
(update :tick/end #(t/>> % (t/of-nanos 1))))))))
(testing "non-interval coincidence"
(doseq [[start-f new-amount] [[t/date t/of-days] [t/date-time t/of-hours]]]
(let [start (start-f)
end (t/>> start (new-amount 2))]
(is (t/coincident? start end (t/>> start (new-amount 1))))
(is (not (t/coincident? start end (t/<< start (new-amount 1)))))
(is (t/coincident? start end start))
(is (t/coincident? start end end))))))
tick
(ns tick.alpha.interval-test
(:require
[clojure.spec.alpha :as s]
[tick.core :as t]
[tick.protocols :as p]
[clojure.test
:refer [deftest is testing run-tests]
:refer-macros [deftest is testing run-tests]]
[tick.alpha.interval :as ti]
#?@(:cljs [[java.time :refer [Instant LocalDateTime LocalTime]]]))
#?(:clj
(:import [java.time LocalDateTime Instant LocalTime])))
(defn moment [t]
(ti/new-interval
t
(t/>> t (t/new-duration 3 :seconds))))
borgeby/jarl
(ns jarl.builtins.http-test
(:require [jarl.builtins.http :refer [builtin-http-send content-type create-request parse-timeout]]
[jarl.exceptions :as errors]
[jarl.types :refer [rego-equal?]]
[jarl.http-client :as http-client]
[jarl.time :as time]
#?(:clj [clojure.test :refer [deftest is testing]]
:cljs [cljs.test :refer [deftest is testing]])
[tick.core :as t]))
(testing "force cache duration"
(let [counter (atom 0)
req {"method" "get" "url" "http://borge.by/bar" "force_cache" true "force_cache_duration_seconds" 30}]
(with-redefs [http-client/send-request
(fn [_]
(swap! counter inc)
{:status 200 :headers {"content-type" "application/json"} :body "{\"foo\":\"bar\"}"})]
(let [res {"body" {"foo" "bar"} "headers" {"content-type" "application/json"}
"raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
now (time/now-ns)
exp (time/instant->ns (t/>> (t/now) (t/new-duration 60 :seconds)))
bctx {:time-now-ns now}]
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 minute
(is (= @counter 2))))))
(testing "cache from response headers, cache-control"
(let [counter (atom 0)
req {"method" "get" "url" "http://borge.by" "cache" true}]
(with-redefs [http-client/send-request
(fn [_]
(swap! counter inc)
{:status 200
; note upper-case keys
:headers {"Content-Type" "application/json"
"Cache-Control" "ignored, max-age=30, also=ignored"
"Date" "Sat, 01 Jan 2022 15:10:05 GMT"}
:body "{\"foo\":\"bar\"}"})]
(let [res {"body" {"foo" "bar"}
"headers" {"content-type" "application/json"
"cache-control" "ignored, max-age=30, also=ignored"
"date" "Sat, 01 Jan 2022 15:10:05 GMT"}
"raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
now (t/instant (t/zoned-date-time "2022-01-01T15:10:05Z"))
exp (time/instant->ns (t/>> now (t/new-duration 60 :seconds)))
bctx {:time-now-ns (time/instant->ns now)}]
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 minute
(is (= @counter 2))))))
(testing "cache from response headers, expires"
(let [counter (atom 0)
req {"method" "get" "url" "http://borge.by/jarl" "cache" true}]
(with-redefs [http-client/send-request
(fn [_]
(swap! counter inc)
{:status 200
:headers {"content-type" "application/json"
"date" "Sat, 01 Jan 2022 15:10:05 GMT"
"expires" "Sat, 01 Jan 2022 15:40:05 GMT"}
:body "{\"foo\":\"bar\"}"})]
(let [res {"body" {"foo" "bar"}
"headers" {"content-type" "application/json"
"date" "Sat, 01 Jan 2022 15:10:05 GMT"
"expires" "Sat, 01 Jan 2022 15:40:05 GMT"}
"raw_body" "{\"foo\":\"bar\"}" "status" "200 OK" "status_code" 200}
now (t/instant (t/zoned-date-time "2022-01-01T15:10:05Z"))
exp (time/instant->ns (t/>> now (t/new-duration 60 :minutes)))
bctx {:time-now-ns (time/instant->ns now)}]
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context bctx}) res))
(is (= @counter 1))
(is (= (builtin-http-send {:args [req] :builtin-context {:time-now-ns exp}}) res)) ; fast-forward 1 hour
(is (= @counter 2)))))))