From 0fd1a6bd16de1ba7a582ec8f87f75ede0f226e25 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sun, 8 Dec 2024 19:34:38 +0200 Subject: [PATCH 1/3] test: Add unit tests --- Makefile | 2 +- pkg/utils/date_time_test.go | 62 +++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 pkg/utils/date_time_test.go diff --git a/Makefile b/Makefile index 31b0b3f..5bab394 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ run-docker: ## test: Run unit tests .PHONY: test test: check-go - @go test ./... + @go test -v -count=1 ./... # ------------------------------------------------------------------------------------------------- # tools && shared diff --git a/pkg/utils/date_time_test.go b/pkg/utils/date_time_test.go new file mode 100644 index 0000000..e0a341d --- /dev/null +++ b/pkg/utils/date_time_test.go @@ -0,0 +1,62 @@ +package utils + +import ( + "testing" + "time" + + "github.com/stretchr/testify/assert" +) + +func TestFormatToAgo(t *testing.T) { + now := time.Now() + testCases := []struct { + name string + input time.Time + expectedOutput string + }{ + { + name: "10 minutes ago", + input: now.Add(-10 * time.Minute), + expectedOutput: "10 minutes ago", + }, + { + name: "2 hours ago", + input: now.Add(-2 * time.Hour), + expectedOutput: "2 hours ago", + }, + { + name: "1 day ago", + input: now.Add(-24 * time.Hour), + expectedOutput: "1 day ago", + }, + { + name: "1 week ago", + input: now.Add(-7 * 24 * time.Hour), + expectedOutput: "1 week ago"}, + { + name: "4 minutes from now", + input: now.Add(5 * time.Minute), + expectedOutput: "4 minutes from now", + }, + { + name: "6 days from now", + input: now.Add(7 * 24 * time.Hour), + expectedOutput: "6 days from now", + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + actualOutput := FormatToAgo(testCase.input) + + assert.Equal(t, testCase.expectedOutput, actualOutput) + }) + } +} + +func TestFormatToDateTime(t *testing.T) { + input := time.Date(2024, 12, 12, 12, 30, 0, 0, time.UTC) + actualOutput := FormatToDateTime(input) + + assert.Equal(t, "2024-12-12 12:30", actualOutput) +} From 7ca17a3b23ac201a26da8669ee333c1d5afd7c17 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sun, 8 Dec 2024 19:47:09 +0200 Subject: [PATCH 2/3] fix: Fix lint --- go.mod | 4 ++++ go.sum | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/go.mod b/go.mod index e1e7a21..a7c3ffc 100644 --- a/go.mod +++ b/go.mod @@ -5,12 +5,14 @@ go 1.23 require ( github.com/a-h/templ v0.2.793 github.com/dustin/go-humanize v1.0.1 + github.com/stretchr/testify v1.8.4 gorm.io/driver/sqlite v1.5.6 gorm.io/gorm v1.25.12 modernc.org/sqlite v1.34.2 ) require ( + github.com/davecgh/go-spew v1.1.1 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect github.com/jinzhu/inflection v1.0.0 // indirect @@ -18,10 +20,12 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-sqlite3 v1.14.24 // indirect github.com/ncruces/go-strftime v0.1.9 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d // indirect golang.org/x/sys v0.28.0 // indirect golang.org/x/text v0.21.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect modernc.org/gc/v3 v3.0.0-20241004144649-1aea3fae8852 // indirect modernc.org/libc v1.61.4 // indirect modernc.org/mathutil v1.6.0 // indirect diff --git a/go.sum b/go.sum index e9bdf17..9e4b0d5 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/a-h/templ v0.2.793 h1:Io+/ocnfGWYO4VHdR0zBbf39PQlnzVCVVD+wEEs6/qY= github.com/a-h/templ v0.2.793/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= @@ -24,6 +26,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d h1:0olWaB5pg3+oychR51GUVCEsGkeCU/2JxjBgIo4f3M0= golang.org/x/exp v0.0.0-20241204233417-43b7b7cde48d/go.mod h1:qj5a5QZpwLU2NLQudwIN5koi3beDhSAlJwa67PuM98c= golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4= @@ -37,6 +41,10 @@ golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8= golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8= From afb476c3e7f68961bfa780d0df3dc538c107b0b5 Mon Sep 17 00:00:00 2001 From: Yevhen Pavlov Date: Sun, 8 Dec 2024 19:52:26 +0200 Subject: [PATCH 3/3] fix: Fix lint --- pkg/utils/date_time_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/utils/date_time_test.go b/pkg/utils/date_time_test.go index e0a341d..2043fab 100644 --- a/pkg/utils/date_time_test.go +++ b/pkg/utils/date_time_test.go @@ -32,7 +32,8 @@ func TestFormatToAgo(t *testing.T) { { name: "1 week ago", input: now.Add(-7 * 24 * time.Hour), - expectedOutput: "1 week ago"}, + expectedOutput: "1 week ago", + }, { name: "4 minutes from now", input: now.Add(5 * time.Minute),