public async Task <string> CreateAsync(BankAccountCreateServiceModel model) { if (!this.IsEntityStateValid(model) || !this.Context.Users.Any(u => u.Id == model.UserId)) { return(null); } string generatedUniqueId; do { generatedUniqueId = this.uniqueIdHelper.GenerateAccountUniqueId(); } while (this.Context.Accounts.Any(a => a.UniqueId == generatedUniqueId)); if (model.Name == null) { model.Name = generatedUniqueId; } var dbModel = Mapper.Map <BankAccount>(model); dbModel.UniqueId = generatedUniqueId; await this.Context.Accounts.AddAsync(dbModel); await this.Context.SaveChangesAsync(); return(dbModel.Id); }
public async Task CreateAsync_WithValidModel_AndEmptyName_ShouldSetRandomString_And_ShouldReturnNonEmptyString_And_InsertInDatabase() { // Arrange var count = this.dbContext.Accounts.Count(); await this.SeedUserAsync(); var model = new BankAccountCreateServiceModel { UserId = SampleBankAccountUserId }; // Act var result = await this.bankAccountService.CreateAsync(model); // Assert result .Should() .NotBeNullOrEmpty() .And .BeAssignableTo <string>(); this.dbContext .Accounts .Should() .HaveCount(count + 1); }
public async Task CreateAsync_WithValidModel_Should__InsertInDatabase() { // Arrange var count = this.dbContext.Accounts.Count(); await this.SeedUserAsync(); var model = new BankAccountCreateServiceModel { UserId = SampleBankAccountUserId }; // Act await this.bankAccountService.CreateAsync(model); this.dbContext .Accounts .Should() .HaveCount(count + 1); }
public async Task CreateAsync_WithValidModel_ShouldReturnNonEmptyString() { // Arrange await this.SeedUserAsync(); // CreatedOn is not required since it has default value which is set from the class - Datetime.UtcNow var model = new BankAccountCreateServiceModel { Name = SampleBankAccountName, UserId = SampleBankAccountUserId, CreatedOn = DateTime.UtcNow }; // Act var result = await this.bankAccountService.CreateAsync(model); // Assert result .Should() .NotBeNullOrEmpty() .And .BeAssignableTo <string>(); }
public async Task CreateAsync_WithInvalidUserId_ShouldReturnNull_And_NotInsertInDatabase() { // Arrange await this.SeedUserAsync(); var model = new BankAccountCreateServiceModel { Name = SampleBankAccountName, UserId = null }; // Act var result = await this.bankAccountService.CreateAsync(model); // Assert result .Should() .BeNull(); this.dbContext .Accounts .Should() .BeEmpty(); }
public async Task CreateAsync_WithInvalidNameLength_ShouldReturnNull_And_NotInsertInDatabase() { // Arrange await this.SeedUserAsync(); // Name is invalid when it's longer than 35 characters var model = new BankAccountCreateServiceModel { Name = new string('c', 36), UserId = SampleBankAccountUserId }; // Act var result = await this.bankAccountService.CreateAsync(model); // Assert result .Should() .BeNull(); this.dbContext .Accounts .Should() .BeEmpty(); }