Exemple #1
0
        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));
            }
        }
Exemple #2
0
 public static AccountState FromDto(AccountStateDto dto, RepositoryBag repositories)
 {
     if (dto == null)
     {
         throw new ArgumentNullException(nameof(dto));
     }
     return(new AccountState(dto.Id.Value, dto.AccountId, new Money(dto.Funds, true), dto.Timestamp, dto.IsClosed, repositories));
 }
        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);
            }
        }
        private AccountStateDto BuildAccountStateDto(long accountId)
        {
            AccountStateDto dto = new AccountStateDto()
            {
                AccountId = accountId,
                Funds     = FundsOption.GetValue(0).InternalValue,
                IsClosed  = false,
                Timestamp = DateTime.Now
            };

            return(dto);
        }
        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);
            }
        }
        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);
        }
        public void AccountStateToDto()
        {
            RepositoryBag repositoryBag = new RepositoryBag();
            long          id            = 3;
            long          accountId     = 8;
            Money         funds         = 123.45;
            DateTime      timestamp     = DateTime.Now;
            bool          isClosed      = false;

            AccountState state = new AccountState(id, accountId, funds, timestamp, isClosed, repositoryBag);

            AccountStateDto dto = state.ToDto();

            Assert.Equal(id, dto.Id);
            Assert.Equal(accountId, dto.AccountId);
            Assert.Equal(funds.InternalValue, dto.Funds);
            Assert.Equal(timestamp, dto.Timestamp);
            Assert.Equal(isClosed, dto.IsClosed);
        }
        public void TestAccountStateDtoEquals()
        {
            DateTime        timestamp = DateTime.Now;
            AccountStateDto state1    = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            AccountStateDto state2 = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            Assert.Equal(state1, state1);
            Assert.Equal(state1, state2);
        }
        public void TestAccountStateDtoGetHashCode()
        {
            DateTime        timestamp = DateTime.Now;
            AccountStateDto state1    = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            AccountStateDto state2 = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            Assert.Equal(state1.GetHashCode(), state1.GetHashCode());
            Assert.Equal(state1.GetHashCode(), state2.GetHashCode());
        }
        public void TestAccountStateDtoEquals_Fails()
        {
            DateTime        timestamp = DateTime.Now;
            AccountStateDto state1    = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            AccountStateDto differentId = new AccountStateDto()
            {
                Id        = 2,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            AccountStateDto differentAccountId = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 2,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp
            };

            AccountStateDto differentFunds = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12346,
                IsClosed  = false,
                Timestamp = timestamp
            };

            AccountStateDto differentIsClosed = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = true,
                Timestamp = timestamp
            };

            AccountStateDto differentTimestamp = new AccountStateDto()
            {
                Id        = 1,
                AccountId = 1,
                Funds     = 12345,
                IsClosed  = false,
                Timestamp = timestamp.AddMilliseconds(1)
            };

            Assert.NotEqual(state1, differentId);
            Assert.NotEqual(state1, differentAccountId);
            Assert.NotEqual(state1, differentFunds);
            Assert.NotEqual(state1, differentIsClosed);
            Assert.NotEqual(state1, differentTimestamp);
        }
Exemple #11
0
        public void TestGetLatestStateByAccountId_WithDate()
        {
            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.Today;
                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(-10);
                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(-20);
                AccountStateDto accountState3   = new AccountStateDto()
                {
                    AccountId = account.Id.Value,
                    Funds     = (new Money(800)).InternalValue,
                    IsClosed  = false,
                    Timestamp = state3Timestamp
                };
                isInsertSuccessful &= repo.Upsert(accountState3);

                //Act
                AccountStateDto a = repo.GetLatestByAccountId(account.Id.Value, state3Timestamp);
                AccountStateDto b = repo.GetLatestByAccountId(account.Id.Value, state3Timestamp.AddDays(5));    // half way between 3 and 2
                AccountStateDto c = repo.GetLatestByAccountId(account.Id.Value, state2Timestamp);
                AccountStateDto d = repo.GetLatestByAccountId(account.Id.Value, state2Timestamp.AddDays(5));    //Half way between 2 and 1
                AccountStateDto e = repo.GetLatestByAccountId(account.Id.Value, state1Timestamp);
                AccountStateDto f = repo.GetLatestByAccountId(account.Id.Value, state1Timestamp.AddDays(5));    //5 days in future

                //Assert
                Assert.True(isInsertSuccessful);

                Assert.Equal(800, new Money(a.Funds, true));
                Assert.Equal(800, new Money(b.Funds, true));
                Assert.Equal(500, new Money(c.Funds, true));
                Assert.Equal(500, new Money(d.Funds, true));
                Assert.Equal(100, new Money(e.Funds, true));
                Assert.Equal(100, new Money(f.Funds, true));
            }
        }
Exemple #12
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);
            }
        }
Exemple #13
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);
            }
        }