go-mstdn/ent/application_marshaljson_tes...

135 lines
3.4 KiB
Go
Raw Permalink Normal View History

2023-09-27 07:33:04 +00:00
package ent_test
import (
"testing"
2024-08-08 19:49:22 +00:00
"github.com/reiver/go-json"
"github.com/reiver/go-jsonpp"
"github.com/reiver/go-nul"
"github.com/reiver/go-opt"
2023-09-27 07:33:04 +00:00
"github.com/reiver/go-mstdn/ent"
2023-09-27 07:33:04 +00:00
)
func TestApplication_MarshalJSON(t *testing.T) {
tests := []struct{
Application ent.Application
Expected string
}{
2024-08-08 23:49:58 +00:00
{
Application: ent.Application{},
Expected: "{}",
},
2023-09-27 07:33:04 +00:00
{
Application: ent.Application{
Name: opt.Something("acme app"),
2024-08-08 19:49:22 +00:00
WebSite: nul.Null[string](),
2023-09-27 07:33:04 +00:00
VapidKey: opt.Something("BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"),
},
Expected:
`{`+
`"name":"acme app"`+
`,`+
`"website":null`+
`,`+
`"vapid_key":"BHgNMADAUjgYgM4PZtHkY3yTQRYD-ibS_qrWYg2KPBRidocowKcOc-8YpyItumamkGph2bk8FuryT4-p3Eymwz8"`+
`}`,
},
{
Application: ent.Application{
Name: opt.Something("client fish"),
2023-09-27 08:52:38 +00:00
WebSite: nul.Something("http://example.fish/"),
VapidKey: opt.Something("BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"),
2023-09-27 07:33:04 +00:00
},
Expected:
`{`+
`"name":"client fish"`+
`,`+
2023-09-27 08:52:38 +00:00
`"website":"http://example.fish/"`+
2023-09-27 07:33:04 +00:00
`,`+
`"vapid_key":"BLV6IwZiUgNnReINKtfgpt-zNCUF8jXTIsvA7Pa1-TTTLOEkeG-UtWVhKraRGgAcGUnrMBBzQPPFxTEao7L_Oz"`+
`}`,
},
{
Application: ent.Application{
Name: opt.Something("frontodon"),
2023-09-27 08:52:38 +00:00
WebSite: nul.Something("http://something.tld/"),
VapidKey: opt.Something("BLCtzyFc2xtnAqWYW3m4M0v47Uym9mKd5yQMvY3FLWzdXN2vpjzF3iQ413fKwNOITkhJ6_tlvTZELL876uokpM4"),
2023-09-27 07:33:04 +00:00
ClientID: opt.Something("22"),
},
Expected:
`{`+
`"name":"frontodon"`+
`,`+
`"website":"http://something.tld/"`+
`,`+
2023-09-27 08:52:38 +00:00
`"vapid_key":"BLCtzyFc2xtnAqWYW3m4M0v47Uym9mKd5yQMvY3FLWzdXN2vpjzF3iQ413fKwNOITkhJ6_tlvTZELL876uokpM4"`+
2023-09-27 07:33:04 +00:00
`,`+
`"client_id":"22"`+
`}`,
},
{
Application: ent.Application{
Name: opt.Something("super-gorilla"),
2023-09-27 08:52:38 +00:00
WebSite: nul.Something("http://example.gorilla/"),
VapidKey: opt.Something("BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"),
2023-09-27 07:33:04 +00:00
ClientID: opt.Something("22"),
2023-09-27 08:52:38 +00:00
ClientSecret: opt.Something("NwlOvca6TQjFutgobDM6voupU8kIqzN0h_oa1pEqBD4"),
2023-09-27 07:33:04 +00:00
},
Expected:
`{`+
`"name":"super-gorilla"`+
`,`+
2023-09-27 08:52:38 +00:00
`"website":"http://example.gorilla/"`+
2023-09-27 07:33:04 +00:00
`,`+
`"vapid_key":"BNi9UYyWxKVULR-FEaCWt3NuAHrlyyIz7zYRyIyLP0Q46ePHsiLbd8wHG3VXy-wTAgzFsRb5pGxJZDeX3FROwlE"`+
`,`+
`"client_id":"22"`+
`,`+
`"client_secret":"NwlOvca6TQjFutgobDM6voupU8kIqzN0h_oa1pEqBD4"`+
`}`,
},
}
for testNumber, test := range tests {
actualBytes, err := json.Marshal(test.Application)
if nil != err {
t.Errorf("For test #%d, did not expect an error but actually got one.", testNumber)
t.Logf("ERROR: (%T) %s", err, err)
t.Logf("EXPECTED:\n%s", test.Expected)
t.Logf("EXPECTED:\n%s", jsonpp.SPrettyPrint([]byte(test.Expected)))
t.Logf("APPLICATION: %#v", test.Application)
continue
}
{
actual := string(actualBytes)
expected := test.Expected
if expected != actual {
t.Errorf("For test #%d, the actual value is not what was expected.", testNumber)
t.Logf("EXPECTED:\n%s", expected)
t.Logf("ACTUAL:\n%s", actual)
t.Logf("EXPECTED:\n%s", jsonpp.SPrettyPrint([]byte(expected)))
t.Logf("ACTUAL:\n%s", jsonpp.SPrettyPrint([]byte(actual)))
t.Logf("APPLICATION: %#v", test.Application)
continue
}
}
}
}