add stuff
All checks were successful
Build and Publish / BuildAndDeploy (push) Successful in 1m52s

This commit is contained in:
matst80
2024-11-10 22:46:52 +01:00
parent e8643b5899
commit bc6c3ee692
3 changed files with 47 additions and 1 deletions

View File

@@ -145,7 +145,7 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, erro
Price: msg.Price, Price: msg.Price,
Image: msg.Image, Image: msg.Image,
}) })
c.TotalPrice += msg.Price c.TotalPrice += msg.Price * int64(msg.Quantity)
} }
case RemoveItemType: case RemoveItemType:
//msg, ok := message.Content.(*messages.RemoveItem) //msg, ok := message.Content.(*messages.RemoveItem)

43
cart-grain_test.go Normal file
View File

@@ -0,0 +1,43 @@
package main
import (
"testing"
"time"
messages "git.tornberg.me/go-cart-actor/proto"
)
func TestAddToCart(t *testing.T) {
grain, err := spawn(ToCartId("kalle"))
if err != nil {
t.Errorf("Error spawning: %v\n", err)
}
if len(grain.Items) != 0 {
t.Errorf("Expected 0 items, got %d\n", len(grain.Items))
}
addItem := messages.AddItem{
Quantity: 2,
Price: 100,
Sku: "123",
Name: "Test item",
Image: "test.jpg",
}
ts := time.Now().Unix()
msg := &Message{
TimeStamp: &ts,
Type: AddItemType,
Content: &addItem,
}
result, err := grain.HandleMessage(msg, false)
if err != nil {
t.Errorf("Error handling message: %v\n", err)
}
if len(result) == 0 {
t.Errorf("Expected result, got nil\n")
}
if grain.TotalPrice != 200 {
t.Errorf("Expected total price 200, got %d\n", grain.TotalPrice)
}
}

View File

@@ -36,6 +36,9 @@ var (
func spawn(id CartId) (*CartGrain, error) { func spawn(id CartId) (*CartGrain, error) {
grainSpawns.Inc() grainSpawns.Inc()
ret := &CartGrain{ ret := &CartGrain{
lastItemId: 0,
lastDeliveryId: 0,
Deliveries: []string{},
Id: id, Id: id,
Items: []CartItem{}, Items: []CartItem{},
storageMessages: []Message{}, storageMessages: []Message{},