public void CanSetFlushModeToNever()
        {
            ArrangeRecords();

            using (var conversation = new ScopedConversation(ConversationFlushMode.Explicit))
            {
                BlogLazy blog;
                using (new ConversationalScope(conversation))
                {
                    blog        = BlogLazy.FindAll().First();
                    blog.Author = "Anonymous";
                    blog.Save();
                    BlogLazy.FindAll();                     // Triggers flushing if allowed
                }

                Assert.That(blog.Author, Is.EqualTo("Anonymous"));

                // Outside any ConversationalScope session-per-request is used
                Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Markus"));

                conversation.Flush();
            }

            Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Anonymous"));
        }
        public void BasicScenario()
        {
            // Arrange
            ArrangeRecords();

        	// Act
            IScopeConversation conversation = new ScopedConversation();
            BlogLazy queriedBlog;
            using (new ConversationalScope(conversation))
            {
                queriedBlog = BlogLazy.Find(1);
            }

            // No scope here
            var fetchedPosts = queriedBlog.PublishedPosts;
            var firstPost = fetchedPosts[0] as PostLazy;
            firstPost.Published = false;

            using (new ConversationalScope(conversation))
            {
                firstPost.SaveAndFlush();
                queriedBlog.Refresh();
            }

            // Assert

            // Again, we're querying lazy properties out of scope
            Assert.That(queriedBlog.PublishedPosts, Is.Empty);
            Assert.That(queriedBlog.Posts, Is.Not.Empty);

            conversation.Dispose();
        }
        public void CanRestartAConversationWithFreshSessions()
        {
            ISession s1, s2;

            using (var c = new ScopedConversation())
            {
                using (new ConversationalScope(c))
                {
                    BlogLazy.FindAll();
                    s1 = BlogLazy.Holder.CreateSession(typeof(BlogLazy));
                }

                c.Restart();

                using (new ConversationalScope(c))
                {
                    BlogLazy.FindAll();
                    s2 = BlogLazy.Holder.CreateSession(typeof(BlogLazy));
                }

                Assert.That(s1, Is.Not.SameAs(s2));
                Assert.That(s1.IsOpen, Is.False);
                Assert.That(s2.IsOpen, Is.True);
            }
        }
        public void BasicScenario()
        {
            // Arrange
            ArrangeRecords();

            // Act
            IScopeConversation conversation = new ScopedConversation();
            BlogLazy           queriedBlog;

            using (new ConversationalScope(conversation))
            {
                queriedBlog = BlogLazy.Find(1);
            }

            // No scope here
            var fetchedPosts = queriedBlog.PublishedPosts;
            var firstPost    = fetchedPosts[0] as PostLazy;

            firstPost.Published = false;

            using (new ConversationalScope(conversation))
            {
                firstPost.SaveAndFlush();
                queriedBlog.Refresh();
            }

            // Assert

            // Again, we're querying lazy properties out of scope
            Assert.That(queriedBlog.PublishedPosts, Is.Empty);
            Assert.That(queriedBlog.Posts, Is.Not.Empty);

            conversation.Dispose();
        }
예제 #5
0
 public void CanSetFlushModeAfterCreation()
 {
     using (var conv = new ScopedConversation())
     {
         conv.FlushMode = ConversationFlushMode.Explicit;
         Assert.That(conv.FlushMode, Is.EqualTo(ConversationFlushMode.Explicit));
     }
 }
예제 #6
0
 public void ConversationIsCanceledAfterSilentError()
 {
     using (var c = new ScopedConversation())
     {
         c.ExecuteSilently(() => new PostLazy().SaveWithException());
         Assert.That(c.IsCanceled);
     }
 }
 public void CannotFlushAfterCancel()
 {
     using (var conv = new ScopedConversation(ConversationFlushMode.Explicit))
     {
         conv.Cancel();
         Assert.Throws<ActiveRecordException>(() => { conv.Flush(); });
     }
 }
