|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Text.Json; |
3 | 4 | using Orb.Models; |
4 | 5 |
|
5 | 6 | namespace Orb.Tests.Models; |
@@ -75,4 +76,152 @@ public void FieldRoundtrip_Works() |
75 | 76 | Assert.Equal(expectedEndDate, model.EndDate); |
76 | 77 | Assert.Equal(expectedStartDate, model.StartDate); |
77 | 78 | } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void SerializationRoundtrip_Works() |
| 82 | + { |
| 83 | + var model = new AdjustmentInterval |
| 84 | + { |
| 85 | + ID = "id", |
| 86 | + Adjustment = new PlanPhaseUsageDiscountAdjustment() |
| 87 | + { |
| 88 | + ID = "id", |
| 89 | + AdjustmentType = PlanPhaseUsageDiscountAdjustmentAdjustmentType.UsageDiscount, |
| 90 | + AppliesToPriceIDs = ["string"], |
| 91 | + Filters = |
| 92 | + [ |
| 93 | + new() |
| 94 | + { |
| 95 | + Field = Filter23Field.PriceID, |
| 96 | + Operator = Filter23Operator.Includes, |
| 97 | + Values = ["string"], |
| 98 | + }, |
| 99 | + ], |
| 100 | + IsInvoiceLevel = true, |
| 101 | + PlanPhaseOrder = 0, |
| 102 | + Reason = "reason", |
| 103 | + ReplacesAdjustmentID = "replaces_adjustment_id", |
| 104 | + UsageDiscount = 0, |
| 105 | + }, |
| 106 | + AppliesToPriceIntervalIDs = ["string"], |
| 107 | + EndDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"), |
| 108 | + StartDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"), |
| 109 | + }; |
| 110 | + |
| 111 | + string json = JsonSerializer.Serialize(model); |
| 112 | + var deserialized = JsonSerializer.Deserialize<AdjustmentInterval>(json); |
| 113 | + |
| 114 | + Assert.Equal(model, deserialized); |
| 115 | + } |
| 116 | + |
| 117 | + [Fact] |
| 118 | + public void FieldRoundtripThroughSerialization_Works() |
| 119 | + { |
| 120 | + var model = new AdjustmentInterval |
| 121 | + { |
| 122 | + ID = "id", |
| 123 | + Adjustment = new PlanPhaseUsageDiscountAdjustment() |
| 124 | + { |
| 125 | + ID = "id", |
| 126 | + AdjustmentType = PlanPhaseUsageDiscountAdjustmentAdjustmentType.UsageDiscount, |
| 127 | + AppliesToPriceIDs = ["string"], |
| 128 | + Filters = |
| 129 | + [ |
| 130 | + new() |
| 131 | + { |
| 132 | + Field = Filter23Field.PriceID, |
| 133 | + Operator = Filter23Operator.Includes, |
| 134 | + Values = ["string"], |
| 135 | + }, |
| 136 | + ], |
| 137 | + IsInvoiceLevel = true, |
| 138 | + PlanPhaseOrder = 0, |
| 139 | + Reason = "reason", |
| 140 | + ReplacesAdjustmentID = "replaces_adjustment_id", |
| 141 | + UsageDiscount = 0, |
| 142 | + }, |
| 143 | + AppliesToPriceIntervalIDs = ["string"], |
| 144 | + EndDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"), |
| 145 | + StartDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"), |
| 146 | + }; |
| 147 | + |
| 148 | + string json = JsonSerializer.Serialize(model); |
| 149 | + var deserialized = JsonSerializer.Deserialize<AdjustmentInterval>(json); |
| 150 | + Assert.NotNull(deserialized); |
| 151 | + |
| 152 | + string expectedID = "id"; |
| 153 | + AdjustmentIntervalAdjustment expectedAdjustment = new PlanPhaseUsageDiscountAdjustment() |
| 154 | + { |
| 155 | + ID = "id", |
| 156 | + AdjustmentType = PlanPhaseUsageDiscountAdjustmentAdjustmentType.UsageDiscount, |
| 157 | + AppliesToPriceIDs = ["string"], |
| 158 | + Filters = |
| 159 | + [ |
| 160 | + new() |
| 161 | + { |
| 162 | + Field = Filter23Field.PriceID, |
| 163 | + Operator = Filter23Operator.Includes, |
| 164 | + Values = ["string"], |
| 165 | + }, |
| 166 | + ], |
| 167 | + IsInvoiceLevel = true, |
| 168 | + PlanPhaseOrder = 0, |
| 169 | + Reason = "reason", |
| 170 | + ReplacesAdjustmentID = "replaces_adjustment_id", |
| 171 | + UsageDiscount = 0, |
| 172 | + }; |
| 173 | + List<string> expectedAppliesToPriceIntervalIDs = ["string"]; |
| 174 | + DateTimeOffset expectedEndDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"); |
| 175 | + DateTimeOffset expectedStartDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"); |
| 176 | + |
| 177 | + Assert.Equal(expectedID, deserialized.ID); |
| 178 | + Assert.Equal(expectedAdjustment, deserialized.Adjustment); |
| 179 | + Assert.Equal( |
| 180 | + expectedAppliesToPriceIntervalIDs.Count, |
| 181 | + deserialized.AppliesToPriceIntervalIDs.Count |
| 182 | + ); |
| 183 | + for (int i = 0; i < expectedAppliesToPriceIntervalIDs.Count; i++) |
| 184 | + { |
| 185 | + Assert.Equal( |
| 186 | + expectedAppliesToPriceIntervalIDs[i], |
| 187 | + deserialized.AppliesToPriceIntervalIDs[i] |
| 188 | + ); |
| 189 | + } |
| 190 | + Assert.Equal(expectedEndDate, deserialized.EndDate); |
| 191 | + Assert.Equal(expectedStartDate, deserialized.StartDate); |
| 192 | + } |
| 193 | + |
| 194 | + [Fact] |
| 195 | + public void Validation_Works() |
| 196 | + { |
| 197 | + var model = new AdjustmentInterval |
| 198 | + { |
| 199 | + ID = "id", |
| 200 | + Adjustment = new PlanPhaseUsageDiscountAdjustment() |
| 201 | + { |
| 202 | + ID = "id", |
| 203 | + AdjustmentType = PlanPhaseUsageDiscountAdjustmentAdjustmentType.UsageDiscount, |
| 204 | + AppliesToPriceIDs = ["string"], |
| 205 | + Filters = |
| 206 | + [ |
| 207 | + new() |
| 208 | + { |
| 209 | + Field = Filter23Field.PriceID, |
| 210 | + Operator = Filter23Operator.Includes, |
| 211 | + Values = ["string"], |
| 212 | + }, |
| 213 | + ], |
| 214 | + IsInvoiceLevel = true, |
| 215 | + PlanPhaseOrder = 0, |
| 216 | + Reason = "reason", |
| 217 | + ReplacesAdjustmentID = "replaces_adjustment_id", |
| 218 | + UsageDiscount = 0, |
| 219 | + }, |
| 220 | + AppliesToPriceIntervalIDs = ["string"], |
| 221 | + EndDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"), |
| 222 | + StartDate = DateTimeOffset.Parse("2019-12-27T18:11:19.117Z"), |
| 223 | + }; |
| 224 | + |
| 225 | + model.Validate(); |
| 226 | + } |
78 | 227 | } |
0 commit comments