|
| 1 | +/* |
| 2 | + * QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals. |
| 3 | + * Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Collections.Generic; |
| 18 | +using Newtonsoft.Json; |
| 19 | +using NUnit.Framework; |
| 20 | +using QuantConnect.Util; |
| 21 | + |
| 22 | +namespace QuantConnect.Tests.Common.Util; |
| 23 | + |
| 24 | +public class DecimalJsonConverterTests |
| 25 | +{ |
| 26 | + private static readonly JsonSerializerSettings settings = new JsonSerializerSettings |
| 27 | + { |
| 28 | + Converters = new List<JsonConverter> |
| 29 | + { |
| 30 | + new DecimalJsonConverter() |
| 31 | + } |
| 32 | + }; |
| 33 | + |
| 34 | + [TestCase("1", 1, true)] |
| 35 | + [TestCase("0", 0, true)] |
| 36 | + [TestCase("-1", -1, true)] |
| 37 | + [TestCase("1.333", 1.333, true)] |
| 38 | + [TestCase("1.333", 1.333, false)] |
| 39 | + [TestCase("9e-8", 0.00000009, true)] |
| 40 | + [TestCase("1.3117285e+06", 1311728.5, true)] |
| 41 | + [TestCase("9e-8", 0.00000009, false)] |
| 42 | + [TestCase("1.3117285e+06", 1311728.5, false)] |
| 43 | + public void DecimalStringConverterTests(string value, decimal expected, bool quote = true) |
| 44 | + { |
| 45 | + var jsonString = TestObject<decimal>.CreateJsonObject(value, quote); |
| 46 | + var obj = JsonConvert.DeserializeObject<TestObject<decimal>>(jsonString, settings); |
| 47 | + |
| 48 | + Assert.AreEqual(expected, obj.Value); |
| 49 | + } |
| 50 | + |
| 51 | + [TestCase("1", 1, true)] |
| 52 | + [TestCase("0", 0, true)] |
| 53 | + [TestCase("-1", -1, true)] |
| 54 | + [TestCase("1.333", 1.333, true)] |
| 55 | + [TestCase("1.333", 1.333, false)] |
| 56 | + [TestCase("9e-8", 0.00000009, true)] |
| 57 | + [TestCase("1.3117285e+06", 1311728.5, true)] |
| 58 | + [TestCase("9e-8", 0.00000009, false)] |
| 59 | + [TestCase("1.3117285e+06", 1311728.5, false)] |
| 60 | + public void NullableDecimalStringConverterTests(string value, decimal expected, bool quote = true) |
| 61 | + { |
| 62 | + var jsonString = TestObject<decimal?>.CreateJsonObject(value, quote); |
| 63 | + var obj = JsonConvert.DeserializeObject<TestObject<decimal?>>(jsonString, settings); |
| 64 | + |
| 65 | + Assert.AreEqual(expected, obj.Value); |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void ThrowsConversionError() |
| 70 | + { |
| 71 | + var jsonString = """ |
| 72 | + {"value": "qwerty"} |
| 73 | + """; |
| 74 | + |
| 75 | + Assert.Throws<FormatException>(() => |
| 76 | + JsonConvert.DeserializeObject<TestObject<decimal>>(jsonString, settings)); |
| 77 | + } |
| 78 | + |
| 79 | + [Test] |
| 80 | + public void NullableDecimalHandlesNull() |
| 81 | + { |
| 82 | + var jsonString = """ |
| 83 | + {"value": null} |
| 84 | + """; |
| 85 | + var obj = JsonConvert.DeserializeObject<TestObject<decimal?>>(jsonString, settings); |
| 86 | + |
| 87 | + Assert.IsNull(obj.Value); |
| 88 | + } |
| 89 | + |
| 90 | + [Test] |
| 91 | + public void NonNullableDecimalThrowsOnNull() |
| 92 | + { |
| 93 | + var jsonString = """ |
| 94 | + {"value": null} |
| 95 | + """; |
| 96 | + |
| 97 | + Assert.Throws<JsonSerializationException>(() => |
| 98 | + JsonConvert.DeserializeObject<TestObject<decimal>>(jsonString, settings)); |
| 99 | + } |
| 100 | + |
| 101 | + [Test] |
| 102 | + public void SerializesDecimalAsString() |
| 103 | + { |
| 104 | + var obj = new TestObject<decimal> { Value = 1.333m }; |
| 105 | + var json = JsonConvert.SerializeObject(obj, settings); |
| 106 | + var jsonOrigin = JsonConvert.SerializeObject(obj); |
| 107 | + |
| 108 | + Assert.AreEqual(jsonOrigin, json); |
| 109 | + } |
| 110 | + |
| 111 | + [Test] |
| 112 | + public void SerializesNullableDecimalAsStringFallbackToOriginBehavior() |
| 113 | + { |
| 114 | + var obj = new TestObject<decimal?> { Value = 1.333m }; |
| 115 | + var json = JsonConvert.SerializeObject(obj, settings); |
| 116 | + var jsonOrigin = JsonConvert.SerializeObject(obj); |
| 117 | + |
| 118 | + Assert.AreEqual(jsonOrigin, json); |
| 119 | + } |
| 120 | + |
| 121 | + [Test] |
| 122 | + public void SerializesNullableDecimalNullAsNull() |
| 123 | + { |
| 124 | + var obj = new TestObject<decimal?> { Value = null }; |
| 125 | + var json = JsonConvert.SerializeObject(obj, settings); |
| 126 | + var jsonOrigin = JsonConvert.SerializeObject(obj); |
| 127 | + |
| 128 | + Assert.AreEqual(jsonOrigin, json); |
| 129 | + } |
| 130 | + |
| 131 | + private class TestObject<T> |
| 132 | + { |
| 133 | + public T Value { get; set; } |
| 134 | + |
| 135 | + public static string CreateJsonObject(string value, bool quote = false) |
| 136 | + { |
| 137 | + if (quote) |
| 138 | + { |
| 139 | + value = $"\"{value}\""; |
| 140 | + } |
| 141 | + |
| 142 | + return $$""" |
| 143 | + {"value": {{value}}} |
| 144 | + """; |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments