From 7d1f70b0926107929fc17f46b5c0b3a6033b3b5d Mon Sep 17 00:00:00 2001 From: Maxime BATISTA Date: Fri, 2 Jun 2023 09:08:23 +0200 Subject: [PATCH] end UT --- LocalServices/AccountRecipesPreferences.cs | 5 +-- Tests/AccountOwnedRecipesTest.cs | 26 +++++++++++ Tests/AccountRecipesPreferencesTests.cs | 43 +++++++++++++++++-- .../CatastrophicPreformancesDatabaseTests.cs | 43 ------------------- Tests/UnitTest1.cs | 15 ------- 5 files changed, 67 insertions(+), 65 deletions(-) create mode 100644 Tests/AccountOwnedRecipesTest.cs delete mode 100644 Tests/CatastrophicPreformancesDatabaseTests.cs delete mode 100644 Tests/UnitTest1.cs diff --git a/LocalServices/AccountRecipesPreferences.cs b/LocalServices/AccountRecipesPreferences.cs index 0582cf2..8086b2b 100644 --- a/LocalServices/AccountRecipesPreferences.cs +++ b/LocalServices/AccountRecipesPreferences.cs @@ -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/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 index 7d81a9a..cb3f499 100644 --- a/Tests/AccountRecipesPreferencesTests.cs +++ b/Tests/AccountRecipesPreferencesTests.cs @@ -1,9 +1,46 @@ using LocalServices; using LocalServices.Data; - +using Models; +using System.Collections.Immutable; + namespace Tests { - internal class AccountRecipesPreferencesTests - { + 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/CatastrophicPreformancesDatabaseTests.cs b/Tests/CatastrophicPreformancesDatabaseTests.cs deleted file mode 100644 index 9742179..0000000 --- a/Tests/CatastrophicPreformancesDatabaseTests.cs +++ /dev/null @@ -1,43 +0,0 @@ -using LocalServices.Data; -using Models; -using static System.Runtime.InteropServices.JavaScript.JSType; - -namespace Tests -{ - public class CatastrophicPreformancesDatabaseTests - { - - 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()); - /* - [Fact] - public void TestInsertRecipe() - { - var db = new CatastrophicPerformancesDatabase("/tmp"); - db.InsertUser(SAMPLE_USER); - db.InsertRecipe(SAMPLE_RECIPE); - Assert.Contains(SAMPLE_RECIPE, db.ListAllRecipes()); - - } - - [Fact] - public void TestInsertRate() - { - var db = new CatastrophicPerformancesDatabase("/tmp"); - var rate = new RecipeRate(true, 4); - db.InsertUser(SAMPLE_USER); - db.InsertRate(SAMPLE_USER.Id, SAMPLE_RECIPE.Info.Id, rate); - Assert.Equal(rate, db.GetRecipeRate(SAMPLE_USER.Id, SAMPLE_RECIPE.Info.Id)); - Assert.True(db.ListRatesOf(SAMPLE_USER.Id)[SAMPLE_USER.Id] == rate); - } - - [Fact] - public void TestInsertWeeklyList() - { - var db = new CatastrophicPerformancesDatabase("/tmp"); - db.InsertUser(SAMPLE_USER); - db.InsertRecipe(SAMPLE_RECIPE); - db.InsertInUserList(SAMPLE_USER.Id, SAMPLE_RECIPE.Info.Id, 99); - Assert.True(db.GetRecipeListOf(SAMPLE_USER.Id)[SAMPLE_RECIPE.Info.Id] == 99); - }*/ - } -} 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); - } - } - -} -