package book

import (
	"net/http/httptest"
	"testing"

	"github.com/gin-gonic/gin"
)

func TestBindIDsAcceptsDocumentedIdsArrayQuery(t *testing.T) {
	gin.SetMode(gin.TestMode)
	ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
	ctx.Request = httptest.NewRequest("DELETE", "/book/deleteBookByIds?ids[]=1&ids[]=2", nil)

	ids, ok := bindIDs(ctx)
	if !ok {
		t.Fatal("bindIDs() ok = false, want true")
	}
	if len(ids.Ids) != 2 || ids.Ids[0] != 1 || ids.Ids[1] != 2 {
		t.Fatalf("ids = %#v, want [1 2]", ids.Ids)
	}
}

func TestBindIDsRejectsEmptyIDList(t *testing.T) {
	gin.SetMode(gin.TestMode)
	ctx, _ := gin.CreateTestContext(httptest.NewRecorder())
	ctx.Request = httptest.NewRequest("DELETE", "/book/deleteBookByIds", nil)

	_, ok := bindIDs(ctx)
	if ok {
		t.Fatal("bindIDs() ok = true, want false")
	}
}
