using System; using Microsoft.VisualStudio.TestTools.UnitTesting; using ZeroLevel.Services.ObjectMapping; using ZeroMappingTest.Models; using System.Collections.Generic; namespace ZeroMappingTest { [TestClass] public class MappingTest { [TestMethod] public void TestAbstractClassGetInfo() { // Arrange var mapper = TypeMapper.Create(); // Act var list = new List(); mapper.TraversalMembers(f => list.Add(f.Name)); // Assert Assert.IsTrue(mapper.Exists("Id")); Assert.IsTrue(mapper.Exists("Title")); Assert.IsTrue(mapper.Exists("Description")); Assert.IsTrue(list.Contains("Id")); Assert.IsTrue(list.Contains("Title")); Assert.IsTrue(list.Contains("Description")); Assert.IsFalse(mapper.Exists("Version")); Assert.IsFalse(mapper.Exists("Created")); Assert.IsFalse(list.Contains("Version")); Assert.IsFalse(list.Contains("Created")); Assert.AreEqual(mapper.EntityType, typeof(BaseClass)); } [TestMethod] public void TestInheritedClassGetInfo() { // Arrange var mapper = TypeMapper.Create(); // Act var list = new List(); mapper.TraversalMembers(f => list.Add(f.Name)); // Assert Assert.IsTrue(mapper.Exists("Id")); Assert.IsTrue(mapper.Exists("Title")); Assert.IsTrue(mapper.Exists("Description")); Assert.IsTrue(mapper.Exists("Number")); Assert.IsTrue(mapper.Exists("Balance")); Assert.IsTrue(mapper.Exists("ReadOnlyProperty")); Assert.IsTrue(mapper.Exists("WriteOnlyProperty")); Assert.IsTrue(list.Contains("Id")); Assert.IsTrue(list.Contains("Title")); Assert.IsTrue(list.Contains("Description")); Assert.IsTrue(list.Contains("Number")); Assert.IsTrue(list.Contains("Balance")); Assert.IsTrue(list.Contains("ReadOnlyProperty")); Assert.IsTrue(list.Contains("WriteOnlyProperty")); Assert.IsFalse(mapper.Exists("HiddenField")); Assert.IsFalse(mapper.Exists("Version")); Assert.IsFalse(mapper.Exists("Created")); Assert.IsFalse(list.Contains("HiddenField")); Assert.IsFalse(list.Contains("Version")); Assert.IsFalse(list.Contains("Created")); Assert.AreEqual(mapper.EntityType, typeof(ChildClass)); } [TestMethod] public void TestAbstractClassMapping() { // Arrange var instance = new ChildClass { Id = Guid.Empty, Title = "title", Description = "description", WriteOnlyProperty = 100, Balance = 100, Number = 100 }; var mapper = TypeMapper.Create(); // Act var id = Guid.NewGuid(); var title = "New title"; var description = "New description"; mapper.Set(instance, "Id", id); mapper.Set(instance, "Title", title); mapper.Set(instance, "Description", description); // Assert Assert.AreEqual(mapper.Get(instance, "Id"), id); Assert.AreEqual(mapper.Get(instance, "Title"), title); Assert.AreEqual(mapper.Get(instance, "Description"), description); Assert.AreEqual(mapper.Get(instance, "Id"), id); Assert.AreEqual(mapper.Get(instance, "Title"), title); Assert.AreEqual(mapper.Get(instance, "Description"), description); try { mapper.Get(instance, "Number"); Assert.Fail("Must be inaccessability"); } catch { } } [TestMethod] public void TestInheritedClassMapping() { // Arrange var instance = new ChildClass { Id = Guid.Empty, Title = "title", Description = "description", WriteOnlyProperty = 100, Balance = 100, Number = 100 }; var mapper = TypeMapper.Create(); // Act var id = Guid.NewGuid(); var title = "New title"; var description = "New description"; var number = 5465; var balance = 5555; mapper.Set(instance, "Id", id); mapper.Set(instance, "Title", title); mapper.Set(instance, "Description", description); mapper.Set(instance, "Number", number); mapper.Set(instance, "Balance", balance); // Assert Assert.AreEqual(mapper.Get(instance, "Id"), id); Assert.AreEqual(mapper.Get(instance, "Title"), title); Assert.AreEqual(mapper.Get(instance, "Description"), description); Assert.AreEqual(mapper.Get(instance, "Number"), number); Assert.AreEqual(mapper.Get(instance, "Balance"), balance); Assert.AreEqual(mapper.Get(instance, "Id"), id); Assert.AreEqual(mapper.Get(instance, "Title"), title); Assert.AreEqual(mapper.Get(instance, "Description"), description); Assert.AreEqual(mapper.Get(instance, "Number"), number); Assert.AreEqual(mapper.Get(instance, "Balance"), balance); try { var test = 1000; mapper.Set(instance, "ReadOnlyProperty", test); Assert.Fail("There should be no possibility to set a value."); } catch { } try { mapper.Get(instance, "WriteOnlyProperty"); Assert.Fail("There should be no possibility to get a value."); } catch { } try { mapper.GetOrDefault(instance, "WriteOnlyProperty", null); } catch { Assert.Fail("It should be possible to get the default value."); } try { mapper.Get(instance, "Number"); } catch(Exception ex) { Assert.Fail(ex.Message); } } [TestMethod] public void TestMapperscaching() { // Arrange var mapper1 = TypeMapper.Create(); var mapper2 = TypeMapper.Create(); var mapper3 = TypeMapper.Create(false); // Act // Assert Assert.AreSame(mapper1, mapper2); Assert.AreNotSame(mapper1, mapper3); Assert.AreNotSame(mapper3, mapper2); } } }