public void ListTrackedObject_Логгируется()
        {
            var cycleException = new InvalidOperationException("CycleDetected");
            var expected       = "\r\nTrackedObject.Current.TypeName = RootEntityWithInheritanceMapping\r\nTrackedObject.Current:\r\nCurrent.Id = 0\r\nTrackedObject.IsInteresting = False\r\nTrackedObject.IsDeleted = False\r\nTrackedObject.IsModified = False\r\nTrackedObject.IsDead = False\r\nTrackedObject.IsWeaklyTracked = False\r\n";

            var trackedList = new List <TrackedObject>();

            for (int i = 0; i < 10; i++)
            {
                var trackedObjectMock = new Mock <TrackedObject>(MockBehavior.Strict);
                trackedObjectMock.Setup(tom => tom.Current).Returns(new RootEntityWithInheritanceMapping());
                trackedObjectMock.Setup(tom => tom.IsInteresting).Returns(false);
                trackedObjectMock.Setup(tom => tom.IsDeleted).Returns(false);
                trackedObjectMock.Setup(tom => tom.IsModified).Returns(false);
                trackedObjectMock.Setup(tom => tom.IsDead).Returns(false);
                trackedObjectMock.Setup(tom => tom.IsWeaklyTracked).Returns(false);

                trackedList.Add(trackedObjectMock.Object);
            }

            var trackedObjectLogFormatter = new TrackedObjectLogFormatter();

            trackedObjectLogFormatter.LogTrackedList(trackedList, "TrackedList", cycleException);


            for (int i = 0; i < 10; i++)
            {
                var data = cycleException.Data[$"{i} element in TrackedList"].ToString();

                Assert.AreEqual(expected, data);
            }
        }
Exemplo n.º 2
0
        private void BuildDependencyOrderedList(TrackedObject item, List <TrackedObject> list, Dictionary <TrackedObject, VisitState> visited)
        {
            if (visited.TryGetValue(item, out VisitState state))
            {
                if (state == VisitState.Before)
                {
                    var cycleException = Error.CycleDetected();

                    var logFormatter = new TrackedObjectLogFormatter();

                    logFormatter.LogTrackedObject(item, "CurrentTrackedObject", cycleException);
                    logFormatter.LogTrackedList(list, "List<TrackedObject>", cycleException);
                    logFormatter.LogTrackedObjectVisitState(visited, "Dictionary<TrackedObject, VisitState> visited", cycleException);

                    throw cycleException;
                }
                return;
            }

            visited[item] = VisitState.Before;

            if (item.IsInteresting)
            {
                if (item.IsDeleted)
                {
                    // if 'item' is deleted
                    //    all objects that used to refer to 'item' must be ordered before item
                    foreach (TrackedObject other in this.originalChildReferences[item])
                    {
                        if (other != item)
                        {
                            this.BuildDependencyOrderedList(other, list, visited);
                        }
                    }
                }
                else
                {
                    // if 'item' is new or changed
                    //   for all objects 'other' that 'item' refers to along association 'assoc'
                    //      if 'other' is new then 'other' must be ordered before 'item'
                    //      if 'assoc' is pure one-to-one and some other item 'prevItem' used to refer to 'other'
                    //         then 'prevItem' must be ordered before 'item'
                    foreach (MetaAssociation assoc in item.Type.Associations)
                    {
                        if (assoc.IsForeignKey)
                        {
                            TrackedObject other = this.currentParentEdges[assoc, item];
                            if (other != null)
                            {
                                if (other.IsNew)
                                {
                                    // if other is new, visit other first (since item's FK depends on it)
                                    if (other != item || item.Type.DBGeneratedIdentityMember != null)
                                    {
                                        this.BuildDependencyOrderedList(other, list, visited);
                                    }
                                }
                                else if ((assoc.IsUnique || assoc.ThisKeyIsPrimaryKey))
                                {
                                    TrackedObject prevItem = this.originalChildEdges[assoc, other];
                                    if (prevItem != null && other != item)
                                    {
                                        this.BuildDependencyOrderedList(prevItem, list, visited);
                                    }
                                }
                            }
                        }
                    }
                }

                list.Add(item);
            }

            visited[item] = VisitState.After;
        }