feat(backend): shift existing research areas display_order automatically on insert and update
- Automatically increment display_order (+1) for all existing research areas with display_order >= target order in a transaction - Handle display_order re-ordering gracefully during update operations - Add unit test TestResearchAreasDisplayOrderShift to verify shift behavior on create/update - Pass all Go tests and E2E test suites cleanly
This commit is contained in:
@@ -241,6 +241,10 @@ type APIResponse[T any] struct {
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func ptrString(s string) *string {
|
||||
return &s
|
||||
}
|
||||
|
||||
func doRequest(r http.Handler, method, target string, body any, token string) *httptest.ResponseRecorder {
|
||||
var reqBody []byte
|
||||
if body != nil {
|
||||
@@ -349,6 +353,51 @@ func TestResearchProjectsCRUD(t *testing.T) {
|
||||
assert.Equal(t, http.StatusNoContent, wDelete.Code)
|
||||
}
|
||||
|
||||
func TestResearchAreasDisplayOrderShift(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
r := router.SetupRouter(db, testAdminToken)
|
||||
|
||||
// Fetch initial research areas
|
||||
wInit := doRequest(r, "GET", "/api/v1/research-areas", nil, "")
|
||||
assert.Equal(t, http.StatusOK, wInit.Code)
|
||||
var initResp APIResponse[[]models.ResearchArea]
|
||||
require.NoError(t, json.Unmarshal(wInit.Body.Bytes(), &initResp))
|
||||
initAreas := initResp.Data
|
||||
require.NotEmpty(t, initAreas)
|
||||
|
||||
// Target display_order = 2 (should shift existing 2..N to 3..N+1)
|
||||
newArea := models.ResearchArea{
|
||||
NameEn: "Quantum Internet Protocols",
|
||||
NameKr: ptrString("양자 인터넷 프로토콜"),
|
||||
DisplayOrder: 2,
|
||||
}
|
||||
|
||||
wCreate := doRequest(r, "POST", "/api/v1/research-areas", newArea, testAdminToken)
|
||||
assert.Equal(t, http.StatusCreated, wCreate.Code)
|
||||
var createResp APIResponse[models.ResearchArea]
|
||||
require.NoError(t, json.Unmarshal(wCreate.Body.Bytes(), &createResp))
|
||||
createdID := createResp.Data.ID
|
||||
assert.Equal(t, 2, createResp.Data.DisplayOrder)
|
||||
|
||||
// Fetch updated list and assert display_order shifted correctly
|
||||
wAfter := doRequest(r, "GET", "/api/v1/research-areas", nil, "")
|
||||
assert.Equal(t, http.StatusOK, wAfter.Code)
|
||||
var afterResp APIResponse[[]models.ResearchArea]
|
||||
require.NoError(t, json.Unmarshal(wAfter.Body.Bytes(), &afterResp))
|
||||
afterAreas := afterResp.Data
|
||||
|
||||
assert.Equal(t, len(initAreas)+1, len(afterAreas))
|
||||
assert.Equal(t, "Quantum Internet Protocols", afterAreas[1].NameEn)
|
||||
assert.Equal(t, 2, afterAreas[1].DisplayOrder)
|
||||
assert.Equal(t, initAreas[1].NameEn, afterAreas[2].NameEn)
|
||||
assert.Equal(t, 3, afterAreas[2].DisplayOrder)
|
||||
|
||||
// Clean up
|
||||
wDel := doRequest(r, "DELETE", "/api/v1/research-areas/"+strconvItoa(createdID), nil, testAdminToken)
|
||||
assert.Equal(t, http.StatusNoContent, wDel.Code)
|
||||
}
|
||||
|
||||
func TestMembersAndAlumniCRUD(t *testing.T) {
|
||||
db := setupTestDB(t)
|
||||
defer db.Close()
|
||||
|
||||
Reference in New Issue
Block a user