-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package stream_chat | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestDuration_MarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input PartitionTTL | ||
Check failure on line 12 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 12 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 12 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 12 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
want string | ||
}{ | ||
{ | ||
name: "Zero", | ||
input: PartitionTTL(0), | ||
Check failure on line 17 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 17 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 17 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 17 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
want: `null`, | ||
}, | ||
{ | ||
name: "Hours", | ||
input: PartitionTTL(24 * time.Hour), | ||
Check failure on line 22 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 22 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 22 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 22 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
want: `"24h0m0s"`, | ||
}, | ||
{ | ||
name: "Mixed", | ||
input: PartitionTTL(24*time.Hour + 30*time.Minute + 15*time.Second), | ||
Check failure on line 27 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 27 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 27 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 27 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
want: `"24h30m15s"`, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := tt.input.MarshalJSON() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if string(got) != tt.want { | ||
t.Errorf("Duration.MarshalJSON() = %q, want %q", string(got), tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestDuration_UnmarshalJSON(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
want PartitionTTL | ||
Check failure on line 49 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 49 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 49 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 49 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
wantErr bool | ||
}{ | ||
{ | ||
name: "Hours", | ||
input: `"4h"`, | ||
want: PartitionTTL(4 * time.Hour), | ||
Check failure on line 55 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 55 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 55 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 55 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
}, | ||
{ | ||
name: "Mixed", | ||
input: `"2h30m"`, | ||
want: PartitionTTL(2*time.Hour + 30*time.Minute), | ||
Check failure on line 60 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 60 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 60 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 60 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
}, | ||
{ | ||
name: "Full", | ||
input: `"6h0m0s"`, | ||
want: PartitionTTL(6 * time.Hour), | ||
Check failure on line 65 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 65 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 65 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 65 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
}, | ||
{ | ||
name: "Invalid", | ||
input: "daily", | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
var got PartitionTTL | ||
Check failure on line 76 in channel_config_test.go GitHub Actions / π· Test & Build (1.18)
Check failure on line 76 in channel_config_test.go GitHub Actions / π· Test & Build (1.19)
Check failure on line 76 in channel_config_test.go GitHub Actions / π· Test & Build (1.20)
Check failure on line 76 in channel_config_test.go GitHub Actions / π· Test & Build (1.21)
|
||
err := json.Unmarshal([]byte(tt.input), &got) | ||
if (err != nil) != tt.wantErr { | ||
t.Fatalf("Error = %q, want error: %t", err, tt.wantErr) | ||
} | ||
if got.String() != tt.want.String() { | ||
t.Errorf("Duration.UnmarshalJSON() = %q, want %q", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |