package dto

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

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

func bindErrorMutation(t *testing.T, body string) (ErrorRecordMutationRequest, error) {
	t.Helper()
	context, _ := gin.CreateTestContext(httptest.NewRecorder())
	context.Request = httptest.NewRequest("PUT", "/sysError/updateSysError", strings.NewReader(body))
	context.Request.Header.Set("Content-Type", "application/json")
	var request ErrorRecordMutationRequest
	return request, context.ShouldBindJSON(&request)
}

func TestErrorRecordMutationAcceptsExplicitEmptyPointerFields(t *testing.T) {
	request, err := bindErrorMutation(t, `{"ID":1,"form":"","info":"","solution":""}`)
	if err != nil {
		t.Fatal(err)
	}
	if request.Form == nil || *request.Form != "" || request.Info == nil || *request.Info != "" || request.Solution == nil || *request.Solution != "" {
		t.Fatalf("explicit empty values lost: form=%#v info=%#v solution=%#v", request.Form, request.Info, request.Solution)
	}
}

func TestErrorRecordMutationRequiresFormButKeepsOtherFieldsOmittable(t *testing.T) {
	request, err := bindErrorMutation(t, `{"ID":1}`)
	if err == nil {
		t.Fatal("missing form unexpectedly accepted")
	}
	if request.Info != nil || request.Solution != nil {
		t.Fatalf("omitted fields = (%#v, %#v), want nil", request.Info, request.Solution)
	}
}
