package orders_test import ( "sync" "testing" "gorm.io/gorm/schema" "backend/internal/modules/orders" ) // Régression : un champ de type map sans Valuer/Scanner faisait échouer // l'analyse du modèle par GORM au premier accès à la base, alors que // `go build` passait. Chaque colonne JSONB d'Order est vérifiée ici. func TestOrderModelHasInfoSupColumn(t *testing.T) { s, err := schema.Parse(&orders.Order{}, &sync.Map{}, schema.NamingStrategy{}) if err != nil { t.Fatalf("schema.Parse(Order) error = %v", err) } f := s.LookUpField("info_sup") if f == nil { t.Fatalf("colonne info_sup absente (colonnes: %v)", s.DBNames) } if got := string(f.DataType); got != "jsonb" { t.Errorf("info_sup : type %q, attendu jsonb", got) } } func TestInfoSup_RoundTrip(t *testing.T) { in := orders.InfoSup{orders.ContactAddress: "12 rue de Paris", orders.ContactHour: "18:30"} v, err := in.Value() if err != nil { t.Fatal(err) } var out orders.InfoSup if err := out.Scan(v); err != nil { t.Fatal(err) } if len(out) != 2 || out[orders.ContactAddress] != "12 rue de Paris" || out[orders.ContactHour] != "18:30" { t.Errorf("relu = %v, attendu %v", out, in) } } // La base renvoie du JSONB en []byte (pgx) ou en string selon le driver. func TestInfoSup_ScanAcceptsBytesAndString(t *testing.T) { for name, src := range map[string]any{ "bytes": []byte(`{"hour":"09:00"}`), "string": `{"hour":"09:00"}`, } { var out orders.InfoSup if err := out.Scan(src); err != nil { t.Errorf("%s : %v", name, err) continue } if out[orders.ContactHour] != "09:00" { t.Errorf("%s : relu = %v", name, out) } } } func TestInfoSup_NilAndNullAreEmpty(t *testing.T) { var nilMap orders.InfoSup v, err := nilMap.Value() if err != nil || v != "{}" { t.Errorf("Value(nil) = %v, %v ; attendu \"{}\"", v, err) } var out orders.InfoSup if err := out.Scan(nil); err != nil || out == nil || len(out) != 0 { t.Errorf("Scan(nil) = %v, %v ; attendu une map vide non nil", out, err) } // Une map vide non nil s'écrit aussi {} (colonne NOT NULL). if v, err := (orders.InfoSup{}).Value(); err != nil || v != "{}" { t.Errorf("Value(vide) = %v, %v ; attendu \"{}\"", v, err) } } func TestInfoSup_ScanRejectsInvalidInput(t *testing.T) { var out orders.InfoSup if err := out.Scan([]byte(`{`)); err == nil { t.Error("Scan d'un JSON invalide : erreur attendue") } if err := out.Scan(42); err == nil { t.Error("Scan d'un type inattendu : erreur attendue") } } // Contacts et InfoSup ne doivent pas partager d'état : écrire l'un ne change pas l'autre. func TestContactsAndInfoSup_AreIndependent(t *testing.T) { o := orders.Order{ Contacts: orders.Contacts{orders.ContactWhatsapp: "+33612345678"}, InfoSup: orders.InfoSup{orders.ContactHour: "18:30"}, } if _, ok := o.InfoSup[orders.ContactWhatsapp]; ok { t.Error("InfoSup contient une clé de Contacts") } if _, ok := o.Contacts[orders.ContactHour]; ok { t.Error("Contacts contient une clé d'InfoSup") } }