|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | + |
| 6 | +namespace Mapster.CollectionChangeTracking.Tests |
| 7 | +{ |
| 8 | + [TestClass] |
| 9 | + public class CollectionChangeTrackingTests |
| 10 | + { |
| 11 | + public class Model |
| 12 | + { |
| 13 | + public int Id { get; set; } |
| 14 | + public string Text { get; set; } |
| 15 | + } |
| 16 | + |
| 17 | + public class Entity : IComparable<Entity> |
| 18 | + { |
| 19 | + public int Id { get; set; } |
| 20 | + public string Text { get; set; } |
| 21 | + |
| 22 | + public int CompareTo(Entity other) |
| 23 | + { |
| 24 | + return Id.CompareTo(other.Id); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + [TestInitialize] |
| 29 | + public void Initialize() |
| 30 | + { |
| 31 | + TypeAdapterConfig<Model[], SortedSet<Entity>>.NewConfig() |
| 32 | + .TrackCollectionChanges((Model m) => m.Id, (Entity e) => e.Id); |
| 33 | + |
| 34 | + TypeAdapterConfig<IEnumerable<Model>, LinkedList<Entity>>.NewConfig() |
| 35 | + .TrackCollectionChanges((Model m) => m.Id, (Entity e) => e.Id); |
| 36 | + |
| 37 | + //TypeAdapterConfig<IEnumerable<Model>, ICollection<Entity>>.NewConfig() |
| 38 | + // .TrackCollectionChanges((Model m) => m.Id, (Entity e) => e.Id); |
| 39 | + } |
| 40 | + |
| 41 | + [TestMethod] |
| 42 | + public void ShouldMapNullSource_WithoutDestination() |
| 43 | + { |
| 44 | + SortedSet<Entity> res = TypeAdapter.Adapt<Model[], SortedSet<Entity>>(null); |
| 45 | + |
| 46 | + Assert.IsNull(res); |
| 47 | + } |
| 48 | + |
| 49 | + [TestMethod] |
| 50 | + public void ShouldMapNullSource_ToExistingDestination() |
| 51 | + { |
| 52 | + var dest = new SortedSet<Entity> { |
| 53 | + new Entity { Id = 1, Text = "a" }, |
| 54 | + new Entity { Id = 2, Text = "b" }, |
| 55 | + new Entity { Id = 3, Text = "c" }, |
| 56 | + }; |
| 57 | + |
| 58 | + SortedSet<Entity> res = TypeAdapter.Adapt<Model[], SortedSet<Entity>>(null, dest); |
| 59 | + |
| 60 | + Assert.IsNull(res); |
| 61 | + } |
| 62 | + |
| 63 | + [TestMethod] |
| 64 | + public void ShouldMapSource_WithoutDestination() |
| 65 | + { |
| 66 | + var src = new[] { |
| 67 | + new Model { Id = 1, Text = "A" }, |
| 68 | + new Model { Id = 2, Text = "B" }, |
| 69 | + new Model { Id = 3, Text = "C" }, |
| 70 | + new Model { Id = 4, Text = "D" }, |
| 71 | + }; |
| 72 | + |
| 73 | + SortedSet<Entity> res = TypeAdapter.Adapt<Model[], SortedSet<Entity>>(src); |
| 74 | + |
| 75 | + Assert.IsNotNull(res); |
| 76 | + Assert.AreEqual(src.Length, res.Count); |
| 77 | + |
| 78 | + Assert.IsTrue(src.Select(m => m.Id).SequenceEqual(res.Select(e => e.Id))); |
| 79 | + Assert.IsTrue(src.Select(m => m.Text).SequenceEqual(res.Select(e => e.Text))); |
| 80 | + } |
| 81 | + |
| 82 | + [TestMethod] |
| 83 | + public void ShouldMapSource_ToExistingDestination() |
| 84 | + { |
| 85 | + var src = new[] { |
| 86 | + new Model { Id = 1, Text = "A" }, |
| 87 | + new Model { Id = 2, Text = "B" }, |
| 88 | + new Model { Id = 4, Text = "D" }, |
| 89 | + new Model { Id = 5, Text = "E" }, |
| 90 | + }; |
| 91 | + |
| 92 | + var dest = new SortedSet<Entity> { |
| 93 | + new Entity { Id = 1, Text = "a" }, |
| 94 | + new Entity { Id = 2, Text = "b" }, |
| 95 | + new Entity { Id = 3, Text = "c" }, |
| 96 | + }; |
| 97 | + |
| 98 | + var destArray = new Entity[3]; |
| 99 | + dest.CopyTo(destArray, 0); |
| 100 | + |
| 101 | + SortedSet<Entity> res = TypeAdapter.Adapt(src, dest); |
| 102 | + |
| 103 | + var resArray = new Entity[4]; |
| 104 | + res.CopyTo(resArray, 0); |
| 105 | + |
| 106 | + Assert.IsNotNull(res); |
| 107 | + Assert.AreEqual(src.Length, res.Count); |
| 108 | + |
| 109 | + // should preserve objects with keys both in source and destination |
| 110 | + Assert.AreSame(destArray[0], resArray[0]); |
| 111 | + Assert.AreSame(destArray[1], resArray[1]); |
| 112 | + // should add objects with keys in source but not in destination |
| 113 | + Assert.IsNotNull(resArray[2]); |
| 114 | + Assert.IsNotNull(resArray[3]); |
| 115 | + // should remove objects with keys in destination but not in source |
| 116 | + Assert.AreNotSame(destArray[2], resArray[2]); |
| 117 | + Assert.AreNotSame(destArray[2], resArray[3]); |
| 118 | + |
| 119 | + // should map values of collection items |
| 120 | + Assert.IsTrue(src.OrderBy(m => m.Id).Select(m => m.Id).SequenceEqual(res.Select(e => e.Id))); |
| 121 | + Assert.IsTrue(src.OrderBy(m => m.Id).Select(m => m.Text).SequenceEqual(res.Select(e => e.Text))); |
| 122 | + } |
| 123 | + |
| 124 | + private static IEnumerable<Model> BuildSource() |
| 125 | + { |
| 126 | + yield return new Model { Id = 1, Text = "A" }; |
| 127 | + yield return new Model { Id = 2, Text = "B" }; |
| 128 | + yield return new Model { Id = 4, Text = "D" }; |
| 129 | + yield return new Model { Id = 5, Text = "E" }; |
| 130 | + } |
| 131 | + |
| 132 | + public class ModelWrapper |
| 133 | + { |
| 134 | + public IEnumerable<Model> Collection { get; set; } |
| 135 | + } |
| 136 | + |
| 137 | + public class EntityWrapper |
| 138 | + { |
| 139 | + public ICollection<Entity> Collection { get; set; } |
| 140 | + } |
| 141 | + |
| 142 | + [TestMethod] |
| 143 | + public void ShouldMapAnyIEnumerable_ToAnyICollection() |
| 144 | + { |
| 145 | + var dest = new LinkedList<Entity>(new[] { |
| 146 | + new Entity { Id = 1, Text = "a" }, |
| 147 | + new Entity { Id = 2, Text = "b" }, |
| 148 | + new Entity { Id = 3, Text = "c" }, |
| 149 | + }); |
| 150 | + |
| 151 | + var destArray = new Entity[3]; |
| 152 | + dest.CopyTo(destArray, 0); |
| 153 | + |
| 154 | + var destWrapper = new EntityWrapper { Collection = dest }; |
| 155 | + var srcWrapper = new ModelWrapper { Collection = BuildSource() }; |
| 156 | + |
| 157 | + var res = TypeAdapter.Adapt(srcWrapper, destWrapper); |
| 158 | + |
| 159 | + var resArray = new Entity[4]; |
| 160 | + res.Collection.CopyTo(resArray, 0); |
| 161 | + |
| 162 | + // should preserve collection type |
| 163 | + Assert.AreEqual(dest.GetType(), res.Collection.GetType()); |
| 164 | + // should preserve objects with keys both in source and destination |
| 165 | + Assert.AreSame(destArray[0], resArray[0]); |
| 166 | + Assert.AreSame(destArray[1], resArray[1]); |
| 167 | + // should add objects with keys in source but not in destination |
| 168 | + Assert.IsNotNull(resArray[2]); |
| 169 | + Assert.IsNotNull(resArray[3]); |
| 170 | + // should remove objects with keys in destination but not in source |
| 171 | + Assert.AreNotSame(destArray[2], resArray[2]); |
| 172 | + Assert.AreNotSame(destArray[2], resArray[3]); |
| 173 | + |
| 174 | + // should map values of collection items |
| 175 | + Assert.IsTrue(BuildSource().Select(m => m.Id).SequenceEqual(res.Collection.Select(e => e.Id))); |
| 176 | + Assert.IsTrue(BuildSource().Select(m => m.Text).SequenceEqual(res.Collection.Select(e => e.Text))); |
| 177 | + } |
| 178 | + } |
| 179 | +} |
0 commit comments