Пример #1
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);
            }
        }
Пример #2
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));
            }
        }
        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);
            }
        }
Пример #4
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();
            }
        }
Пример #5
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));
            }
        }