< Summary

Class:FastMigrations.Tests.EditorMode.FastMigrationConverterTests
Assembly:FastMigrations.Tests.EditorMode
File(s):/github/workspace/FastMigrations.Unity/Assets/FastMigrations/Tests/EditorMode/FastMigrationConverterTests.cs
Covered lines:213
Uncovered lines:0
Coverable lines:213
Total lines:451
Line coverage:100% (213 of 213)
Covered branches:0
Total branches:0
Covered methods:25
Total methods:25
Method coverage:100% (25 of 25)

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
EmptyJson_Deserialize_DoesNotThrow()0%110100%
JsonWithoutVersion_DeserializeV0_DoesNotThrow()0%110100%
JsonV0_DeserializeV0_Pass()0%110100%
JsonV1_DeserializeStruct_Pass()0%110100%
JsonV1_DeserializeV1_NotCallMigrate()0%110100%
JsonWithoutVersion_DeserializeV1_MigratorCalled()0%110100%
JsonWithoutVersion_DeserializePersonWithJsonCtor_CtorCalledMigratorCalled()0%110100%
JsonWithoutVersion_DeserializeWithOtherMigrator_Pass()0%110100%
JsonWithoutVersion_DeserializeV1WithoutMigrationMethod_ThrowMigrationException()0%110100%
JsonWithoutVersion_DeserializeAsV1WithIgnore_Pass()0%110100%
JsonWith2MigratableObject_Deserialize_MigratorCalled()0%110100%
MigratableObjectsInsideMigratableObject_Deserialize_MigratorCalled()0%110100%
MigratableParentWithMigratableChild_Deserialize_MigratorCalled()0%110100%
NestedJson_Deserialize_Pass()0%110100%
JsonWithRefs_DeserializeWith_PreserveReferencesHandlingAll_Pass()0%110100%
JsonWithRefs_DeserializeWith_PreserveReferencesHandlingObjects_Pass()0%110100%
Array_Deserialize_MigratorCalled()0%110100%
List_Deserialize_MigratorCalled()0%110100%
Dictionary_Deserialize_MigratorCalled()0%110100%
JsonWithoutVersion_DeserializeAsVersion2_Pass()0%110100%
JsonV1_DeserializeAsVersion2_Pass()0%110100%
TwoPersonsWithoutVersion_Populate_MigrationsCalled()0%110100%
WriteJson()0%110100%
TearDown()0%110100%
SetUp()0%110100%

File(s)

