diff --git a/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs b/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs index 4fc71644..bf77f8a3 100644 --- a/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs +++ b/CryptoExchange.Net/Testing/Comparers/SystemTextJsonComparer.cs @@ -8,6 +8,7 @@ using System.Text.Json; using System.Text.Json.Serialization; using CryptoExchange.Net.Converters; using CryptoExchange.Net.Converters.SystemTextJson; +using CryptoExchange.Net.Testing.Exceptions; #pragma warning disable IL2026 #pragma warning disable IL2070 @@ -191,7 +192,7 @@ namespace CryptoExchange.Net.Testing.Comparers if (property is null) // Property not found - throw new Exception($"{method}: Missing property `{prop.Name}` on `{obj.GetType().Name}`"); + throw new MissingPropertyException(method, obj.GetType().Name, prop.Name, prop.Value.ValueKind == JsonValueKind.Null ? "[null]" : prop.Value.ToString()); var getMethod = property.GetGetMethod(); if (getMethod is null) diff --git a/CryptoExchange.Net/Testing/Exceptions/MissingPropertyException.cs b/CryptoExchange.Net/Testing/Exceptions/MissingPropertyException.cs new file mode 100644 index 00000000..7769c92f --- /dev/null +++ b/CryptoExchange.Net/Testing/Exceptions/MissingPropertyException.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Runtime.Serialization; +using System.Text; + +namespace CryptoExchange.Net.Testing.Exceptions +{ + internal class MissingPropertyException : Exception + { + public MissingPropertyException(string method, string objName, string propName, string value) + : base($"{method}: Missing property `{propName}` on `{objName}`, value: {value.Substring(0, Math.Min(50, value.Length))}") + { + } + } +} diff --git a/CryptoExchange.Net/Testing/RestIntegrationTest.cs b/CryptoExchange.Net/Testing/RestIntegrationTest.cs index 5d09c702..4159fa8c 100644 --- a/CryptoExchange.Net/Testing/RestIntegrationTest.cs +++ b/CryptoExchange.Net/Testing/RestIntegrationTest.cs @@ -1,6 +1,7 @@ using CryptoExchange.Net.Interfaces; using CryptoExchange.Net.Objects; using CryptoExchange.Net.Testing.Comparers; +using CryptoExchange.Net.Testing.Exceptions; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -55,6 +56,26 @@ namespace CryptoExchange.Net.Testing return true; } + /// + /// Execute a REST endpoint call and check for any errors or warnings. Also checks for missing fields in the response mapping + /// + /// + /// List for outputting warnings + /// The call expression + /// Whether this is an authenticated request + /// Nested property to use for comparing when checking for missing fields + /// Properties to ignore when checking for missing fields + /// Whether to use the single array item as compare when checking for missing fields + /// + public Task RunAndCheckResult( + List warningExceptionsHolder, + Expression>>> expression, + bool authRequest, + string? compareNestedProperty = null, + List? ignoreProperties = null, + bool? useSingleArrayItem = null) + => RunAndCheckResult(expression, authRequest, true, compareNestedProperty, ignoreProperties, useSingleArrayItem, warningExceptionsHolder); + /// /// Execute a REST endpoint call and check for any errors or warnings. /// @@ -65,13 +86,15 @@ namespace CryptoExchange.Net.Testing /// Nested property to use for comparing when checking for missing fields /// Properties to ignore when checking for missing fields /// Whether to use the single array item as compare when checking for missing fields + /// List for outputting warnings public async Task RunAndCheckResult( Expression>>> expression, bool authRequest, bool checkMissingFields = false, string? compareNestedProperty = null, List? ignoreProperties = null, - bool? useSingleArrayItem = null) + bool? useSingleArrayItem = null, + List? warningExceptionsHolder = null) { if (!ShouldRun()) return; @@ -112,9 +135,14 @@ namespace CryptoExchange.Net.Testing if (originalData == null) throw new Exception($"Original data needs to be enabled in the client options to check for missing fields"); - try { + try + { SystemTextJsonComparer.CompareData(expressionBody.Method.Name, data, originalData, compareNestedProperty, ignoreProperties, useSingleArrayItem ?? false); } + catch (MissingPropertyException mpe) + { + warningExceptionsHolder?.Add(mpe); + } catch (Exception ex) { throw new Exception($"Compare failed: {ex.Message}; original data: {originalData}", ex);