public DbContextCheckResult(bool areEqual = true, DbContextCheckEntry entry = null)
 {
     this.AreEqual = areEqual;
     Differences   = new List <DbContextCheckEntry>();
     if (entry != null)
     {
         Differences.Add(entry);
     }
 }
 public static DbContextCheckEntry ForObject(string id, string objectName, Object objectValue, string idPropertyName)
 {
     DbContextCheckEntry entry = new DbContextCheckEntry();
     entry.ObjectId = id;
     entry.ObjectName = objectName;
     entry.ObjectValue = objectValue;
     entry.IdPropertyName = idPropertyName;
     return entry;
 }
 public DbContextCheckResult(bool areEqual = true, DbContextCheckEntry entry = null)
 {
     this.AreEqual = areEqual;
     Differences = new List<DbContextCheckEntry>();
     if (entry != null)
     {
         Differences.Add(entry);
     }
 }
        public static DbContextCheckEntry ForObject(string id, string objectName, Object objectValue, string idPropertyName)
        {
            DbContextCheckEntry entry = new DbContextCheckEntry();

            entry.ObjectId       = id;
            entry.ObjectName     = objectName;
            entry.ObjectValue    = objectValue;
            entry.IdPropertyName = idPropertyName;
            return(entry);
        }
        private static async Task<IList<DbContextCheckEntry>> CheckCollectionCount(IEnumerable<DbSet> expectedDbSets, DbContext actualContext)
        {
            IList<DbContextCheckEntry> result = new List<DbContextCheckEntry>();
            foreach (DbSet dbSet in expectedDbSets)
            {
                var expectedList = await dbSet.ToListAsync();
                var actualList = await actualContext.Set(dbSet.ElementType.Name).ToListAsync();
                if (actualList.Count != expectedList.Count)
                {
                    DbContextCheckEntry entry = new DbContextCheckEntry();
                    entry.ActualPropertyContent = actualList.Count.ToString();
                    entry.ExpectedPropertyContent = expectedList.Count.ToString();
                    entry.PropertyName = "Collection Count";
                    entry.ObjectName = dbSet.ElementType.Name;
                    entry.ObjectId = "-";
                    result.Add(entry);
                }
            }

            return result;
        }
Пример #6
0
        private static async Task <IList <DbContextCheckEntry> > CheckCollectionCount(IEnumerable <DbSet> expectedDbSets, DbContext actualContext)
        {
            IList <DbContextCheckEntry> result = new List <DbContextCheckEntry>();

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

                var actualList = await actualContext.Set(dbSet.ElementType.Name).ToListAsync();

                if (actualList.Count != expectedList.Count)
                {
                    DbContextCheckEntry entry = new DbContextCheckEntry();
                    entry.ActualPropertyContent   = actualList.Count.ToString();
                    entry.ExpectedPropertyContent = expectedList.Count.ToString();
                    entry.PropertyName            = "Collection Count";
                    entry.ObjectName = dbSet.ElementType.Name;
                    entry.ObjectId   = "-";
                    result.Add(entry);
                }
            }

            return(result);
        }
Пример #7
0
        internal static IList <DbContextCheckEntry> ToDbContextCheckEntry(this IEnumerable <Difference> differences,
                                                                          Object objectId, string objectName, Object objectValue, string idColumnName = null)
        {
            IList <DbContextCheckEntry> entries = new List <DbContextCheckEntry>();

            foreach (var item in differences)
            {
                //The property starts with a dot in KellermannDifferences
                var propName = item.PropertyName;
                if (item.PropertyName.StartsWith("."))
                {
                    propName = item.PropertyName.Remove(0, 1);
                }

                var entry = DbContextCheckEntry
                            .ForObject(objectId.ToString(), objectName, objectValue, idColumnName)
                            .WithProperty(propName)
                            .Was(item.Object2Value)
                            .InsteadOf(item.Object1Value);
                entries.Add(entry);
            }

            return(entries);
        }
Пример #8
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());
        }