/github/workspace/FastMigrations.Unity/Assets/FastMigrations/Tests/EditorMode/FastMigrationConverterTests.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using FastMigrations.Runtime;
 3using Newtonsoft.Json;
 4using Newtonsoft.Json.Converters;
 5using NUnit.Framework;
 6
 7namespace FastMigrations.Tests.EditorMode
 8{
 9    public sealed class FastMigrationConverterTests
 10    {
 11        [Test]
 12        public void EmptyJson_Deserialize_DoesNotThrow()
 113        {
 114            var json = @"{}";
 115            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 16
 117            PersonV0WithoutMigrateMethod person = null;
 218            Assert.DoesNotThrow(() => person = JsonConvert.DeserializeObject<PersonV0WithoutMigrateMethod>(json, migrato
 119            Assert.IsNotNull(person);
 120        }
 21
 22        [Test]
 23        public void JsonWithoutVersion_DeserializeV0_DoesNotThrow()
 124        {
 125            var json = @"{""name"":""Alex Kozorezov"",""age"":27 }";
 126            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 27
 228            Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<PersonV0WithoutMigrateMethod>(json, migrator));
 129        }
 30
 31        [Test]
 32        public void JsonV0_DeserializeV0_Pass()
 133        {
 134            var json = @"{""name"":""Alex Kozorezov"",""age"":27, ""JsonVersion"":0 }";
 135            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 236            Assert.DoesNotThrow(() => JsonConvert.DeserializeObject<PersonV0WithoutMigrateMethod>(json, migrator));
 137        }
 38
 39        [Test]
 40        public void JsonV1_DeserializeStruct_Pass()
 141        {
 142            var json = @"{""name"":""Alex Kozorezov"",""age"":27}";
 43
 144            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 45
 146            PersonStructV1 person = default;
 247            Assert.DoesNotThrow(() => person = JsonConvert.DeserializeObject<PersonStructV1>(json, migrator));
 48
 149            Assert.AreEqual("Alex Kozorezov", person.Name);
 150            Assert.AreEqual(27, person.Age);
 151        }
 52
 53        [Test]
 54        public void JsonV1_DeserializeV1_NotCallMigrate()
 155        {
 156            var json = @"{""name"":""Alex Kozorezov"",""age"":27, ""JsonVersion"":1}";
 57
 158            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 159            var person = JsonConvert.DeserializeObject<PersonV1>(json, migrator);
 60
 161            Assert.NotNull(person);
 162            Assert.IsFalse(MethodCallHandler.MethodsCallInfoByType.ContainsKey(typeof(PersonV1)));
 163            Assert.AreEqual(1, migrator.ReadJsonCalledCount);
 164            Assert.AreEqual(0, migrator.WriteJsonCalledCount);
 165        }
 66
 67        [Test]
 68        public void JsonWithoutVersion_DeserializeV1_MigratorCalled()
 169        {
 170            var json = @"{""name"":""Alex Kozorezov"",""age"":27 }";
 71
 172            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 173            var person = JsonConvert.DeserializeObject<PersonV1>(json, migrator);
 74
 175            Assert.NotNull(person);
 176            Assert.AreEqual(1, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount);
 177            Assert.AreEqual(1, migrator.ReadJsonCalledCount);
 178            Assert.AreEqual(0, migrator.WriteJsonCalledCount);
 179        }
 80
 81        [Test]
 82        public void JsonWithoutVersion_DeserializePersonWithJsonCtor_CtorCalledMigratorCalled()
 183        {
 184            var json = @"{""name"":""Alex Kozorezov"",""age"":27}";
 85
 186            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 187            var person = JsonConvert.DeserializeObject<PersonJsonCtor>(json, migrator);
 88
 189            Assert.NotNull(person);
 190            Assert.AreEqual(1, MethodCallHandler.MethodsCallInfoByType[typeof(PersonJsonCtor)].MethodCallCount);
 191            Assert.IsTrue(person.IsJsonCtorCalled);
 192        }
 93
 94        [Test]
 95        public void JsonWithoutVersion_DeserializeWithOtherMigrator_Pass()
 196        {
 197            var json = @"{
 98""person1"":{""name"":""Alex Kozorezov"",""age"":27},
 99""Version"":""1.2.3""
 100}";
 101
 1102            var migrator1 = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1103            var migrator2 = new VersionConverter();
 104
 1105            TwoPersonsTwoDifferentMigratableMock persons = null;
 2106            Assert.DoesNotThrow(() => persons = JsonConvert.DeserializeObject<TwoPersonsTwoDifferentMigratableMock>(json
 107
 1108            Assert.NotNull(persons);
 109
 1110            Assert.NotNull(persons.Person1);
 1111            Assert.AreEqual("Alex Kozorezov", persons.Person1.Name);
 1112            Assert.AreEqual(27, persons.Person1.Age);
 113
 1114            Assert.NotNull(persons.Version);
 1115            Assert.AreEqual(persons.Version.Major, 1);
 1116            Assert.AreEqual(persons.Version.Minor, 2);
 1117            Assert.AreEqual(persons.Version.Build, 3);
 1118        }
 119
 120        [Test]
 121        public void JsonWithoutVersion_DeserializeV1WithoutMigrationMethod_ThrowMigrationException()
 1122        {
 1123            var json = @"{""name"":""Alex Kozorezov"",""age"":27 }";
 124
 1125            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 126
 2127            Assert.Throws<MigrationException>(() => JsonConvert.DeserializeObject<PersonV1WithoutMigrationMethod>(json, 
 1128        }
 129
 130        [Test]
 131        public void JsonWithoutVersion_DeserializeAsV1WithIgnore_Pass()
 1132        {
 1133            var json = @"{""name"":""Alex Kozorezov"",""age"":27 }";
 134
 1135            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.Ignore);
 136
 1137            var person = JsonConvert.DeserializeObject<PersonV1WithoutMigrationMethod>(json, migrator);
 1138            Assert.NotNull(person);
 1139        }
 140
 141        [Test]
 142        public void JsonWith2MigratableObject_Deserialize_MigratorCalled()
 1143        {
 1144            var json = @"
 145{
 146    ""person1"":{""name"":""Alex Kozorezov"",""age"":27},
 147    ""person2"":{""name"":""Mikhail Suvorov"",""age"":31}
 148}";
 1149            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1150            var persons = JsonConvert.DeserializeObject<TwoPersonsV1NotMigratableMock>(json, migrator);
 151
 1152            Assert.NotNull(persons);
 1153            Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount);
 1154            Assert.AreEqual(2, migrator.ReadJsonCalledCount);
 1155        }
 156
 157        [Test]
 158        public void MigratableObjectsInsideMigratableObject_Deserialize_MigratorCalled()
 1159        {
 1160            var json = @"
 161{
 162    ""person1"":{""name"":""Alex Kozorezov"",""age"":27},
 163    ""person2"":{""name"":""Mikhail Suvorov"",""age"":31}
 164}";
 165
 1166            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1167            var persons = JsonConvert.DeserializeObject<TwoPersonsMigratableMock>(json, migrator);
 168
 1169            Assert.NotNull(persons);
 1170            Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount);
 1171            Assert.AreEqual(1, MethodCallHandler.MethodsCallInfoByType[typeof(TwoPersonsMigratableMock)].MethodCallCount
 1172            Assert.AreEqual(3, migrator.ReadJsonCalledCount);
 1173        }
 174
 175        [Test]
 176        public void MigratableParentWithMigratableChild_Deserialize_MigratorCalled()
 1177        {
 1178            var json = @"{}";
 179
 1180            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.Ignore);
 1181            var childV10 = JsonConvert.DeserializeObject<ChildV10Mock>(json, migrator);
 182
 1183            Assert.NotNull(childV10);
 1184            MethodCallHandler.MethodsCallInfo childCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(ChildV10Moc
 1185            MethodCallHandler.MethodsCallInfo parentCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(ParentMock
 186
 1187            Assert.AreEqual(3, childCallInfo.MethodCallCount);
 1188            Assert.AreEqual(1, childCallInfo.VersionsCalled[0]);
 1189            Assert.AreEqual(2, childCallInfo.VersionsCalled[1]);
 1190            Assert.AreEqual(10, childCallInfo.VersionsCalled[2]);
 191
 1192            Assert.AreEqual(2, parentCallInfo.MethodCallCount);
 1193            Assert.AreEqual(1, parentCallInfo.VersionsCalled[0]);
 1194            Assert.AreEqual(2, parentCallInfo.VersionsCalled[1]);
 1195        }
 196
 197        [Test]
 198        public void NestedJson_Deserialize_Pass()
 1199        {
 1200            var json = @"
 201{""name"":""Alex Kozorezov"",
 202    ""person"":{""name"":""Mikhail Suvorov"",
 203        ""person"": {""name"":""I'm fan of CySharp fan""}
 204    }
 205}";
 206
 1207            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1208            var person = JsonConvert.DeserializeObject<PersonV1NestedMock>(json, migrator);
 209
 1210            Assert.NotNull(person);
 1211            Assert.AreEqual("Alex Kozorezov", person.Name);
 212
 1213            MethodCallHandler.MethodsCallInfo methodsCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1N
 1214            Assert.AreEqual(1, methodsCallInfo.MethodCallCount);
 1215            Assert.AreEqual(1, methodsCallInfo.VersionsCalled[0]);
 1216        }
 217
 218        [Test]
 219        public void JsonWithRefs_DeserializeWith_PreserveReferencesHandlingAll_Pass()
 1220        {
 221            // from: https://www.newtonsoft.com/json/help/html/preservereferenceshandlingobject.htm
 1222            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";
 1254            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1255            Directory directory = JsonConvert.DeserializeObject<Directory>(json, new JsonSerializerSettings { PreserveRe
 256
 1257            Assert.NotNull(directory);
 258
 1259            var directoryCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(Directory)];
 1260            var fileCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(File)];
 261
 1262            Assert.AreEqual(1, directoryCallInfo.MethodCallCount);
 1263            Assert.AreEqual(1, fileCallInfo.MethodCallCount);
 1264        }
 265
 266        [Test]
 267        public void JsonWithRefs_DeserializeWith_PreserveReferencesHandlingObjects_Pass()
 1268        {
 269            // from: https://www.newtonsoft.com/json/help/html/preservereferenceshandlingobject.htm
 1270            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";
 1299            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1300            Directory directory = JsonConvert.DeserializeObject<Directory>(json, new JsonSerializerSettings { PreserveRe
 301
 1302            Assert.NotNull(directory);
 303
 1304            var directoryCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(Directory)];
 1305            var fileCallInfo = MethodCallHandler.MethodsCallInfoByType[typeof(File)];
 306
 1307            Assert.AreEqual(1, directoryCallInfo.MethodCallCount);
 1308            Assert.AreEqual(1, fileCallInfo.MethodCallCount);
 1309        }
 310
 311        [Test]
 312        public void Array_Deserialize_MigratorCalled()
 1313        {
 1314            var json = @"
 315{
 316    ""persons"":
 317        [
 318            {""name"":""Alex Kozorezov"",""age"":27},
 319            {""name"":""Mikhail Suvorov"",""age"":31}
 320        ]
 321}";
 322
 1323            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1324            var persons = JsonConvert.DeserializeObject<PersonsDataStructureMock<PersonV1[]>>(json, migrator);
 325
 1326            Assert.NotNull(persons);
 1327            Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount);
 1328        }
 329
 330        [Test]
 331        public void List_Deserialize_MigratorCalled()
 1332        {
 1333            var json = @"
 334{
 335    ""persons"":
 336        [
 337            {""name"":""Alex Kozorezov"",""age"":27},
 338            {""name"":""Mikhail Suvorov"",""age"":31}
 339        ]
 340}";
 341
 1342            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1343            var persons = JsonConvert.DeserializeObject<PersonsDataStructureMock<List<PersonV1>>>(json, migrator);
 344
 1345            Assert.NotNull(persons);
 1346            Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount);
 1347        }
 348
 349        [Test]
 350        public void Dictionary_Deserialize_MigratorCalled()
 1351        {
 1352            var json = @"
 353{
 354    ""persons"":
 355    {
 356        ""person1"":{""name"":""Alex Kozorezov"",""age"":27},
 357        ""person2"":{""name"":""Mikhail Suvorov"",""age"":31}
 358    }
 359}";
 360
 1361            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1362            var persons = JsonConvert.DeserializeObject<PersonsDataStructureMock<Dictionary<string, PersonV1>>>(json, mi
 363
 1364            Assert.NotNull(persons);
 1365            Assert.AreEqual(2, MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)].MethodCallCount);
 1366        }
 367
 368        [Test]
 369        public void JsonWithoutVersion_DeserializeAsVersion2_Pass()
 1370        {
 1371            var json = @"{""name"":""Alex Kozorezov"",""age"":27}";
 372
 1373            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1374            var person = JsonConvert.DeserializeObject<PersonV2>(json, migrator);
 375
 1376            Assert.NotNull(person);
 1377            Assert.AreEqual("Alex Kozorezov", person.FullName);
 1378            Assert.AreEqual("Alex", person.Name);
 1379            Assert.AreEqual("Kozorezov", person.Surname);
 1380            Assert.AreEqual(1997, person.BirthYear);
 381
 1382            MethodCallHandler.MethodsCallInfo callInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV2)];
 1383            Assert.AreEqual(2, callInfo.MethodCallCount);
 1384            Assert.AreEqual(1, callInfo.VersionsCalled[0]);
 1385            Assert.AreEqual(2, callInfo.VersionsCalled[1]);
 1386        }
 387
 388        [Test]
 389        public void JsonV1_DeserializeAsVersion2_Pass()
 1390        {
 1391            var json = @"{""name"":""Alex Kozorezov"",""age"":27, ""JsonVersion"":1 }";
 392
 1393            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1394            var person = JsonConvert.DeserializeObject<PersonV2>(json, migrator);
 395
 1396            Assert.NotNull(person);
 1397            Assert.AreEqual("Alex Kozorezov", person.FullName);
 1398            Assert.AreEqual("Alex", person.Name);
 1399            Assert.AreEqual("Kozorezov", person.Surname);
 1400            Assert.AreEqual(1997, person.BirthYear);
 401
 1402            MethodCallHandler.MethodsCallInfo callInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV2)];
 1403            Assert.AreEqual(1, callInfo.MethodCallCount);
 1404            Assert.AreEqual(2, callInfo.VersionsCalled[0]);
 1405        }
 406
 407        [Test]
 408        public void TwoPersonsWithoutVersion_Populate_MigrationsCalled()
 1409        {
 1410            var json = @"
 411{
 412    ""person1"":{""name"":""Alex Kozorezov"",""age"":27 },
 413    ""person2"":{""name"":""Mikhail Suvorov"",""age"":31 }
 414}";
 415
 1416            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1417            var person = new TwoPersonsV1NotMigratableMock { Person1 = new PersonV1() };
 1418            JsonConvert.PopulateObject(json, person, new JsonSerializerSettings { Converters = { migrator } });
 419
 1420            MethodCallHandler.MethodsCallInfo callInfo = MethodCallHandler.MethodsCallInfoByType[typeof(PersonV1)];
 1421            Assert.AreEqual(2, callInfo.MethodCallCount);
 1422            Assert.AreEqual(1, callInfo.VersionsCalled[0]);
 1423            Assert.AreEqual(1, callInfo.VersionsCalled[1]);
 1424        }
 425
 426        [Test]
 427        public void WriteJson()
 1428        {
 1429            var persomV0 = new PersonV0WithoutMigrateMethod { Name = "Alex Kozorezov", Age = 27 };
 1430            var personV1 = new PersonV1 { Name = "Alex Kozorezov", Age = 27 };
 1431            var personV2 = new PersonV2 { FullName = "Alex Kozorezov", Name = "Alex", Surname = "Kozorezov", BirthYear =
 432
 1433            var migrator = new FastMigrationsConverterMock(MigratorMissingMethodHandling.ThrowException);
 1434            var jsonV0 = JsonConvert.SerializeObject(persomV0, migrator);
 1435            var jsonV1 = JsonConvert.SerializeObject(personV1, migrator);
 1436            var jsonV2 = JsonConvert.SerializeObject(personV2, migrator);
 437
 1438            Assert.IsFalse(jsonV0.Contains($@"""{MigratorConstants.VersionJsonFieldName}"":0"));
 1439            Assert.IsTrue(jsonV1.Contains($@"""{MigratorConstants.VersionJsonFieldName}"":1"));
 1440            Assert.IsTrue(jsonV2.Contains($@"""{MigratorConstants.VersionJsonFieldName}"":2"));
 1441        }
 442
 443        [TearDown]
 444        public void TearDown()
 23445        {
 23446            MethodCallHandler.Clear();
 23447        }
 448
 46449        [SetUp] public void SetUp() { }
 450    }
 451}