Skip to content
Snippets Groups Projects
Unverified Commit c6599093 authored by Cal Herries's avatar Cal Herries Committed by GitHub
Browse files

Add test for temporal extract and now function (#26922)

* Add now with extract test

* Test against UTC too

* Refactor based on Ngoc's suggestion
parent 86eb24a6
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
[java-time :as t]
[metabase.driver :as driver]
[metabase.models :refer [Card]]
[metabase.query-processor.timezone :as qp.timezone]
[metabase.test :as mt]
[metabase.util.date-2 :as u.date]))
......@@ -389,6 +390,38 @@
:limit 1})
mt/rows first))))))
(defn- close-minute?
"Tests whether two minute integers are within 1 minute of each other on the clock.
0 and 59 are considered close."
[a b]
(or (<= (mod (- b a) 60) 1)
(<= (mod (- a b) 60) 1)))
(defn- close-hour?
"Tests whether two hour integers are within 1 hour of each other on the clock.
0 and 23 are considered close."
[a b]
(or (<= (mod (- b a) 24) 1)
(<= (mod (- a b) 24) 1)))
(deftest now-with-extract-test
(mt/test-drivers (mt/normal-drivers-with-feature :now :temporal-extract)
(testing "now should work with temporal extract functions according to the report timezone"
(doseq [timezone ["UTC" "Asia/Kathmandu"]] ; UTC+5:45 all year
(mt/with-temporary-setting-values [report-timezone timezone]
(let [[minute hour] (->> (mt/run-mbql-query venues
{:expressions {"minute" [:get-minute [:now]]
"hour" [:get-hour [:now]]}
:fields [[:expression "minute"]
[:expression "hour"]]
:limit 1})
(mt/formatted-rows [int int])
first)
results-timezone (mt/with-everything-store (qp.timezone/results-timezone-id))
now (t/local-date-time (t/zone-id results-timezone))]
(is (true? (close-minute? minute (.getMinute now)))
(is (true? (close-hour? hour (.getHour now)))))))))))
(deftest datetime-math-with-extract-test
(mt/test-drivers (mt/normal-drivers-with-feature :date-arithmetics)
(mt/dataset times-mixed
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment