Exemplo n.º 1
0
        public static async Task <DbContextCheckResult> Compare(DbContext expectedctx, DbContext actualContext, params string[] ignoreFields)
        {
            IEnumerable <DbSet> expectedDbSets = expectedctx.DbSets();

            IList <DbContextCheckEntry> collectionCountResult = await CheckCollectionCount(expectedDbSets, actualContext);

            if (collectionCountResult.Count > 0)
            {
                return(new DbContextCheckResult(false, collectionCountResult));
            }

            foreach (DbSet dbSet in expectedDbSets)
            {
                var expectedList = await dbSet.ToListAsync();

                var ignoredMembers = GetIgnoredMembers(dbSet.ElementType.Name, ignoreFields);

                foreach (Object expectedObject in expectedList)
                {
                    Object actualObject = null;

                    var idProp = expectedctx.KeyMemberFor(dbSet.ElementType);

                    var prop = expectedObject.GetType().GetProperty(idProp, BindingFlags.Public | BindingFlags.Instance);

                    if (prop != null)
                    {
                        var idValue = prop.GetValue(expectedObject);

                        var actualDbSet = actualContext.Set(dbSet.ElementType);

                        actualObject = actualDbSet.Find(idValue);

                        if (actualObject == null)
                        {
                            string columnName         = expectedctx.MappedColumnName(dbSet.ElementType.Name, prop.Name);
                            DbContextCheckEntry entry = DbContextCheckEntry
                                                        .ForObject(idValue.ToString(), dbSet.ElementType.Name, null, columnName)
                                                        .WithDescription("we couldn't find an actual object with expected id  : "
                                                                         + idValue + " - this can be caused because the id is auto generated. You could adapt the ids of the expected object");
                            return(new DbContextCheckResult(false, entry));
                        }
                        else
                        {
                            //Issue #1 : we should only check for entity that have unchanged state.
                            if (actualContext.Entry(actualObject).State ==
                                expectedctx.Entry(expectedObject).State)
                            {
                                ComparisonResult cr = DbContextComparer.CompareObjects(expectedObject, actualObject, ignoredMembers);

                                if (!cr.AreEqual)
                                {
                                    string columnName = expectedctx.MappedColumnName(dbSet.ElementType.Name, prop.Name);
                                    return(new DbContextCheckResult(false,
                                                                    cr.Differences.ToDbContextCheckEntry(idValue.ToString(), dbSet.ElementType.Name, actualObject, columnName)));
                                }
                            }
                            else
                            {
                                string columnName         = actualContext.MappedColumnName(dbSet.ElementType.Name, prop.Name);
                                DbContextCheckEntry entry =
                                    DbContextCheckEntry
                                    .ForObject(idValue.ToString(), dbSet.ElementType.Name, actualObject, columnName)
                                    .WithProperty("EntityState").Was(actualContext.Entry(actualObject).State.ToString())
                                    .InsteadOf(expectedctx.Entry(expectedObject).State.ToString());
                                return(new DbContextCheckResult(false, entry));
                            }
                        }
                    }
                }
            }

            return(new DbContextCheckResult());
        }
Exemplo n.º 2
0
 public static async Task <DbContextCheckResult> CompareTo(this DbContext dbcontext, DbContext expectedContext, params string[] ignoredProperties)
 {
     return(await DbContextComparer.Compare(expectedContext, dbcontext, ignoredProperties));
 }