Skip to content

Commit

Permalink
Merge pull request #261 from commercetools/chore/add-integration-test…
Browse files Browse the repository at this point in the history
…s-00

Add Integration Tests for .NET SDK
  • Loading branch information
kodiakhq[bot] authored Feb 16, 2024
2 parents 6623504 + 90a1efa commit 89f0237
Show file tree
Hide file tree
Showing 44 changed files with 3,747 additions and 104 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using commercetools.Sdk.Api.Models.Carts;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Base.Client;
using commercetools.Sdk.Api.Client;
using commercetools.Sdk.Api.Extensions;
using static commercetools.Api.IntegrationTests.GenericFixture;


namespace commercetools.Api.IntegrationTests.Cart
{
public class CartFixture
{
#region DraftBuilds

public static CartDraft DefaultCartDraft(CartDraft cartDraft)
{
var randomInt = TestingUtility.RandomInt();
cartDraft.Country = "DE";
cartDraft.Currency = "EUR";
cartDraft.ItemShippingAddresses = new List<IBaseAddress>()
{
new BaseAddress() { Country = "DE", Key = $"Key{randomInt}" }
};

return cartDraft;
}

public static CartDraft DefaultCartDraftWithKey(CartDraft draft, string key)
{
var cartDraft = DefaultCartDraft(draft);
cartDraft.Key = key;

return cartDraft;
}

#endregion

public static async Task<ICart> CreateCart(ProjectApiRoot client, CartDraft cartDraft)
{
return await client
.Carts()
.Post(cartDraft)
.ExecuteAsync();
}

public static async Task DeleteCart(ProjectApiRoot client, ICart cart)
{
try
{
await client
.Carts()
.WithKey(cart.Key)
.Delete()
.WithVersion(cart.Version)
.ExecuteAsync();
}
catch (Exception)
{
// ignored
}
}

#region WithCart

public static async Task WithCart(ProjectApiRoot client, Func<CartDraft, CartDraft> draftAction, Action<ICart> func)
{
await With(client, new CartDraft(), draftAction, func, CreateCart, DeleteCart);
}

public static async Task WithCart(ProjectApiRoot client, Func<CartDraft, CartDraft> draftAction,
Func<ICart, Task> func)
{
await WithAsync(client, new CartDraft(), draftAction, func, CreateCart, DeleteCart);
}

public static async Task WithCart(ProjectApiRoot client, Func<ICart, Task> func)
{
await WithAsync(client, new CartDraft(), DefaultCartDraft, func, CreateCart, DeleteCart);
}

public static async Task WithUpdateableCart(ProjectApiRoot client, Func<ICart, Task<ICart>> func)
{
await WithUpdateableAsync(client, new CartDraft(), DefaultCartDraft, func, CreateCart, DeleteCart);
}

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using commercetools.Base.Client.Error;
using commercetools.Sdk.Api.Client;
using commercetools.Sdk.Api.Models.Carts;
using Xunit;
using static commercetools.Api.IntegrationTests.Cart.CartFixture;

namespace commercetools.Api.IntegrationTests.Cart
{
[Collection("Integration Tests")]
public class CartIntegrationTests
{
private readonly ProjectApiRoot _client;

public CartIntegrationTests(ServiceProviderFixture serviceProviderFixture)
{
this._client = serviceProviderFixture.GetService<ProjectApiRoot>();
}

[Fact]
public async Task CreateCart()
{
var key = $"CreateCart-{TestingUtility.RandomString()}";
await WithCart(
_client,
cartDraft => DefaultCartDraftWithKey(cartDraft, key),
cart => { Assert.Equal(key, cart.Key); }
);
}

[Fact]
public async Task GetCartById()
{
var key = $"GetCartById-{TestingUtility.RandomString()}";
await WithCart(
_client, cartDraft => DefaultCartDraftWithKey(cartDraft, key),
async cart =>
{
Assert.NotNull(cart);
var retrievedCart = await _client
.Carts()
.WithId(cart.Id)
.Get()
.ExecuteAsync();
Assert.NotNull(retrievedCart);
Assert.Equal(key, retrievedCart.Key);
});
}

[Fact]
public async Task GetCartByKey()
{
var key = $"GetCartByKey-{TestingUtility.RandomString()}";
await WithCart(
_client, cartDraft => DefaultCartDraftWithKey(cartDraft, key),
async cart =>
{
Assert.NotNull(cart);
var retrievedCart = await _client
.Carts()
.WithKey(cart.Key)
.Get()
.ExecuteAsync();
Assert.NotNull(retrievedCart);
Assert.Equal(key, retrievedCart.Key);
}
);
}

[Fact]
public async Task QueryCarts()
{
var key = $"QueryCarts-{TestingUtility.RandomString()}";
await WithCart(
_client, cartDraft => DefaultCartDraftWithKey(cartDraft, key),
async cart =>
{
Assert.NotNull(cart);
var returnedSet = await _client
.Carts()
.Get()
.WithQuery(q => q.Key().Is(cart.Key))
.ExecuteAsync();
Assert.Single(returnedSet.Results);
Assert.Equal(key, returnedSet.Results[0].Key);
}
);
}

[Fact]
public async Task DeleteCartById()
{
var key = $"DeleteCartById-{TestingUtility.RandomString()}";
await WithCart(
_client, cartDraft => DefaultCartDraftWithKey(cartDraft, key),
async cart =>
{
Assert.NotNull(cart);
await _client
.Carts()
.WithId(cart.Id)
.Delete()
.WithVersion(cart.Version)
.ExecuteAsync();
await Assert.ThrowsAsync<NotFoundException>(
() => _client
.Carts().WithId(cart.Id)
.Get()
.ExecuteAsync()
);
}
);
}

[Fact]
public async Task DeleteCartByKey()
{
var key = $"DeleteCartByKey-{TestingUtility.RandomString()}";
await WithCart(
_client, cartDraft => DefaultCartDraftWithKey(cartDraft, key),
async cart =>
{
Assert.NotNull(cart);
await _client
.Carts()
.WithKey(cart.Key)
.Delete()
.WithVersion(cart.Version)
.ExecuteAsync();
await Assert.ThrowsAsync<NotFoundException>(
() => _client
.Carts()
.WithId(cart.Id)
.Get()
.ExecuteAsync()
);
}
);
}

[Fact]
public async Task UpdateCartByIdChangeCountry()
{
await WithUpdateableCart(_client, async cart =>
{
Assert.NotNull(cart);
const string name = "US";
var action = new CartSetCountryAction()
{
Action = "setCountry",
Country = name
};
var update = new CartUpdate()
{
Version = cart.Version,
Actions = new List<ICartUpdateAction>() { action }
};
try
{
var updatedCart = await _client
.Carts()
.WithId(cart.Id)
.Post(update)
.ExecuteAsync();
Assert.Equal(name, updatedCart.Country);
return updatedCart;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
using System;
using System.Threading.Tasks;
using commercetools.Sdk.Api.Models.Common;
using commercetools.Sdk.Api.Client;
using commercetools.Sdk.Api.Models.CartDiscounts;
using static commercetools.Api.IntegrationTests.GenericFixture;

namespace commercetools.Api.IntegrationTests.CartDiscount
{
public class CartDiscountFixtures
{
#region DraftBuilds

public static CartDiscountDraft DefaultCartDiscountDraft(CartDiscountDraft cartDiscountDraft)
{
var randomString = TestingUtility.RandomString();

cartDiscountDraft.Name = new LocalizedString { { "en", $"$Name-{randomString}" } };
cartDiscountDraft.CartPredicate = $"country=\"DE\"";
cartDiscountDraft.Value = new CartDiscountValueRelativeDraft() { Type = "relative", Permyriad = 10 };
cartDiscountDraft.Target = new CartDiscountShippingCostTarget() { Type = "shipping" };
cartDiscountDraft.SortOrder = TestingUtility.RandomSortOrder();
cartDiscountDraft.RequiresDiscountCode = true;
cartDiscountDraft.IsActive = false;

return cartDiscountDraft;
}

public static CartDiscountDraft DefaultCartDiscountDraftWithKey(CartDiscountDraft draft, string key)
{
var cartDiscountDraft = DefaultCartDiscountDraft(draft);
cartDiscountDraft.Key = key;

return cartDiscountDraft;
}

#endregion

#region CreateAndDelete

public static async Task<ICartDiscount> CreateCartDiscount(ProjectApiRoot client, CartDiscountDraft cartDiscountDraft)
{
try
{
var resource = await client
.CartDiscounts()
.Post(cartDiscountDraft)
.ExecuteAsync();

return resource;
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

public static async Task DeleteCartDiscount(ProjectApiRoot client, ICartDiscount cartDiscount)
{
try
{
await client
.CartDiscounts()
.WithId(cartDiscount.Id)
.Delete()
.WithVersion(cartDiscount.Version)
.ExecuteAsync();
}
catch (Exception)
{
// ignored
}
}

#endregion

#region WithDiscountCodes

public static async Task WithCartDiscount(ProjectApiRoot client,
Func<CartDiscountDraft, CartDiscountDraft> draftAction, Action<ICartDiscount> func)
{
await With(client, new CartDiscountDraft(), draftAction, func, CreateCartDiscount, DeleteCartDiscount);
}

public static async Task WithCartDiscount(ProjectApiRoot client,
Func<CartDiscountDraft, CartDiscountDraft> draftAction,
Func<ICartDiscount, Task> func)
{
await WithAsync(client, new CartDiscountDraft(), draftAction, func, CreateCartDiscount,
DeleteCartDiscount);
}

public static async Task WithCartDiscount(ProjectApiRoot client, Func<ICartDiscount, Task> func)
{
await WithAsync(client, new CartDiscountDraft(), DefaultCartDiscountDraft, func, CreateCartDiscount,
DeleteCartDiscount);
}

public static async Task WithUpdateableCartDiscount(ProjectApiRoot client,
Func<ICartDiscount, Task<ICartDiscount>> func)
{
await WithUpdateableAsync(client, new CartDiscountDraft(), DefaultCartDiscountDraft, func,
CreateCartDiscount, DeleteCartDiscount);
}

#endregion
}
}
Loading

0 comments on commit 89f0237

Please sign in to comment.