Add the nullable FIGI migration, owner-scoped asset identity operations, and the explicit index search and selection flow. Refs #4 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
87 lines
3.3 KiB
Go
87 lines
3.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/tradarr/backend/internal/config"
|
|
"github.com/tradarr/backend/internal/models"
|
|
)
|
|
|
|
func TestAssetIdentityConversionAmbiguityAndUndoPostgres(t *testing.T) {
|
|
db := ticket4AssetDB(t)
|
|
if _, err := db.Exec(`INSERT INTO user_assets(id,user_id,symbol,name) VALUES ($1,$2,'SPX','legacy')`, ticket4AssetID, ticket4OwnerA); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
h := New(models.NewRepository(db), &config.Config{}, nil, nil, nil, nil)
|
|
router := gin.New()
|
|
router.Use(func(c *gin.Context) { c.Set("userID", ticket4OwnerA) })
|
|
router.POST("/assets", h.AddMyAsset)
|
|
router.DELETE("/assets/:id", h.RemoveMyAsset)
|
|
call := func(method, path, body string, want int) map[string]any {
|
|
t.Helper()
|
|
request := httptest.NewRequest(method, path, bytes.NewBufferString(body))
|
|
request.Header.Set("Content-Type", "application/json")
|
|
w := httptest.NewRecorder()
|
|
router.ServeHTTP(w, request)
|
|
if w.Code != want {
|
|
t.Fatalf("%s %s status=%d want=%d body=%s", method, path, w.Code, want, w.Body.String())
|
|
}
|
|
if want != http.StatusCreated {
|
|
return nil
|
|
}
|
|
var envelope struct{ Data map[string]any }
|
|
if err := json.Unmarshal(w.Body.Bytes(), &envelope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return envelope.Data
|
|
}
|
|
first := call("POST", "/assets", `{"symbol":" spx ","name":"S&P 500","figi":"BBG000SPX001"}`, 201)
|
|
if first["id"] != ticket4AssetID || first["symbol"] != "SPX" {
|
|
t.Fatalf("conversion did not keep ID/normalize: %v", first)
|
|
}
|
|
again := call("POST", "/assets", `{"symbol":"SPX","name":"S&P 500","figi":"BBG000SPX001"}`, 201)
|
|
if again["id"] != first["id"] {
|
|
t.Fatal("same FIGI duplicated")
|
|
}
|
|
second := call("POST", "/assets", `{"symbol":"SPX","name":"Other index","figi":"BBG000SPX002"}`, 201)
|
|
if second["id"] == first["id"] {
|
|
t.Fatal("distinct FIGIs collapsed")
|
|
}
|
|
// A legacy row can coexist, but cannot be automatically converted when
|
|
// another identity already shares the symbol.
|
|
call("POST", "/assets", `{"symbol":"SPX","name":"ambiguous legacy"}`, 201)
|
|
const snapshot = `SELECT jsonb_agg(to_jsonb(a) ORDER BY id)::text FROM user_assets a`
|
|
var before, after string
|
|
if err := db.QueryRow(snapshot).Scan(&before); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call("POST", "/assets", `{"symbol":"SPX","name":"must not persist","figi":"BBG000SPX003"}`, 409)
|
|
if err := db.QueryRow(snapshot).Scan(&after); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if before != after {
|
|
t.Fatalf("ambiguous request mutated rows: %s -> %s", before, after)
|
|
}
|
|
call("DELETE", "/assets/spx", "", 204)
|
|
var legacy, selected int
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM user_assets WHERE figi IS NULL`).Scan(&legacy); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM user_assets WHERE figi IS NOT NULL`).Scan(&selected); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if legacy != 0 || selected != 2 {
|
|
t.Fatalf("legacy removal affected FIGI rows: %d/%d", legacy, selected)
|
|
}
|
|
call("DELETE", "/assets/"+first["id"].(string), "", 204)
|
|
call("POST", "/assets", `{"symbol":"SPX","name":"S&P 500","figi":"BBG000SPX001"}`, 201)
|
|
if err := db.QueryRow(`SELECT COUNT(*) FROM user_assets WHERE figi IS NOT NULL`).Scan(&selected); err != nil || selected != 2 {
|
|
t.Fatalf("undo count=%d err=%v", selected, err)
|
|
}
|
|
}
|