Inheritance: ActiveRecordBase
		public void RollbackVote()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			using(TransactionScope transaction = new TransactionScope())
			{
				Blog blog = new Blog();
				blog.Author = "hammett";
				blog.Name = "some name";
				blog.Save();

				Post post = new Post(blog, "title", "post contents", "Castle");
				post.Save();

				// pretend something went wrong

				transaction.VoteRollBack();
			}

			Blog[] blogs = Blog.FindAll();
			Assert.AreEqual(0, blogs.Length);

			Post[] posts = Post.FindAll();
			Assert.AreEqual(0, posts.Length);
		}
		// overrides the setup in the base class
		public void Prepare()
		{
			ActiveRecordStarter.ResetInitializationFlag();
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Blog), typeof(Post));

			ActiveRecordStarter.CreateSchema();

			Post.DeleteAll();
			Blog.DeleteAll();

			Blog blog = new Blog();
			blog.Name = "hammett's blog";
			blog.Author = "hamilton verissimo";

			blog.Save();

			Post p;

			p = new Post(blog, "a", "b", "c");
			p.Save();

			p = new Post(blog, "d", "e", "c");
			p.Save();

			p = new Post(blog, "f", "g", "h");
			p.Save();
		}
        public void AnExceptionInvalidatesTheScopeAndPreventItsFlushing()
        {
            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
            Recreate();

            Post.DeleteAll();
            Blog.DeleteAll();

            Blog blog;
            Post post;

            // Prepare
            using(new SessionScope())
            {
                blog = new Blog();
                blog.Author = "hammett";
                blog.Name = "some name";
                blog.Save();

                post = new Post(blog, "title", "contents", "castle");
                post.Save();
            }

            using(SessionScope session = new SessionScope())
            {
                Assert.IsFalse(session.HasSessionError);

                try
                {
                    // Forcing an exception
                    post = new Post(new Blog(100), "title", "contents", "castle");
                    post.SaveAndFlush();
                    Assert.Fail("Expecting exception as this operation violates a FK constraint");
                }
                catch(ActiveRecordException)
                {
                    // Exception expected
                }

                Assert.IsTrue(session.HasSessionError);
            }
        }
		public void NestedSessionScopeUsage()
		{
			ActiveRecordStarter.Initialize( GetConfigSource(), typeof(Post), typeof(Blog) );
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			using(new SessionScope())
			{
				Blog blog = new Blog();

				using(new SessionScope())
				{
					blog.Author = "hammett";
					blog.Name = "some name";
					blog.Save();
				}

				using(new SessionScope())
				{
					Post post = new Post(blog, "title", "post contents", "Castle");
					post.Save();
				}
			}

			Blog[] blogs = Blog.FindAll();
			Assert.AreEqual( 1, blogs.Length );

			Post[] posts = Post.FindAll();
			Assert.AreEqual( 1, posts.Length );
		}
Esempio n. 5
0
		public void RelationsOneToManyWithWhereAndOrder()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			Blog blog = new Blog();
			blog.Name = "hammett's blog";
			blog.Author = "hamilton verissimo";
			blog.Save();

			Post post1 = new Post(blog, "title1", "contents", "category1");
			Post post2 = new Post(blog, "title2", "contents", "category2");
			Post post3 = new Post(blog, "title3", "contents", "category3");

			post1.Save();
			Thread.Sleep(1000); // Its a smalldatetime (small precision)
			post2.Save();
			Thread.Sleep(1000); // Its a smalldatetime (small precision)
			post3.Published = true;
			post3.Save();

			blog = Blog.Find(blog.Id);

			Assert.IsNotNull(blog);
			Assert.AreEqual(2, blog.UnPublishedPosts.Count);
			Assert.AreEqual(1, blog.PublishedPosts.Count);

			Assert.AreEqual(3, blog.RecentPosts.Count);
			Assert.AreEqual(post3.Id, ((Post) blog.RecentPosts[0]).Id);
			Assert.AreEqual(post2.Id, ((Post) blog.RecentPosts[1]).Id);
			Assert.AreEqual(post1.Id, ((Post) blog.RecentPosts[2]).Id);
		}
