Beispiel #1
0
        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_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 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);
            }
        }
Beispiel #4
0
        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 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();
            }
        }
Beispiel #6
0
        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);
            }
        }
Beispiel #7
0
        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 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);
            }
        }
Beispiel #9
0
        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);
            }
        }
Beispiel #11
0
        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);
            }
        }
Beispiel #13
0
        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 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 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 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));
            }
        }
Beispiel #17
0
        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 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();
            }
        }
Beispiel #21
0
        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();
            }
        }
        public void AddAccountCommand_WithOptions()
        {
            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 -p 4 -c category -d description -f 10 -y Sink", out action);

                Assert.True(success);
                Assert.IsType <AddAccountCommand>(action);

                AddAccountCommand addAccount = (AddAccountCommand)action;
                Assert.Equal("name", addAccount.AccountName.GetValue(null));

                Assert.Equal(4L, addAccount.PriorityOption.GetValue(null));
                Assert.Equal("category", addAccount.CategoryNameOption.GetValue(null));
                Assert.Equal("description", addAccount.DescriptionOption.GetValue(null));
                Assert.Equal(new Money(10), addAccount.FundsOption.GetValue(null));
                Assert.Equal(AccountKind.Sink, addAccount.AccountTypeOption.GetValue(null));
            }
        }
Beispiel #23
0
        public void ListAccountCommandActionKind()
        {
            ListAccountCommand action = new ListAccountCommand("ls accounts", SetupUtil.CreateMockRepositoryBag(string.Empty, null));

            Assert.Equal(CommandActionKind.ListAccount, action.CommandActionKind);
        }
Beispiel #24
0
        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);
            }
        }
        public void ListAccountCommand_Options()
        {
            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  = 2,
                    Description = "Description",
                    Name        = "Name",
                    Priority    = 10
                });
                stateRepo.Upsert(new Data.Models.AccountStateDto()
                {
                    AccountId = 1,
                    Funds     = 10,
                    IsClosed  = false,
                    Timestamp = DateTime.Today
                });

                //Id will be 2
                repo.Upsert(new Data.Models.AccountDto()
                {
                    AccountKind = AccountKind.Category,
                    CategoryId  = null,
                    Description = "",
                    Name        = "Category",
                    Priority    = 5
                });
                stateRepo.Upsert(new Data.Models.AccountStateDto()
                {
                    AccountId = 2,
                    Funds     = 100,
                    IsClosed  = false,
                    Timestamp = DateTime.Today
                });

                //Id will be 3
                repo.Upsert(new Data.Models.AccountDto()
                {
                    AccountKind = AccountKind.Category,
                    CategoryId  = null,
                    Description = "",
                    Name        = "Category2",
                    Priority    = 5
                });
                stateRepo.Upsert(new Data.Models.AccountStateDto()
                {
                    AccountId = 3,
                    Funds     = 50,
                    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("list accounts -n Name -c Category -d Description -y Source -p (4,6) -f (90,110)", out action);

                Assert.True(success);
                Assert.IsType <ListAccountCommand>(action);

                ListAccountCommand command = (ListAccountCommand)action;
                Assert.Equal("Name", command.NameOption.GetValue("N/A"));
                Assert.Equal("Description", command.DescriptionOption.GetValue("N/A"));
                Assert.Equal(2L, command.CategoryIdOption.GetValue("N/A"));
                Assert.Equal(AccountKind.Source, command.AccountTypeOption.GetValue(AccountKind.Sink));

                Range <long>  expectedPriorityRange = new Range <long>(4, 6, false, false);
                Range <Money> expectedFundsRange    = new Range <Money>(90, 110, false, false);
                Assert.Equal(expectedFundsRange, command.FundsOption.GetValue(null));
                Assert.Equal(expectedPriorityRange, command.PriorityOption.GetValue(null));
            }
        }
