Changeset 14739


Ignore:
Timestamp:
03/09/17 13:52:38 (3 months ago)
Author:
gkronber
Message:

#2520: added a sketch for a unit test for conversion (and set svn:ignore properties)

Location:
branches/PersistenceOverhaul
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/PersistenceOverhaul/HeuristicLab.Persistence/4.0/Core/StaticCache.cs

    r14714 r14739  
    222222      }
    223223    }
     224    // mainly for testing
     225    public void DeregisterType(Guid guid) {
     226      lock (locker) {
     227        type2Guid.Remove(guid2Type[guid]);
     228        guid2Type.Remove(guid);
     229      }
     230    }
    224231    //public void RegisterTypeAndFullName(MemberSelection type) {
    225232    //  lock (locker) {
  • branches/PersistenceOverhaul/HeuristicLab.Tests/HeuristicLab.Persistence-3.3/UseCasesPersistenceNew.cs

    r14714 r14739  
    3333using HeuristicLab.Algorithms.GeneticAlgorithm;
    3434using HeuristicLab.Common;
     35using HeuristicLab.Core;
    3536using HeuristicLab.Data;
    3637using HeuristicLab.Persistence;
     
    21042105    }
    21052106
     2107
     2108    #region conversion
     2109
     2110    [StorableType("F9F51075-490C-48E3-BF64-14514A210149")]
     2111    private class OldBaseType {
     2112      [Storable]
     2113      public ItemCollection<IItem> items;
     2114    }
     2115    [StorableType("D211A828-6440-4E72-A8C7-AA4F9B4FFA75")]
     2116    private class OldType : OldBaseType {
     2117      [Storable]
     2118      public IntValue a;
     2119
     2120      [Storable]
     2121      public ItemList<IntValue> vals;
     2122
     2123      [Storable]
     2124      public OldType mySelf;
     2125
     2126      [Storable]
     2127      public Tuple<int, int> tup;
     2128
     2129      [Storable]
     2130      public int x;
     2131      [Storable]
     2132      public int y;
     2133    }
     2134
     2135
     2136    [StorableType("B72C58C8-3321-4706-AA94-578F57337070")]
     2137    private class NewBaseType {
     2138      [Storable]
     2139      public DoubleValue[] items;
     2140    }
     2141    [StorableType("00000000-0000-0000-0000-BADCAFFEE000")] // for testing
     2142    private partial class NewType : NewBaseType {
     2143      [Storable]
     2144      public int a;
     2145
     2146      [Storable]
     2147      public int[] val;
     2148
     2149      [Storable]
     2150      public NewType mySelf;
     2151
     2152      [Storable]
     2153      public int TupItem1;
     2154
     2155      [Storable]
     2156      public int TupItem2;
     2157
     2158      [Storable]
     2159      public Point coords;
     2160    }
     2161
     2162    // 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      //}
     2199    }
     2200
     2201    [TestMethod]
     2202    [TestCategory("Persistence4")]
     2203    [TestProperty("Time", "short")]
     2204    public void TestConversion() {
     2205      var test = new Func<OldType>(() => {
     2206        var p = new OldType();
     2207        p.a = new IntValue(1);
     2208        p.mySelf = p;
     2209        p.vals = new ItemList<IntValue>(new IntValue[] { p.a, new IntValue(2), new IntValue(3) });
     2210        var dv = new DoubleValue(1.0);
     2211        p.items = new ItemCollection<IItem>(new IItem[] { dv, dv, p.a });
     2212        return p;
     2213      });
     2214
     2215      ProtoBufSerializer serializer = new ProtoBufSerializer();
     2216      var old = test();
     2217      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;
     2223      Assert.AreEqual(restored.a, old.a.Value);
     2224      Assert.IsTrue(restored.val.SequenceEqual(old.vals.Select(iv => iv.Value)));
     2225      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]);
     2227      Assert.AreSame(restored, restored.mySelf);
     2228
     2229      string msg = Profile(test);
     2230      Console.WriteLine(msg);
     2231    }
     2232
     2233#endregion
     2234
    21062235    [TestMethod]
    21072236    [TestCategory("Persistence4")]
  • branches/PersistenceOverhaul/Types

    • Property svn:ignore set to
      .vs
  • branches/PersistenceOverhaul/Types/Types

    • Property svn:ignore set to
      obj
Note: See TracChangeset for help on using the changeset viewer.