Fixing AddItem and adding more tests/cartservice/CartServiceTests

Updated AddItem functionality so it handles update quantities scenario.
Added tests
This commit is contained in:
Simon Zeltser 2018-06-27 10:33:18 -07:00
parent 400d51a9fe
commit 546562fe53
2 changed files with 56 additions and 24 deletions

View file

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using cartservice.interfaces; using cartservice.interfaces;
using Google.Protobuf; using Google.Protobuf;
@ -40,14 +41,23 @@ namespace cartservice.cartstore
if (value.IsNull) if (value.IsNull)
{ {
cart = new Hipstershop.Cart(); cart = new Hipstershop.Cart();
cart.UserId = userId;
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
} }
else else
{ {
cart = Hipstershop.Cart.Parser.ParseFrom(value); cart = Hipstershop.Cart.Parser.ParseFrom(value);
var existingItem = cart.Items.SingleOrDefault(i => i.ProductId == productId);
if (existingItem == null)
{
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
}
else
{
existingItem.Quantity += quantity;
}
} }
cart.UserId = userId;
cart.Items.Add(new Hipstershop.CartItem { ProductId = productId, Quantity = quantity });
await db.HashSetAsync(userId, new[]{ new HashEntry(CART_FIELD_NAME, cart.ToByteArray()) }); await db.HashSetAsync(userId, new[]{ new HashEntry(CART_FIELD_NAME, cart.ToByteArray()) });
} }

View file

@ -36,7 +36,49 @@ namespace cartservice
} }
[Fact] [Fact]
public async Task AddItem_ItemInserted() public async Task AddItem_ItemExists_Udpated()
{
string userId = Guid.NewGuid().ToString();
// Construct server's Uri
string targetUri = $"{serverHostName}:{port}";
// Create a GRPC communication channel between the client and the server
var channel = new Channel(targetUri, ChannelCredentials.Insecure);
var client = new CartServiceClient(channel);
var request = new AddItemRequest
{
UserId = userId,
Item = new CartItem
{
ProductId = "1",
Quantity = 1
}
};
// First add - nothing should fail
await client.AddItemAsync(request);
// Second add of existing product - quantity should be updated
await client.AddItemAsync(request);
var getCartRequest = new GetCartRequest
{
UserId = userId
};
var cart = await client.GetCartAsync(getCartRequest);
Assert.NotNull(cart);
Assert.Equal(userId, cart.UserId);
Assert.Single(cart.Items);
Assert.Equal(2, cart.Items[0].Quantity);
// Cleanup
await client.EmptyCartAsync(new EmptyCartRequest{ UserId = userId });
}
[Fact]
public async Task AddItem_New_Inserted()
{ {
string userId = Guid.NewGuid().ToString(); string userId = Guid.NewGuid().ToString();
@ -46,8 +88,6 @@ namespace cartservice
// Create a GRPC communication channel between the client and the server // Create a GRPC communication channel between the client and the server
var channel = new Channel(targetUri, ChannelCredentials.Insecure); var channel = new Channel(targetUri, ChannelCredentials.Insecure);
//ar interceptorObject = new ObjecT();
//var channel.Intercept(interceptorObject);
// Create a proxy object to work with the server // Create a proxy object to work with the server
var client = new CartServiceClient(channel); var client = new CartServiceClient(channel);
@ -61,30 +101,12 @@ namespace cartservice
} }
}; };
/*
for (int i = 0; i < 3; i++)
{
try
{
Console.WriteLine("Try " + i+1);
await client.AddItemAsync(request);
break;
}
catch (Exception)
{
await Task.Delay(1000);
continue;
}
}
*/
await client.AddItemAsync(request); await client.AddItemAsync(request);
var getCartRequest = new GetCartRequest var getCartRequest = new GetCartRequest
{ {
UserId = userId UserId = userId
}; };
//await client.EmptyCartAsync(nameof)
//await client.EmptyCartAsync(new EmptyCartRequest{ UserId = userId });
var cart = await client.GetCartAsync(getCartRequest); var cart = await client.GetCartAsync(getCartRequest);
Assert.NotNull(cart); Assert.NotNull(cart);
Assert.Equal(userId, cart.UserId); Assert.Equal(userId, cart.UserId);