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

This commit is contained in:
matst80
2024-11-10 23:05:24 +01:00
parent d01b6675b4
commit c5bc17c44d
6 changed files with 38 additions and 24 deletions

View File

@@ -44,7 +44,7 @@ type CartGrain struct {
lastDeliveryId int lastDeliveryId int
storageMessages []Message storageMessages []Message
Id CartId `json:"id"` Id CartId `json:"id"`
Items []CartItem `json:"items"` Items []*CartItem `json:"items"`
TotalPrice int64 `json:"totalPrice"` TotalPrice int64 `json:"totalPrice"`
Deliveries []string `json:"deliveries,omitempty"` Deliveries []string `json:"deliveries,omitempty"`
} }
@@ -152,17 +152,18 @@ func (c *CartGrain) HandleMessage(message *Message, isReplay bool) ([]byte, erro
} }
if !found { if !found {
c.lastItemId++ c.lastItemId++
c.Items = append(c.Items, CartItem{ c.Items = append(c.Items, &CartItem{
Id: c.lastItemId, Id: c.lastItemId,
Quantity: int(msg.Quantity),
Sku: msg.Sku, Sku: msg.Sku,
Name: msg.Name, Name: msg.Name,
Price: msg.Price, Price: msg.Price,
Image: msg.Image, Image: msg.Image,
}) })
}
c.TotalPrice += msg.Price * int64(msg.Quantity) c.TotalPrice += msg.Price * int64(msg.Quantity)
} }
}
case RemoveItemType: case RemoveItemType:
//msg, ok := message.Content.(*messages.RemoveItem) //msg, ok := message.Content.(*messages.RemoveItem)
case AddDeliveryType: case AddDeliveryType:

View File

@@ -39,5 +39,23 @@ func TestAddToCart(t *testing.T) {
if grain.TotalPrice != 200 { if grain.TotalPrice != 200 {
t.Errorf("Expected total price 200, got %d\n", grain.TotalPrice) t.Errorf("Expected total price 200, got %d\n", grain.TotalPrice)
} }
if len(grain.Items) != 1 {
t.Errorf("Expected 1 item, got %d\n", len(grain.Items))
}
if grain.Items[0].Quantity != 2 {
t.Errorf("Expected quantity 2, got %d\n", grain.Items[0].Quantity)
}
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.Items[0].Quantity != 4 {
t.Errorf("Expected quantity 4, got %d\n", grain.Items[0].Quantity)
}
if grain.TotalPrice != 400 {
t.Errorf("Expected total price 400, got %d\n", grain.TotalPrice)
}
} }

View File

@@ -40,7 +40,7 @@ func spawn(id CartId) (*CartGrain, error) {
lastDeliveryId: 0, lastDeliveryId: 0,
Deliveries: []string{}, Deliveries: []string{},
Id: id, Id: id,
Items: []CartItem{}, Items: []*CartItem{},
storageMessages: []Message{}, storageMessages: []Message{},
TotalPrice: 0, TotalPrice: 0,
} }

View File

@@ -10,7 +10,7 @@ func TestQueue(t *testing.T) {
return &CartGrain{ return &CartGrain{
Id: id, Id: id,
storageMessages: []Message{}, storageMessages: []Message{},
Items: []CartItem{}, Items: []*CartItem{},
TotalPrice: 0, TotalPrice: 0,
}, nil }, nil
}) })
@@ -22,11 +22,7 @@ func TestQueue(t *testing.T) {
err = pool.AddRemote("localhost") err = pool.AddRemote("localhost")
if err != nil { if err != nil {
t.Errorf("Error adding remote: %v", err) t.Errorf("Error adding remote: %v", err)
} return
r := pool.remotes[0]
if len(r.PacketQueue.Packets) != 1 {
t.Errorf("Expected 1 packet, got %d", len(r.PacketQueue.Packets))
} }
} }

View File

@@ -387,7 +387,6 @@ func (p *SyncedPool) AddRemote(host string) error {
return err return err
} }
//pool := NewRemoteGrainPool(host)
remote := RemoteHost{ remote := RemoteHost{
Client: client, Client: client,
MissedPings: 0, MissedPings: 0,

View File

@@ -12,7 +12,7 @@ func TestConnection(t *testing.T) {
return &CartGrain{ return &CartGrain{
Id: id, Id: id,
storageMessages: []Message{}, storageMessages: []Message{},
Items: []CartItem{}, Items: []*CartItem{},
TotalPrice: 0, TotalPrice: 0,
}, nil }, nil
}) })
@@ -29,8 +29,8 @@ func TestConnection(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("Error negotiating: %v", err) t.Errorf("Error negotiating: %v", err)
} }
if len(allHosts) != 1 { if len(allHosts) != 0 {
t.Errorf("Expected 1 host, got %d", len(allHosts)) t.Errorf("Expected 0 host, (host should be known) got %d", len(allHosts))
} }
data, err := pool.Get(ToCartId("kalle")) data, err := pool.Get(ToCartId("kalle"))