| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using FastMigrations.Runtime; |
| | | 4 | | using Newtonsoft.Json; |
| | | 5 | | using Newtonsoft.Json.Linq; |
| | | 6 | | |
| | | 7 | | namespace FastMigrations.Tests.EditorMode |
| | | 8 | | { |
| | | 9 | | public static class MethodCallHandler |
| | | 10 | | { |
| | | 11 | | public static IReadOnlyDictionary<Type, MethodsCallInfo> MethodsCallInfoByType => m_methodsCallInfoByType; |
| | | 12 | | private static readonly Dictionary<Type, MethodsCallInfo> m_methodsCallInfoByType = new Dictionary<Type, Methods |
| | | 13 | | |
| | | 14 | | public static void RegisterMethodCall(Type type, string methodName) |
| | | 15 | | { |
| | | 16 | | var methodVersion = int.Parse(methodName.Split('_')[1]); |
| | | 17 | | |
| | | 18 | | if (m_methodsCallInfoByType.ContainsKey(type)) |
| | | 19 | | m_methodsCallInfoByType[type].VersionsCalled.Add(methodVersion); |
| | | 20 | | else |
| | | 21 | | m_methodsCallInfoByType.Add(type, new MethodsCallInfo(methodVersion)); |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public static void Clear() |
| | | 25 | | { |
| | | 26 | | m_methodsCallInfoByType.Clear(); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public class MethodsCallInfo |
| | | 30 | | { |
| | | 31 | | public readonly List<int> VersionsCalled; |
| | | 32 | | public int MethodCallCount => VersionsCalled.Count; |
| | | 33 | | |
| | | 34 | | public MethodsCallInfo(int methodVersions) |
| | | 35 | | { |
| | | 36 | | VersionsCalled = new List<int> { methodVersions }; |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | [Migratable(0)] |
| | | 42 | | public sealed class PersonV0WithoutMigrateMethod |
| | | 43 | | { |
| | | 44 | | [JsonProperty("name")] public string Name; |
| | | 45 | | [JsonProperty("age")] public int Age; |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | [Migratable(1)] |
| | | 49 | | public sealed class PersonV1 |
| | | 50 | | { |
| | | 51 | | [JsonProperty("name")] public string Name; |
| | | 52 | | [JsonProperty("age")] public int Age; |
| | | 53 | | |
| | | 54 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 55 | | { |
| | | 56 | | MethodCallHandler.RegisterMethodCall(typeof(PersonV1), nameof(Migrate_1)); |
| | | 57 | | return jsonObj; |
| | | 58 | | } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [Migratable(1)] |
| | | 62 | | public struct PersonStructV1 |
| | | 63 | | { |
| | | 64 | | [JsonProperty("name")] public string Name; |
| | | 65 | | [JsonProperty("age")] public int Age; |
| | | 66 | | |
| | | 67 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 68 | | { |
| | | 69 | | MethodCallHandler.RegisterMethodCall(typeof(PersonStructV1), nameof(Migrate_1)); |
| | | 70 | | return jsonObj; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | [Migratable(2)] |
| | | 75 | | public sealed class PersonV2 |
| | | 76 | | { |
| | | 77 | | [JsonProperty("fullName")] public string FullName; |
| | | 78 | | [JsonProperty("name")] public string Name; |
| | | 79 | | [JsonProperty("surname")] public string Surname; |
| | | 80 | | [JsonProperty("birthYear")] public int BirthYear; |
| | | 81 | | |
| | | 82 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 83 | | { |
| | | 84 | | MethodCallHandler.RegisterMethodCall(typeof(PersonV2), nameof(Migrate_1)); |
| | | 85 | | return jsonObj; |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | private static JObject Migrate_2(JObject jsonObj) |
| | | 89 | | { |
| | | 90 | | JToken nameToken = jsonObj["name"]; |
| | | 91 | | jsonObj.Add("fullName", nameToken); |
| | | 92 | | |
| | | 93 | | JToken ageToken = jsonObj["age"]; |
| | | 94 | | jsonObj.Add("birthYear", 2024 - ageToken.ToObject<int>()); |
| | | 95 | | jsonObj.Remove("age"); |
| | | 96 | | |
| | | 97 | | var oldNameSplit = nameToken.ToObject<string>().Split(' '); |
| | | 98 | | jsonObj.Remove("name"); |
| | | 99 | | jsonObj.Add("name", oldNameSplit[0]); |
| | | 100 | | jsonObj.Add("surname", oldNameSplit[1]); |
| | | 101 | | |
| | | 102 | | MethodCallHandler.RegisterMethodCall(typeof(PersonV2), nameof(Migrate_2)); |
| | | 103 | | |
| | | 104 | | return jsonObj; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | [Migratable(1)] |
| | | 109 | | public sealed class PersonJsonCtor |
| | | 110 | | { |
| | | 111 | | [NonSerialized] public readonly bool IsJsonCtorCalled; |
| | | 112 | | |
| | | 113 | | [JsonProperty("name")] public string Name; |
| | | 114 | | [JsonProperty("age")] public int Age; |
| | | 115 | | |
| | 0 | 116 | | public PersonJsonCtor() { } |
| | | 117 | | |
| | | 118 | | [JsonConstructor] |
| | 1 | 119 | | public PersonJsonCtor(string name, int age) |
| | 1 | 120 | | { |
| | 1 | 121 | | Name = name; |
| | 1 | 122 | | Age = age; |
| | 1 | 123 | | IsJsonCtorCalled = true; |
| | 1 | 124 | | } |
| | | 125 | | |
| | | 126 | | private static JObject Migrate_1(JObject jsonObj) |
| | 1 | 127 | | { |
| | 1 | 128 | | MethodCallHandler.RegisterMethodCall(typeof(PersonJsonCtor), nameof(Migrate_1)); |
| | 1 | 129 | | return jsonObj; |
| | 1 | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | [Migratable(1)] |
| | | 134 | | public sealed class PersonV1WithoutMigrationMethod |
| | | 135 | | { |
| | | 136 | | [JsonProperty("name")] public string Name; |
| | | 137 | | [JsonProperty("age")] public int Age; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | public class TwoPersonsV1NotMigratableMock |
| | | 141 | | { |
| | | 142 | | [JsonProperty("person1")] public PersonV1 Person1; |
| | | 143 | | [JsonProperty("person2")] public PersonV1 Person2; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | public class TwoPersonsTwoDifferentMigratableMock |
| | | 147 | | { |
| | | 148 | | [JsonProperty("person1")] public PersonV1 Person1; |
| | | 149 | | public Version Version; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | [Migratable(1)] |
| | | 153 | | public class TwoPersonsMigratableMock |
| | | 154 | | { |
| | | 155 | | [JsonProperty("person1")] public PersonV1 Person1; |
| | | 156 | | [JsonProperty("person2")] public PersonV1 Person2; |
| | | 157 | | |
| | | 158 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 159 | | { |
| | | 160 | | MethodCallHandler.RegisterMethodCall(typeof(TwoPersonsMigratableMock), nameof(Migrate_1)); |
| | | 161 | | return jsonObj; |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | public class PersonsDataStructureMock<T> |
| | | 166 | | { |
| | | 167 | | [JsonProperty("persons")] public T Persons; |
| | | 168 | | } |
| | | 169 | | |
| | | 170 | | [Migratable(2)] |
| | | 171 | | public class ParentMock |
| | | 172 | | { |
| | | 173 | | protected static JObject Migrate_1(JObject jsonObj) |
| | | 174 | | { |
| | | 175 | | MethodCallHandler.RegisterMethodCall(typeof(ParentMock), nameof(Migrate_1)); |
| | | 176 | | return jsonObj; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | protected static JObject Migrate_2(JObject jsonObj) |
| | | 180 | | { |
| | | 181 | | MethodCallHandler.RegisterMethodCall(typeof(ParentMock), nameof(Migrate_2)); |
| | | 182 | | return jsonObj; |
| | | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | [Migratable(10)] |
| | | 187 | | public class ChildV10Mock : ParentMock |
| | | 188 | | { |
| | | 189 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 190 | | { |
| | | 191 | | jsonObj = ParentMock.Migrate_1(jsonObj); |
| | | 192 | | MethodCallHandler.RegisterMethodCall(typeof(ChildV10Mock), nameof(Migrate_1)); |
| | | 193 | | return jsonObj; |
| | | 194 | | } |
| | | 195 | | |
| | | 196 | | private static JObject Migrate_2(JObject jsonObj) |
| | | 197 | | { |
| | | 198 | | MethodCallHandler.RegisterMethodCall(typeof(ChildV10Mock), nameof(Migrate_2)); |
| | | 199 | | return jsonObj; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private static JObject Migrate_10(JObject jsonObj) |
| | | 203 | | { |
| | | 204 | | jsonObj = ParentMock.Migrate_2(jsonObj); |
| | | 205 | | MethodCallHandler.RegisterMethodCall(typeof(ChildV10Mock), nameof(Migrate_10)); |
| | | 206 | | return jsonObj; |
| | | 207 | | } |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | [Migratable(1)] |
| | | 211 | | public class PersonV1NestedMock |
| | | 212 | | { |
| | | 213 | | [JsonProperty("name")] public string Name; |
| | | 214 | | [JsonProperty("child")] public PersonV1NestedMock Child; |
| | | 215 | | |
| | | 216 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 217 | | { |
| | | 218 | | MethodCallHandler.RegisterMethodCall(typeof(PersonV1NestedMock), nameof(Migrate_1)); |
| | | 219 | | return jsonObj; |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | [Migratable(1)] |
| | | 224 | | public class Directory |
| | | 225 | | { |
| | | 226 | | public string Name { get; set; } |
| | | 227 | | public Directory Parent { get; set; } |
| | | 228 | | public IList<File> Files { get; set; } |
| | | 229 | | |
| | | 230 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 231 | | { |
| | | 232 | | MethodCallHandler.RegisterMethodCall(typeof(Directory), nameof(Migrate_1)); |
| | | 233 | | return jsonObj; |
| | | 234 | | } |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | [Migratable(1)] |
| | | 238 | | public class File |
| | | 239 | | { |
| | | 240 | | public string Name { get; set; } |
| | | 241 | | public Directory Parent { get; set; } |
| | | 242 | | |
| | | 243 | | private static JObject Migrate_1(JObject jsonObj) |
| | | 244 | | { |
| | | 245 | | MethodCallHandler.RegisterMethodCall(typeof(File), nameof(Migrate_1)); |
| | | 246 | | return jsonObj; |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | } |