You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
5.8 KiB

  1. using System;
  2. using System.Linq;
  3. using Xunit;
  4. using TMDbLib.Objects.Authentication;
  5. using TMDbLib.Objects.General;
  6. using TMDbLib.Objects.Lists;
  7. using TMDbLib.Objects.Movies;
  8. using TMDbLib.Objects.Search;
  9. using TMDbLibTests.Helpers;
  10. using TMDbLibTests.JsonHelpers;
  11. namespace TMDbLibTests
  12. {
  13. public class ClientListsTests : TestBase
  14. {
  15. private const string TestListId = "528349d419c2954bd21ca0a8";
  16. [Fact]
  17. public void TestList()
  18. {
  19. // Get list
  20. GenericList list = Config.Client.GetListAsync(TestListId).Result;
  21. Assert.NotNull(list);
  22. Assert.Equal(TestListId, list.Id);
  23. Assert.Equal(list.ItemCount, list.Items.Count);
  24. foreach (SearchMovie movieResult in list.Items)
  25. {
  26. Assert.NotNull(movieResult);
  27. // Ensure all movies point to this list
  28. int page = 1;
  29. SearchContainer<ListResult> movieLists = Config.Client.GetMovieListsAsync(movieResult.Id).Result;
  30. while (movieLists != null)
  31. {
  32. // Check if the current result page contains the relevant list
  33. if (movieLists.Results.Any(s => s.Id == TestListId))
  34. {
  35. movieLists = null;
  36. continue;
  37. }
  38. // See if there is an other page we could try, if not the test fails
  39. if (movieLists.Page < movieLists.TotalPages)
  40. movieLists = Config.Client.GetMovieListsAsync(movieResult.Id, ++page).Result;
  41. else
  42. throw new Exception($"Movie '{movieResult.Title}' was not linked to the test list");
  43. }
  44. }
  45. }
  46. [Fact]
  47. public void TestListIsMoviePresentFailure()
  48. {
  49. Assert.False(Config.Client.GetListIsMoviePresentAsync(TestListId, IdHelper.Terminator).Result);
  50. Config.Client.SetSessionInformation(Config.UserSessionId, SessionType.UserSession);
  51. // Clear list
  52. Assert.True(Config.Client.ListClearAsync(TestListId).Result);
  53. // Verify Avatar is not present
  54. Assert.False(Config.Client.GetListIsMoviePresentAsync(TestListId, IdHelper.Avatar).Result);
  55. // Add Avatar
  56. Assert.True(Config.Client.ListAddMovieAsync(TestListId, IdHelper.Avatar).Result);
  57. // Verify Avatar is present
  58. Assert.True(Config.Client.GetListIsMoviePresentAsync(TestListId, IdHelper.Avatar).Result);
  59. }
  60. [Fact]
  61. public void TestListCreateAndDelete()
  62. {
  63. const string listName = "Test List 123";
  64. Config.Client.SetSessionInformation(Config.UserSessionId, SessionType.UserSession);
  65. string newListId = Config.Client.ListCreateAsync(listName).Result;
  66. Assert.False(string.IsNullOrWhiteSpace(newListId));
  67. GenericList newlyAddedList = Config.Client.GetListAsync(newListId).Result;
  68. Assert.NotNull(newlyAddedList);
  69. Assert.Equal(listName, newlyAddedList.Name);
  70. Assert.Equal("", newlyAddedList.Description); // "" is the default value
  71. Assert.Equal("en", newlyAddedList.Iso_639_1); // en is the default value
  72. Assert.Equal(0, newlyAddedList.ItemCount);
  73. Assert.Equal(0, newlyAddedList.Items.Count);
  74. Assert.False(string.IsNullOrWhiteSpace(newlyAddedList.CreatedBy));
  75. Assert.True(Config.Client.ListDeleteAsync(newListId).Result);
  76. }
  77. [Fact]
  78. public void TestListDeleteFailure()
  79. {
  80. Config.Client.SetSessionInformation(Config.UserSessionId, SessionType.UserSession);
  81. // Try removing a list with an incorrect id
  82. Assert.False(Config.Client.ListDeleteAsync("bla").Result);
  83. }
  84. [Fact]
  85. public void TestListAddAndRemoveMovie()
  86. {
  87. Config.Client.SetSessionInformation(Config.UserSessionId, SessionType.UserSession);
  88. // Add a new movie to the list
  89. Assert.True(Config.Client.ListAddMovieAsync(TestListId, IdHelper.EvanAlmighty).Result);
  90. // Try again, this time it should fail since the list already contains this movie
  91. Assert.False(Config.Client.ListAddMovieAsync(TestListId, IdHelper.EvanAlmighty).Result);
  92. // Get list and check if the item was added
  93. GenericList listAfterAdd = Config.Client.GetListAsync(TestListId).Result;
  94. Assert.True(listAfterAdd.Items.Any(m => m.Id == IdHelper.EvanAlmighty));
  95. // Remove the previously added movie from the list
  96. Assert.True(Config.Client.ListRemoveMovieAsync(TestListId, IdHelper.EvanAlmighty).Result);
  97. // Get list and check if the item was removed
  98. GenericList listAfterRemove = Config.Client.GetListAsync(TestListId).Result;
  99. Assert.False(listAfterRemove.Items.Any(m => m.Id == IdHelper.EvanAlmighty));
  100. }
  101. [Fact]
  102. public void TestListClear()
  103. {
  104. Config.Client.SetSessionInformation(Config.UserSessionId, SessionType.UserSession);
  105. // Add a new movie to the list
  106. Assert.True(Config.Client.ListAddMovieAsync(TestListId, IdHelper.MadMaxFuryRoad).Result);
  107. // Get list and check if the item was added
  108. GenericList listAfterAdd = Config.Client.GetListAsync(TestListId).Result;
  109. Assert.True(listAfterAdd.Items.Any(m => m.Id == IdHelper.MadMaxFuryRoad));
  110. // Clear the list
  111. Assert.True(Config.Client.ListClearAsync(TestListId).Result);
  112. // Get list and check that all items were removed
  113. GenericList listAfterRemove = Config.Client.GetListAsync(TestListId).Result;
  114. Assert.False(listAfterRemove.Items.Any());
  115. }
  116. }
  117. }