| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using FastMigrations.Runtime; |
| | | 3 | | using Newtonsoft.Json; |
| | | 4 | | using Newtonsoft.Json.Converters; |
| | | 5 | | using NUnit.Framework; |
| | | 6 | | |
| | | 7 | | namespace FastMigrations.Tests.EditorMode |
| | | 8 | | { |
| | | 9 | | public sealed class FastMigrationConverterTests |
| | | 10 | | { |
| | | 11 | | [Test] |
| | | 12 | | public void EmptyJson_Deserialize_DoesNotThrow() |
| | 1 | 13 | | { |
| | 1 | 14 | | var json = @"{}"; |
| | 1 | 15 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | | 16 | | |
| | 1 | 17 | | PersonV0WithoutMigrateMethod person = null; |
| | 2 | 18 | | Assert.DoesNotThrow(() => person = JsonConvert.DeserializeObject<PersonV0WithoutMigrateMethod>(json, migrato |
| | 1 | 19 | | Assert.IsNotNull(person); |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | [Test] |
| | | 23 | | public void JsonWithoutVersion_DeserializeV0_DoesNotThrow() |
| | 1 | 24 | | { |
| | 1 | 25 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27 }"; |
| | 1 | 26 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | | 27 | | |
| | 2 | 28 | | Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<PersonV0WithoutMigrateMethod>(json, migrator)); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | [Test] |
| | | 32 | | public void JsonV0_DeserializeV0_Pass() |
| | 1 | 33 | | { |
| | 1 | 34 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27, ""JsonVersion"":0 }"; |
| | 1 | 35 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 2 | 36 | | Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<PersonV0WithoutMigrateMethod>(json, migrator)); |
| | 1 | 37 | | } |
| | | 38 | | |
| | | 39 | | [Test] |
| | | 40 | | public void JsonV1_DeserializeStruct_Pass() |
| | 1 | 41 | | { |
| | 1 | 42 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27}"; |
| | | 43 | | |
| | 1 | 44 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | | 45 | | |
| | 1 | 46 | | PersonStructV1 person = default; |
| | 2 | 47 | | Assert.DoesNotThrow(() => person = JsonConvert.DeserializeObject<PersonStructV1>(json, migrator)); |
| | | 48 | | |
| | 1 | 49 | | Assert.AreEqual("Alex Kozorezov", person.Name); |
| | 1 | 50 | | Assert.AreEqual(27, person.Age); |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | [Test] |
| | | 54 | | public void JsonV1_DeserializeV1_NotCallMigrate() |
| | 1 | 55 | | { |
| | 1 | 56 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27, ""JsonVersion"":1}"; |
| | | 57 | | |
| | 1 | 58 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 59 | | var person = JsonConvert.DeserializeObject<PersonV1>(json, migrator); |
| | | 60 | | |
| | 1 | 61 | | Assert.NotNull(person); |
| | 1 | 62 | | Assert.IsFalse(MethodCallHandler.MethodsCallInfoByType.ContainsKey(typeof(PersonV1))); |
| | 1 | 63 | | Assert.AreEqual(1, migrator.ReadJsonCalledCount); |
| | 1 | 64 | | Assert.AreEqual(0, migrator.WriteJsonCalledCount); |
| | 1 | 65 | | } |
| | | 66 | | |
| | | 67 | | [Test] |
| | | 68 | | public void JsonWithoutVersion_DeserializeV1_MigratorCalled() |
| | 1 | 69 | | { |
| | 1 | 70 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27 }"; |
| | | 71 | | |
| | 1 | 72 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 73 | | var person = JsonConvert.DeserializeObject<PersonV1>(json, migrator); |
| | | 74 | | |
| | 1 | 75 | | Assert.NotNull(person); |
| | 1 | 76 | | Assert.AreEqual(1, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount); |
| | 1 | 77 | | Assert.AreEqual(1, migrator.ReadJsonCalledCount); |
| | 1 | 78 | | Assert.AreEqual(0, migrator.WriteJsonCalledCount); |
| | 1 | 79 | | } |
| | | 80 | | |
| | | 81 | | [Test] |
| | | 82 | | public void JsonWithoutVersion_DeserializePersonWithJsonCtor_CtorCalledMigratorCalled() |
| | 1 | 83 | | { |
| | 1 | 84 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27}"; |
| | | 85 | | |
| | 1 | 86 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 87 | | var person = JsonConvert.DeserializeObject<PersonJsonCtor>(json, migrator); |
| | | 88 | | |
| | 1 | 89 | | Assert.NotNull(person); |
| | 1 | 90 | | Assert.AreEqual(1, MethodCallHandler.MethodsCallInfoByType[typeof(PersonJsonCtor)].MethodCallCount); |
| | 1 | 91 | | Assert.IsTrue(person.IsJsonCtorCalled); |
| | 1 | 92 | | } |
| | | 93 | | |
| | | 94 | | [Test] |
| | | 95 | | public void JsonWithoutVersion_DeserializeWithOtherMigrator_Pass() |
| | 1 | 96 | | { |
| | 1 | 97 | | var json = @"{ |
| | | 98 | | ""person1"":{""name"":""Alex Kozorezov"",""age"":27}, |
| | | 99 | | ""Version"":""1.2.3"" |
| | | 100 | | }"; |
| | | 101 | | |
| | 1 | 102 | | var migrator1 = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 103 | | var migrator2 = new VersionConverter(); |
| | | 104 | | |
| | 1 | 105 | | TwoPersonsTwoDifferentMigratableMock persons = null; |
| | 2 | 106 | | Assert.DoesNotThrow(() => persons = JsonConvert.DeserializeObject<TwoPersonsTwoDifferentMigratableMock>(json |
| | | 107 | | |
| | 1 | 108 | | Assert.NotNull(persons); |
| | | 109 | | |
| | 1 | 110 | | Assert.NotNull(persons.Person1); |
| | 1 | 111 | | Assert.AreEqual("Alex Kozorezov", persons.Person1.Name); |
| | 1 | 112 | | Assert.AreEqual(27, persons.Person1.Age); |
| | | 113 | | |
| | 1 | 114 | | Assert.NotNull(persons.Version); |
| | 1 | 115 | | Assert.AreEqual(persons.Version.Major, 1); |
| | 1 | 116 | | Assert.AreEqual(persons.Version.Minor, 2); |
| | 1 | 117 | | Assert.AreEqual(persons.Version.Build, 3); |
| | 1 | 118 | | } |
| | | 119 | | |
| | | 120 | | [Test] |
| | | 121 | | public void JsonWithoutVersion_DeserializeV1WithoutMigrationMethod_ThrowMigrationException() |
| | 1 | 122 | | { |
| | 1 | 123 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27 }"; |
| | | 124 | | |
| | 1 | 125 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | | 126 | | |
| | 2 | 127 | | Assert.Throws<MigrationException>(() => JsonConvert.DeserializeObject<PersonV1WithoutMigrationMethod>(json, |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | [Test] |
| | | 131 | | public void JsonWithoutVersion_DeserializeAsV1WithIgnore_Pass() |
| | 1 | 132 | | { |
| | 1 | 133 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27 }"; |
| | | 134 | | |
| | 1 | 135 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.Ignore); |
| | | 136 | | |
| | 1 | 137 | | var person = JsonConvert.DeserializeObject<PersonV1WithoutMigrationMethod>(json, migrator); |
| | 1 | 138 | | Assert.NotNull(person); |
| | 1 | 139 | | } |
| | | 140 | | |
| | | 141 | | [Test] |
| | | 142 | | public void JsonWith2MigratableObject_Deserialize_MigratorCalled() |
| | 1 | 143 | | { |
| | 1 | 144 | | var json = @" |
| | | 145 | | { |
| | | 146 | | ""person1"":{""name"":""Alex Kozorezov"",""age"":27}, |
| | | 147 | | ""person2"":{""name"":""Mikhail Suvorov"",""age"":31} |
| | | 148 | | }"; |
| | 1 | 149 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 150 | | var persons = JsonConvert.DeserializeObject<TwoPersonsV1NotMigratableMock>(json, migrator); |
| | | 151 | | |
| | 1 | 152 | | Assert.NotNull(persons); |
| | 1 | 153 | | Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount); |
| | 1 | 154 | | Assert.AreEqual(2, migrator.ReadJsonCalledCount); |
| | 1 | 155 | | } |
| | | 156 | | |
| | | 157 | | [Test] |
| | | 158 | | public void MigratableObjectsInsideMigratableObject_Deserialize_MigratorCalled() |
| | 1 | 159 | | { |
| | 1 | 160 | | var json = @" |
| | | 161 | | { |
| | | 162 | | ""person1"":{""name"":""Alex Kozorezov"",""age"":27}, |
| | | 163 | | ""person2"":{""name"":""Mikhail Suvorov"",""age"":31} |
| | | 164 | | }"; |
| | | 165 | | |
| | 1 | 166 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 167 | | var persons = JsonConvert.DeserializeObject<TwoPersonsMigratableMock>(json, migrator); |
| | | 168 | | |
| | 1 | 169 | | Assert.NotNull(persons); |
| | 1 | 170 | | Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount); |
| | 1 | 171 | | Assert.AreEqual(1, MethodCallHandler.MethodsCallInfoByType[typeof(TwoPersonsMigratableMock)].MethodCallCount |
| | 1 | 172 | | Assert.AreEqual(3, migrator.ReadJsonCalledCount); |
| | 1 | 173 | | } |
| | | 174 | | |
| | | 175 | | [Test] |
| | | 176 | | public void MigratableParentWithMigratableChild_Deserialize_MigratorCalled() |
| | 1 | 177 | | { |
| | 1 | 178 | | var json = @"{}"; |
| | | 179 | | |
| | 1 | 180 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.Ignore); |
| | 1 | 181 | | var childV10 = JsonConvert.DeserializeObject<ChildV10Mock>(json, migrator); |
| | | 182 | | |
| | 1 | 183 | | Assert.NotNull(childV10); |
| | 1 | 184 | | MethodCallHandler.MethodsCallInfo childCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(ChildV10Moc |
| | 1 | 185 | | MethodCallHandler.MethodsCallInfo parentCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(ParentMock |
| | | 186 | | |
| | 1 | 187 | | Assert.AreEqual(3, childCallInfo.MethodCallCount); |
| | 1 | 188 | | Assert.AreEqual(1, childCallInfo.VersionsCalled[0]); |
| | 1 | 189 | | Assert.AreEqual(2, childCallInfo.VersionsCalled[1]); |
| | 1 | 190 | | Assert.AreEqual(10, childCallInfo.VersionsCalled[2]); |
| | | 191 | | |
| | 1 | 192 | | Assert.AreEqual(2, parentCallInfo.MethodCallCount); |
| | 1 | 193 | | Assert.AreEqual(1, parentCallInfo.VersionsCalled[0]); |
| | 1 | 194 | | Assert.AreEqual(2, parentCallInfo.VersionsCalled[1]); |
| | 1 | 195 | | } |
| | | 196 | | |
| | | 197 | | [Test] |
| | | 198 | | public void NestedJson_Deserialize_Pass() |
| | 1 | 199 | | { |
| | 1 | 200 | | var json = @" |
| | | 201 | | {""name"":""Alex Kozorezov"", |
| | | 202 | | ""person"":{""name"":""Mikhail Suvorov"", |
| | | 203 | | ""person"": {""name"":""I'm fan of CySharp fan""} |
| | | 204 | | } |
| | | 205 | | }"; |
| | | 206 | | |
| | 1 | 207 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 208 | | var person = JsonConvert.DeserializeObject<PersonV1NestedMock>(json, migrator); |
| | | 209 | | |
| | 1 | 210 | | Assert.NotNull(person); |
| | 1 | 211 | | Assert.AreEqual("Alex Kozorezov", person.Name); |
| | | 212 | | |
| | 1 | 213 | | MethodCallHandler.MethodsCallInfo methodsCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1N |
| | 1 | 214 | | Assert.AreEqual(1, methodsCallInfo.MethodCallCount); |
| | 1 | 215 | | Assert.AreEqual(1, methodsCallInfo.VersionsCalled[0]); |
| | 1 | 216 | | } |
| | | 217 | | |
| | | 218 | | [Test] |
| | | 219 | | public void JsonWithRefs_DeserializeWith_PreserveReferencesHandlingAll_Pass() |
| | 1 | 220 | | { |
| | | 221 | | // from: https://www.newtonsoft.com/json/help/html/preservereferenceshandlingobject.htm |
| | 1 | 222 | | var json = @" |
| | | 223 | | { |
| | | 224 | | ""$id"": ""1"", |
| | | 225 | | ""Name"": ""My Documents"", |
| | | 226 | | ""Parent"": { |
| | | 227 | | ""$id"": ""2"", |
| | | 228 | | ""Name"": ""Root"", |
| | | 229 | | ""Parent"": null, |
| | | 230 | | ""Files"": |
| | | 231 | | [ |
| | | 232 | | { |
| | | 233 | | ""$ref"": ""3"" |
| | | 234 | | } |
| | | 235 | | ] |
| | | 236 | | }, |
| | | 237 | | ""Files"": { |
| | | 238 | | ""$id"": ""3"", |
| | | 239 | | ""$values"": [ |
| | | 240 | | { |
| | | 241 | | ""$id"": ""4"", |
| | | 242 | | ""Name"": ""ImportantLegalDocument.docx"", |
| | | 243 | | ""Parent"": { |
| | | 244 | | ""$ref"": ""1"" |
| | | 245 | | } |
| | | 246 | | }, |
| | | 247 | | { |
| | | 248 | | ""$ref"": ""4"" |
| | | 249 | | } |
| | | 250 | | ] |
| | | 251 | | } |
| | | 252 | | } |
| | | 253 | | "; |
| | 1 | 254 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 255 | | Directory directory = JsonConvert.DeserializeObject<Directory>(json, new JsonSerializerSettings { PreserveRe |
| | | 256 | | |
| | 1 | 257 | | Assert.NotNull(directory); |
| | | 258 | | |
| | 1 | 259 | | var directoryCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(Directory)]; |
| | 1 | 260 | | var fileCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(File)]; |
| | | 261 | | |
| | 1 | 262 | | Assert.AreEqual(1, directoryCallInfo.MethodCallCount); |
| | 1 | 263 | | Assert.AreEqual(1, fileCallInfo.MethodCallCount); |
| | 1 | 264 | | } |
| | | 265 | | |
| | | 266 | | [Test] |
| | | 267 | | public void JsonWithRefs_DeserializeWith_PreserveReferencesHandlingObjects_Pass() |
| | 1 | 268 | | { |
| | | 269 | | // from: https://www.newtonsoft.com/json/help/html/preservereferenceshandlingobject.htm |
| | 1 | 270 | | var json = @" |
| | | 271 | | { |
| | | 272 | | ""$id"": ""1"", |
| | | 273 | | ""Name"": ""My Documents"", |
| | | 274 | | ""Parent"": { |
| | | 275 | | ""$id"": ""2"", |
| | | 276 | | ""Name"": ""Root"", |
| | | 277 | | ""Parent"": null, |
| | | 278 | | ""Files"": |
| | | 279 | | [ |
| | | 280 | | { |
| | | 281 | | ""$ref"": ""3"" |
| | | 282 | | } |
| | | 283 | | ] |
| | | 284 | | }, |
| | | 285 | | ""Files"": [ |
| | | 286 | | { |
| | | 287 | | ""$id"": ""3"", |
| | | 288 | | ""Name"": ""ImportantLegalDocument.docx"", |
| | | 289 | | ""Parent"": { |
| | | 290 | | ""$ref"": ""1"" |
| | | 291 | | } |
| | | 292 | | }, |
| | | 293 | | { |
| | | 294 | | ""$ref"": ""3"" |
| | | 295 | | } |
| | | 296 | | ] |
| | | 297 | | } |
| | | 298 | | "; |
| | 1 | 299 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 300 | | Directory directory = JsonConvert.DeserializeObject<Directory>(json, new JsonSerializerSettings { PreserveRe |
| | | 301 | | |
| | 1 | 302 | | Assert.NotNull(directory); |
| | | 303 | | |
| | 1 | 304 | | var directoryCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(Directory)]; |
| | 1 | 305 | | var fileCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(File)]; |
| | | 306 | | |
| | 1 | 307 | | Assert.AreEqual(1, directoryCallInfo.MethodCallCount); |
| | 1 | 308 | | Assert.AreEqual(1, fileCallInfo.MethodCallCount); |
| | 1 | 309 | | } |
| | | 310 | | |
| | | 311 | | [Test] |
| | | 312 | | public void Array_Deserialize_MigratorCalled() |
| | 1 | 313 | | { |
| | 1 | 314 | | var json = @" |
| | | 315 | | { |
| | | 316 | | ""persons"": |
| | | 317 | | [ |
| | | 318 | | {""name"":""Alex Kozorezov"",""age"":27}, |
| | | 319 | | {""name"":""Mikhail Suvorov"",""age"":31} |
| | | 320 | | ] |
| | | 321 | | }"; |
| | | 322 | | |
| | 1 | 323 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 324 | | var persons = JsonConvert.DeserializeObject<PersonsDataStructureMock<PersonV1[]>>(json, migrator); |
| | | 325 | | |
| | 1 | 326 | | Assert.NotNull(persons); |
| | 1 | 327 | | Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount); |
| | 1 | 328 | | } |
| | | 329 | | |
| | | 330 | | [Test] |
| | | 331 | | public void List_Deserialize_MigratorCalled() |
| | 1 | 332 | | { |
| | 1 | 333 | | var json = @" |
| | | 334 | | { |
| | | 335 | | ""persons"": |
| | | 336 | | [ |
| | | 337 | | {""name"":""Alex Kozorezov"",""age"":27}, |
| | | 338 | | {""name"":""Mikhail Suvorov"",""age"":31} |
| | | 339 | | ] |
| | | 340 | | }"; |
| | | 341 | | |
| | 1 | 342 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 343 | | var persons = JsonConvert.DeserializeObject<PersonsDataStructureMock<List<PersonV1>>>(json, migrator); |
| | | 344 | | |
| | 1 | 345 | | Assert.NotNull(persons); |
| | 1 | 346 | | Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount); |
| | 1 | 347 | | } |
| | | 348 | | |
| | | 349 | | [Test] |
| | | 350 | | public void Dictionary_Deserialize_MigratorCalled() |
| | 1 | 351 | | { |
| | 1 | 352 | | var json = @" |
| | | 353 | | { |
| | | 354 | | ""persons"": |
| | | 355 | | { |
| | | 356 | | ""person1"":{""name"":""Alex Kozorezov"",""age"":27}, |
| | | 357 | | ""person2"":{""name"":""Mikhail Suvorov"",""age"":31} |
| | | 358 | | } |
| | | 359 | | }"; |
| | | 360 | | |
| | 1 | 361 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 362 | | var persons = JsonConvert.DeserializeObject<PersonsDataStructureMock<Dictionary<string, PersonV1>>>(json, mi |
| | | 363 | | |
| | 1 | 364 | | Assert.NotNull(persons); |
| | 1 | 365 | | Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount); |
| | 1 | 366 | | } |
| | | 367 | | |
| | | 368 | | [Test] |
| | | 369 | | public void JsonWithoutVersion_DeserializeAsVersion2_Pass() |
| | 1 | 370 | | { |
| | 1 | 371 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27}"; |
| | | 372 | | |
| | 1 | 373 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 374 | | var person = JsonConvert.DeserializeObject<PersonV2>(json, migrator); |
| | | 375 | | |
| | 1 | 376 | | Assert.NotNull(person); |
| | 1 | 377 | | Assert.AreEqual("Alex Kozorezov", person.FullName); |
| | 1 | 378 | | Assert.AreEqual("Alex", person.Name); |
| | 1 | 379 | | Assert.AreEqual("Kozorezov", person.Surname); |
| | 1 | 380 | | Assert.AreEqual(1997, person.BirthYear); |
| | | 381 | | |
| | 1 | 382 | | MethodCallHandler.MethodsCallInfo callInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV2)]; |
| | 1 | 383 | | Assert.AreEqual(2, callInfo.MethodCallCount); |
| | 1 | 384 | | Assert.AreEqual(1, callInfo.VersionsCalled[0]); |
| | 1 | 385 | | Assert.AreEqual(2, callInfo.VersionsCalled[1]); |
| | 1 | 386 | | } |
| | | 387 | | |
| | | 388 | | [Test] |
| | | 389 | | public void JsonV1_DeserializeAsVersion2_Pass() |
| | 1 | 390 | | { |
| | 1 | 391 | | var json = @"{""name"":""Alex Kozorezov"",""age"":27, ""JsonVersion"":1 }"; |
| | | 392 | | |
| | 1 | 393 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 394 | | var person = JsonConvert.DeserializeObject<PersonV2>(json, migrator); |
| | | 395 | | |
| | 1 | 396 | | Assert.NotNull(person); |
| | 1 | 397 | | Assert.AreEqual("Alex Kozorezov", person.FullName); |
| | 1 | 398 | | Assert.AreEqual("Alex", person.Name); |
| | 1 | 399 | | Assert.AreEqual("Kozorezov", person.Surname); |
| | 1 | 400 | | Assert.AreEqual(1997, person.BirthYear); |
| | | 401 | | |
| | 1 | 402 | | MethodCallHandler.MethodsCallInfo callInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV2)]; |
| | 1 | 403 | | Assert.AreEqual(1, callInfo.MethodCallCount); |
| | 1 | 404 | | Assert.AreEqual(2, callInfo.VersionsCalled[0]); |
| | 1 | 405 | | } |
| | | 406 | | |
| | | 407 | | [Test] |
| | | 408 | | public void TwoPersonsWithoutVersion_Populate_MigrationsCalled() |
| | 1 | 409 | | { |
| | 1 | 410 | | var json = @" |
| | | 411 | | { |
| | | 412 | | ""person1"":{""name"":""Alex Kozorezov"",""age"":27 }, |
| | | 413 | | ""person2"":{""name"":""Mikhail Suvorov"",""age"":31 } |
| | | 414 | | }"; |
| | | 415 | | |
| | 1 | 416 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 417 | | var person = new TwoPersonsV1NotMigratableMock { Person1 = new PersonV1() }; |
| | 1 | 418 | | JsonConvert.PopulateObject(json, person, new JsonSerializerSettings { Converters = { migrator } }); |
| | | 419 | | |
| | 1 | 420 | | MethodCallHandler.MethodsCallInfo callInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)]; |
| | 1 | 421 | | Assert.AreEqual(2, callInfo.MethodCallCount); |
| | 1 | 422 | | Assert.AreEqual(1, callInfo.VersionsCalled[0]); |
| | 1 | 423 | | Assert.AreEqual(1, callInfo.VersionsCalled[1]); |
| | 1 | 424 | | } |
| | | 425 | | |
| | | 426 | | [Test] |
| | | 427 | | public void WriteJson() |
| | 1 | 428 | | { |
| | 1 | 429 | | var persomV0 = new PersonV0WithoutMigrateMethod { Name = "Alex Kozorezov", Age = 27 }; |
| | 1 | 430 | | var personV1 = new PersonV1 { Name = "Alex Kozorezov", Age = 27 }; |
| | 1 | 431 | | var personV2 = new PersonV2 { FullName = "Alex Kozorezov", Name = "Alex", Surname = "Kozorezov", BirthYear = |
| | | 432 | | |
| | 1 | 433 | | var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException); |
| | 1 | 434 | | var jsonV0 = JsonConvert.SerializeObject(persomV0, migrator); |
| | 1 | 435 | | var jsonV1 = JsonConvert.SerializeObject(personV1, migrator); |
| | 1 | 436 | | var jsonV2 = JsonConvert.SerializeObject(personV2, migrator); |
| | | 437 | | |
| | 1 | 438 | | Assert.IsFalse(jsonV0.Contains($@"""{MigratorConstants.VersionJsonFieldName}"":0")); |
| | 1 | 439 | | Assert.IsTrue(jsonV1.Contains($@"""{MigratorConstants.VersionJsonFieldName}"":1")); |
| | 1 | 440 | | Assert.IsTrue(jsonV2.Contains($@"""{MigratorConstants.VersionJsonFieldName}"":2")); |
| | 1 | 441 | | } |
| | | 442 | | |
| | | 443 | | [TearDown] |
| | | 444 | | public void TearDown() |
| | 23 | 445 | | { |
| | 23 | 446 | | MethodCallHandler.Clear(); |
| | 23 | 447 | | } |
| | | 448 | | |
| | 46 | 449 | | [SetUp] public void SetUp() { } |
| | | 450 | | } |
| | | 451 | | } |