Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/20/17 15:56:55 (7 years ago)
Author:
gkronber
Message:

#2520 added versions to storable types and implemented conversion unit test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r14739 r14771  
    21082108    #region conversion
    21092109
    2110     [StorableType("F9F51075-490C-48E3-BF64-14514A210149")]
     2110    [StorableType("F9F51075-490C-48E3-BF64-14514A210149", 1)]
    21112111    private class OldBaseType {
    21122112      [Storable]
    21132113      public ItemCollection<IItem> items;
    21142114    }
    2115     [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75")]
    2116     private class OldType : OldBaseType {
     2115    [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75", 1)]
     2116    private class V1 : OldBaseType {
    21172117      [Storable]
    21182118      public IntValue a;
     
    21222122
    21232123      [Storable]
    2124       public OldType mySelf;
     2124      public V1 mySelf;
    21252125
    21262126      [Storable]
     
    21342134
    21352135
    2136     [StorableType("B72C58C8-3321-4706-AA94-578F57337070")]
     2136    [StorableType("B72C58C8-3321-4706-AA94-578F57337070", 2)]
    21372137    private class NewBaseType {
    21382138      [Storable]
    21392139      public DoubleValue[] items;
    21402140    }
    2141     [StorableType("00000000-0000-0000-0000-BADCAFFEE000")] // for testing
    2142     private partial class NewType : NewBaseType {
     2141    [StorableType("00000000-0000-0000-0000-BADCAFFEE000", 2)] // for testing (version 2)
     2142    private partial class V2 : NewBaseType {
    21432143      [Storable]
    21442144      public int a;
     
    21482148
    21492149      [Storable]
    2150       public NewType mySelf;
     2150      public V2 mySelf;
    21512151
    21522152      [Storable]
     
    21612161
    21622162    // conversion part
    2163     private partial class NewType : NewBaseType {
    2164       //[Storable(AllowOneWay = true, Name = "items")]
    2165       //private ItemCollection<IItem> itemsCompat {
    2166       //  set { items = value.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray(); }
    2167       //}
    2168 
    2169       //[Storable(AllowOneWay = true, Name = "a")]
    2170       //private IntValue aBackwardsCompat { set { a = value.Value; } }
    2171 
    2172       //[StorableConversion(Name = "a", Version=1)]
    2173       //private int ConvertA(IntValue oldValue) { return oldValue.Value; }
    2174       //[StorableConversion(Name = "newA", Version=2)]
    2175       //private double ConvertA(int oldValue) { return (double)oldValue; }
    2176 
    2177       //[StorableConversion(OldNames= new [] {"x", "y"}, Name = "point", Version=2)]
    2178       //public Point ConvertXY(int x, int y) { return new Point(x, y); }
    2179 
    2180       //[StorableConversion(OldNames= new [] {tup}, Names = [] {"TupItem1", "TupItem2"}, Version=2)]
    2181       //public IEnumerable<object> ConvertTup(Tuple<int, int> tup) {
    2182       //  yield return tup.Item1;
    2183       //  yield return tup.Item2;
    2184       //}
    2185       //
    2186       //[Storable(AllowOneWay = true, Name = "val")]
    2187       //private ItemList<IntValue> valCompat { set { val = value.Select(iv => iv.Value).ToArray(); } }
    2188       //
    2189       //[Storable(AllowOneWay = true, Name = "mySelf")]
    2190       //private NewType mySelfCompat { set { mySelf = value; } }
    2191 
    2192 
    2193 
    2194       //[StorableConversion(SrcVersion = 1)]
    2195       //private Dictionary<string, object> ConvertAll(Dictionary<string, object> values)
    2196       //{
    2197       // 
    2198       //}
     2163    private partial class V2 : NewBaseType {
     2164      [StorableConversion(srcVersion: 1)]
     2165      private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {
     2166        var newValues = new Dictionary<string, object>();
     2167        var items = (ItemCollection<IItem>)values["OldBaseType.items"];
     2168        newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray();
     2169        newValues["V2.a"] = ((IntValue)values["V1.a"]).Value;
     2170        newValues["V2.val"] = ((ItemList<IntValue>)values["V1.vals"]).Select(iv => iv.Value).ToArray();
     2171
     2172        newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly
     2173
     2174        var tup = (Tuple<int, int>)values["V1.tup"];
     2175        if (tup != null) {
     2176          newValues["V2.TupItem1"] = tup.Item1;
     2177          newValues["V2.TupItem2"] = tup.Item2;
     2178        }
     2179
     2180        newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]);
     2181
     2182        return newValues;
     2183      }
     2184    }
     2185
     2186    [StorableType("00000000-0000-0000-0000-BADCAFFEE200", 3)] // for testing (version 3)
     2187    private partial class V3 : NewBaseType {
     2188      [Storable]
     2189      public int a;
     2190
     2191      [Storable]
     2192      public int[] val;
     2193
     2194      [Storable]
     2195      public V3 mySelf;
     2196
     2197      [Storable]
     2198      public Tuple<int, int> tup;
     2199
     2200      [Storable]
     2201      public Point coords;
     2202    }
     2203
     2204    // conversion part
     2205    private partial class V3 {
     2206      [StorableConversion(srcVersion: 1)]
     2207      private static Dictionary<string, object> ConvertV1(Dictionary<string, object> values) {
     2208        var newValues = new Dictionary<string, object>();
     2209        var items = (ItemCollection<IItem>)values["OldBaseType.items"];
     2210        newValues["NewBaseType.items"] = items.Select(iv => new DoubleValue((double)(((dynamic)iv).Value))).ToArray();
     2211        newValues["V2.a"] = ((IntValue)values["V1.a"]).Value;
     2212        newValues["V2.val"] = ((ItemList<IntValue>)values["V1.vals"]).Select(iv => iv.Value).ToArray();
     2213
     2214        newValues["V2.mySelf"] = values["V1.mySelf"]; // myself type will be mapped correctly
     2215
     2216        var tup = (Tuple<int, int>)values["V1.tup"];
     2217        if (tup != null) {
     2218          newValues["V2.TupItem1"] = tup.Item1;
     2219          newValues["V2.TupItem2"] = tup.Item2;
     2220        }
     2221
     2222        newValues["V2.coords"] = new Point((int)values["V1.x"], (int)values["V1.y"]);
     2223
     2224        return newValues;
     2225      }
     2226
     2227      [StorableConversion(srcVersion: 2)]
     2228      private static Dictionary<string, object> ConvertV2(Dictionary<string, object> values) {
     2229        var newValues = new Dictionary<string, object>();
     2230        newValues["NewBaseType.items"] = values["NewBaseType.items"];
     2231        newValues["V3.a"] = values["V2.a"];
     2232        newValues["V3.val"] = values["V2.val"];
     2233        newValues["V3.mySelf"] = values["V2.mySelf"];
     2234
     2235        newValues["V3.tup"] = Tuple.Create((int)values["V2.TupItem1"], (int)values["V2.TupItem2"]);
     2236        newValues["V3.coords"] = values["V2.coords"];
     2237
     2238        return newValues;
     2239      }
    21992240    }
    22002241
     
    22032244    [TestProperty("Time", "short")]
    22042245    public void TestConversion() {
    2205       var test = new Func<OldType>(() => {
    2206         var p = new OldType();
     2246      var test = new Func<V1>(() => {
     2247        var p = new V1();
    22072248        p.a = new IntValue(1);
    22082249        p.mySelf = p;
    22092250        p.vals = new ItemList<IntValue>(new IntValue[] { p.a, new IntValue(2), new IntValue(3) });
     2251        p.tup = Tuple.Create(17, 4);
    22102252        var dv = new DoubleValue(1.0);
    22112253        p.items = new ItemCollection<IItem>(new IItem[] { dv, dv, p.a });
     
    22162258      var old = test();
    22172259      serializer.Serialize(old, tempFile);
    2218       Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(OldType)).Guid);
    2219       Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(OldType)).Guid, typeof(NewType));
    2220 
    2221       object o = serializer.Deserialize(tempFile);
    2222       var restored = (NewType)o;
     2260      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid);
     2261      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid);
     2262
     2263      Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid, typeof(V2));
     2264
     2265      object o = serializer.Deserialize(tempFile);
     2266      var restored = (V2)o;
    22232267      Assert.AreEqual(restored.a, old.a.Value);
    22242268      Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value)));
    22252269      Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));
    2226       Assert.AreSame(restored.items[0], restored.items[1]);
     2270      // Assert.AreSame(restored.items[0], restored.items[1]);
    22272271      Assert.AreSame(restored, restored.mySelf);
    22282272
    2229       string msg = Profile(test);
    2230       Console.WriteLine(msg);
    2231     }
    2232 
    2233 #endregion
     2273      //string msg = Profile(test);
     2274      //Console.WriteLine(msg);
     2275    }
     2276
     2277    [TestMethod]
     2278    [TestCategory("Persistence4")]
     2279    [TestProperty("Time", "short")]
     2280    public void TestConversion2() {
     2281      var test = new Func<V2>(() => {
     2282        var p = new V2();
     2283        p.a = 1;
     2284        p.mySelf = p;
     2285        p.val = new int[] {2, 3, 4};
     2286        p.TupItem1 = 17;
     2287        p.TupItem2 = 4;
     2288        p.items = new DoubleValue[] {new DoubleValue(1.0), new DoubleValue(2.0) };
     2289        return p;
     2290      });
     2291
     2292      ProtoBufSerializer serializer = new ProtoBufSerializer();
     2293      var old = test();
     2294      serializer.Serialize(old, tempFile);
     2295      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V1)).Guid);
     2296      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid);
     2297      Mapper.StaticCache.DeregisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V3)).Guid);
     2298
     2299      Mapper.StaticCache.RegisterType(StorableTypeAttribute.GetStorableTypeAttribute(typeof(V2)).Guid, typeof(V3));
     2300
     2301      object o = serializer.Deserialize(tempFile);
     2302      var restored = (V3)o;
     2303      Assert.AreEqual(restored.a, old.a);
     2304      Assert.IsTrue(restored.val.SequenceEqual(old.val));
     2305      Assert.IsTrue(restored.items.Select(item => item.Value).SequenceEqual(old.items.Select(iv => (double)((dynamic)iv).Value)));
     2306      // Assert.AreSame(restored.items[0], restored.items[1]);
     2307      Assert.AreSame(restored, restored.mySelf);
     2308
     2309      //string msg = Profile(test);
     2310      //Console.WriteLine(msg);
     2311    }
     2312
     2313    #endregion
    22342314
    22352315    [TestMethod]
Note: See TracChangeset for help on using the changeset viewer.