Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Count only the query executions under test (#79726)

Ben Grabowe3708068c393
diff --git a/test/metabase/analytics/stats_test.clj b/test/metabase/analytics/stats_test.clj
index da3d4a38e5d..28ef1aeda3a 100644
--- a/test/metabase/analytics/stats_test.clj
+++ b/test/metabase/analytics/stats_test.clj
@@ -245,22 +245,19 @@
(doseq [tz ["Pacific/Auckland" "Europe/Helsinki"]]
(testing tz
(mt/with-app-db-timezone-id! tz
- (let [get-executions #(:executions (#'stats/execution-metrics))
- before (get-executions)]
- (mt/with-temp [:model/QueryExecution _ (merge query-execution-defaults
- {:started_at (-> (t/offset-date-time (t/zone-id "UTC"))
- (t/minus (t/days 30))
- (t/plus (t/minutes 10)))})]
- (is (= (inc before)
- (get-executions))
- "execution metrics include query executions since 30 days ago"))
- (mt/with-temp [:model/QueryExecution _ (merge query-execution-defaults
- {:started_at (-> (t/offset-date-time (t/zone-id "UTC"))
- (t/minus (t/days 30))
- (t/minus (t/minutes 10)))})]
- (is (= before
- (get-executions))
- "the executions metrics exclude query executions before 30 days ago"))))))))
+ (mt/with-temp [:model/QueryExecution {inside :id} (merge query-execution-defaults
+ {:started_at (-> (t/offset-date-time (t/zone-id "UTC"))
+ (t/minus (t/days 30))
+ (t/plus (t/minutes 10)))})
+ :model/QueryExecution {outside :id} (merge query-execution-defaults
+ {:started_at (-> (t/offset-date-time (t/zone-id "UTC"))
+ (t/minus (t/days 30))
+ (t/minus (t/minutes 10)))})]
+ ;; QueryExecutions are written asynchronously in batches, so rows queued by other tests can commit at any
+ ;; moment. Count against a table holding only the two rows under test instead of against a delta.
+ (t2/delete! :model/QueryExecution :id [:not-in [inside outside]])
+ (is (= 1 (:executions (#'stats/execution-metrics)))
+ "the 30 day cutoff includes the execution 10 minutes after it and excludes the one 10 minutes before")))))))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | Pulses & Alerts |