mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 05:55:51 +01:00
Add unit tests for date related helpers
This commit is contained in:
@@ -4,6 +4,8 @@ import gitbucket.core.controller.Context
|
|||||||
import gitbucket.core.service.RepositoryService.RepositoryInfo
|
import gitbucket.core.service.RepositoryService.RepositoryInfo
|
||||||
import org.scalatest.FunSpec
|
import org.scalatest.FunSpec
|
||||||
import org.scalatest.mockito.MockitoSugar
|
import org.scalatest.mockito.MockitoSugar
|
||||||
|
import java.util.Date
|
||||||
|
import java.util.TimeZone
|
||||||
|
|
||||||
class HelpersSpec extends FunSpec with MockitoSugar {
|
class HelpersSpec extends FunSpec with MockitoSugar {
|
||||||
|
|
||||||
@@ -64,4 +66,77 @@ class HelpersSpec extends FunSpec with MockitoSugar {
|
|||||||
assert(after == """<a href="http://exa"mple.com">http://exa"mple.com</a>""")
|
assert(after == """<a href="http://exa"mple.com">http://exa"mple.com</a>""")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe("datetimeAgo") {
|
||||||
|
it("should render a time within a minute") {
|
||||||
|
val time = System.currentTimeMillis()
|
||||||
|
val datetime = datetimeAgo(new Date(time))
|
||||||
|
assert(datetime == "just now")
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should render a time 1 minute ago") {
|
||||||
|
val time = System.currentTimeMillis() - (60 * 1000)
|
||||||
|
val datetime = datetimeAgo(new Date(time))
|
||||||
|
assert(datetime == "1 minute ago")
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should render a time 2 minute ago") {
|
||||||
|
val time = System.currentTimeMillis() - (60 * 1000 * 2)
|
||||||
|
val datetime = datetimeAgo(new Date(time))
|
||||||
|
assert(datetime == "2 minutes ago")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("datetimeRFC3339") {
|
||||||
|
it("should format date as RFC3339 format") {
|
||||||
|
val time = 1546961077224L
|
||||||
|
val datetime = datetimeRFC3339(new Date(time))
|
||||||
|
assert(datetime == "2019-01-08T15:24:37Z")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("date") {
|
||||||
|
it("should format date as yyyy-MM-dd with default timezone") {
|
||||||
|
val defaultTimeZone = TimeZone.getDefault
|
||||||
|
|
||||||
|
try {
|
||||||
|
val time = 1546961077247L
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
|
||||||
|
val datetimeUTC = date(new Date(time))
|
||||||
|
assert(datetimeUTC == "2019-01-08")
|
||||||
|
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("JST"))
|
||||||
|
val datetimeJST = date(new Date(time))
|
||||||
|
assert(datetimeJST == "2019-01-09")
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
TimeZone.setDefault(defaultTimeZone)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("hashDate") {
|
||||||
|
it("should format date as yyyyMMDDHHmmss with default timezone") {
|
||||||
|
val defaultTimeZone = TimeZone.getDefault
|
||||||
|
|
||||||
|
try {
|
||||||
|
val time = 1546961077247L
|
||||||
|
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
|
||||||
|
val hash = hashDate(new Date(time))
|
||||||
|
assert(hash == "20190108152437")
|
||||||
|
} finally {
|
||||||
|
TimeZone.setDefault(defaultTimeZone)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("hashQuery") {
|
||||||
|
it("should return same value for multiple calls") {
|
||||||
|
val time = 1546961077247L
|
||||||
|
val hash1 = hashQuery
|
||||||
|
Thread.sleep(500)
|
||||||
|
val hash2 = hashQuery
|
||||||
|
assert(hash1 == hash2)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user