dcbot/internal/twhelp/twhelp_test.go
Dawid Wysokiński eec0909c78
All checks were successful
continuous-integration/drone/push Build is passing
feat: notifications (#25)
Reviewed-on: #25
2022-10-27 09:56:40 +00:00

259 lines
5.7 KiB
Go

package twhelp_test
import (
"encoding/json"
"testing"
"gitea.dwysokinski.me/twhelp/dcbot/internal/twhelp"
"github.com/stretchr/testify/assert"
)
func TestNullTribeMeta_MarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
tribe twhelp.NullTribeMeta
expectedJSON string
}{
{
name: "OK: null 1",
tribe: twhelp.NullTribeMeta{
Tribe: twhelp.TribeMeta{},
Valid: false,
},
expectedJSON: "null",
},
{
name: "OK: null 2",
tribe: twhelp.NullTribeMeta{
Tribe: twhelp.TribeMeta{ID: 1234},
Valid: false,
},
expectedJSON: "null",
},
{
name: "OK: valid struct",
tribe: twhelp.NullTribeMeta{
Tribe: twhelp.TribeMeta{
ID: 997,
Name: "name 997",
Tag: "tag 997",
ProfileURL: "profile-997",
},
Valid: true,
},
expectedJSON: `{"id":997,"name":"name 997","tag":"tag 997","profileUrl":"profile-997"}`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
b, err := json.Marshal(tt.tribe)
assert.NoError(t, err)
assert.JSONEq(t, tt.expectedJSON, string(b))
})
}
}
func TestNullTribeMeta_UnmarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
json string
expectedTribe twhelp.NullTribeMeta
expectedJSONSyntaxError bool
}{
{
name: "OK: null",
json: "null",
expectedTribe: twhelp.NullTribeMeta{
Tribe: twhelp.TribeMeta{},
Valid: false,
},
},
{
name: "OK: valid struct",
//nolint:lll
json: `{"id":997,"name":"name 997","tag":"tag 997","profileUrl":"profile-997"}`,
expectedTribe: twhelp.NullTribeMeta{
Tribe: twhelp.TribeMeta{
ID: 997,
Name: "name 997",
Tag: "tag 997",
ProfileURL: "profile-997",
},
Valid: true,
},
},
{
name: "ERR: invalid tribe 1",
json: "2022-07-30T14:13:12.0000005Z",
expectedTribe: twhelp.NullTribeMeta{},
expectedJSONSyntaxError: true,
},
{
name: "ERR: invalid tribe 2",
json: "hello world",
expectedTribe: twhelp.NullTribeMeta{},
expectedJSONSyntaxError: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var target twhelp.NullTribeMeta
err := json.Unmarshal([]byte(tt.json), &target)
if tt.expectedJSONSyntaxError {
var syntaxError *json.SyntaxError
assert.ErrorAs(t, err, &syntaxError)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.expectedTribe, target)
})
}
}
func TestNullPlayerMeta_MarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
player twhelp.NullPlayerMeta
expectedJSON string
}{
{
name: "OK: null 1",
player: twhelp.NullPlayerMeta{
Player: twhelp.PlayerMeta{},
Valid: false,
},
expectedJSON: "null",
},
{
name: "OK: null 2",
player: twhelp.NullPlayerMeta{
Player: twhelp.PlayerMeta{ID: 1234},
Valid: false,
},
expectedJSON: "null",
},
{
name: "OK: valid struct",
player: twhelp.NullPlayerMeta{
Player: twhelp.PlayerMeta{
ID: 997,
Name: "name 997",
ProfileURL: "profile-997",
Tribe: twhelp.NullTribeMeta{
Valid: true,
Tribe: twhelp.TribeMeta{
ID: 1234,
Name: "name 997",
ProfileURL: "profile-997",
Tag: "tag 997",
},
},
},
Valid: true,
},
//nolint:lll
expectedJSON: `{"id":997,"name":"name 997","profileUrl":"profile-997","tribe":{"id":1234,"name":"name 997","profileUrl":"profile-997","tag":"tag 997"}}`,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
b, err := json.Marshal(tt.player)
assert.NoError(t, err)
assert.JSONEq(t, tt.expectedJSON, string(b))
})
}
}
func TestNullPlayerMeta_UnmarshalJSON(t *testing.T) {
t.Parallel()
tests := []struct {
name string
json string
expectedPlayer twhelp.NullPlayerMeta
expectedJSONSyntaxError bool
}{
{
name: "OK: null",
json: "null",
expectedPlayer: twhelp.NullPlayerMeta{
Player: twhelp.PlayerMeta{},
Valid: false,
},
},
{
name: "OK: valid struct",
//nolint:lll
json: `{"id":997,"name":"name 997","profileUrl":"profile-997","tribe":{"id":1234,"name":"name 997","profileUrl":"profile-997","tag":"tag 997"}}`,
expectedPlayer: twhelp.NullPlayerMeta{
Player: twhelp.PlayerMeta{
ID: 997,
Name: "name 997",
ProfileURL: "profile-997",
Tribe: twhelp.NullTribeMeta{
Valid: true,
Tribe: twhelp.TribeMeta{
ID: 1234,
Name: "name 997",
ProfileURL: "profile-997",
Tag: "tag 997",
},
},
},
Valid: true,
},
},
{
name: "ERR: invalid tribe 1",
json: "2022-07-30T14:13:12.0000005Z",
expectedPlayer: twhelp.NullPlayerMeta{},
expectedJSONSyntaxError: true,
},
{
name: "ERR: invalid tribe 2",
json: "hello world",
expectedPlayer: twhelp.NullPlayerMeta{},
expectedJSONSyntaxError: true,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
var target twhelp.NullPlayerMeta
err := json.Unmarshal([]byte(tt.json), &target)
if tt.expectedJSONSyntaxError {
var syntaxError *json.SyntaxError
assert.ErrorAs(t, err, &syntaxError)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.expectedPlayer, target)
})
}
}