Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
Metabase
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Engineering Digital Service
Metabase
Commits
30c0bd02
Unverified
Commit
30c0bd02
authored
9 months ago
by
Alexander Solovyov
Committed by
GitHub
9 months ago
Browse files
Options
Downloads
Patches
Plain Diff
/api/testing/set-time: ability to set JVM time during development (#45230)
parent
aa110f2e
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/metabase/api/testing.clj
+18
-0
18 additions, 0 deletions
src/metabase/api/testing.clj
test/metabase/api/testing_test.clj
+25
-1
25 additions, 1 deletion
test/metabase/api/testing_test.clj
with
43 additions
and
1 deletion
src/metabase/api/testing.clj
+
18
−
0
View file @
30c0bd02
...
...
@@ -4,9 +4,12 @@
[
clojure.java.jdbc
:as
jdbc
]
[
clojure.string
:as
str
]
[
compojure.core
:refer
[
POST
]]
[
java-time.api
:as
t
]
[
java-time.clock
]
[
metabase.api.common
:as
api
]
[
metabase.config
:as
config
]
[
metabase.db
:as
mdb
]
[
metabase.util.date-2
:as
u.date
]
[
metabase.util.files
:as
u.files
]
[
metabase.util.log
:as
log
]
[
metabase.util.malli.schema
:as
ms
])
...
...
@@ -125,4 +128,19 @@
{
:status
200
:body
body
}))
(
api/defendpoint
POST
"/set-time"
"Make java-time see world at exact time."
[
:as
{{
:keys
[
time
add-ms
]}
:body
}]
{
time
[
:maybe
ms/TemporalString
]
add-ms
[
:maybe
ms/Int
]}
(
let
[
clock
(
when-let
[
time
'
(
cond
time
(
u.date/parse
time
)
add-ms
(
t/plus
(
t/zoned-date-time
)
(
t/duration
add-ms
:millis
)))]
(
t/mock-clock
(
t/instant
time
'
)
(
t/zone-id
time
'
)))]
;; if time' is `nil`, we'll get system clock back
(
alter-var-root
#
'java-time.clock/*clock*
(
constantly
clock
))
{
:result
(
if
clock
:set
:reset
)
:time
(
t/instant
)}))
(
api/define-routes
)
This diff is collapsed.
Click to expand it.
test/metabase/api/testing_test.clj
+
25
−
1
View file @
30c0bd02
...
...
@@ -3,10 +3,13 @@
[
clojure.java.io
:as
io
]
[
clojure.java.jdbc
:as
jdbc
]
[
clojure.test
:refer
:all
]
[
java-time.api
:as
t
]
[
java-time.clock
]
[
metabase.api.testing
:as
testing
]
[
metabase.db
:as
mdb
]
[
metabase.test
:as
mt
]
[
metabase.util
:as
u
]))
[
metabase.util
:as
u
]
[
metabase.util.date-2
:as
u.date
]))
(
set!
*warn-on-reflection*
true
)
...
...
@@ -54,3 +57,24 @@
(
mt/with-temp-empty-app-db
[
_conn
:h2
]
(
#
'testing/restore-snapshot!
snapshot-name
)
(
is
(
=
[{
:a
1
}]
(
jdbc/query
{
:datasource
(
mdb/app-db
)}
[
"select a from test_view"
]))))))
(
deftest
set-time-test
(
try
(
let
[
t
(
t/zoned-date-time
2024
7
8
15
00
00
)]
(
testing
"You can set exact date and reset it back"
(
is
(
=
{
:result
"set"
:time
"2024-07-08T15:00:00Z"
}
(
mt/user-http-request
:rasta
:post
200
"testing/set-time"
{
:time
(
u.date/format
t
)})))
(
is
(
=?
{
:result
"reset"
:time
string?
}
(
mt/user-http-request
:rasta
:post
200
"testing/set-time"
))))
(
testing
"You can move date with `add-ms`"
(
is
(
=
{
:result
"set"
:time
"2024-07-08T15:00:00Z"
}
(
mt/user-http-request
:rasta
:post
200
"testing/set-time"
{
:time
(
u.date/format
t
)})))
(
is
(
=
{
:result
"set"
:time
"2024-07-08T15:00:10Z"
}
(
mt/user-http-request
:rasta
:post
200
"testing/set-time"
{
:add-ms
10000
})))
(
is
(
=?
{
:result
"reset"
:time
string?
}
(
mt/user-http-request
:rasta
:post
200
"testing/set-time"
)))))
(
finally
(
alter-var-root
#
'java-time.clock/*clock*
(
constantly
nil
)))))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment