コード例 #1
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);
            }
        }
コード例 #2
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);

                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);
            }
        }
コード例 #3
0
        public void map_movie_dto_to_model_when_more_character_per_actor()
        {
            //Arrange
            ITranslator <FilmDTO, Actor> dtotoModelTranslator = new DtoToModelTranslator <FilmDTO, Actor>();
            IEnumerable <FilmDTO>        movies = new List <FilmDTO>()
            {
                new FilmDTO()
                {
                    name  = "Movie1",
                    roles =
                        new List <Role>()
                    {
                        new Role()
                        {
                            name = "roleName1", actor = "actorName1"
                        },
                        new Role()
                        {
                            name = "roleName2", actor = "actorName1"
                        }
                    }
                }
            };

            //Act
            var actual = dtotoModelTranslator.Translate(movies) as List <Actor>;

            //Assert
            Assert.IsNotNull(actual);
        }
コード例 #4
0
        private Account GetAccount()
        {
            AccountDto dto = Repositories.AccountRepository.GetByName(NameOption.GetValue(null));

            if (dto == null)
            {
                return(null);
            }
            return(DtoToModelTranslator.FromDto(dto, DateOption.GetValue(DateTime.Today), Repositories));
        }
コード例 #5
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);
            }
        }
コード例 #6
0
        private List <Account> GetAccounts()
        {
            var dtos = Repositories.AccountRepository.GetAccounts((long?)IdOption.GetValue(null), NameOption.GetValue(null), (long?)CategoryIdOption.GetValue(null), DescriptionOption.GetValue(null), PriorityOption.GetValue(null), (AccountKind?)AccountTypeOption.GetValue(null), FundsOption.GetValue(null), false);

            List <Account> accounts = new List <Account>();

            foreach (var dto in dtos)
            {
                accounts.Add(DtoToModelTranslator.FromDto(dto, DateTime.Today, Repositories));
            }
            return(accounts);
        }
コード例 #7
0
        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);
            }
        }
コード例 #8
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);
            }
        }
コード例 #9
0
        public FilterCriteria GetFilterCriteria()
        {
            FilterCriteria criteria = new FilterCriteria();

            //Id
            if (IdOption.IsDataValid)
            {
                criteria.AddField(Account.PROP_ID.DisplayName, $"= {IdOption.GetValue(EMPTY_OPTION)}");
            }

            //Category
            if (CategoryIdOption.IsDataValid)
            {
                Account category = DtoToModelTranslator.FromDto(Repositories.AccountRepository.GetById(CategoryIdOption.GetValue(-1)), DateTime.Today, Repositories);

                criteria.AddField(Account.PROP_CATEGORY.DisplayName, $"= {category.Name}");
            }

            //Description
            if (DescriptionOption.IsDataValid)
            {
                criteria.AddField(Account.PROP_DESCRIPTION.DisplayName, $"contains \"{DescriptionOption.GetValue(EMPTY_OPTION)}\"");
            }

            //Name
            if (NameOption.IsDataValid)
            {
                criteria.AddField(Account.PROP_NAME.DisplayName, $"contains \"{NameOption.GetValue(EMPTY_OPTION)}\"");
            }

            //Priority
            if (PriorityOption.IsDataValid)
            {
                criteria.AddField(Account.PROP_PRIORITY.DisplayName, $"= {PriorityOption.GetValue(EMPTY_OPTION)}");
            }

            //Funds
            if (FundsOption.IsDataValid)
            {
                criteria.AddField(AccountState.PROP_FUNDS.DisplayName, $"= {FundsOption.GetValue(EMPTY_OPTION)}");
            }

            //Account Type
            if (AccountTypeOption.IsDataValid)
            {
                criteria.AddField(Account.PROP_ACCOUNT_KIND.DisplayName, $"= {AccountTypeOption.GetValue(EMPTY_OPTION)}");
            }

            return(criteria);
        }
コード例 #10
0
        protected override bool TryDoAction(ILog log, IEnumerable <ICommandActionListener> listeners = null)
        {
            CreateCommandResult <Account> result = new CreateCommandResult <Account>(this, false, null);

            if (Repositories.AccountRepository.DoesNameExist(AccountName.GetValue(String.Empty)))
            {
                TransmitResult(result, listeners);
                return(false);
            }
            else if (CategoryNameOption.IsDataValid &&
                     !Repositories.AccountRepository.DoesNameExist(CategoryNameOption.GetValue(String.Empty)))
            {
                //invalid category name
                TransmitResult(result, listeners);
                return(false);
            }

            AccountDto accountDto = BuildAccountDto();

            bool successful = Repositories.AccountRepository.Upsert(accountDto);

            AccountId.SetData(accountDto.Id.Value);

            if (!accountDto.Id.HasValue)
            {
                log?.WriteLine("Error occurred while adding account. Account was not assigned a valid Id.", LogLevel.Error);
                TransmitResult(result, listeners);
                return(false);
            }

            AccountStateDto accountStateDto = BuildAccountStateDto(accountDto.Id.Value);

            successful &= Repositories.AccountStateRepository.Upsert(accountStateDto);

            if (successful)
            {
                log?.WriteLine($"Added account \"{accountDto.Name}\"", LogLevel.Normal);

                result = new CreateCommandResult <Account>(this, successful, DtoToModelTranslator.FromDto(accountDto, DateTime.Today, Repositories));
            }

            TransmitResult(result, listeners);
            return(successful);
        }
コード例 #11
0
        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();
            }
        }
コード例 #12
0
        private bool DeleteAccount(long id, bool isRecursive, ILog log, List <Account> deletedAccounts)
        {
            //Populate list
            AccountDto dto            = Repositories.AccountRepository.GetById(id);
            Account    deletedAccount = DtoToModelTranslator.FromDto(dto, DateTime.Today, Repositories);

            deletedAccounts.Add(deletedAccount);

            //Delete the account
            bool successful = Repositories.AccountStateRepository.CloseAccount(id);

            //Delete child accounts (if recursive)
            if (isRecursive)
            {
                IEnumerable <long> childAccountIds = Repositories.AccountRepository.GetChildAccountIds(id);
                foreach (var child in childAccountIds)
                {
                    successful &= DeleteAccount(id, isRecursive, log, deletedAccounts);
                }
            }

            return(successful);
        }
コード例 #13
0
        public void FromDto_Transaction()
        {
            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
                };

                //Act
                Transaction transaction = DtoToModelTranslator.FromDto(transactionDto, repositories);

                //Assert
                Assert.Equal(timestamp, transaction.Timestamp);
                Assert.Equal(sourceAccountId, transaction.SourceAccountId);
                Assert.Equal(destAccountId, transaction.DestinationAccountId);
                Assert.Equal(transferAmount, transaction.TransferAmount);
                Assert.Equal(memo, transaction.Memo);
            }
        }
コード例 #14
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);
            }
        }
コード例 #15
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);
            }
        }