improve AssertEqual to handle annoying json.Unmarshal type conversions...

This commit is contained in:
Mike Schwörer 2023-01-17 20:41:45 +01:00
parent ff24493ff3
commit 2eb6292733
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
2 changed files with 121 additions and 10 deletions

View File

@ -224,7 +224,7 @@ func TestSendCompatMessageByQuery(t *testing.T) {
url.QueryEscape("message content"),
url.QueryEscape("2"),
url.QueryEscape("624dbe5e-6d03-47cd-9a0e-a306faa2e977"),
url.QueryEscape("1673894797")), nil)
url.QueryEscape(fmt.Sprintf("%d", time.Now().Unix()+666))), nil)
tt.AssertEqual(t, "success", true, r2["success"])
tt.AssertEqual(t, "suppress_send", false, r2["suppress_send"])
@ -241,7 +241,7 @@ func TestSendCompatMessageByQuery(t *testing.T) {
url.QueryEscape("message content"),
url.QueryEscape("2"),
url.QueryEscape("624dbe5e-6d03-47cd-9a0e-a306faa2e977"),
url.QueryEscape("1673894797")), nil)
url.QueryEscape(fmt.Sprintf("%d", time.Now().Unix()+666))), nil)
tt.AssertEqual(t, "success", true, r3["success"])
tt.AssertEqual(t, "suppress_send", true, r3["suppress_send"])
}
@ -278,7 +278,7 @@ func TestSendCompatMessageByFormData(t *testing.T) {
"content": "message content",
"priority": "2",
"msg_id": "624dbe5e-6d03-47cd-9a0e-a306faa2e977",
"timestamp": "1673894797",
"timestamp": fmt.Sprintf("%d", time.Now().Unix()-666),
})
tt.AssertEqual(t, "success", true, r2["success"])
tt.AssertEqual(t, "suppress_send", false, r2["suppress_send"])
@ -296,7 +296,7 @@ func TestSendCompatMessageByFormData(t *testing.T) {
"content": "message content",
"priority": "2",
"msg_id": "624dbe5e-6d03-47cd-9a0e-a306faa2e977",
"timestamp": "1673894797",
"timestamp": fmt.Sprintf("%d", time.Now().Unix()-666),
})
tt.AssertEqual(t, "success", true, r3["success"])
tt.AssertEqual(t, "suppress_send", true, r3["suppress_send"])
@ -324,13 +324,13 @@ func TestCompatInfo(t *testing.T) {
tt.AssertEqual(t, "success", true, r1["success"])
tt.AssertEqual(t, "fcm_token_set", true, r1["fcm_token_set"])
tt.AssertEqual(t, "is_pro", 0, int(r1["is_pro"].(float64)))
tt.AssertEqual(t, "is_pro", 0, r1["is_pro"])
tt.AssertEqual(t, "message", "ok", r1["message"])
tt.AssertEqual(t, "quota", 0, int(r1["quota"].(float64)))
tt.AssertEqual(t, "quota_max", 50, int(r1["quota_max"].(float64)))
tt.AssertEqual(t, "unack_count", 0, int(r1["unack_count"].(float64)))
tt.AssertEqual(t, "user_id", userid, int64(r1["user_id"].(float64)))
tt.AssertEqual(t, "user_key", userkey, r1["user_key"].(string))
tt.AssertEqual(t, "quota", 0, r1["quota"])
tt.AssertEqual(t, "quota_max", 50, r1["quota_max"])
tt.AssertEqual(t, "unack_count", 0, r1["unack_count"])
tt.AssertEqual(t, "user_id", userid, r1["user_id"])
tt.AssertEqual(t, "user_key", userkey, r1["user_key"])
}
func TestCompatAck(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"math"
"reflect"
"runtime/debug"
"strings"
@ -34,6 +35,111 @@ func AssertJsonMapEqual(t *testing.T, key string, expected map[string]any, actua
}
func AssertEqual(t *testing.T, key string, expected any, actual any) {
// try to fix types, kinda hacky, but its only unit tests...
switch vex := expected.(type) {
case int:
switch vac := actual.(type) {
case int:
// same
case int32:
expected = int64(vex)
actual = int64(vac)
case int64:
expected = int64(vex)
case float32:
if IsWholeFloat(vac) {
expected = int64(vex)
actual = int64(vac)
}
case float64:
if IsWholeFloat(vac) {
expected = int64(vex)
actual = int64(vac)
}
}
case int32:
switch vac := actual.(type) {
case int:
expected = int64(vex)
actual = int64(vac)
case int32:
// same
case int64:
expected = int64(vex)
case float32:
if IsWholeFloat(vac) {
expected = int64(vex)
actual = int64(vac)
}
case float64:
if IsWholeFloat(vac) {
expected = int64(vex)
actual = int64(vac)
}
}
case int64:
switch vac := actual.(type) {
case int:
actual = int64(vac)
case int32:
actual = int64(vac)
case int64:
// same
case float32:
if IsWholeFloat(vac) {
actual = int64(vac)
}
case float64:
if IsWholeFloat(vac) {
actual = int64(vac)
}
}
case float32:
switch vac := actual.(type) {
case int:
if IsWholeFloat(vex) {
expected = int64(vex)
actual = int64(vac)
}
case int32:
if IsWholeFloat(vex) {
expected = int64(vex)
actual = int64(vac)
}
case int64:
if IsWholeFloat(vex) {
expected = int64(vex)
}
case float32:
// same
case float64:
expected = float64(vex)
}
case float64:
switch vac := actual.(type) {
case int:
if IsWholeFloat(vex) {
expected = int64(vex)
actual = int64(vac)
}
case int32:
if IsWholeFloat(vex) {
expected = int64(vex)
actual = int64(vac)
}
case int64:
if IsWholeFloat(vex) {
expected = int64(vex)
}
case float32:
actual = float64(vac)
case float64:
// same
}
}
if expected != actual {
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
@ -226,3 +332,8 @@ func AssertMappedSet[T langext.OrderedConstraint](t *testing.T, key string, expe
t.FailNow()
}
}
func IsWholeFloat[T langext.FloatConstraint](v T) bool {
_, frac := math.Modf(math.Abs(float64(v)))
return frac == 0.0
}