Beispiel #1
0
        public async Task Can_add_to_ToMany_relationship()
        {
            // Arrange
            BankAccount existingAccount = _fakers.BankAccount.Generate();

            existingAccount.Cards = _fakers.DebitCard.Generate(1);

            DebitCard existingDebitCard = _fakers.DebitCard.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.AddRange(existingAccount, existingDebitCard);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new[]
                {
                    new
                    {
                        type = "debitCards",
                        id   = existingDebitCard.StringId
                    }
                }
            };

            string route = $"/bankAccounts/{existingAccount.StringId}/relationships/cards";

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecutePostAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                BankAccount accountInDatabase = await dbContext.BankAccounts.Include(account => account.Cards).FirstWithIdAsync(existingAccount.Id);

                accountInDatabase.Cards.Should().HaveCount(2);
            });
        }
Beispiel #2
0
        public async Task Can_get_primary_resource_by_ID()
        {
            // Arrange
            DebitCard card = _fakers.DebitCard.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.DebitCards.Add(card);
                await dbContext.SaveChangesAsync();
            });

            string route = "/debitCards/" + card.StringId;

            // Act
            (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync <Document>(route);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.OK);

            responseDocument.SingleData.Should().NotBeNull();
            responseDocument.SingleData.Id.Should().Be(card.StringId);
        }
Beispiel #3
0
        public async Task Can_update_resource_with_relationship()
        {
            // Arrange
            BankAccount existingAccount = _fakers.BankAccount.Generate();

            existingAccount.Cards = _fakers.DebitCard.Generate(1);

            DebitCard existingCard = _fakers.DebitCard.Generate();

            string newIban = _fakers.BankAccount.Generate().Iban;

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.AddRange(existingAccount, existingCard);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new
                {
                    type       = "bankAccounts",
                    id         = existingAccount.StringId,
                    attributes = new
                    {
                        iban = newIban
                    },
                    relationships = new
                    {
                        cards = new
                        {
                            data = new[]
                            {
                                new
                                {
                                    type = "debitCards",
                                    id   = existingCard.StringId
                                }
                            }
                        }
                    }
                }
            };

            string route = "/bankAccounts/" + existingAccount.StringId;

            // Act
            (HttpResponseMessage httpResponse, string responseDocument) = await _testContext.ExecutePatchAsync <string>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.NoContent);

            responseDocument.Should().BeEmpty();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                BankAccount accountInDatabase = await dbContext.BankAccounts.Include(account => account.Cards).FirstWithIdAsync(existingAccount.Id);

                accountInDatabase.Iban.Should().Be(newIban);

                accountInDatabase.Cards.Should().HaveCount(1);
                accountInDatabase.Cards[0].Id.Should().Be(existingCard.Id);
                accountInDatabase.Cards[0].StringId.Should().Be(existingCard.StringId);
            });
        }
Beispiel #4
0
        public async Task Can_create_resource_with_relationship()
        {
            // Arrange
            BankAccount existingAccount = _fakers.BankAccount.Generate();
            DebitCard   newCard         = _fakers.DebitCard.Generate();

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                dbContext.BankAccounts.Add(existingAccount);
                await dbContext.SaveChangesAsync();
            });

            var requestBody = new
            {
                data = new
                {
                    type       = "debitCards",
                    attributes = new
                    {
                        ownerName = newCard.OwnerName,
                        pinCode   = newCard.PinCode
                    },
                    relationships = new
                    {
                        account = new
                        {
                            data = new
                            {
                                type = "bankAccounts",
                                id   = existingAccount.StringId
                            }
                        }
                    }
                }
            };

            const string route = "/debitCards";

            // Act
            (HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAsync <Document>(route, requestBody);

            // Assert
            httpResponse.Should().HaveStatusCode(HttpStatusCode.Created);

            responseDocument.SingleData.Attributes["ownerName"].Should().Be(newCard.OwnerName);
            responseDocument.SingleData.Attributes["pinCode"].Should().Be(newCard.PinCode);

            var codec     = new HexadecimalCodec();
            int newCardId = codec.Decode(responseDocument.SingleData.Id);

            await _testContext.RunOnDatabaseAsync(async dbContext =>
            {
                DebitCard cardInDatabase = await dbContext.DebitCards.Include(card => card.Account).FirstWithIdAsync(newCardId);

                cardInDatabase.OwnerName.Should().Be(newCard.OwnerName);
                cardInDatabase.PinCode.Should().Be(newCard.PinCode);

                cardInDatabase.Account.Should().NotBeNull();
                cardInDatabase.Account.Id.Should().Be(existingAccount.Id);
                cardInDatabase.Account.StringId.Should().Be(existingAccount.StringId);
            });
        }