コード例 #1
0
        public async Task Handle_ValidId_ShouldActivateReceipt(int amount)
        {
            var receiptId = await CreateReceipt();

            var item = GetReceiptItem(new Random().Next(10, 100000));

            var command = new CreateReceiptItemCommand
            {
                Name      = "dd",
                ItemGroup = item.ItemGroup.Value,
                Price     = item.Price,
                Count     = item.Count,
                ReceiptId = receiptId
            };

            command.UserIds = amount switch
            {
                1 => new List <string> {
                    SecondUser.Id
                },
                2 => new List <string> {
                    SecondUser.Id, User.Id
                },
                _ => command.UserIds
            };

            await SendAsync(command);

            var context = CreateContext();
            //create
            var firstOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id);

            firstOweRecord.Should().NotBeNull();
            firstOweRecord !.Amount.Should().Be(command.Count * command.Price / command.UserIds.Count); //10k

            var deleteCommand = new DeleteReceiptCommand {
                Id = receiptId
            };

            await SendAsync(deleteCommand);

            context = CreateContext();

            //delete
            var deletedEntity = context.Receipts.FirstOrDefault(x => x.Id == receiptId);

            deletedEntity.Should().NotBeNull();
            deletedEntity !.Deleted.Should().BeCloseTo(DateTime.Now, 1000);
            deletedEntity !.DeletedByUserId.Should().Be(User.Id);

            var secondOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id);

            secondOweRecord.Should().NotBeNull();
            secondOweRecord !.Amount.Should().Be(0); //0


            //activate
            var reactivateCommand = new ActivateReceiptCommand {
                ReceiptId = receiptId
            };

            await SendAsync(reactivateCommand);

            context = CreateContext();

            var thirdOweRecord = context.OweRecords.FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id);

            thirdOweRecord.Should().NotBeNull();
            thirdOweRecord !.Amount.Should().Be(command.Count * command.Price / command.UserIds.Count);

            var activatedEntity = context.Receipts.FirstOrDefault(x => x.Id == receiptId);

            activatedEntity.Should().NotBeNull();
            activatedEntity !.Deleted.Should().BeNull();
            activatedEntity !.DeletedByUserId.Should().BeNull();
        }