From e811fda8638d72237802bf30ce20baebde38f494 Mon Sep 17 00:00:00 2001 From: Thomas Peetz Date: Sun, 4 Oct 2026 23:05:53 +0200 Subject: [PATCH] add test for KontorTime --- kontor-gin/internal/cmd/export.go | 2 +- kontor-gin/internal/cmd/root.go | 2 +- kontor-gin/internal/helpers/time.go | 3 +- kontor-gin/internal/helpers/time_test.go | 54 ++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 kontor-gin/internal/helpers/time_test.go diff --git a/kontor-gin/internal/cmd/export.go b/kontor-gin/internal/cmd/export.go index 37f7e5d..536e892 100644 --- a/kontor-gin/internal/cmd/export.go +++ b/kontor-gin/internal/cmd/export.go @@ -149,7 +149,7 @@ var exportCmd = &cobra.Command{ "user/mail": &mail, } for path, value := range registry { - GetJSON(client, login.Token, fmt.Sprintf("%s/%s/%s", server, "api/v1", path), value) + GetJSON(client, login.Token, fmt.Sprintf("%s/%s/%s", server, apiVersion, path), value) } export := ExportData{ Sport: sports, diff --git a/kontor-gin/internal/cmd/root.go b/kontor-gin/internal/cmd/root.go index d285702..4d7303a 100644 --- a/kontor-gin/internal/cmd/root.go +++ b/kontor-gin/internal/cmd/root.go @@ -37,6 +37,6 @@ func Execute() { } func init() { - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/kli.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "c", "config file (default is $HOME/.config/kli.yaml)") rootCmd.PersistentFlags().CountVarP(&verbose, "verbose", "v", "increase verbosity (-v, -vv, -vvv)") } diff --git a/kontor-gin/internal/helpers/time.go b/kontor-gin/internal/helpers/time.go index cff44c3..f61b4b4 100644 --- a/kontor-gin/internal/helpers/time.go +++ b/kontor-gin/internal/helpers/time.go @@ -39,7 +39,8 @@ type KontorTime struct { var layouts = []string{ "2006-01-02 15:04:05.000000", - "2006-01-02T15:04:05.000000Z", + "2006-01-02T15:04:05.999Z", + "2006-01-02", } func (kt KontorTime) MarshalJSON() ([]byte, error) { diff --git a/kontor-gin/internal/helpers/time_test.go b/kontor-gin/internal/helpers/time_test.go new file mode 100644 index 0000000..b4590b9 --- /dev/null +++ b/kontor-gin/internal/helpers/time_test.go @@ -0,0 +1,54 @@ +package helpers + +import ( + "encoding/json" + "testing" +) + +func TestMultiFormatTimeUnmarshal(t *testing.T) { + tests := []struct { + name string + input string + wantErr bool + want string // Expected time string for comparison + }{ + { + name: "Date Only", + input: `"2005-04-01"`, + wantErr: false, + want: "2005-04-01 00:00:00 +0000 UTC", + }, + { + name: "KontorTime Format", + input: `"2026-04-30T13:54:26.334Z"`, + wantErr: false, + want: "2026-04-30 13:54:26.334 +0000 UTC", + }, + { + name: "Null Value", + input: `null`, + wantErr: false, + want: "", // Zero time + }, + { + name: "Invalid Format", + input: `"not-a-date"`, + wantErr: true, + want: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var m KontorTime + err := json.Unmarshal([]byte(tt.input), &m) + if (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !tt.wantErr && !m.Time.IsZero() && m.Time.String() != tt.want { + t.Errorf("UnmarshalJSON() = %v, want %v", m.Time.String(), tt.want) + } + }) + } +}