public void TestAddAccountActionExecute_DuplicateName_WithListeners() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AccountDto dto = new AccountDto() { Name = "Test Account" }; repo.Upsert(dto); AddAccountCommand action = new AddAccountCommand("new account \"Test Account\"", repositories, "Test Account"); action.FundsOption.SetData(123.45); action.DescriptionOption.SetData("Test Description"); Mock <ICommandActionListener> mockListener = new Mock <ICommandActionListener>(); mockListener.Setup(x => x.OnCommand(It.Is <CreateCommandResult <Account> >(a => !a.IsSuccessful && a.CreatedItem == null))).Verifiable(); List <ICommandActionListener> listeners = new List <ICommandActionListener>(); listeners.Add(mockListener.Object); //Act bool successful = action.TryExecute(mockLog.Object, listeners); //Assert Assert.False(successful); mockListener.VerifyAll(); } }
public void TestAddAccountActionExecute() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AddAccountCommand action = new AddAccountCommand("new account \"Test Account\"", repositories, "Test Account"); action.FundsOption.SetData(123.45); action.DescriptionOption.SetData("Test Description"); //Act bool successful = action.TryExecute(mockLog.Object); Account account = DtoToModelTranslator.FromDto(repo.GetById(1), DateTime.Today, repositories); AccountState state = DtoToModelTranslator.FromDto(accountStateRepo.GetLatestByAccountId(1), repositories); //Assert Assert.True(successful); Assert.Equal("Test Account", account.Name); Assert.Null(account.CategoryId); Assert.Equal(Account.DEFAULT_PRIORITY, account.Priority); Assert.Equal(Data.Enums.AccountKind.Sink, account.AccountKind); Assert.Equal("Test Description", account.Description); Assert.NotNull(state); Assert.Equal(123.45, state.Funds); Assert.False(state.IsClosed); } }
public void TestDoesNameExist_False() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, log.Object); AccountDto account = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "account", Priority = 5, CategoryId = null }; repo.Upsert(account); //Act bool missingAccountExists = repo.DoesNameExist("doesNotExist"); //Assert Assert.False(missingAccountExists); } }
public void TestAddAccountActionExecute_NameNotExist() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); DetailAccountCommand action = new DetailAccountCommand("detail account missing", repositories); action.NameOption.SetData("missing"); ReadDetailsCommandResult <Account> result = null; Mock <ICommandActionListener> mockListener = new Mock <ICommandActionListener>(); mockListener.Setup(x => x.OnCommand(It.IsAny <ReadDetailsCommandResult <Account> >())).Callback <ReadDetailsCommandResult <Account> >((y) => { result = y; }); //Act bool successful = action.TryExecute(mockLog.Object, new [] { mockListener.Object }); //Assert Assert.False(successful); Assert.False(result.IsSuccessful); Assert.Null(result.Item); } }
public void FromDto_Account() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountDto accountDto = new AccountDto() { Id = 1, Name = "Test Account", AccountKind = Data.Enums.AccountKind.Source, CategoryId = null, Description = "", Priority = 7 }; RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object); //Act Account account = DtoToModelTranslator.FromDto(accountDto, DateTime.Today, repositories); //Assert Assert.Equal("Test Account", account.Name); Assert.Null(account.CategoryId); Assert.Equal(7, account.Priority); Assert.Equal(Data.Enums.AccountKind.Source, account.AccountKind); } }
public void Account_ToDto() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object); AccountDto accountDto = new AccountDto() { Id = 1, Name = "Test Account", AccountKind = AccountKind.Source, CategoryId = null, Description = "", Priority = 7 }; Account account = new Account(1, "Test Account", null, 7, AccountKind.Source, "", DateTime.Today, repositories); //Act AccountDto toDto = account.ToDto(); //Assert Assert.Equal(accountDto, toDto); } }
public void TestRemoveAllStatesByAccountId_NoStates() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository accountRepo = new AccountRepository(testDbInfo.ConnectionString, log.Object); AccountStateRepository repo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); AccountDto account = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "account", Priority = 5, CategoryId = null }; bool isInsertSuccessful = accountRepo.Upsert(account); //Act List <AccountStateDto> allStatesBeforeRemove = repo.GetAllByAccountId(account.Id.Value); bool isSuccessful = repo.RemoveAllByAccountId(account.Id.Value); List <AccountStateDto> allStatesAfterRemove = repo.GetAllByAccountId(account.Id.Value); //Assert Assert.True(isInsertSuccessful); Assert.False(isSuccessful); Assert.Empty(allStatesBeforeRemove); Assert.Empty(allStatesAfterRemove); } }
public void Transaction_ToDto() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object); DateTime timestamp = DateTime.Now; long? sourceAccountId = 1; long? destAccountId = 3; Money transferAmount = 123.45; string memo = "memo"; TransactionDto transactionDto = new TransactionDto() { Id = 1, Timestamp = timestamp, SourceAccountId = sourceAccountId, DestinationAccountId = destAccountId, TransferAmount = transferAmount.InternalValue, Memo = memo }; Transaction transaction = new Transaction(1, timestamp, sourceAccountId, destAccountId, transferAmount, memo, repositories); //Act TransactionDto toDto = transaction.ToDto(); //Assert Assert.Equal(transactionDto, toDto); } }
public void TestTransactionRepositoryInsertNew_NullSourceNullDest() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); Mock <TransactionDto> mockDto = new Mock <TransactionDto>(); mockDto.SetupAllProperties(); mockDto.SetupGet(t => t.SourceAccountId).Returns((long?)null); mockDto.SetupGet(t => t.DestinationAccountId).Returns((long?)null); //Skip set timestamp property var repo = new TransactionRepository(testDbInfo.ConnectionString, log.Object); //Act bool success = repo.Upsert(mockDto.Object); //Assert //Ensure update failed Assert.False(success); //Ensure failure was logged log.Verify(l => l.WriteLine(It.IsAny <string>(), LogLevel.Error)); //Ensure ID was not set Assert.Null(mockDto.Object.Id); } }
public void TestTransactionRepositoryUpdate_NullSourceNullDest() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository accountRepo = new AccountRepository(testDbInfo.ConnectionString, log.Object); Dictionary <string, long> accountIds = SetupAccounts(accountRepo, "A1", "A2"); Mock <TransactionDto> mockDto = new Mock <TransactionDto>(); mockDto.SetupAllProperties(); mockDto.SetupGet(t => t.TransferAmount).Returns(123_45); mockDto.SetupGet(t => t.Timestamp).Returns(DateTime.Today); mockDto.Object.SourceAccountId = accountIds["A1"]; mockDto.Object.DestinationAccountId = accountIds["A2"]; var repo = new TransactionRepository(testDbInfo.ConnectionString, log.Object); //First upsert will be an insert repo.Upsert(mockDto.Object); mockDto.Object.SourceAccountId = null; mockDto.Object.DestinationAccountId = null; //Act bool successful = repo.Upsert(mockDto.Object); //Assert update failed Assert.False(successful); //Ensure failure was logged log.Verify(l => l.WriteLine(It.IsAny <string>(), LogLevel.Error)); } }
public void TestGetIdByName_NameDoesNotExist() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, log.Object); AccountDto account = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "account", Priority = 5, CategoryId = null }; repo.Upsert(account); //Act long missingAccountId = repo.GetIdByName("doesNotExist"); //Assert Assert.Equal(-1, missingAccountId); } }
public void TestAddAccountActionExecute_DuplicateName() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AccountDto dto = new AccountDto() { Name = "Test Account" }; repo.Upsert(dto); AddAccountCommand action = new AddAccountCommand("new account \"Test Account\"", repositories, "Test Account"); action.FundsOption.SetData(123.45); action.DescriptionOption.SetData("Test Description"); //Act bool successful = action.TryExecute(mockLog.Object); //Assert Assert.False(successful); } }
public void TestGetLatestStateByAccountId() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository accountRepo = new AccountRepository(testDbInfo.ConnectionString, log.Object); AccountStateRepository repo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); AccountDto account = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "account", Priority = 5, CategoryId = null }; bool isInsertSuccessful = accountRepo.Upsert(account); DateTime state1Timestamp = DateTime.Now; AccountStateDto accountState = new AccountStateDto() { AccountId = account.Id.Value, Funds = (new Money(100)).InternalValue, IsClosed = false, Timestamp = state1Timestamp }; isInsertSuccessful &= repo.Upsert(accountState); DateTime state2Timestamp = state1Timestamp.AddDays(-1); AccountStateDto accountState2 = new AccountStateDto() { AccountId = account.Id.Value, Funds = (new Money(500)).InternalValue, IsClosed = false, Timestamp = state2Timestamp }; isInsertSuccessful &= repo.Upsert(accountState2); DateTime state3Timestamp = state1Timestamp.AddDays(-2); AccountStateDto accountState3 = new AccountStateDto() { AccountId = account.Id.Value, Funds = (new Money(800)).InternalValue, IsClosed = false, Timestamp = state3Timestamp }; isInsertSuccessful &= repo.Upsert(accountState3); //Act AccountStateDto latest = repo.GetLatestByAccountId(account.Id.Value); //Assert Assert.True(isInsertSuccessful); Assert.Equal(100, new Money(latest.Funds, true)); } }
public void TestDoesNameExist_True() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, log.Object); AccountDto parent = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "parent", Priority = 5, CategoryId = null }; bool isParentInsertSuccessful = repo.Upsert(parent); AccountDto child1 = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "child1", Priority = 5, CategoryId = parent.Id }; bool isChild1InsertSuccessful = repo.Upsert(child1); AccountDto child2 = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "child2", Priority = 5, CategoryId = parent.Id }; bool isChild2InsertSuccessful = repo.Upsert(child2); //Act bool parentExists = repo.DoesNameExist("parent"); bool child1Exists = repo.DoesNameExist("child1"); bool child2Exists = repo.DoesNameExist("child2"); //Assert Assert.True(isParentInsertSuccessful); Assert.True(isChild1InsertSuccessful); Assert.True(isChild2InsertSuccessful); Assert.True(parentExists); Assert.True(child1Exists); Assert.True(child2Exists); } }
public void TestDeleteAccountActionExecute() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AccountDto accountDto = new AccountDto() { AccountKind = Data.Enums.AccountKind.Sink, CategoryId = null, Description = "test account", Name = "Test Account", Priority = 5 }; bool isInsertSuccessful = repo.Upsert(accountDto); long accountId = accountDto.Id.Value; int accountCountBeforeDelete = repo.GetAll().Count(); AccountStateDto stateDto = new AccountStateDto() { AccountId = accountId, Funds = 0, Timestamp = DateTime.Now, IsClosed = false }; isInsertSuccessful &= accountStateRepo.Upsert(stateDto); int stateCountBeforeDelete = accountStateRepo.GetAll().Count(); DeleteAccountCommand action = new DeleteAccountCommand("rm account \"Test Account\"", repositories); action.AccountName.SetData("Test Account"); action.IsRecursiveOption.SetData(false); //Act bool successful = action.TryExecute(mockLog.Object); int accountCountAfterDelete = repo.GetAll().Count(); int stateCountAfterDelete = accountStateRepo.GetAll().Count(); bool isClosed = accountStateRepo.GetLatestByAccountId(accountId).IsClosed; //Assert Assert.True(isInsertSuccessful); Assert.True(successful); Assert.Equal(1, accountCountBeforeDelete); Assert.Equal(1, accountCountAfterDelete); Assert.Equal(1, stateCountBeforeDelete); Assert.Equal(2, stateCountAfterDelete); Assert.True(isClosed); } }
public void ListAccountCommandExecute_PriorityOption() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AccountDto account1 = new AccountDto() { Id = null, AccountKind = AccountKind.Sink, Name = "account1", Priority = 5, Description = "account1 description", CategoryId = null }; AccountDto account2 = new AccountDto() { Id = null, AccountKind = AccountKind.Sink, Name = "account2", Priority = 10, Description = "account2 description", CategoryId = null }; UpsertAccount(account1, repositories); UpsertAccount(account2, repositories); ListAccountCommand action = new ListAccountCommand("ls accounts", repositories); action.PriorityOption.SetData(new Range <long>(4, 6)); FakeCommandActionListener listener = new FakeCommandActionListener(); //Act bool successful = action.TryExecute(mockLog.Object, new ICommandActionListener[] { listener }); IEnumerable <ReadCommandResult <Account> > results = listener.GetResults <ReadCommandResult <Account> >(); IEnumerable <Account> accounts = results.First().FilteredItems; //Assert Assert.True(successful); Assert.True(listener.OnlyHasType <ReadCommandResult <Account> >()); Assert.Equal(1, results.Count()); Assert.Equal(1, accounts.Count()); Assert.Contains(DtoToModelTranslator.FromDto(account1, DateTime.Today, repositories), accounts); Assert.DoesNotContain(DtoToModelTranslator.FromDto(account2, DateTime.Today, repositories), accounts); } }
public void TestAccountStatePropertyValues() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository accountRepo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, accountRepo, accountStateRepo); var accountDto = new AccountDto() { Name = "account", Priority = 5, AccountKind = AccountKind.Sink }; accountRepo.Upsert(accountDto); long accountId = accountDto.Id.Value; Money funds = 123.45; DateTime timestamp = DateTime.Now; bool isClosed = false; AccountStateDto accountStateDto = new AccountStateDto() { AccountId = accountId, IsClosed = isClosed, Funds = funds.InternalValue, Timestamp = timestamp }; accountStateRepo.Upsert(accountStateDto); long id = accountStateDto.Id.Value; //Act Account account = DtoToModelTranslator.FromDto(accountDto, DateTime.Today, repositories); AccountState state = DtoToModelTranslator.FromDto(accountStateDto, repositories); var propertyValues = state.GetPropertyValues().ToList(); //Assert Assert.Equal(5, propertyValues.Count); Assert.Equal(id, propertyValues.First(x => x.Property.Equals(AccountState.PROP_ID)).Value); Assert.Equal(account, propertyValues.First(x => x.Property.Equals(AccountState.PROP_ACCOUNT)).Value); Assert.Equal(funds, propertyValues.First(x => x.Property.Equals(AccountState.PROP_FUNDS)).Value); Assert.Equal(isClosed, propertyValues.First(x => x.Property.Equals(AccountState.PROP_STATUS)).Value); Assert.Equal(timestamp, propertyValues.First(x => x.Property.Equals(AccountState.PROP_TIMESTAMP)).Value); } }
public void TestAddAccountActionExecute() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AccountDto account1 = new AccountDto() { Id = null, AccountKind = AccountKind.Sink, Name = "account1", Priority = 123, Description = "account1 description", CategoryId = null }; AccountDto account2 = new AccountDto() { Id = null, AccountKind = AccountKind.Sink, Name = "account2", Priority = 5, Description = "account2 description", CategoryId = null }; UpsertAccount(account1, repositories); UpsertAccount(account2, repositories); DetailAccountCommand action = new DetailAccountCommand("detail account account1", repositories); action.NameOption.SetData("account1"); ReadDetailsCommandResult <Account> result = null; Mock <ICommandActionListener> mockListener = new Mock <ICommandActionListener>(); mockListener.Setup(x => x.OnCommand(It.IsAny <ReadDetailsCommandResult <Account> >())).Callback <ReadDetailsCommandResult <Account> >((y) => { result = y; }); //Act bool successful = action.TryExecute(mockLog.Object, new [] { mockListener.Object }); Account account = DtoToModelTranslator.FromDto(repo.GetById(1), DateTime.Today, repositories); //Assert Assert.True(successful); Assert.NotNull(result); Assert.Equal(account1.Id, result.Item.Id); } }
public void AddAccountCommand_NoName() { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); CommandInterpreter interpreter = new CommandInterpreter(SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object), BudgetCliCommands.BuildCommandLibrary()); ICommandAction action; bool success = interpreter.TryParseCommand("new account", out action); Assert.False(success); Assert.Null(action); } }
public void TestOpenAccount_BadAccountId() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountStateRepository repo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); //Act bool successful = repo.ReOpenAccount(1); //Assert Assert.False(successful); } }
public void TestRemoveAllStatesByAccountId_InvalidAccount() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountStateRepository repo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); //Act bool isSuccessful = repo.RemoveAllByAccountId(1); //Assert Assert.False(isSuccessful); } }
public void TestAccountAddFromDtoConstructor() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountDto categoryDto = new AccountDto() { Name = "Test Category", AccountKind = Data.Enums.AccountKind.Category, CategoryId = null, Description = "", Priority = 3 }; repo.Upsert(categoryDto); AccountDto accountDto = new AccountDto() { Name = "Test Account", AccountKind = Data.Enums.AccountKind.Source, CategoryId = categoryDto.Id, Description = "", Priority = 7 }; repo.Upsert(accountDto); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo); //Act Account account = new Account(accountDto.Id.Value, accountDto.Name, accountDto.CategoryId, accountDto.Priority, accountDto.AccountKind, accountDto.Description, DateTime.Today, repositories); Account category = account.Category; //Assert Assert.Equal("Test Account", account.Name); Assert.Equal(categoryDto.Id, account.CategoryId); Assert.Equal(7, account.Priority); Assert.Equal(Data.Enums.AccountKind.Source, account.AccountKind); Assert.Equal("Test Category", category.Name); Assert.Null(category.CategoryId); Assert.Equal(3, category.Priority); Assert.Equal(Data.Enums.AccountKind.Category, category.AccountKind); } }
public void SystemCommand(string input, CommandKind expectedKind) { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); CommandInterpreter interpreter = new CommandInterpreter(SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object), BudgetCliCommands.BuildCommandLibrary()); ICommandAction action; bool success = interpreter.TryParseCommand(input, out action); Assert.True(success); Assert.NotNull(action); Assert.IsType(typeof(SystemCommand), action); Assert.Equal(expectedKind, ((SystemCommand)action).CommandKind); } }
public void DetailAccountCommand_WithDate() { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository stateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); //Id will be 1 repo.Upsert(new Data.Models.AccountDto() { AccountKind = AccountKind.Source, CategoryId = null, Description = "Description", Name = "Name", Priority = 5 }); stateRepo.Upsert(new Data.Models.AccountStateDto() { AccountId = 1, Funds = 10, IsClosed = false, Timestamp = DateTime.Today.AddDays(-10) }); stateRepo.Upsert(new Data.Models.AccountStateDto() { AccountId = 1, Funds = 100, IsClosed = false, Timestamp = DateTime.Today }); CommandInterpreter interpreter = new CommandInterpreter(SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, stateRepo), BudgetCliCommands.BuildCommandLibrary()); ICommandAction action; bool success = interpreter.TryParseCommand("detail account \"Name\" -d yesterday", out action); Assert.True(success); Assert.IsType <DetailAccountCommand>(action); DetailAccountCommand command = (DetailAccountCommand)action; Assert.Equal("Name", command.NameOption.GetValue("N/A")); Assert.Equal(DateTime.Today.AddDays(-1), command.DateOption.GetValue(null)); } }
public void ListAccountCommand_NullListeners() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); ListAccountCommand action = new ListAccountCommand("ls accounts", repositories); //Act bool successful = action.TryExecute(mockLog.Object, null); //Passes automatically if no exceptions were thrown } }
public void AddAccountCommand() { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); CommandInterpreter interpreter = new CommandInterpreter(SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object), BudgetCliCommands.BuildCommandLibrary()); ICommandAction action; bool success = interpreter.TryParseCommand("new account \"name\"", out action); Assert.True(success); Assert.IsType <AddAccountCommand>(action); AddAccountCommand addAccount = (AddAccountCommand)action; Assert.Equal("name", addAccount.AccountName.GetValue(null)); } }
public void DeleteAccountCommand_Recursive() { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); CommandInterpreter interpreter = new CommandInterpreter(SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object), BudgetCliCommands.BuildCommandLibrary()); ICommandAction action; bool success = interpreter.TryParseCommand("rm account test -r", out action); Assert.True(success); Assert.IsType <DeleteAccountCommand>(action); DeleteAccountCommand deleteAccount = (DeleteAccountCommand)action; Assert.Equal("test", deleteAccount.AccountName.GetValue(null)); Assert.True(deleteAccount.IsRecursiveOption.GetValue(false)); } }
public void TestAddAccountActionExecute_WithListeners() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> mockLog = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object); AccountStateRepository accountStateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, accountStateRepo); AddAccountCommand action = new AddAccountCommand("new account \"Test Account\"", repositories, "Test Account"); action.FundsOption.SetData(123.45); action.DescriptionOption.SetData("Test Description"); Mock <ICommandActionListener> mockListener = new Mock <ICommandActionListener>(); mockListener.Setup(x => x.OnCommand(It.Is <CreateCommandResult <Account> >(a => a.IsSuccessful && a.CreatedItem.Name.Equals("Test Account")))).Verifiable(); List <ICommandActionListener> listeners = new List <ICommandActionListener>(); listeners.Add(mockListener.Object); //Act bool successful = action.TryExecute(mockLog.Object, listeners); Account account = DtoToModelTranslator.FromDto(repo.GetById(1), DateTime.Today, repositories); AccountState state = DtoToModelTranslator.FromDto(accountStateRepo.GetLatestByAccountId(1), repositories); //Assert Assert.True(successful); Assert.Equal("Test Account", account.Name); Assert.Null(account.CategoryId); Assert.Equal(Account.DEFAULT_PRIORITY, account.Priority); Assert.Equal(Data.Enums.AccountKind.Sink, account.AccountKind); Assert.Equal("Test Description", account.Description); Assert.NotNull(state); Assert.Equal(123.45, state.Funds); Assert.False(state.IsClosed); mockListener.VerifyAll(); } }
public void TestTryDoAction() { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object); SystemCommand command = new SystemCommand("version", CommandKind.Version); List <ICommandActionListener> listeners = new List <ICommandActionListener>(); Mock <ICommandActionListener> mockListener = new Mock <ICommandActionListener>(); mockListener.Setup(x => x.OnCommand(It.Is <SystemCommandResult>(x => x.CommandKind == CommandKind.Version && x.IsSuccessful && x.CommandAction == command))).Verifiable(); listeners.Add(mockListener.Object); command.TryExecute(mockLog.Object, listeners); mockListener.VerifyAll(); } }
public void TestTryDoAction() { using (var testDbInfo = SetupUtil.CreateTestDb()) { Mock <ILog> mockLog = new Mock <ILog>(); RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object); HelpCommand command = new HelpCommand("new account -h", repositories, BudgetCliCommands.CMD_NEW_ACCOUNT); List <ICommandActionListener> listeners = new List <ICommandActionListener>(); Mock <ICommandActionListener> mockListener = new Mock <ICommandActionListener>(); mockListener.Setup(x => x.OnCommand(It.IsAny <HelpCommandResult>())).Verifiable(); listeners.Add(mockListener.Object); command.TryExecute(mockLog.Object, listeners); mockListener.VerifyAll(); } }