From b5e857497a31b8df9aeb1d13db53bdc5c5fa692b Mon Sep 17 00:00:00 2001 From: Varun Teja <35555010+VarunSaiTeja@users.noreply.github.com> Date: Sun, 19 Jun 2022 13:50:53 +0530 Subject: [PATCH] complex type test cases added --- Sample/Properties/launchSettings.json | 2 +- Shared/Query.cs | 8 +++++++ Shared/Student.cs | 23 +++++++++++++++++++ test/ValidationTests.cs | 16 +++++++++++++ ...tion_Works_On_StudentWithOutScoreCard.snap | 20 ++++++++++++++++ ...idation_Works_On_StudentWithScoreCard.snap | 5 ++++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 Shared/Student.cs create mode 100644 test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithOutScoreCard.snap create mode 100644 test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithScoreCard.snap diff --git a/Sample/Properties/launchSettings.json b/Sample/Properties/launchSettings.json index 5a0ac48..05b051d 100644 --- a/Sample/Properties/launchSettings.json +++ b/Sample/Properties/launchSettings.json @@ -12,7 +12,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "graphql", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Shared/Query.cs b/Shared/Query.cs index 06ffb85..34da49b 100644 --- a/Shared/Query.cs +++ b/Shared/Query.cs @@ -14,5 +14,13 @@ public class Query public string CheckDuplicateEmail([EmailAddress, Required][DuplicateEmailValidtor] string email) => "You are good to go, this email not registred yet."; public string ArgIsInput(MyInput input) => input.Email; + + public string CheckPass(Student student) + { + if (student.ScoreCard.TotalScore > 30) + return $"{student.FirstName} at {student.ScoreCard.School} is passed"; + else + return $"{student.FirstName} at {student.ScoreCard.School} is failed"; + } } } diff --git a/Shared/Student.cs b/Shared/Student.cs new file mode 100644 index 0000000..8b65480 --- /dev/null +++ b/Shared/Student.cs @@ -0,0 +1,23 @@ +using Graph.ArgumentValidator; +using System.ComponentModel.DataAnnotations; + +namespace Shared +{ + [Validatable] + public class Student + { + [Required(AllowEmptyStrings = false)] + public string FirstName { get; set; } + [Required] + public ScoreCardInfo ScoreCard { get; set; } + + public class ScoreCardInfo + { + [Required] + public int TotalScore { get; set; } + + [Required(AllowEmptyStrings = false)] + public string School { get; set; } + } + } +} diff --git a/test/ValidationTests.cs b/test/ValidationTests.cs index 7293a47..dea7e1c 100644 --- a/test/ValidationTests.cs +++ b/test/ValidationTests.cs @@ -74,5 +74,21 @@ public async Task Ensure_Validation_Works_On_NonDuplicateEmail() result.MatchSnapshot(); } + + [Fact] + public async Task Ensure_Validation_Works_On_StudentWithScoreCard() + { + var result = await ExecuteRequest("query{checkPass(student: { firstName: \"Varun\",scoreCard: { school: \"Gayatri\",totalScore: 85} })}"); + + result.MatchSnapshot(); + } + + [Fact] + public async Task Ensure_Validation_Works_On_StudentWithOutScoreCard() + { + var result = await ExecuteRequest("query{checkPass(student: { firstName: \"Varun\"})}"); + + result.MatchSnapshot(); + } } } diff --git a/test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithOutScoreCard.snap b/test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithOutScoreCard.snap new file mode 100644 index 0000000..92e2085 --- /dev/null +++ b/test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithOutScoreCard.snap @@ -0,0 +1,20 @@ +{ + "errors": [ + { + "message": "\u0060scoreCard\u0060 is a required field and cannot be null.", + "locations": [ + { + "line": 1, + "column": 26 + } + ], + "path": [ + "checkPass" + ], + "extensions": { + "field": "scoreCard", + "specifiedBy": "http://spec.graphql.org/October2021/#sec-Input-Object-Required-Fields" + } + } + ] +} diff --git a/test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithScoreCard.snap b/test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithScoreCard.snap new file mode 100644 index 0000000..0d4019e --- /dev/null +++ b/test/__snapshots__/ValidationTests.Ensure_Validation_Works_On_StudentWithScoreCard.snap @@ -0,0 +1,5 @@ +{ + "data": { + "checkPass": "Varun at Gayatri is passed" + } +}