public void It_Returns_The_Saved_Played_Game() { //--arrange SetupExpectationsForExistingPlayedGame(); var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = _existingGameDefinitionId }; //--act var result = autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, TransactionSource.RestApi, currentUser); //--assert result.ShouldBeSameAs(_expectedSavedPlayedGame); }
public PlayedGame UpdatePlayedGame(UpdatedGame updatedGame, TransactionSource transactionSource, ApplicationUser currentUser) { if (updatedGame.GamingGroupId.HasValue) { _securedEntityValidator.RetrieveAndValidateAccess<GamingGroup>(updatedGame.GamingGroupId.Value, currentUser); } _securedEntityValidator.RetrieveAndValidateAccess<PlayedGame>(updatedGame.PlayedGameId, currentUser); _securedEntityValidator.RetrieveAndValidateAccess<GameDefinition>(updatedGame.GameDefinitionId, currentUser); var playedGameWithStuff = _dataContext.GetQueryable<PlayedGame>() .Where(x => x.Id == updatedGame.PlayedGameId) .Include(x => x.ApplicationLinkages) .Include(x => x.GameDefinition) .Include(x => x.PlayerGameResults) .FirstOrDefault(); if (playedGameWithStuff == null) { throw new EntityDoesNotExistException(typeof(PlayedGame), updatedGame.PlayedGameId); } var gamingGroupId = updatedGame.GamingGroupId ?? playedGameWithStuff.GamingGroupId; ValidateAccessToPlayers(updatedGame.PlayerRanks, gamingGroupId, currentUser); var playerGameResults = MakePlayerGameResults(updatedGame, playedGameWithStuff.GameDefinition?.BoardGameGeekGameDefinitionId); var updatedPlayedGame = TransformNewlyCompletedGameIntoPlayedGame(updatedGame, gamingGroupId, currentUser.Id, playerGameResults); updatedPlayedGame.Id = updatedGame.PlayedGameId; updatedPlayedGame.DateUpdated = DateTime.UtcNow; CleanupPlayerResultsAndApplicationLinkages(currentUser, playedGameWithStuff); var returnPlayedGame = _dataContext.Save(updatedPlayedGame, currentUser); //--Entity Framework doesn't appear to save new child entities when updating the parent entity so we have to save them separately playerGameResults.ForEach(x => { x.PlayedGameId = returnPlayedGame.Id; _dataContext.Save(x, currentUser); }); CreateApplicationLinkages(updatedGame.ApplicationLinkages, updatedGame.PlayedGameId); DoPostSaveStuff(transactionSource, currentUser, playedGameWithStuff.Id, playedGameWithStuff.GameDefinitionId, playerGameResults); return returnPlayedGame; }
public void It_Creates_The_Played_Game_Application_Linkages() { //--arrange SetupExpectationsForExistingPlayedGame(); var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = _existingGameDefinitionId, ApplicationLinkages = new List<ApplicationLinkage> { new ApplicationLinkage() } }; var transactionSource = TransactionSource.RestApi; //--act autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, transactionSource, currentUser); //--assert autoMocker.ClassUnderTest.AssertWasCalled(mock => mock.CreateApplicationLinkages(updatedGame.ApplicationLinkages, _existingPlayedGameId)); }
public void It_Does_All_Of_The_Post_Save_Stuff() { //--arrange SetupExpectationsForExistingPlayedGame(); var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = _existingGameDefinitionId }; var transactionSource = TransactionSource.RestApi; //--act autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, transactionSource, currentUser); //--assert var args = autoMocker.ClassUnderTest.GetArgumentsForCallsMadeOn( mock => mock.DoPostSaveStuff( Arg<TransactionSource>.Is.Anything, Arg<ApplicationUser>.Is.Anything, Arg<int>.Is.Anything, Arg<int>.Is.Anything, Arg<List<PlayerGameResult>>.Is.Anything)); args.ShouldNotBeNull(); args.Count.ShouldBe(1); var firstCall = args[0]; var actualTransactionSource = (TransactionSource)firstCall[0]; actualTransactionSource.ShouldBe(transactionSource); var actualUser = firstCall[1] as ApplicationUser; actualUser.ShouldBeSameAs(currentUser); var actualPlayedGameId = (int)firstCall[2]; actualPlayedGameId.ShouldBe(_existingPlayedGameId); var actualGameDefinitionId = (int) firstCall[3]; actualGameDefinitionId.ShouldBe(_existingGameDefinitionId); var actualPlayerGameResults = firstCall[4] as List<PlayerGameResult>; actualPlayerGameResults.ShouldBeSameAs(_expectedNewPlayerGameResults); }
public void It_Reconstructs_The_Played_Game_Then_Updates_It() { //--arrange SetupExpectationsForExistingPlayedGame(); int differentGameDefinitionId = _existingGameDefinitionId + 1; var playerRank1 = new PlayerRank { PlayerId = 100, GameRank = 1 }; var playerRank2 = new PlayerRank { PlayerId = 101, GameRank = 1 }; var playerRank3 = new PlayerRank { PlayerId = 100, GameRank = 1 }; var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = differentGameDefinitionId, PlayerRanks = new List<PlayerRank> { playerRank1, playerRank2, playerRank3 }, DatePlayed = DateTime.UtcNow }; //--act autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, TransactionSource.RestApi, currentUser); //--assert autoMocker.ClassUnderTest.AssertWasCalled( partialMock => partialMock.MakePlayerGameResults(updatedGame, _existingBoardGameGeekGameDefinitionId)); autoMocker.ClassUnderTest.AssertWasCalled( partialMock => partialMock.TransformNewlyCompletedGameIntoPlayedGame(updatedGame, _existingGamingGroupId, currentUser.Id, _expectedNewPlayerGameResults)); var arguments = autoMocker.Get<IDataContext>() .GetArgumentsForCallsMadeOn( x => x.Save(Arg<PlayedGame>.Is.Anything, Arg<ApplicationUser>.Is.Anything)); arguments.ShouldNotBeNull(); arguments.Count.ShouldBe(1); var actualPlayedGame = arguments[0][0] as PlayedGame; actualPlayedGame.ShouldNotBeNull(); actualPlayedGame.DateUpdated.Date.ShouldBe(DateTime.UtcNow.Date); actualPlayedGame.Id.ShouldBe(updatedGame.PlayedGameId); }
public void It_Clears_Out_Existing_Application_Linkages() { //--arrange SetupExpectationsForExistingPlayedGame(); int differentGameDefinitionId = _existingGameDefinitionId + 1; var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = differentGameDefinitionId }; //--act autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, TransactionSource.RestApi, currentUser); //--assert autoMocker.Get<IDataContext>().AssertWasCalled(mock => mock.DeleteById<PlayedGameApplicationLinkage>(_existingPlayedGameApplicationLinkageId1, currentUser)); autoMocker.Get<IDataContext>().AssertWasCalled(mock => mock.DeleteById<PlayedGameApplicationLinkage>(_existingPlayedGameApplicationLinkageId2, currentUser)); }
public void It_Validates_Access_To_The_Players_In_The_Game() { //--arrange SetupExpectationsForExistingPlayedGame(); int differentGameDefinitionId = _existingGameDefinitionId + 1; var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = differentGameDefinitionId }; //--act autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, TransactionSource.RestApi, currentUser); //--assert autoMocker.ClassUnderTest.AssertWasCalled(partialMock => partialMock.ValidateAccessToPlayers(updatedGame.PlayerRanks, _existingGamingGroupId, currentUser)); }
public void It_Validates_Access_To_The_New_Game_Definition() { //--arrange SetupExpectationsForExistingPlayedGame(); int differentGameDefinitionId = _existingGameDefinitionId + 1; var updatedGame = new UpdatedGame { PlayedGameId = _existingPlayedGameId, GameDefinitionId = differentGameDefinitionId }; //--act autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, TransactionSource.RestApi, currentUser); //--assert autoMocker.Get<ISecuredEntityValidator>().AssertWasCalled(mock => mock.RetrieveAndValidateAccess<GameDefinition>(updatedGame.GameDefinitionId, currentUser)); }
public void It_Throws_An_EntityDoesNotExistException_If_The_Specified_Played_Game_Id_Does_Not_Match_An_Existing_Game() { //--arrange autoMocker.Get<IDataContext>().Expect(mock => mock.GetQueryable<PlayedGame>()).Return(new List<PlayedGame>().AsQueryable()); var playerRanks = new List<PlayerRank>(); var updatedGame = new UpdatedGame { GameDefinitionId = gameDefinition.Id, PlayerRanks = playerRanks, PlayedGameId = -1 }; var expectedException = new EntityDoesNotExistException(typeof(PlayedGame), updatedGame.PlayedGameId); //--act var actualException = Assert.Throws<EntityDoesNotExistException>( () => autoMocker.ClassUnderTest.UpdatePlayedGame(updatedGame, TransactionSource.RestApi, currentUser)); //--assert actualException.Message.ShouldBe(expectedException.Message); }