Added Unit Tests und Logik zur Reservierung von Teilmengen und Persitierung in der Datenbank im Service mittels ps.ReserviereBestellungen()
This commit is contained in:
parent
ac37cbfacd
commit
ed601a7199
|
|
@ -224,11 +224,12 @@ func TestFetchDataReservation(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
|
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 {
|
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) {
|
func TestFetchDataReservationVarition2(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 24 }, "context": { "country": "EU", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 24 }, "context": { "country": "EU", "state": "" }}`
|
||||||
|
|
||||||
|
|
@ -243,11 +244,12 @@ func TestFetchDataReservationVarition2(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
|
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 {
|
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) {
|
func TestFetchDataReservationVarition3(t *testing.T) {
|
||||||
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
payloadStr := `{ "products": { "A6053": 5 }, "context": { "country": "AT", "state": "" }}`
|
||||||
|
|
||||||
|
|
@ -262,8 +264,77 @@ func TestFetchDataReservationVarition3(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Das Datafetchen und umwandeln in eine Map muss sauber funktionieren! %s", err)
|
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 {
|
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,8 @@ type Product struct {
|
||||||
Warehouse string `json:"warehouse"`
|
Warehouse string `json:"warehouse"`
|
||||||
Quantity int `json:"quantity"`
|
Quantity int `json:"quantity"`
|
||||||
Delivery int `json:"delivery_time"`
|
Delivery int `json:"delivery_time"`
|
||||||
|
WhdId int `json:"-"`
|
||||||
|
DeliveryId int `json:"-"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type OutgoingProducts struct {
|
type OutgoingProducts struct {
|
||||||
|
|
@ -38,6 +40,26 @@ func NewProductService(connectionString string) *ProductService {
|
||||||
return &p
|
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) {
|
func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, error) {
|
||||||
result := []OutgoingProducts{}
|
result := []OutgoingProducts{}
|
||||||
for key, item := range payload.Products {
|
for key, item := range payload.Products {
|
||||||
|
|
@ -56,11 +78,11 @@ func (p *ProductService) FetchData(payload *Container) ([]OutgoingProducts, erro
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if db_products.Amount >= gebrauchteProduktAnzahl {
|
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
|
gebrauchteProduktAnzahl = 0
|
||||||
op.Products[key] = append(op.Products[key], newProduct)
|
op.Products[key] = append(op.Products[key], newProduct)
|
||||||
} else if db_products.Amount < gebrauchteProduktAnzahl {
|
} 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
|
gebrauchteProduktAnzahl -= db_products.Amount
|
||||||
op.Products[key] = append(op.Products[key], newProduct)
|
op.Products[key] = append(op.Products[key], newProduct)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue