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 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 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)); } }
private ICommandAction BuildDetailAccountCommand(string input, CommandUsageMatchData matchData) { DetailAccountCommand cmd = new DetailAccountCommand(input, Repositories); string name; if (matchData.TryGetArgValue(BudgetCliCommands.ARG_ACCOUNT_NAME, out name)) { cmd.NameOption.SetData(name); } DateTime date; if (matchData.TryGetArgValue(BudgetCliCommands.OPT_DATE.Arguments[0], out date)) { cmd.DateOption.SetData(date); } return(cmd); }
public void TestAddAccountActionExecute_PastState() { 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 }; UpsertAccount(account1, repositories); DateTime state1Timestamp = DateTime.Today.AddDays(-100); AccountStateDto accountState = new AccountStateDto() { AccountId = account1.Id.Value, Funds = (new Money(100)).InternalValue, IsClosed = false, Timestamp = state1Timestamp }; accountStateRepo.Upsert(accountState); DateTime state2Timestamp = DateTime.Today.AddDays(-500); AccountStateDto accountState2 = new AccountStateDto() { AccountId = account1.Id.Value, Funds = (new Money(200)).InternalValue, IsClosed = false, Timestamp = state2Timestamp }; accountStateRepo.Upsert(accountState2); DetailAccountCommand action = new DetailAccountCommand("detail account account1", repositories); action.NameOption.SetData("account1"); action.DateOption.SetData(state1Timestamp.AddDays(-1)); //Latest state should be at timestamp2 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.True(successful); Assert.NotNull(result); Assert.Equal(account1.Id, result.Item.Id); Assert.Equal(200, result.Item.CurrentState.Funds); } }