move cartservice tests (#322)
This commit is contained in:
parent
4d8848bc5e
commit
173ac3f071
5 changed files with 3 additions and 0 deletions
3
src/cartservice/.dockerignore
Normal file
3
src/cartservice/.dockerignore
Normal file
|
@ -0,0 +1,3 @@
|
|||
**/*.sh
|
||||
**/*.bat
|
||||
tests/
|
3
src/cartservice/tests/.gitignore
vendored
Normal file
3
src/cartservice/tests/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
/bin/*
|
||||
/obj/*
|
||||
/.vs/*
|
136
src/cartservice/tests/CartServiceTests.cs
Normal file
136
src/cartservice/tests/CartServiceTests.cs
Normal file
|
@ -0,0 +1,136 @@
|
|||
// Copyright 2018 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Grpc.Core;
|
||||
using Hipstershop;
|
||||
using Xunit;
|
||||
using static Hipstershop.CartService;
|
||||
|
||||
namespace cartservice
|
||||
{
|
||||
public class E2ETests
|
||||
{
|
||||
private static string serverHostName = "localhost";
|
||||
private static int port = 7070;
|
||||
|
||||
[Fact]
|
||||
public async Task GetItem_NoAddItemBefore_EmptyCartReturned()
|
||||
{
|
||||
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 GetCartRequest
|
||||
{
|
||||
UserId = userId,
|
||||
};
|
||||
|
||||
var cart = await client.GetCartAsync(request);
|
||||
Assert.NotNull(cart);
|
||||
|
||||
// All grpc objects implement IEquitable, so we can compare equality with by-value semantics
|
||||
Assert.Equal(new Cart(), cart);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddItem_ItemExists_Updated()
|
||||
{
|
||||
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();
|
||||
|
||||
// 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);
|
||||
|
||||
// Create a proxy object to work with the server
|
||||
var client = new CartServiceClient(channel);
|
||||
|
||||
var request = new AddItemRequest
|
||||
{
|
||||
UserId = userId,
|
||||
Item = new CartItem
|
||||
{
|
||||
ProductId = "1",
|
||||
Quantity = 1
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
|
||||
await client.EmptyCartAsync(new EmptyCartRequest{ UserId = userId });
|
||||
cart = await client.GetCartAsync(getCartRequest);
|
||||
Assert.Empty(cart.Items);
|
||||
}
|
||||
}
|
||||
}
|
30
src/cartservice/tests/cartservice.tests.csproj
Normal file
30
src/cartservice/tests/cartservice.tests.csproj
Normal file
|
@ -0,0 +1,30 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Protobuf" Version="3.6.0" />
|
||||
<PackageReference Include="Google.Protobuf.Tools" Version="3.6.0" />
|
||||
<PackageReference Include="Grpc" Version="1.12.0" />
|
||||
<PackageReference Include="Grpc.Tools" Version="1.12.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\cartservice\cartservice.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="xunit.runner.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
6
src/cartservice/tests/cartservice.tests.csproj.user
Normal file
6
src/cartservice/tests/cartservice.tests.csproj.user
Normal file
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<ShowAllFiles>true</ShowAllFiles>
|
||||
</PropertyGroup>
|
||||
</Project>
|
Loading…
Add table
Add a link
Reference in a new issue