예제 #8
0
 public void CannotFlushAfterCancel()
 {
     using (var conv = new ScopedConversation(ConversationFlushMode.Explicit))
     {
         conv.Cancel();
         Assert.Throws <ActiveRecordException>(() => { conv.Flush(); });
     }
 }
예제 #9
0
 public void ConversationIsCanceledAfterErrorInExecute()
 {
     using (var c = new ScopedConversation())
     {
         Assert.Throws <ApplicationException>(() => c.Execute(() => new PostLazy().SaveWithException()));
         Assert.That(c.IsCanceled);
     }
 }
예제 #10
0
 public void CanCheckWhetherAConversationWasCanceled()
 {
     using (var c = new ScopedConversation())
     {
         Assert.That(c.IsCanceled, Is.False);
         c.Cancel();
         Assert.That(c.IsCanceled);
     }
 }
 public void CanCheckWhetherAConversationWasCanceled()
 {
     using (var c = new ScopedConversation())
     {
         Assert.That(c.IsCanceled, Is.False);
         c.Cancel();
         Assert.That(c.IsCanceled);
     }
 }
 public void CanRestartAConversation()
 {
     using (var c = new ScopedConversation())
     {
         Assert.That(c.IsCanceled, Is.False);
         c.Cancel();
         Assert.That(c.IsCanceled);
         c.Restart();
         Assert.That(c.IsCanceled, Is.False);
     }
 }
예제 #13
0
 public void CanRestartAConversation()
 {
     using (var c = new ScopedConversation())
     {
         Assert.That(c.IsCanceled, Is.False);
         c.Cancel();
         Assert.That(c.IsCanceled);
         c.Restart();
         Assert.That(c.IsCanceled, Is.False);
     }
 }
예제 #14
0
        public void TriggersEventWhenCanceling()
        {
            bool triggered = false;

            using (var c = new ScopedConversation())
            {
                c.Canceled += (conv, args) => triggered = true;
                c.Cancel();
            }

            Assert.That(triggered);
        }
예제 #15
0
        public void TriggersWithRightArgsWhenCanceling()
        {
            bool triggered  = false;
            bool userCancel = false;

            using (var c = new ScopedConversation())
            {
                c.Canceled += (conv, args) => { triggered  = true;
                                                userCancel = args.CanceledByUser; };
                c.Cancel();
            }

            Assert.That(triggered);
            Assert.That(userCancel);
        }
예제 #16
0
        public void TriggersWithRightArgsWhenCancelingAfterException()
        {
            bool      triggered = false;
            Exception ex        = null;

            using (var c = new ScopedConversation())
            {
                c.Canceled += (conv, args) =>
                {
                    triggered = true;
                    ex        = args.Exception;
                };
                c.ExecuteSilently(() => new PostLazy().SaveWithException());
            }

            Assert.That(triggered);
            Assert.That(ex, Is.Not.Null
                        .And.InstanceOf <ApplicationException>());
        }
예제 #17
0
        public void ThrowsReasonableErrorMessageWhenUsedAfterCancel()
        {
            using (var conversation = new ScopedConversation())
            {
                conversation.Cancel();

                var ex = Assert.Throws <ActiveRecordException>(() =>
                {
                    using (new ConversationalScope(conversation))
                        BlogLazy.FindAll();
                }
                                                               );

                Assert.That(ex.Message.Contains("cancel"));
                Assert.That(ex.Message.Contains("ConversationalScope"));
                Assert.That(ex.Message.Contains("session"));
                Assert.That(ex.Message.Contains("request"));
            }
        }
예제 #18
0
        public void CannotSetFlushModeAfterUsingTheConversation()
        {
            using (var c = new ScopedConversation())
            {
                using (new ConversationalScope(c))
                {
                    BlogLazy.FindAll();
                }

                var ex = Assert.Throws <ActiveRecordException>(
                    () => c.FlushMode = ConversationFlushMode.Explicit);

                Assert.That(ex.Message, Is.Not.Null
                            .And.Contains("FlushMode")
                            .And.Contains("set")
                            .And.Contains("after")
                            .And.Contains("session"));
            }
        }
        public void CannotSetFlushModeAfterUsingTheConversation()
        {
            using (var c = new ScopedConversation())
            {
                using (new ConversationalScope(c))
                {
                    BlogLazy.FindAll();
                }

                var ex = Assert.Throws<ActiveRecordException>(
                    ()=>c.FlushMode=ConversationFlushMode.Explicit);

                Assert.That(ex.Message, Is.Not.Null
                    .And.Contains("FlushMode")
                    .And.Contains("set")
                    .And.Contains("after")
                    .And.Contains("session"));
            }
        }
