refactor(tests): remove getJSONInt helper, use float64 for JSON numeric values
Remove the getJSONInt helper function and update all test assertions to expect float64 instead of int for JSON numeric fields, as Go's JSON decoder unmarshals all numbers to float64 by default. This simplifies the codebase by removing an unnecessary conversion helper and makes tests more accurate to the actual JSON format. Changes: - Remove getJSONInt function from book_matching_test.go - Update 5 assertions in book_matching_test.go to use float64 - Update 2 assertions in collections_bulk_test.go to use float64 - Update 2 assertions in media_bulk_test.go to use float64 - Add nil checks for optional numeric fields to prevent panics Affected tests: - TestBookMatchingBulkLink - TestBookMatchingAutoLink - TestCollectionsBulkOperations - TestMediaBulkOperations Note: Some test failures remain (API returning 400 instead of 200) but these are legitimate test issues unrelated to type assertions.
This commit is contained in:
@@ -11,24 +11,6 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// getJSONInt converts an interface{} value to int, handling both int and float64
|
||||
func getJSONInt(v interface{}) int {
|
||||
switch val := v.(type) {
|
||||
case int:
|
||||
return val
|
||||
case float64:
|
||||
return int(val)
|
||||
case int32:
|
||||
return int(val)
|
||||
case int64:
|
||||
return int(val)
|
||||
case float32:
|
||||
return int(val)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// TestBookMatchingQueryBooks tests the book query endpoint
|
||||
func TestBookMatchingQueryBooks(t *testing.T) {
|
||||
t.Run("QueryBooks_WithoutAuth", func(t *testing.T) {
|
||||
@@ -188,9 +170,9 @@ func TestBookMatchingBulkLink(t *testing.T) {
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Equal(t, 0, getJSONInt(result["total"]))
|
||||
assert.Equal(t, 0, getJSONInt(result["successful"]))
|
||||
assert.Equal(t, 0, getJSONInt(result["failed"]))
|
||||
assert.Equal(t, 0.0, result["total"])
|
||||
assert.Equal(t, 0.0, result["successful"])
|
||||
assert.Equal(t, 0.0, result["failed"])
|
||||
})
|
||||
|
||||
t.Run("BulkLinkBooks_InvalidUnlinkedBookID", func(t *testing.T) {
|
||||
@@ -276,7 +258,7 @@ func TestBookMatchingBulkLink(t *testing.T) {
|
||||
var result map[string]interface{}
|
||||
json.NewDecoder(resp.Body).Decode(&result)
|
||||
|
||||
assert.Equal(t, 3, getJSONInt(result["total"]))
|
||||
assert.Equal(t, 3.0, result["total"])
|
||||
results := result["results"].([]interface{})
|
||||
assert.Equal(t, 3, len(results))
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user