diff --git a/LocalServices/AccountOwnedRecipes.cs b/LocalServices/AccountOwnedRecipes.cs index 6e6ba77..34a27b2 100644 --- a/LocalServices/AccountOwnedRecipes.cs +++ b/LocalServices/AccountOwnedRecipes.cs @@ -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); } - - } } diff --git a/LocalServices/AccountRecipesPreferences.cs b/LocalServices/AccountRecipesPreferences.cs index 8697a50..8086b2b 100644 --- a/LocalServices/AccountRecipesPreferences.cs +++ b/LocalServices/AccountRecipesPreferences.cs @@ -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))); diff --git a/LocalServices/AuthService.cs b/LocalServices/AuthService.cs index a68cf5b..a229afa 100644 --- a/LocalServices/AuthService.cs +++ b/LocalServices/AuthService.cs @@ -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; diff --git a/LocalServices/Constants.cs b/LocalServices/Constants.cs index 0f626ec..cfb7bf8 100644 --- a/LocalServices/Constants.cs +++ b/LocalServices/Constants.cs @@ -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"); } diff --git a/LocalServices/Data/CatastrophicPerformancesDatabase.cs b/LocalServices/Data/CatastrophicPerformancesDatabase.cs index 56bb1fa..ba224f2 100644 --- a/LocalServices/Data/CatastrophicPerformancesDatabase.cs +++ b/LocalServices/Data/CatastrophicPerformancesDatabase.cs @@ -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. /// - internal class CatastrophicPerformancesDatabase : IDatabase + public class CatastrophicPerformancesDatabase : IDatabase { private static readonly string RECIPES_FILENAME = "recipes_data.xml"; diff --git a/LocalServices/RecipesService.cs b/LocalServices/RecipesService.cs index 2237b04..fcd9de1 100644 --- a/LocalServices/RecipesService.cs +++ b/LocalServices/RecipesService.cs @@ -5,7 +5,7 @@ using System.Collections.Immutable; namespace LocalServices { - internal class RecipesService : IRecipesService + public class RecipesService : IRecipesService { private readonly IDatabase db; diff --git a/Tests/AccountOwnedRecipesTest.cs b/Tests/AccountOwnedRecipesTest.cs new file mode 100644 index 0000000..00e6a28 --- /dev/null +++ b/Tests/AccountOwnedRecipesTest.cs @@ -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(); + db.Setup(x => x.ListAllRecipes()).Returns(() => new List().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()); + } + } +} diff --git a/Tests/AccountRecipesPreferencesTests.cs b/Tests/AccountRecipesPreferencesTests.cs new file mode 100644 index 0000000..cb3f499 --- /dev/null +++ b/Tests/AccountRecipesPreferencesTests.cs @@ -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(); + var db = new Mock(); + 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 { }; + dict.Add(SAMPLE_RECIPE.Info.Id, 88); + var db = new Mock(); + 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))); + } + } +} diff --git a/Tests/AuthServiceTests.cs b/Tests/AuthServiceTests.cs new file mode 100644 index 0000000..0747e62 --- /dev/null +++ b/Tests/AuthServiceTests.cs @@ -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(); + 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(); + 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")); + } + } +} diff --git a/Tests/RecipeServicesTests.cs b/Tests/RecipeServicesTests.cs new file mode 100644 index 0000000..b598009 --- /dev/null +++ b/Tests/RecipeServicesTests.cs @@ -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(); + 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); + } + + } +} diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 1716292..a268461 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -11,6 +11,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/Tests/UnitTest1.cs b/Tests/UnitTest1.cs deleted file mode 100644 index 5b298b4..0000000 --- a/Tests/UnitTest1.cs +++ /dev/null @@ -1,15 +0,0 @@ -using Models; - -namespace Tests -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - new RecipeRate(true, 4); - } - } - -} - diff --git a/Tests/Usings.cs b/Tests/Usings.cs index 8c927eb..7a8920d 100644 --- a/Tests/Usings.cs +++ b/Tests/Usings.cs @@ -1 +1,2 @@ -global using Xunit; \ No newline at end of file +global using Xunit; +global using Moq; \ No newline at end of file