예제 #20
0
        public void SessionsAreKeptThroughoutTheConversation()
        {
            IScopeConversation conversation = new ScopedConversation();
            ISession           session      = null;

            using (new ConversationalScope(conversation))
            {
                BlogLazy.FindAll();
                session = BlogLazy.Holder.CreateSession(typeof(BlogLazy));
            }

            Assert.That(session.IsOpen);

            using (new ConversationalScope(conversation))
            {
                BlogLazy.FindAll();
                Assert.That(BlogLazy.Holder.CreateSession(typeof(BlogLazy)), Is.SameAs(session));
            }

            conversation.Dispose();
            Assert.That(session.IsOpen, Is.False);
        }
        public void CanUseIConversationDirectly()
        {
            ArrangeRecords();

            using (IConversation conversation = new ScopedConversation())
            {
                BlogLazy blog = null;
                conversation.Execute(() => { blog = BlogLazy.FindAll().First(); });

                Assert.That(blog, Is.Not.Null);
                Assume.That(blog.Author, Is.EqualTo("Markus"));

                // Lazy access
                Assert.That(blog.PublishedPosts.Count, Is.EqualTo(1));

                blog.Author = "Anonymous";

                conversation.Execute(() => blog.Save());
            }

            Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Anonymous"));
        }
        public void CanCancelConversations()
        {
            ArrangeRecords();
            using (var conversation = new ScopedConversation())
            {
                BlogLazy blog = null;
                using (new ConversationalScope(conversation))
                {
                    blog = BlogLazy.FindAll().First();
                }

                blog.Author = "Somebody else";

                using (new ConversationalScope(conversation))
                {
                    blog.SaveAndFlush();
                }

                conversation.Cancel();
            }
            Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Markus"));
        }
		public void CanCancelConversations()
		{
			ArrangeRecords();
    		using (var conversation = new ScopedConversation())
    		{
    			BlogLazy blog = null;
    			using (new ConversationalScope(conversation))
    			{
    				blog = BlogLazy.FindAll().First();
    			}

    			blog.Author = "Somebody else";

    			using (new ConversationalScope(conversation))
    			{
    				blog.SaveAndFlush();
    			}

    			conversation.Cancel();
    		}
			Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Markus"));
		}
        public void TriggersWithRightArgsWhenCanceling()
        {
            bool triggered = false;
            bool userCancel = false;
            using (var c = new ScopedConversation())
            {
                c.Canceled += (conv, args) => { triggered = true;
                                              	userCancel = args.CanceledByUser; };
                c.Cancel();
            }

            Assert.That(triggered);
            Assert.That(userCancel);
        }
    	public void CanUseIConversationDirectly()
    	{
    		ArrangeRecords();

    		using (IConversation conversation = new ScopedConversation())
    		{
    			BlogLazy blog = null;
    			conversation.Execute(() => { blog = BlogLazy.FindAll().First(); });

				Assert.That(blog, Is.Not.Null);
				Assume.That(blog.Author, Is.EqualTo("Markus"));

				// Lazy access
				Assert.That(blog.PublishedPosts.Count, Is.EqualTo(1));

    			blog.Author = "Anonymous";

    			conversation.Execute(() => blog.Save());
				
    		}

			Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Anonymous"));
    	}
    	public void CanRestartAConversationWithFreshSessions()
    	{
    		ISession s1, s2;
    		using (var c = new ScopedConversation())
    		{
    			using (new ConversationalScope(c))
    			{
    				BlogLazy.FindAll();
    				s1 = BlogLazy.Holder.CreateSession(typeof (BlogLazy));
    			}
				
				c.Restart();

				using (new ConversationalScope(c))
				{
					BlogLazy.FindAll();
					s2 = BlogLazy.Holder.CreateSession(typeof(BlogLazy));
				}    			

				Assert.That(s1, Is.Not.SameAs(s2));
    			Assert.That(s1.IsOpen, Is.False);
    			Assert.That(s2.IsOpen, Is.True);
    		}
    	}
 public void ConversationIsCanceledAfterErrorInExecute()
 {
     using (var c = new ScopedConversation())
     {
         Assert.Throws<ApplicationException>(() => c.Execute(() => new PostLazy().SaveWithException()));
         Assert.That(c.IsCanceled);
     }
 }
 public void CanSetFlushModeAfterCreation()
 {
     using (var conv=new ScopedConversation())
     {
         conv.FlushMode = ConversationFlushMode.Explicit;
         Assert.That(conv.FlushMode, Is.EqualTo(ConversationFlushMode.Explicit));
     }
 }
        public void TriggersWithRightArgsWhenCancelingAfterException()
        {
            bool triggered = false;
            Exception ex = null;
            using (var c = new ScopedConversation())
            {
                c.Canceled += (conv, args) =>
                {
                    triggered = true;
                    ex = args.Exception;
                };
                c.ExecuteSilently(()=>new PostLazy().SaveWithException());
            }

            Assert.That(triggered);
            Assert.That(ex, Is.Not.Null
                .And.InstanceOf<ApplicationException>());
        }
        public void TriggersEventWhenCanceling()
        {
            bool triggered = false;
            using (var c = new ScopedConversation())
            {
                c.Canceled += (conv,args) => triggered = true;
                c.Cancel();
            }

            Assert.That(triggered);
        }
        public void ThrowsReasonableErrorMessageWhenUsedAfterCancel()
        {
            using (var conversation = new ScopedConversation())
            {
                conversation.Cancel();

                var ex = Assert.Throws<ActiveRecordException>(() =>
                                                              	{
                                                              		using (new ConversationalScope(conversation))
                                                              			BlogLazy.FindAll();
                                                              	}
                    );

                Assert.That(ex.Message.Contains("cancel"));
                Assert.That(ex.Message.Contains("ConversationalScope"));
                Assert.That(ex.Message.Contains("session"));
                Assert.That(ex.Message.Contains("request"));
            }
        }
        public void SessionsAreKeptThroughoutTheConversation()
        {
            IScopeConversation conversation = new ScopedConversation();
            ISession session = null;

            using (new ConversationalScope(conversation))
            {
                BlogLazy.FindAll();
                session = BlogLazy.Holder.CreateSession(typeof (BlogLazy));
            }

            Assert.That(session.IsOpen);

            using (new ConversationalScope(conversation))
            {
                BlogLazy.FindAll();
                Assert.That(BlogLazy.Holder.CreateSession(typeof(BlogLazy)), Is.SameAs(session));
            }

            conversation.Dispose();
            Assert.That(session.IsOpen, Is.False);
        }
		public void CanSetFlushModeToNever()
		{
			ArrangeRecords();

			using (var conversation = new ScopedConversation(ConversationFlushMode.Explicit))
			{
				BlogLazy blog;
				using (new ConversationalScope(conversation))
				{
					blog = BlogLazy.FindAll().First();
					blog.Author = "Anonymous";
					blog.Save();
					BlogLazy.FindAll(); // Triggers flushing if allowed
				}

				Assert.That(blog.Author, Is.EqualTo("Anonymous"));

				// Outside any ConversationalScope session-per-request is used
				Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Markus"));

				conversation.Flush();
			}

			Assert.That(BlogLazy.FindAll().First().Author, Is.EqualTo("Anonymous"));
		}
 public void ConversationIsCanceledAfterSilentError()
 {
     using (var c = new ScopedConversation())
     {
         c.ExecuteSilently(() => new PostLazy().SaveWithException());
         Assert.That(c.IsCanceled);
     }
 }