Ejemplo n.º 1
0
        public void ShouldHaveModifiedEvent()
        {
            // Arrange
            Guid   expectedId     = Guid.NewGuid();
            string expectedName   = "ModifiedName";
            string expectedSymbol = "ModifiedSymbol";

            ExpenseType actual = ExpenseType.Create(expectedId, Guid.NewGuid(), "Name",
                                                    "Symbol");

            actual.FlushUncommitedEvents();

            // Act
            actual.Modify(expectedName, expectedSymbol, 1);

            // Assert
            Assert.IsTrue(actual.Events != null);
            Assert.IsTrue(actual.Events.Count == 1);
            var events = actual.FlushUncommitedEvents();

            Assert.IsTrue(events != null);
            Assert.IsTrue(events.Length == 1);
            Assert.IsTrue(events[0] is ExpenseTypeModifiedEvent);
            ExpenseTypeModifiedEvent @event = events[0] as ExpenseTypeModifiedEvent;

            Assert.IsTrue(@event.EntityId == expectedId);
            Assert.IsTrue(@event.Name == expectedName);
            Assert.IsTrue(@event.Symbol == expectedSymbol);
        }
        public async Task HandleAsync(CreateExpenseTypeCommand message, CancellationToken token = default(CancellationToken))
        {
            var type = ExpenseType.Create(Guid.NewGuid(), message.UserId, message.Name, message.Symbol);

            _types.Add(type);
            await _types.SaveChangesAsync(token);
        }
Ejemplo n.º 3
0
        public void ShouldHaveDeletedEvent()
        {
            // Arrange
            Guid expectedId = Guid.NewGuid();

            ExpenseType actual = ExpenseType.Create(expectedId, Guid.NewGuid(), "Name",
                                                    "Symbol");

            actual.FlushUncommitedEvents();

            // Act
            actual.Delete();

            // Assert
            Assert.IsTrue(actual.Events != null);
            Assert.IsTrue(actual.Events.Count == 1);
            var events = actual.FlushUncommitedEvents();

            Assert.IsTrue(events != null);
            Assert.IsTrue(events.Length == 1);
            Assert.IsTrue(events[0] is ExpenseTypeDeletedEvent);
            ExpenseTypeDeletedEvent @event = events[0] as ExpenseTypeDeletedEvent;

            Assert.IsTrue(@event.EntityId == expectedId);
        }
        internal ExpenseType CreateExpenseType(Guid userId, string nameSuffix = "")
        {
            var context     = Factory.Services.GetRequiredService <BillTrackerContext>();
            var expenseType = ExpenseType.Create(userId, $"test-expense-type{nameSuffix}");

            context.ExpenseTypes.Add(expenseType);
            context.SaveChanges();

            return(expenseType);
        }
Ejemplo n.º 5
0
        private async Task InitializeDefaultExpenseTypes(Guid userId)
        {
            var defaultExpenseTypes = await _context.ExpenseTypes.Where(x => x.IsDefault).ToListAsync();

            if (defaultExpenseTypes != null && defaultExpenseTypes.Any())
            {
                _context.ExpenseTypes.AddRange(
                    defaultExpenseTypes.Select(x => ExpenseType.Create(userId, x.Name)));
            }
        }
Ejemplo n.º 6
0
        public void ShouldCreateNewModel()
        {
            // Arrange
            Guid   expectedId     = Guid.NewGuid();
            Guid   expectedUserId = Guid.NewGuid();
            string expectedName   = "Name";
            string expectedSymbol = "Symbol";

            // Act
            ExpenseType actual = ExpenseType.Create(expectedId, expectedUserId, expectedName,
                                                    expectedSymbol);

            // Assert
            Assert.IsTrue(actual != null);
            Assert.IsTrue(actual.Id == expectedId);
            Assert.IsTrue(actual.Name == expectedName);
            Assert.IsTrue(actual.Symbol == expectedSymbol);
            Assert.IsTrue(!string.IsNullOrEmpty(actual.SearchValue));
        }
Ejemplo n.º 7
0
        public async Task ShouldDeleteExisting()
        {
            // Arrange
            ExpenseType entity     = ExpenseType.Create(Guid.NewGuid(), Guid.NewGuid(), "Name", "Symbol");
            var         repository = new Mock <IExpenseTypeRepository>();

            repository.Setup(e =>
                             e.GetByIdAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(entity);
            DeleteExpenseTypeCommand cmd = new DeleteExpenseTypeCommand(entity.Id);

            ExpenseTypeCommandHandler actual = new ExpenseTypeCommandHandler(repository.Object);

            // Act
            await actual.HandleAsync(cmd);

            // Assert
            repository.Verify(e =>
                              e.Delete(It.Is <ExpenseType>(a => a.Equals(entity))),
                              Times.Once
                              );
        }
Ejemplo n.º 8
0
        public async Task ShouldModifyExisting()
        {
            // Arrange
            Guid        expectedId   = Guid.NewGuid();
            string      expectedName = "Modified Name";
            ExpenseType entity       = ExpenseType.Create(expectedId, Guid.NewGuid(), "Name", "Symbol");
            var         repository   = new Mock <IExpenseTypeRepository>();

            repository.Setup(e =>
                             e.GetByIdAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(entity);
            ModifyExpenseTypeCommand cmd =
                new ModifyExpenseTypeCommand(expectedId, expectedName, "Symbol", 1);

            ExpenseTypeCommandHandler actual = new ExpenseTypeCommandHandler(repository.Object);

            // Act
            await actual.HandleAsync(cmd);

            // Assert
            Assert.IsTrue(entity.Id == expectedId);
            Assert.IsTrue(entity.Name == expectedName);
        }
        public async Task <ResultOrError <ExpenseTypeModel> > Handle(CreateExpenseTypeParameters parameters)
        {
            if (!await _context.DoesExist <User>(parameters.UserId))
            {
                return(CommonErrors.UserNotExist);
            }

            var expenseTypeExist = await _context.ExpenseTypes.AnyAsync(
                x => x.Name.ToLower() == parameters.Name.ToLower() &&
                x.UserId == parameters.UserId);

            if (expenseTypeExist)
            {
                return(ExpenseTypeAlreadyExist);
            }

            var type = ExpenseType.Create(parameters.UserId, parameters.Name);
            await _context.ExpenseTypes.AddAsync(type);

            await _context.SaveChangesAsync();

            return(type.ToModel());
        }