public void TestRemoveAllStatesByAccountId_NoStates() { 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); //Act List <AccountStateDto> allStatesBeforeRemove = repo.GetAllByAccountId(account.Id.Value); bool isSuccessful = repo.RemoveAllByAccountId(account.Id.Value); List <AccountStateDto> allStatesAfterRemove = repo.GetAllByAccountId(account.Id.Value); //Assert Assert.True(isInsertSuccessful); Assert.False(isSuccessful); Assert.Empty(allStatesBeforeRemove); Assert.Empty(allStatesAfterRemove); } }
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); } }
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_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_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(); } }
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); } }
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); } }
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 TestRemoveAllStatesByAccountId_InvalidAccount() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountStateRepository repo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); //Act bool isSuccessful = repo.RemoveAllByAccountId(1); //Assert Assert.False(isSuccessful); } }
public void TestOpenAccount_BadAccountId() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountStateRepository repo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); //Act bool successful = repo.ReOpenAccount(1); //Assert Assert.False(successful); } }
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 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 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 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)); } }
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); } }
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)); } }
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); } }
public void TestGetAccounts() { using (var testDbInfo = SetupUtil.CreateTestDb()) { //Arrange Mock <ILog> log = new Mock <ILog>(); AccountRepository repo = new AccountRepository(testDbInfo.ConnectionString, log.Object); AccountStateRepository stateRepo = new AccountStateRepository(testDbInfo.ConnectionString, log.Object); AccountDto parent = new AccountDto() { AccountKind = Enums.AccountKind.Category, Description = String.Empty, Name = "parent", Priority = 5, CategoryId = null }; bool isParentInsertSuccessful = repo.Upsert(parent); AccountDto testAccount = MakeAccount("Test Account"); AccountDto accountWithDescription1 = MakeAccount("AccountWithDescription1", description: "this is a description"); AccountDto accountWithDescription2 = MakeAccount("AccountWithDescription2", description: "descending"); AccountDto accountPriority10 = MakeAccount("AccountWithPriority10", priority: 10); AccountDto accountPriority1 = MakeAccount("AccountWithPriority1", priority: 1); AccountDto childAccount1 = MakeAccount("child1", categoryId: parent.Id); AccountDto childAccount2WithDescription = MakeAccount("child2", categoryId: parent.Id, description: "description"); AccountDto closedAccount = MakeAccount("closed", priority: 100); AccountDto fundedAccount = MakeAccount("funded", priority: 100); AddAccounts(repo, testAccount, accountWithDescription1, accountWithDescription2, accountPriority10, accountPriority1, childAccount1, childAccount2WithDescription, closedAccount, fundedAccount); SetAccountState(stateRepo, parent, 0, false); SetAccountState(stateRepo, testAccount, 0, false); SetAccountState(stateRepo, accountWithDescription1, 0, false); SetAccountState(stateRepo, accountWithDescription2, 0, false); SetAccountState(stateRepo, accountPriority10, 0, false); SetAccountState(stateRepo, accountPriority1, 0, false); SetAccountState(stateRepo, childAccount1, 0, false); SetAccountState(stateRepo, childAccount2WithDescription, 0, false); SetAccountState(stateRepo, closedAccount, 0, true); SetAccountState(stateRepo, fundedAccount, 1000.00, false); //Act int numMatchingTestAccountName = repo.GetAccounts(nameContains: "Test Account").Count(); int numMatchingChildName = repo.GetAccounts(nameContains: "child").Count(); int numMatchingDescription = repo.GetAccounts(descriptionContains: "desc").Count(); int numMatchingCategoryId = repo.GetAccounts(categoryId: parent.Id).Count(); int numInPriorityRange1To5 = repo.GetAccounts(priorityRange: new Range <long>(1, 5)).Count(); int numInPriorityRange6To15 = repo.GetAccounts(priorityRange: new Range <long>(6, 15)).Count(); int numInPriorityRange0To4 = repo.GetAccounts(priorityRange: new Range <long>(0, 4)).Count(); int numExcludingClosed = repo.GetAccounts(includeClosedAccounts: false).Count(); int numIncludingClosed = repo.GetAccounts(includeClosedAccounts: true).Count(); int numFunded = repo.GetAccounts(fundsRange: new Range <Money>(999.00, 1001.00)).Count(); //Assert Assert.Equal(1, numMatchingTestAccountName); Assert.Equal(2, numMatchingChildName); Assert.Equal(3, numMatchingDescription); Assert.Equal(2, numMatchingCategoryId); Assert.Equal(7, numInPriorityRange1To5); Assert.Equal(1, numInPriorityRange6To15); Assert.Equal(1, numInPriorityRange0To4); Assert.Equal(9, numExcludingClosed); Assert.Equal(10, numIncludingClosed); Assert.Equal(1, numFunded); } }