public void CreateItem_VideoAlreadyExists_ItemCreatedVideoNotUpdated() { var 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"; var 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); // Remove entity from NHibernate cache to force DB query to ensure actually created. NHibernateSessionManager.Instance.Clear(); // Ensure that the Video was NOT updated by comparing the new title to the old one. Video videoFromDatabase = VideoDao.Get(videoNotInDatabase.Id); Assert.AreNotEqual(videoFromDatabase.Title, videoTitle); // Ensure that the PlaylistItem was created. PlaylistItem itemFromDatabase = PlaylistItemDao.Get(playlistItem.Id); Assert.NotNull(itemFromDatabase); // Pointers should be self-referential with only one item in the Playlist. Assert.AreEqual(itemFromDatabase.NextItem, itemFromDatabase); Assert.AreEqual(itemFromDatabase.PreviousItem, itemFromDatabase); }
public void CreateItem_NoVideoExists_VideoAndItemCreated() { PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); // Ensure that the Video was created. Video videoFromDatabase = VideoDao.Get(playlistItem.Video.Id); Assert.NotNull(videoFromDatabase); // Ensure that the PlaylistItem was created. PlaylistItem itemFromDatabase = PlaylistItemDao.Get(playlistItem.Id); Assert.NotNull(itemFromDatabase); // Should have a sequence number after saving for sure. Assert.GreaterOrEqual(itemFromDatabase.Sequence, 0); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); // Pointers should be self-referential with only one item in the Playlist. //Assert.AreEqual(itemFromDatabase.NextItem, itemFromDatabase); //Assert.AreEqual(itemFromDatabase.PreviousItem, itemFromDatabase); }
public void Update(PlaylistItem playlistItem) { try { NHibernateSessionManager.Instance.BeginTransaction(); playlistItem.ValidateAndThrow(); playlistItem.Video.ValidateAndThrow(); PlaylistItem knownPlaylistItem = PlaylistItemDao.Get(playlistItem.Id); if (knownPlaylistItem == null) { PlaylistItemDao.Update(playlistItem); } else { PlaylistItemDao.Merge(playlistItem); } NHibernateSessionManager.Instance.CommitTransaction(); } catch (Exception exception) { Logger.Error(exception); NHibernateSessionManager.Instance.RollbackTransaction(); throw; } }
public void DeletePlaylistItem_NoOtherItems_MakesPlaylistEmpty() { PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist); // Now delete the created PlaylistItem and ensure it is removed. PlaylistItemManager.Delete(playlistItem.Id); PlaylistItem deletedPlaylistItem = PlaylistItemDao.Get(playlistItem.Id); Assert.IsNull(deletedPlaylistItem); }
public void DeletePlaylistItem_NoOtherItems_MakesPlaylistEmpty() { PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); // Now delete the created PlaylistItem and ensure it is removed. PlaylistItemManager.Delete(playlistItem.Id); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); PlaylistItem deletedPlaylistItem = PlaylistItemDao.Get(playlistItem.Id); Assert.IsNull(deletedPlaylistItem); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); }
public PlaylistItem Get(Guid id) { PlaylistItem playlistItem; try { playlistItem = PlaylistItemDao.Get(id); } catch (Exception exception) { Logger.Error(exception); throw; } return(playlistItem); }
private void DoUpdate(PlaylistItem playlistItem) { playlistItem.ValidateAndThrow(); playlistItem.Video.ValidateAndThrow(); PlaylistItem knownPlaylistItem = PlaylistItemDao.Get(playlistItem.Id); if (knownPlaylistItem == null) { PlaylistItemDao.Update(playlistItem); } else { PlaylistItemDao.Merge(playlistItem); } }
public void Delete(Guid itemId) { try { PlaylistItem playlistItem = PlaylistItemDao.Get(itemId); // Be sure to remove from Playlist first so that cascade doesn't re-save. playlistItem.Playlist.RemoveItem(playlistItem); PlaylistItemDao.Delete(playlistItem); } catch (Exception exception) { Logger.Error(exception); throw; } }
public void UpdateFirstItem(Guid playlistId, Guid firstItemId) { try { NHibernateSessionManager.Instance.BeginTransaction(); Playlist playlist = PlaylistDao.Get(playlistId); playlist.FirstItem = PlaylistItemDao.Get(firstItemId); PlaylistDao.Update(playlist); NHibernateSessionManager.Instance.CommitTransaction(); } catch (Exception exception) { Logger.Error(exception); NHibernateSessionManager.Instance.RollbackTransaction(); throw; } }
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 = PlaylistItemDao.Get(playlistItem.Id); Assert.AreEqual(itemFromDatabase.Title, updatedItemTitle); }
public void CreateItem_NoVideoExists_VideoAndItemCreated() { PlaylistItem playlistItem = Helpers.CreateItemInPlaylist(Playlist); // Ensure that the Video was created. Video videoFromDatabase = VideoDao.Get(playlistItem.Video.Id); Assert.NotNull(videoFromDatabase); // Ensure that the PlaylistItem was created. PlaylistItem itemFromDatabase = PlaylistItemDao.Get(playlistItem.Id); Assert.NotNull(itemFromDatabase); // Pointers should be self-referential with only one item in the Playlist. Assert.AreEqual(itemFromDatabase.NextItem, itemFromDatabase); Assert.AreEqual(itemFromDatabase.PreviousItem, itemFromDatabase); }
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. PlaylistItem secondItem = Helpers.CreateItemInPlaylist(Playlist); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); // Now delete the first PlaylistItem and ensure it is removed. PlaylistItemManager.Delete(firstItem.Id); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); PlaylistItem deletedPlaylistItem = PlaylistItemDao.Get(firstItem.Id); Assert.IsNull(deletedPlaylistItem); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); }
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; NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); PlaylistItemManager.Update(playlistItem); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); // Check the title of the item from the database -- make sure it updated. PlaylistItem itemFromDatabase = PlaylistItemDao.Get(playlistItem.Id); Assert.AreEqual(itemFromDatabase.Title, updatedItemTitle); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); }
public void Delete(Guid itemId) { try { NHibernateSessionManager.Instance.BeginTransaction(); PlaylistItem playlistItem = PlaylistItemDao.Get(itemId); // Be sure to remove from Playlist first so that cascade doesn't re-save. playlistItem.Playlist.RemoveItem(playlistItem); PlaylistItemDao.Delete(playlistItem); NHibernateSessionManager.Instance.CommitTransaction(); } catch (Exception exception) { Logger.Error(exception); NHibernateSessionManager.Instance.RollbackTransaction(); throw; } }
public void CreateItem_VideoAlreadyExists_ItemCreatedVideoNotUpdated() { var videoNotInDatabase = Helpers.CreateUnsavedVideoWithId(); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); VideoManager.Save(videoNotInDatabase); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); // Change the title for videoInDatabase to check that cascade-update does not affect title. Videos are immutable. const string videoTitle = "A video title"; var 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); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); PlaylistItemManager.Save(playlistItem); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); NHibernateSessionManager.Instance.OpenSessionAndBeginTransaction(); // Ensure that the Video was NOT updated by comparing the new title to the old one. Video videoFromDatabase = VideoDao.Get(videoNotInDatabase.Id); Assert.AreNotEqual(videoFromDatabase.Title, videoTitle); // Ensure that the PlaylistItem was created. PlaylistItem itemFromDatabase = PlaylistItemDao.Get(playlistItem.Id); Assert.NotNull(itemFromDatabase); // Should have a sequence number after saving for sure. Assert.GreaterOrEqual(itemFromDatabase.Sequence, 0); NHibernateSessionManager.Instance.CommitTransactionAndCloseSession(); }
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. PlaylistItem secondItem = Helpers.CreateItemInPlaylist(Playlist); // Now delete the first PlaylistItem and ensure it is removed. PlaylistItemManager.Delete(firstItem.Id); // Remove entity from NHibernate cache to force DB query to ensure actually created. NHibernateSessionManager.Instance.Clear(); PlaylistItem deletedPlaylistItem = PlaylistItemDao.Get(firstItem.Id); Assert.IsNull(deletedPlaylistItem); PlaylistItem updatedPlaylistItem = PlaylistItemDao.Get(secondItem.Id); // The second item's pointers should be self-referencing with only 1 item in the Playlist. Assert.AreEqual(updatedPlaylistItem, updatedPlaylistItem.PreviousItem); Assert.AreEqual(updatedPlaylistItem, updatedPlaylistItem.NextItem); }