public BankOperationType GetOrAddOperationType(string typeName) { BankOperationType operationType = _bankOperationTypeRepository .GetAll().Where(a => a.Name == typeName || a.AlternativeNames == typeName) .SingleOrDefault(); if (operationType == null) { operationType = new BankOperationType(); operationType.Name = typeName; operationType.AlternativeNames = typeName; _bankOperationTypeRepository.Add(operationType); } return operationType; }
public void GivenEmptyContext_WhenOperationsAddedAndSaved_ThenAllPublicFieldsAreStored() { //Given XmlContext saveContext = GetNewContext(); //When var account = new BankAccount() { Number = "ACC1", Name = "Account1", Description = "DescAccount1" }; var accountsRepository = saveContext.GetRepository<IRepository<BankAccount>>(); accountsRepository.Add(account); var opType = new BankOperationType() { Name = "Type1" }; var opTypeRepository = saveContext.GetRepository<IRepository<BankOperationType>>(); opTypeRepository.Add(opType); var ops = new List<BankOperation>() { InitOperationWithTestData(1, account, opType, null), InitOperationWithTestData(2, account, opType, null), InitOperationWithTestData(3, account, opType, null) }; var opRepository = saveContext.GetRepository<IRepository<BankOperation>>(); foreach (var item in ops) { opRepository.Add(item); } var statement = new BankStatement() { FileName = "StatementFile", LoadTime = new DateTime(), Operations = ops }; var statementRepository = saveContext.GetRepository<IRepository<BankStatement>>(); statementRepository.Add(statement); Assert.IsTrue(saveContext.SaveChanges()); //Then - verify if operations can be loaded and have the same test data _saveMock.Setup(a => a.Load()).Returns(_savedContext); XmlContext loadContext = GetNewContext(); var savedOperationRepository = loadContext.GetRepository<IRepository<BankOperation>>(); Assert.AreEqual(3, savedOperationRepository.GetAll().Count()); foreach (var item in savedOperationRepository.GetAll()) { CheckOperation(item, account, opType); } }
public void GivenEmptyContext_WhenSavedState_ThenXmlAsExpected() { //Given var context = GetNewContext(); //When var account = new BankAccount() { Number = "A", Name = "B", Description = "C" }; var accountsRepository = context.GetRepository<IRepository<BankAccount>>(); accountsRepository.Add(account); var opType = new BankOperationType() { Name = "xx" }; var opTypeRepository = context.GetRepository<IRepository<BankOperationType>>(); opTypeRepository.Add(opType); var ops = new List<BankOperation>() { new BankOperation(){Id=1,Description="A",BankAccount=account, Type = opType}, new BankOperation(){Id=2,Description="C",BankAccount =account, Type = opType} }; var opRepository = context.GetRepository<IRepository<BankOperation>>(); foreach (var item in ops) { opRepository.Add(item); } var statement = new BankStatement() { FileName = "A", LoadTime = new DateTime(), Operations = ops }; var statementRepository = context.GetRepository<IRepository<BankStatement>>(); statementRepository.Add(statement); Assert.IsTrue(context.SaveChanges()); //Then _saveMock.Verify(a => a.Save(It.IsAny<XElement>())); Assert.IsNotNull(_savedContext.ToString()); _saveMock.Setup(a => a.Load()).Returns(new XElement(_savedContext)); var context2 = new XmlContext(_saveMock.Object, GetRepoFactory()); }
private BankOperation InitOperationWithTestData( int id, BankAccount account, BankOperationType operationType, Card card) { var operation = new BankOperation() { Id = 1, BankAccount = account, Type = operationType, Card = card }; var properties = GetPropertiesForAutoInitialization(); for (int propertyNumber = 0; propertyNumber < properties.Length; propertyNumber++) { InitPropertyWithTestData(properties[propertyNumber], operation, propertyNumber + 1, id); } return operation; }
private void CheckOperation(BankOperation operation, BankAccount account, BankOperationType opType) { Assert.AreEqual(account.Id, operation.BankAccount.Id); Assert.AreEqual(opType.Id, operation.Type.Id); var properties = GetPropertiesForAutoInitialization(); for (int propertyNumber = 0; propertyNumber < properties.Length; propertyNumber++) { var property = properties[propertyNumber]; var expectedValue = GetDefaultTestValueForProperty(property.PropertyType, propertyNumber + 1, operation.Id); var currentValue = properties[propertyNumber].GetValue(operation); Assert.AreNotEqual(GetDefaultValue(property.PropertyType), currentValue, string.Format("Property {0} has unexpected default value", property.Name)); Assert.AreEqual(expectedValue, currentValue, string.Format("Property {0} has unexpected value. Expected {1}, Current {2}.", property.Name, expectedValue, currentValue)); } }