Beispiel #26
0
        public void TestTransactionPropertyValues()
        {
            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 accountDto1 = new AccountDto()
                {
                    Name        = "source",
                    Priority    = 5,
                    AccountKind = AccountKind.Sink
                };
                accountRepo.Upsert(accountDto1);
                var accountDto2 = new AccountDto()
                {
                    Name        = "dest",
                    Priority    = 5,
                    AccountKind = AccountKind.Sink
                };
                accountRepo.Upsert(accountDto2);
                accountStateRepo.Upsert(new AccountStateDto()
                {
                    AccountId = 1,
                    IsClosed  = false,
                    Funds     = 0,
                    Timestamp = DateTime.Now
                });
                accountStateRepo.Upsert(new AccountStateDto()
                {
                    AccountId = 2,
                    IsClosed  = false,
                    Funds     = 0,
                    Timestamp = DateTime.Now
                });

                Account account1 = DtoToModelTranslator.FromDto(accountDto1, DateTime.Today, repositories);
                Account account2 = DtoToModelTranslator.FromDto(accountDto2, DateTime.Today, repositories);

                long     id              = 1;
                DateTime timestamp       = DateTime.Now;
                long?    sourceAccountId = accountDto1.Id.Value;
                long?    destAccountId   = accountDto2.Id.Value;
                Money    transferAmount  = 123.45;
                string   memo            = "memo";

                //Act
                Transaction transaction    = new Transaction(id, timestamp, sourceAccountId, destAccountId, transferAmount, memo, repositories);
                var         propertyValues = transaction.GetPropertyValues().ToList();

                //Assert
                Assert.Equal(6, propertyValues.Count);

                Assert.Equal(id, propertyValues.First(x => x.Property.Equals(Transaction.PROP_ID)).Value);
                Assert.Equal(timestamp, propertyValues.First(x => x.Property.Equals(Transaction.PROP_TIMESTAMP)).Value);
                Assert.Equal(account1, propertyValues.First(x => x.Property.Equals(Transaction.PROP_SOURCE)).Value);
                Assert.Equal(account2, propertyValues.First(x => x.Property.Equals(Transaction.PROP_DEST)).Value);
                Assert.Equal(memo, propertyValues.First(x => x.Property.Equals(Transaction.PROP_MEMO)).Value);
                Assert.Equal(transferAmount, propertyValues.First(x => x.Property.Equals(Transaction.PROP_AMOUNT)).Value);
            }
        }
Beispiel #27
0
        public void TestAccountPropertyValues()
        {
            using (var testDbInfo = SetupUtil.CreateTestDb())
            {
                //Arrange
                Mock <ILog>            mockLog   = new Mock <ILog>();
                AccountRepository      repo      = new AccountRepository(testDbInfo.ConnectionString, mockLog.Object);
                AccountStateRepository stateRepo = new AccountStateRepository(testDbInfo.ConnectionString, mockLog.Object);

                long        id;
                string      name        = "Test Account";
                long        priority    = 7;
                long?       categoryId  = null;
                string      description = "description";
                AccountKind accountKind = AccountKind.Source;

                Money    initialFunds = 123.45;
                DateTime timestamp    = DateTime.Now;

                AccountDto accountDto = new AccountDto()
                {
                    Name        = name,
                    Priority    = priority,
                    Description = description,
                    CategoryId  = categoryId,
                    AccountKind = accountKind,
                };
                repo.Upsert(accountDto);
                id = accountDto.Id.Value;

                AccountStateDto stateDto = new AccountStateDto()
                {
                    AccountId = id,
                    Funds     = initialFunds.InternalValue,
                    IsClosed  = false,
                    Timestamp = timestamp,
                };
                stateRepo.Upsert(stateDto);


                RepositoryBag repositories = SetupUtil.CreateMockRepositoryBag(testDbInfo.ConnectionString, mockLog.Object, repo, stateRepo);

                //Act
                Account account = new Account(accountDto.Id.Value, accountDto.Name, accountDto.CategoryId, accountDto.Priority, accountDto.AccountKind, accountDto.Description, timestamp, repositories);

                var propertyValues = account.GetPropertyValues().ToList();

                AccountState state = DtoToModelTranslator.FromDto(stateDto, repositories);

                //Assert
                Assert.Equal(7, propertyValues.Count);

                Assert.Equal(id, propertyValues.First(x => x.Property.Equals(Account.PROP_ID)).Value);
                Assert.Equal(name, propertyValues.First(x => x.Property.Equals(Account.PROP_NAME)).Value);
                Assert.Equal(description, propertyValues.First(x => x.Property.Equals(Account.PROP_DESCRIPTION)).Value);
                Assert.Equal(accountKind, propertyValues.First(x => x.Property.Equals(Account.PROP_ACCOUNT_KIND)).Value);
                Assert.Equal(null, propertyValues.First(x => x.Property.Equals(Account.PROP_CATEGORY)).Value);
                Assert.Equal(priority, propertyValues.First(x => x.Property.Equals(Account.PROP_PRIORITY)).Value);
                Assert.Equal(state, propertyValues.First(x => x.Property.Equals(Account.PROP_CURRENT_STATE)).Value);
            }
        }