/
PlaylistItemManagerTest.cs
184 lines (148 loc) · 6.9 KB
/
PlaylistItemManagerTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using FluentValidation;
using NHibernate;
using NUnit.Framework;
using Streamus_Web_API.Domain;
using Streamus_Web_API.Domain.Interfaces;
namespace Streamus_Web_API_Tests.Tests.Manager_Tests
{
[TestFixture]
public class PlaylistItemManagerTest : StreamusTest
{
private User User { get; set; }
private Playlist Playlist { get; set; }
private IPlaylistItemManager PlaylistItemManager;
private IPlaylistManager PlaylistManager;
/// <summary>
/// This code runs before every test.
/// </summary>
[SetUp]
public void SetupContext()
{
PlaylistItemManager = ManagerFactory.GetPlaylistItemManager(Session);
PlaylistManager = ManagerFactory.GetPlaylistManager(Session);
// Ensure that a User exists.
User = Helpers.CreateUser();
// Make a new Playlist object each time to ensure no side-effects from previous test case.
Playlist = User.CreateAndAddPlaylist();
PlaylistManager.Save(Playlist);
}
/// <summary>
/// Create a new PlaylistItem with a Video whose ID is not in the database currently.
/// </summary>
[Test]
public void CreateItem_NoVideoExists_VideoAndItemCreated()
{
PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist);
// Ensure that the PlaylistItem was created.
PlaylistItem itemFromDatabase = PlaylistItemManager.Get(playlistItem.Id);
Assert.NotNull(itemFromDatabase);
// Should have a sequence number after saving for sure.
Assert.GreaterOrEqual(itemFromDatabase.Sequence, 0);
// Pointers should be self-referential with only one item in the Playlist.
//Assert.AreEqual(itemFromDatabase.NextItem, itemFromDatabase);
//Assert.AreEqual(itemFromDatabase.PreviousItem, itemFromDatabase);
}
/// <summary>
/// Create a new PlaylistItem with a Video whose ID is in the database.
/// No update should happen to the Video as it is immutable.
/// </summary>
//[Test]
//public void CreateItem_VideoAlreadyExists_ItemCreatedVideoNotUpdated()
//{
// Video videoNotInDatabase = Helpers.CreateUnsavedVideoWithId();
// VideoManager.Save(videoNotInDatabase);
// // Change the title for videoInDatabase to check that cascade-update does not affect title. Videos are immutable.
// const string videoTitle = "A video title";
// Video videoInDatabase = Helpers.CreateUnsavedVideoWithId(titleOverride: videoTitle);
// // Create a new PlaylistItem and write it to the database.
// string title = videoInDatabase.Title;
// var playlistItem = new PlaylistItem(title, videoInDatabase);
// Playlist.AddItem(playlistItem);
// PlaylistItemManager.Save(playlistItem);
// // Ensure that the Video was NOT updated by comparing the new title to the old one.
// Video videoFromDatabase = VideoManager.Get(videoNotInDatabase.Id);
// Assert.AreNotEqual(videoFromDatabase.Title, videoTitle);
// // Ensure that the PlaylistItem was created.
// PlaylistItem itemFromDatabase = PlaylistItemManager.Get(playlistItem.Id);
// Assert.NotNull(itemFromDatabase);
// // Should have a sequence number after saving for sure.
// Assert.GreaterOrEqual(itemFromDatabase.Sequence, 0);
//}
[Test]
public void CreateItem_NotAddedToPlaylistBeforeSave_ItemNotAdded()
{
// Create a new PlaylistItem and write it to the database.
var playlistItem = Helpers.CreateUnsavedPlaylistItem();
bool validationExceptionEncountered = false;
try
{
// Try to save the item without adding to Playlist. This should fail.
PlaylistItemManager.Save(playlistItem);
}
catch (ValidationException)
{
validationExceptionEncountered = true;
}
Assert.IsTrue(validationExceptionEncountered);
}
[Test]
public void UpdateItemTitle_ItemExistsInDatabase_ItemTitleUpdated()
{
// Create and save a playlistItem to the database.
PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist);
// Change the item's title.
const string updatedItemTitle = "Updated PlaylistItem title";
playlistItem.Title = updatedItemTitle;
PlaylistItemManager.Update(playlistItem);
// Check the title of the item from the database -- make sure it updated.
PlaylistItem itemFromDatabase = PlaylistItemManager.Get(playlistItem.Id);
Assert.AreEqual(itemFromDatabase.Title, updatedItemTitle);
}
/// <summary>
/// Test deleting a single PlaylistItem. The Playlist itself should not have any items in it.
/// </summary>
[Test]
public void DeletePlaylistItem_NoOtherItems_MakesPlaylistEmpty()
{
PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist);
// Now delete the created PlaylistItem and ensure it is removed.
PlaylistItemManager.Delete(playlistItem.Id);
bool exceptionEncountered = false;
try
{
PlaylistItemManager.Get(playlistItem.Id);
}
catch (ObjectNotFoundException)
{
exceptionEncountered = true;
}
Assert.IsTrue(exceptionEncountered);
}
/// <summary>
/// Remove the first PlaylistItem from a Playlist which has two items in it after adds.
/// Leave the second item, but adjust linked list pointers to accomodate.
/// </summary>
[Test]
public void DeleteFirstPlaylistItem_OneOtherItemInPlaylist_LeaveSecondItemAndUpdatePointers()
{
// Create the first PlaylistItem and write it to the database.
PlaylistItem firstItem = Helpers.CreateItemInPlaylist(Playlist);
// Create the second PlaylistItem and write it to the database.
Helpers.CreateItemInPlaylist(Playlist);
// Now delete the first PlaylistItem and ensure it is removed.
PlaylistItemManager.Delete(firstItem.Id);
bool exceptionEncountered = false;
try
{
PlaylistItemManager.Get(firstItem.Id);
}
catch (ObjectNotFoundException)
{
exceptionEncountered = true;
}
Assert.IsTrue(exceptionEncountered);
}
// TODO: Test case where there are 2 PlaylistItems in the Playlist before deleting.
// TODO: Test bulk-delete in one transaction.
}
}