Merge remote-tracking branch 'origin/test'
continuous-integration/drone/push Build is passing Details

master
Maxime BATISTA 2 years ago
commit e836625e42

@ -5,7 +5,7 @@ using System.Collections.Immutable;
namespace Services
{
internal class AccountOwnedRecipes : IAccountOwnedRecipesService
public class AccountOwnedRecipes : IAccountOwnedRecipesService
{
public Account Account { get; init; }
@ -48,7 +48,5 @@ namespace Services
{
return ownedRecipes.Values.ToImmutableList().ConvertAll(r => r.Info);
}
}
}

@ -5,7 +5,7 @@ using System.Collections.Immutable;
namespace LocalServices
{
internal class AccountRecipesPreferences : IAccountRecipesPreferencesService
public class AccountRecipesPreferences : IAccountRecipesPreferencesService
{
private readonly IDatabase db;
@ -56,7 +56,7 @@ namespace LocalServices
public RecipeRate GetRate(RecipeInfo info)
{
RecipeRate rate = null;
RecipeRate? rate = null;
var ratings = db.ListRatesOf(Account.User.Id);
if (!ratings.TryGetValue(info.Id, out rate))
@ -82,7 +82,6 @@ namespace LocalServices
public void AddToFavorites(RecipeInfo info)
{
Guid userId = Account.User.Id;
var ratings = db.ListRatesOf(userId);
RecipeRate rate = GetRate(info);
db.InsertRate(userId, info.Id, new RecipeRate(true, rate.Rate));
@ -91,7 +90,6 @@ namespace LocalServices
public void RemoveFromFavorites(RecipeInfo info)
{
Guid userId = Account.User.Id;
var ratings = db.ListRatesOf(userId);
RecipeRate rate = GetRate(info);
db.InsertRate(userId, info.Id, new RecipeRate(false, rate.Rate));
@ -100,7 +98,6 @@ namespace LocalServices
public void SetReviewScore(RecipeInfo info, uint score)
{
Guid userId = Account.User.Id;
var ratings = db.ListRatesOf(userId);
RecipeRate rate = GetRate(info);
db.InsertRate(userId, info.Id, new RecipeRate(rate.IsFavorite, Math.Min(score, 5)));

@ -1,16 +1,10 @@
using Models;
using Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LocalServices.Data;
using System.Security.Cryptography;
namespace LocalServices
{
internal class AuthService : IAuthService
public class AuthService : IAuthService
{
private readonly IDatabase db;

@ -2,7 +2,7 @@
namespace LocalServices
{
internal class Constants
public class Constants
{
public static readonly Uri DEFAULT_ACCOUNT_IMAGE = new Uri("https://www.pngkey.com/png/full/115-1150152_default-profile-picture-avatar-png-green.png");
}

@ -10,7 +10,7 @@ namespace LocalServices.Data
/// Database implementation with catastrophic performances.
/// This database implementation persists data in json and will save all the data in their files for each mutable requests.
/// </summary>
internal class CatastrophicPerformancesDatabase : IDatabase
public class CatastrophicPerformancesDatabase : IDatabase
{
private static readonly string RECIPES_FILENAME = "recipes_data.xml";

@ -5,7 +5,7 @@ using System.Collections.Immutable;
namespace LocalServices
{
internal class RecipesService : IRecipesService
public class RecipesService : IRecipesService
{
private readonly IDatabase db;

@ -0,0 +1,26 @@
using LocalServices.Data;
using Models;
using Services;
using System.Collections.Immutable;
namespace Tests
{
public class AccountOwnedRecipesTest
{
private static readonly User SAMPLE_USER = new User(new Uri("https://www.referenseo.com/wp-content/uploads/2019/03/image-attractive-960x540.jpg"), "user", Guid.NewGuid());
private static readonly Account SAMPLE_ACC = new Account(SAMPLE_USER, "mail");
private static readonly Recipe SAMPLE_RECIPE = new RecipeBuilder("foo", SAMPLE_USER).Build();
[Fact]
public void UploadRemove()
{
var db = new Mock<IDatabase>();
db.Setup(x => x.ListAllRecipes()).Returns(() => new List<Recipe>().ToImmutableList());
var owned = new AccountOwnedRecipes(SAMPLE_ACC, db.Object);
owned.UploadRecipe(SAMPLE_RECIPE);
Assert.Contains(SAMPLE_RECIPE.Info, owned.GetAccountRecipes());
owned.RemoveRecipe(SAMPLE_RECIPE.Info);
Assert.DoesNotContain(SAMPLE_RECIPE.Info, owned.GetAccountRecipes());
}
}
}

@ -0,0 +1,46 @@
using LocalServices;
using LocalServices.Data;
using Models;
using System.Collections.Immutable;
namespace Tests
{
public class AccountRecipesPreferencesTests
{
private static readonly User SAMPLE_USER = new User(new Uri("https://www.referenseo.com/wp-content/uploads/2019/03/image-attractive-960x540.jpg"), "user", Guid.NewGuid());
private static readonly Account SAMPLE_ACC = new Account(SAMPLE_USER, "mail");
private static readonly Recipe SAMPLE_RECIPE = new RecipeBuilder("foo", SAMPLE_USER).Build();
[Fact]
public void Review()
{
var fav_inserted = false;
var rate_inserted = false;
var dict = new Dictionary<Guid, RecipeRate>();
var db = new Mock<IDatabase>();
db.Setup(x => x.ListRatesOf(SAMPLE_USER.Id)).Returns(() => dict.ToImmutableDictionary());
db.Setup(x => x.InsertRate(SAMPLE_USER.Id, SAMPLE_RECIPE.Info.Id, new RecipeRate(true, 0))).Callback(() => fav_inserted = true);
db.Setup(x => x.InsertRate(SAMPLE_USER.Id, SAMPLE_RECIPE.Info.Id, new RecipeRate(false, 3))).Callback(() => rate_inserted = true);
var pref = new AccountRecipesPreferences(SAMPLE_ACC, db.Object);
pref.AddToFavorites(SAMPLE_RECIPE.Info);
pref.SetReviewScore(SAMPLE_RECIPE.Info, 3);
Assert.True(fav_inserted);
Assert.True(rate_inserted);
}
public void AddWeeklyList()
{
var inserted = false;
var dict = new Dictionary<Guid, uint> { };
dict.Add(SAMPLE_RECIPE.Info.Id, 88);
var db = new Mock<IDatabase>();
db.Setup(x => x.GetRecipeListOf(SAMPLE_USER.Id)).Returns(() => dict);
db.Setup(x => x.InsertInUserList(SAMPLE_USER.Id, SAMPLE_RECIPE.Info.Id, 88)).Callback(() => inserted = true);
var pref = new AccountRecipesPreferences(SAMPLE_ACC, db.Object);
pref.AddToWeeklyList(SAMPLE_RECIPE.Info, 88);
Assert.True(inserted);
Assert.True(pref.GetWeeklyList().Contains((SAMPLE_RECIPE.Info, 88)));
}
}
}

@ -0,0 +1,35 @@

using Models;
using LocalServices.Data;
using Moq;
using LocalServices;
using Services;
namespace Tests
{
public class AuthServiceTests
{
private static readonly User SAMPLE_USER = new User(new Uri("https://www.referenseo.com/wp-content/uploads/2019/03/image-attractive-960x540.jpg"), "user", Guid.NewGuid());
private static readonly Account SAMPLE_ACC = new Account(SAMPLE_USER, "mail");
[Fact]
public void TestLogin()
{
var database = new Mock<IDatabase>();
database.Setup(x => x.GetAccount("mail", "1234")).Returns(SAMPLE_ACC);
var service = new AuthService(database.Object);
var acc = service.Login("mail", "1234");
Assert.Equal(acc, SAMPLE_ACC);
}
[Fact]
public void TestRegister()
{
var database = new Mock<IDatabase>();
database.Setup(x => x.GetAccount("mail", "1234")).Returns(SAMPLE_ACC);
var service = new AuthService(database.Object);
var acc = service.Register("mail", "foo", "1234");
Assert.Equal(acc, new Account(new User(Constants.DEFAULT_ACCOUNT_IMAGE, "foo", acc.User.Id), "mail"));
}
}
}

@ -0,0 +1,25 @@
using LocalServices.Data;
using Models;
using LocalServices;
using Services;
using System.Collections.Immutable;
namespace Tests
{
public class RecipeServicesTests
{
private static readonly User SAMPLE_USER = new User(new Uri("https://www.referenseo.com/wp-content/uploads/2019/03/image-attractive-960x540.jpg"), "user", Guid.NewGuid());
private static readonly Account SAMPLE_ACC = new Account(SAMPLE_USER, "mail");
private static readonly Recipe SAMPLE_RECIPE = new RecipeBuilder("foo", SAMPLE_USER).Build();
[Fact]
public void GetRecipe()
{
var database = new Mock<IDatabase>();
database.Setup(x => x.GetRecipe(SAMPLE_RECIPE.Info.Id)).Returns(SAMPLE_RECIPE);
var service = new RecipesService(database.Object);
Assert.Equal(service.GetRecipe(SAMPLE_RECIPE.Info), SAMPLE_RECIPE);
}
}
}

@ -11,6 +11,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

@ -1,15 +0,0 @@
using Models;
namespace Tests
{
public class UnitTest1
{
[Fact]
public void Test1()
{
new RecipeRate(true, 4);
}
}
}

@ -1 +1,2 @@
global using Xunit;
global using Moq;
Loading…
Cancel
Save