Esempio n. 6
0
		public void RelationsOneToMany()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			Blog blog = new Blog();
			blog.Name = "hammett's blog";
			blog.Author = "hamilton verissimo";
			blog.Save();

			Post post1 = new Post(blog, "title1", "contents", "category1");
			Post post2 = new Post(blog, "title2", "contents", "category2");

			post1.Save();
			post2.Save();

			blog = Blog.Find(blog.Id);

			Assert.IsNotNull(blog);
			Assert.IsNotNull(blog.Posts, "posts collection is null");
			Assert.AreEqual(2, blog.Posts.Count);

			foreach(Post post in blog.Posts)
			{
				Assert.AreEqual(blog.Id, post.Blog.Id);
			}
		}
        public void Updating_detached_entities_collections_does_not_work()
        {
            Initialize();
            Blog blog;

            using (new SessionScope())
            {
                CreateBlog();
                blog = Blog.Find(1);
            }

            using (new StatelessSessionScope())
            {
                blog.Posts = new ArrayList();

                for (int i = 0; i < 10; i++)
                {
                    var post = new Post() { Title = "Post" + i, Created = DateTime.Now};
                    post.Create();
                    blog.Posts.Add(post);
                }

                blog.Update();
            }

            Assert.AreEqual(0, Blog.Find(1).Posts.Count);
        }
        public void Inversively_adding_to_a_detached_entitys_collections_works()
        {
            Initialize();
            Blog blog;

            using (new SessionScope())
            {
                CreateBlog();
                blog = Blog.Find(1);
            }

            using (new StatelessSessionScope())
            {
                for (int i = 0; i < 10; i++)
                {
                    var post = new Post() { Blog = blog, Title = "Post" + i, Created = DateTime.Now };
                    post.Create();
                }
            }

            Assert.AreEqual(10, Blog.Find(1).Posts.Count);
        }
		public void NestedTransactionWithRollbackOnDispose()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			using(new TransactionScope())
			{
				Blog blog = new Blog();

				using(TransactionScope t1 = new TransactionScope(TransactionMode.Inherits, OnDispose.Rollback))
				{
					blog.Author = "hammett";
					blog.Name = "some name";
					blog.Save();

					t1.VoteCommit();
				}

				using(TransactionScope t2 = new TransactionScope(TransactionMode.Inherits, OnDispose.Rollback))
				{
					Post post = new Post(blog, "title", "post contents", "Castle");

					try
					{
						post.SaveWithException();

						t2.VoteCommit(); // Will never be called
					}
					catch(Exception)
					{
						// t2.VoteRollBack();
					}
				}
			}

			Blog[] blogs = Blog.FindAll();
			Assert.AreEqual(0, blogs.Length);

			Post[] posts = Post.FindAll();
			Assert.AreEqual(0, posts.Length);
		}
		public void MixingSessionScopeAndTransactionScopes4()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			Blog b = new Blog();
			Post post = null;

			{
				b.Name = "a";
				b.Author = "x";
				b.Save();

				post = new Post(b, "t", "c", "General");
				post.Save();
			}

			using(new SessionScope())
			{
				using(new TransactionScope(TransactionMode.Inherits))
				{
					b = Blog.Find(b.Id);
					b.Name = "changed";
					b.Save();
				}

				{
					Post post2 = Post.Find(post.Id);
					b = Blog.Find(b.Id);
				}

				using(new TransactionScope(TransactionMode.Inherits))
				{
					Post post2 = Post.Find(post.Id);
					b = Blog.Find(b.Id);
				}
			}

			Blog[] blogs = Blog.FindAll();
			Assert.AreEqual(1, blogs.Length);

			Post[] posts = Post.FindAll();
			Assert.AreEqual(1, posts.Length);
		}
		public void MixingSessionScopeAndTransactionScopes()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();

			using(new SessionScope())
			{
				using(TransactionScope root = new TransactionScope())
				{
					using(TransactionScope t1 = new TransactionScope()) // Isolated
					{
						Blog blog = new Blog();

						Blog.FindAll(); // Just to force a session association

						using(new TransactionScope(TransactionMode.Inherits))
						{
							Blog.FindAll(); // Just to force a session association

							blog.Author = "hammett";
							blog.Name = "some name";
							blog.Save();
						}

						using(new TransactionScope(TransactionMode.Inherits))
						{
							Post post = new Post(blog, "title", "post contents", "Castle");

							post.Save();
						}

						t1.VoteRollBack();
					}

					Blog.FindAll(); // Cant be locked

					using(new TransactionScope())
					{
						Blog blog = new Blog();
						Blog.FindAll(); // Just to force a session association

						using(new TransactionScope())
						{
							Blog.FindAll(); // Just to force a session association

							blog.Author = "hammett";
							blog.Name = "some name";
							blog.Save();
						}

						using(TransactionScope t1n = new TransactionScope(TransactionMode.Inherits))
						{
							Post post = new Post(blog, "title", "post contents", "Castle");

							try
							{
								post.SaveWithException();
							}
							catch(Exception)
							{
								t1n.VoteRollBack();
							}
						}
					}

					root.VoteCommit();
				}
			}

			Blog[] blogs = Blog.FindAll();
			Assert.AreEqual(1, blogs.Length);

			Post[] posts = Post.FindAll();
			Assert.AreEqual(0, posts.Length);
		}