Skip to content

Commit

Permalink
Add Unit test to explicitly validate use of Fragments for common fiel…
Browse files Browse the repository at this point in the history
…ds, etc.
  • Loading branch information
cajuncoding committed Feb 9, 2024
1 parent dc2abc1 commit 4d6eda0
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
120 changes: 120 additions & 0 deletions FlurlGraphQL.Tests/FlurlGraphQLMutationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace FlurlGraphQL.Querying.Tests
{
[TestClass]
public class FlurlGraphQLMutationTests : BaseFlurlGraphQLTest
{

[TestMethod]
public async Task TestMutationWithQueryResultsAsync()
{
var inputPayload = JArray.Parse(@"
[{
""eventId"": 23,
""eventUUID"": ""c5cd1cca-fe4d-490d-a948-985023c6185c"",
""name"": ""RÜFÜS DU SOL"",
""eventType"": ""ONE_OFF"",
""status"": ""APPROVED"",
""eventDate"": ""2018-11-01T07:00:00"",
""announceDate"": null,
""onSaleDate"": null,
""doorTime"": null,
""newElvisVenueId"": 10811,
""internalBudget"": 15000.00000000,
""externalBudget"": 15000.00000000,
""budgetCurrencyCode"": ""USD"",
""budgetExchangeRate"": 1.000000,
""bookerDescription"": null,
""companyMasterId"": 1,
""subledger"": ""H6367996"",
""genreDescription"": ""Dance / Electronic / DJ / Techno / House / Trance"",
""notes"": null,
""headlinerArtists"": [{
""artistMasterId"": 0,
""artistOrdinalSortId"": 1
}
],
""supportingArtists"": [{
""artistMasterId"": 0,
""artistOrdinalSortId"": 1
}
],
""eventContacts"": null,
""shows"": [{
""showId"": 101,
""showName"": ""Show #3"",
""showOrdinalSortId"": 3,
""showDate"": ""2018-11-03T07:00:00"",
""announceDate"": null,
""onSaleDate"": null,
""doorTime"": null
}, {
""showId"": 102,
""showName"": ""Show #2"",
""showOrdinalSortId"": 2,
""showDate"": ""2018-11-02T07:00:00"",
""announceDate"": null,
""onSaleDate"": null,
""doorTime"": null
}, {
""showId"": 103,
""showName"": ""Show #1"",
""showOrdinalSortId"": 1,
""showDate"": ""2018-11-01T07:00:00"",
""announceDate"": null,
""onSaleDate"": null,
""doorTime"": null
}
],
""createdDate"": ""2018-11-02T21:13:34"",
""lastUpdatedDate"": ""2020-06-11T10:00:12""
}]
");

var mutationResult = await "http://localhost:7072/api/v1/graphql?code=Tsk4dixKihdUyRlvlcDu1dHETHYIiCPpLawk%2F27apOsRTr1LbhF7vw%3D%3D"
.WithGraphQLQuery(@"
mutation ($eventInputArray: [EventCreateOrUpdateInput]) {
eventsCreateOrUpdate(input: $eventInputArray) {
eventResults {
eventUUID
eventId
}
errors {
... on Error {
errorCode
message
}
}
}
}
")
.SetGraphQLVariables(new { eventInputArray = inputPayload })
.PostGraphQLQueryAsync()
.ReceiveGraphQLMutationResult<EventsCreateOrUpdateResult>()
.ConfigureAwait(false);


Assert.IsNotNull(mutationResult);
Assert.IsTrue(mutationResult.EventResults.Length > 0);

var jsonText = JsonConvert.SerializeObject(mutationResult, Formatting.Indented);
TestContext.WriteLine(jsonText);
}
}

public class EventsCreateOrUpdateResult
{
public EventResult[] EventResults { get; set; }
}

public class EventResult
{
public Guid? EventUUID { get; set; }
public int? EventId { get; set; }
}
}
60 changes: 60 additions & 0 deletions FlurlGraphQL.Tests/FlurlGraphQLQueryingSimplePostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,66 @@ public async Task TestSimplePostSingleQueryDirectResultsAsync()
TestContext.WriteLine(jsonText);
}

[TestMethod]
public async Task TestSimplePostSingleQueryDirectResultsUsingFragmentsAsync()
{
var results = await GraphQLApiEndpoint
.WithGraphQLQuery(@"
query ($ids: [Int!], $friendsCount: Int!) {
charactersById(ids: $ids) {
...commonFields
appearsIn
height
friends(first: $friendsCount) {
nodes {
...commonFields
}
}
}
}
fragment commonFields on Character {
personalIdentifier
name
}
")
.SetGraphQLVariables(new { ids = new[] { 1000, 2001 }, friendsCount = 2 })
.PostGraphQLQueryAsync()
.ReceiveGraphQLQueryResults<StarWarsCharacter>()
.ConfigureAwait(false);

Assert.IsNotNull(results);
Assert.AreEqual(2, results.Count);

var char1 = results[0];
Assert.IsNotNull(char1);
Assert.AreEqual(1000, char1.PersonalIdentifier);
Assert.AreEqual("Luke Skywalker", char1.Name);
Assert.IsTrue(char1.Height > (decimal)1.5);
Assert.AreEqual(2, char1.Friends.Count);
char1.Friends.ForEach(f =>
{
Assert.IsTrue(!string.IsNullOrWhiteSpace(f.Name));
Assert.IsTrue(f.PersonalIdentifier > 0);
});


var char2 = results[1];
Assert.IsNotNull(char2);
Assert.AreEqual(2001, char2.PersonalIdentifier);
Assert.AreEqual("R2-D2", char2.Name);
Assert.IsTrue(char2.Height > (decimal)1.5);
Assert.AreEqual(2, char2.Friends.Count);
char2.Friends.ForEach(f =>
{
Assert.IsTrue(!string.IsNullOrWhiteSpace(f.Name));
Assert.IsTrue(f.PersonalIdentifier > 0);
});

var jsonText = JsonConvert.SerializeObject(results, Formatting.Indented);
TestContext.WriteLine(jsonText);
}

[TestMethod]
public async Task TestSinglePostQueryRawJsonResponseAsync()
{
Expand Down

0 comments on commit 4d6eda0

Please sign in to comment.