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

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

View File

@@ -39,5 +39,23 @@ func TestAddToCart(t *testing.T) {
if grain.TotalPrice != 200 {
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,
Deliveries: []string{},
Id: id,
Items: []CartItem{},
Items: []*CartItem{},
storageMessages: []Message{},
TotalPrice: 0,
}

View File

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

View File

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

View File

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