This project is read-only.

Automatic mapping tests for EF4 Code First Entities

I test the mappings of all my EF4 entities in one test method.

here is my mapping test class:

    public class MappingTest : IntegrationTestsBase
    {
        private UniRepo u;

        [SetUp]
        public void Start()
        {
            u = new UniRepo(new DbContextFactory());
        }

        [Test]
        public void AutoTest()
        {
            var types = new[]
                            {
                                typeof (Dinner), 
                                typeof (Meal), 
                                typeof (Chef), 
                                typeof (Country), 
                            };

            foreach (var type in types)
            {
                Console.WriteLine("testing " + type.Name);
                dynamic o = Activator.CreateInstance(type).InjectFrom(new Fill(u));
                u.Insert(o);
                u.Save();
                Assert.IsTrue(o.Id != 0);
                Console.WriteLine(type.Name + " ok");
            }
        }
    }

    public class IntegrationTestsBase
    {
        private TransactionScope scope;

        [SetUp]
        public void Initialize()
        {
            scope = new TransactionScope();
        }

        [TearDown]
        public void TestCleanup()
        {
            scope.Dispose();
        }
    }

the ValueInjection used

    public class Fill : NoSourceValueInjection
    {
        private readonly IUniRepo u;

        private static long s;
        private static int i;
        private readonly bool isChild;

        public Fill(IUniRepo u, bool isChild = false)
        {
            this.u = u;
            this.isChild = isChild;
        }

        protected override void Inject(object target)
        {
            var props = target.GetProps();
            for (var j = 0; j < props.Count; j++)
            {
                var p = props[j];
                if (p.PropertyType == typeof(string)) p.SetValue(target, "a" + ++s);
                else if (p.PropertyType == typeof(int) && !p.Name.EndsWith("Id")) p.SetValue(target, ++i);
                else if (p.PropertyType == typeof(DateTime)) p.SetValue(target, DateTime.Now);
                else if (p.PropertyType.IsSubclassOf(typeof(Entity)) && p.PropertyType != target.GetType())
                {
                    dynamic o = Activator.CreateInstance(p.PropertyType).InjectFrom(new Fill(u, true));
                    u.Insert(o);
                    u.Save();
                    p.SetValue(target, o);
                }
                else if (p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(ICollection<>) && !isChild)
                {
                    var t = p.PropertyType.GetGenericArguments()[0];
                    if (!t.IsSubclassOf(typeof(Entity))) continue;

                    var tlist = typeof(List<>).MakeGenericType(t);
                    dynamic list = Activator.CreateInstance(tlist);
                    for (var k = 0; k < 3; k++)
                    {
                        dynamic o = Activator.CreateInstance(t).InjectFrom(new Fill(u, true));
                        u.Insert(o);
                        u.Save();
                        list.Add(o);
                    }
                    p.SetValue(target, list);
                }
            }
        }
    }

My entities:

public class Entity
    {
        public int Id { get; set; }
        public bool IsDeleted { get; set; }
    }

    public class Country : Entity
    {
        public string Name { get; set; }
        public virtual ICollection<Chef> Chefs { get; set; }
        public virtual ICollection<Dinner> Dinners { get; set; }
    }

    public class Meal : Entity
    {
        public string Name { get; set; }
        public string Comments { get; set; }
        public virtual ICollection<Dinner> Dinners { get; set; }
        public bool HasPic { get; set; }
    }

    public class Chef : Entity
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int CountryId { get; set; }
        public virtual Country Country { get; set; }
        public virtual ICollection<Dinner> Dinners { get; set; }
    }

    public class Dinner : Entity
    {
        public string Name { get; set; }
        public int CountryId { get; set; }
        public virtual Country Country { get; set; }
        public int ChefId { get; set; }
        public virtual Chef Chef { get; set; }
        public string Address { get; set; }
        public DateTime Date { get; set; }
        public virtual ICollection<Meal> Meals { get; set; }
    }

the generic repository used:

public class UniRepo : IUniRepo
    {
        private readonly DbContext c;

        public UniRepo(IDbContextFactory a)
        {
            c = a.GetContext();
        }

        public void Insert<T>(T o) where T : Entity
        {
            c.Set<T>().Add(o);
        }

        public void Save()
        {
            c.SaveChanges();
        }

        public T Get<T>(int id) where T : Entity
        {
            return c.Set<T>().Find(id);
        }
    }

Last edited Mar 15, 2011 at 9:16 AM by o, version 4

Comments

No comments yet.