Added Unit Tests und Logik zur Reservierung von Teilmengen und Persitierung in der Datenbank im Service mittels ps.ReserviereBestellungen()

This commit is contained in:
Marco Kittel 2025-07-18 20:42:13 +02:00
parent ac37cbfacd
commit ed601a7199
2 changed files with 104 additions and 11 deletions

View File

@ -224,11 +224,12 @@ func TestFetchDataReservation(t *testing.T) {
if err != nil {
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
}
valTocompare := `[{map[A6053:[{DE 23 2} {EU 1 2} {AT 4 4}]]}]`
valTocompare := `[{map[A6053:[{DE 23 2 7507 34} {EU 1 2 500 60} {AT 4 4 8508 64}]]}]`
if fmt.Sprint(op) != valTocompare {
t.Errorf("Mist, sollte sein %s %s", valTocompare, fmt.Sprint(op))
t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op))
}
}
func TestFetchDataReservationVarition2(t *testing.T) {
payloadStr := `{ "products": { "A6053": 24 }, "context": { "country": "EU", "state": "" }}`
@ -243,11 +244,12 @@ func TestFetchDataReservationVarition2(t *testing.T) {
if err != nil {
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
}
valTocompare := `[{map[A6053:[{DE 23 2} {EU 1 2}]]}]`
valTocompare := `[{map[A6053:[{DE 23 2 7507 34} {EU 1 2 500 60}]]}]`
if fmt.Sprint(op) != valTocompare {
t.Errorf("Mist, sollte sein %s %s", valTocompare, fmt.Sprint(op))
t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op))
}
}
func TestFetchDataReservationVarition3(t *testing.T) {
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
@ -262,8 +264,77 @@ func TestFetchDataReservationVarition3(t *testing.T) {
if err != nil {
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
}
valTocompare := `[{map[A6053:[{AT 4 1} {DE 1 2}]]}]`
valTocompare := `[{map[A6053:[{AT 4 1 8508 63} {DE 1 2 7507 18}]]}]`
if fmt.Sprint(op) != valTocompare {
t.Errorf("Mist, sollte sein %s %s", valTocompare, fmt.Sprint(op))
t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op))
}
}
func TestOutgoingJsonString(t *testing.T) {
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
var payload Container
err := json.Unmarshal([]byte(payloadStr), &payload)
if err != nil {
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
return
}
ps := NewProductService(connectionString)
op, err := ps.FetchData(&payload)
if err != nil {
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
}
valTocompare := `[{map[A6053:[{AT 4 1 8508 63} {DE 1 2 7507 18}]]}]`
if fmt.Sprint(op) != valTocompare {
t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op))
}
jsonResult, err := json.Marshal(op)
jsonStrForApiResponse := `[{"products":{"A6053":[{"warehouse":"AT","quantity":4,"delivery_time":1},{"warehouse":"DE","quantity":1,"delivery_time":2}]}}]`
if string(jsonResult) != jsonStrForApiResponse {
t.Errorf("Mist, sollte sein %s ist aber %s", string(jsonResult), jsonStrForApiResponse)
}
}
func TestOutgoingReservationInDatabase(t *testing.T) {
//Löscht die Tabellen und erzeugt die Testdaten neu.
//Das Verbessert den Überblick
var dbr = NewDatabaseReader(connectionString)
err := Setup(dbr)
if err != nil {
t.Errorf("Setup failed! %s", err)
}
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
var payload Container
err = json.Unmarshal([]byte(payloadStr), &payload)
if err != nil {
t.Errorf("Dieser Fehler beim Konvertieren der JSON Payload sollte nicht passieren! \n%s", err)
return
}
ps := NewProductService(connectionString)
op, err := ps.FetchData(&payload)
if err != nil {
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
}
valTocompare := `[{map[A6053:[{AT 4 1 8508 63} {DE 1 2 7507 18}]]}]`
if fmt.Sprint(op) != valTocompare {
t.Errorf("Mist, sollte sein %s ist aber %s", valTocompare, fmt.Sprint(op))
}
uuids, err := ps.ReserviereBestellungen(op)
if err != nil {
t.Errorf("Das Reservieren ist fehlgeschlagen! \n%s", err)
}
for _, uuid := range *uuids {
status, err := ps.dbr.GetReservationStateById(uuid)
if err != nil || status != "RESERVED" {
t.Errorf("Der Status muss RESERVED sein. Stattdessen ist er: %s", status)
}
}
jsonResult, err := json.Marshal(op)
jsonStrForApiResponse := `[{"products":{"A6053":[{"warehouse":"AT","quantity":4,"delivery_time":1},{"warehouse":"DE","quantity":1,"delivery_time":2}]}}]`
if string(jsonResult) != jsonStrForApiResponse {
t.Errorf("Mist, sollte sein %s ist aber %s", string(jsonResult), jsonStrForApiResponse)
}
}

View File

@ -11,9 +11,11 @@ type Container struct {
}
type Product struct {
Warehouse string `json:"warehouse"`
Quantity int `json:"quantity"`
Delivery int `json:"delivery_time"`
Warehouse string `json:"warehouse"`
Quantity int `json:"quantity"`
Delivery int `json:"delivery_time"`
WhdId int `json:"-"`
DeliveryId int `json:"-"`
}
type OutgoingProducts struct {
@ -38,6 +40,26 @@ func NewProductService(connectionString string) *ProductService {
return &p
}
func (p *ProductService) ReserviereBestellungen(op []OutgoingProducts) (*[]UUID, error) {
var uuids []UUID
for _, hsmp := range op {
for _, prod := range hsmp.Products {
for _, concreteProduct := range prod {
deliveryId := concreteProduct.DeliveryId
warehouseId := concreteProduct.WhdId
amount := concreteProduct.Quantity
// Todo: Handler Injection wäre hier interessant
uuid, err := p.dbr.ReserveReservation(deliveryId, warehouseId, amount)
if err != nil {
return nil, err
}
uuids = append(uuids, uuid)
}
}
}
return &uuids, nil
}
func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, error) {
result := []OutgoingProducts{}
for key, item := range payload.Products {
@ -56,11 +78,11 @@ func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, erro
continue
}
if db_products.Amount >= gebrauchteProduktAnzahl {
newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse}
newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: gebrauchteProduktAnzahl, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId}
gebrauchteProduktAnzahl = 0
op.Products[key] = append(op.Products[key], newProduct)
} else if db_products.Amount < gebrauchteProduktAnzahl {
newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: db_products.Amount, Warehouse: db_products.Warehouse}
newProduct := Product{Delivery: db_products.DeliveryDays, Quantity: db_products.Amount, Warehouse: db_products.Warehouse, WhdId: db_products.Id, DeliveryId: db_products.DeliveryId}
gebrauchteProduktAnzahl -= db_products.Amount
op.Products[key] = append(op.Products[key], newProduct)
}