Inheritance: ActiveRecordBase
Ejemplo n.º 1
0
		public void SimpleOperations()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			Blog[] blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);

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

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

			Blog retrieved = blogs[0];
			Assert.IsNotNull(retrieved);

			Assert.AreEqual(blog.Name, retrieved.Name);
			Assert.AreEqual(blog.Author, retrieved.Author);
		}
		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);
		}
        public void MoreThanOneConnectionWithinTheSessionScope()
        {
            SqlConnection conn = CreateSqlConnection();
            SqlConnection conn2 = CreateSqlConnection2();

            using(new SessionScope())
            {
                foreach(SqlConnection connection in new SqlConnection[] { conn, conn2 })
                {
                    connection.Open();

                    using(new DifferentDatabaseScope(connection))
                    {
                        Blog blog = new Blog();
                        blog.Name = "hammett's blog";
                        blog.Author = "hamilton verissimo";
                        blog.Create();
                    }
                }
            }

            OtherDbBlog[] blogs2 = OtherDbBlog.FindAll();
            Assert.IsNotNull( blogs2 );
            Assert.AreEqual( 1, blogs2.Length );

            Blog[] blogs = Blog.FindAll();
            Assert.IsNotNull( blogs );
            Assert.AreEqual( 1, blogs.Length );
        }
Ejemplo n.º 4
0
 public Post(Blog blog, String title, String contents, String category)
 {
     _blog = blog;
     _title = title;
     _contents = contents;
     _category = category;
 }
		// 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 MoreThanOneConnectionWithinTheSessionScope()
        {
            using(var conn = CreateSqlConnection())
            using(var conn2 = CreateSqlConnection2())
            using(new SessionScope())
            {
                foreach(var connection in new [] { conn, conn2 })
                {
                    connection.Open();

                    using(new DifferentDatabaseScope(connection))
                    {
                        var blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"};
                        blog.Create();
                    }
                }
            }

            using (new SessionScope()) {
                var blogs2 = OtherDbBlog.FindAll().ToArray();
                Assert.IsNotNull( blogs2 );
                Assert.AreEqual( 1, blogs2.Length );

                var blogs = Blog.FindAll().ToArray();
                Assert.IsNotNull( blogs );
                Assert.AreEqual( 1, blogs.Length );
            }
        }
		public void SimpleCase()
		{
			Blog blog = new Blog();
			blog.Name = "hammett's blog";
			blog.Author = "hamilton verissimo";
			blog.Save();

			SqlConnection conn = CreateSqlConnection();

			using(conn)
			{
				conn.Open();

				using(new DifferentDatabaseScope(conn))
				{
					blog.Create();
				}
			}

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

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

            for (int i = 1; i <= 10; i++)
            {
                var blog = new Blog(i) { Name = "n" + i };
                blog.Create();
            }
        }
Ejemplo n.º 9
0
		public void JoinedTable()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), 
				typeof(Post), 
				typeof(Blog), 
				typeof(Company), 
				typeof(Person));

			Recreate();

			Post.DeleteAll();
			Blog.DeleteAll();
			Company.DeleteAll();
			Person.DeleteAll();

			Blog blog = new Blog();
			blog.Name = "Ronalodo's blog";
			blog.Author = "Christiano Ronalodo";
			blog.Save();

			Person person = new Person();
			person.Name = "Ronaldo";
			person.FullName = new FullName();
			person.FullName.First = "Christiano";
			person.FullName.Last = "Ronaldo";
			person.Address = "123 Nutmeg";
			person.City = "Lisbon";
			person.Blog = blog;
			person.Save();

			Person[] people = Person.FindAll();
			Assert.AreEqual( 1, people.Length );

			Person personLoaded = people[0];

			Assert.AreEqual(person.Name, personLoaded.Name);
			Assert.AreEqual(person.FullName.First, personLoaded.FullName.First);
			Assert.AreEqual(person.FullName.Last, personLoaded.FullName.Last);
			Assert.AreEqual(person.Address, personLoaded.Address);
			Assert.AreEqual(person.City, personLoaded.City);
			Assert.AreEqual(blog.Id, personLoaded.Blog.Id);
			Assert.AreEqual(blog.Name, personLoaded.Blog.Name);

			personLoaded.FullName.Middle = "Goal";
			personLoaded.Address = "200 Hatrick";
			personLoaded.Update();

			people = Person.FindAll();
			Assert.AreEqual(1, people.Length);

			Person personUpdated = people[0];
			Assert.AreEqual(personLoaded.FullName.Middle, personUpdated.FullName.Middle);
			Assert.AreEqual(personLoaded.Address, personUpdated.Address);
		}
Ejemplo n.º 10
0
        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 UsingSessionScope()
		{
			ISession session1, session2;

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

				session1 = blog.CurrentSession;
				Assert.IsNotNull(session1);

				SqlConnection conn = CreateSqlConnection();

				using(conn)
				{
					conn.Open();

					using(new DifferentDatabaseScope(conn))
					{
						blog.Create();

						session2 = blog.CurrentSession;
						Assert.IsNotNull(session2);

						Assert.IsFalse( Object.ReferenceEquals(session1, session2) );
					}
				}
			}

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

			OtherDbBlog[] blogs2 = OtherDbBlog.FindAll();
			Assert.IsNotNull( blogs2 );
			Assert.AreEqual( 1, blogs2.Length );
		}
        private void TestBehaviour(DefaultFlushType flushType, FlushMode sessionScopeMode, FlushMode transactionScopeMode)
        {
            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
            Recreate();

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

            DefaultFlushType originalDefaultFlushType = ActiveRecordStarter.ConfigurationSource.DefaultFlushType;
            try
            {
                ((InPlaceConfigurationSource)ActiveRecordStarter.ConfigurationSource).DefaultFlushType = flushType;

                Blog blog = new Blog(); // just for CurrentSession

                using (new SessionScope())
                {
                    Blog.FindAll();
                    Assert.AreEqual(sessionScopeMode, blog.CurrentSession.FlushMode);

                    using (new TransactionScope())
                    {
                        Blog.FindAll();
                        Assert.AreEqual(transactionScopeMode, blog.CurrentSession.FlushMode);
                    }

                    // Properly reset?
                    Blog.FindAll();
                    Assert.AreEqual(sessionScopeMode, blog.CurrentSession.FlushMode);
                }
            }
            finally
            {
                // Restore Default Flush type we corrupted before.
                ((InPlaceConfigurationSource)ActiveRecordStarter.ConfigurationSource).DefaultFlushType = originalDefaultFlushType;
            }
        }
Ejemplo n.º 13
0
		public void SimpleOperations2()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			Blog[] blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);

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

			blogs = Blog.FindAll();
			Assert.AreEqual(blog.Name, blogs[0].Name);
			Assert.AreEqual(blog.Author, blogs[0].Author);

			Assert.IsNotNull(blogs);
			Assert.AreEqual(1, blogs.Length);

			blog.Name = "something else1";
			blog.Author = "something else2";
			blog.Update();

			blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(1, blogs.Length);
			Assert.AreEqual(blog.Name, blogs[0].Name);
			Assert.AreEqual(blog.Author, blogs[0].Author);
		}
		public void DifferentSessionScopesUseDifferentCaches()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			int blogId = 0;

			using (new SessionScope())
			{
				Blog blog = new Blog();
				blog.Author = "MZY";
				blog.Name = "FooBar";
				blog.Save(); // Flushes due to IDENTITY
				blogId = blog.Id;
			}

			using (new SessionScope())
			{
				Blog blog = Blog.Find(blogId);
				blog.Name = "FooBarBaz";

				//Assert.AreEqual(FlushMode.Auto, blog.CurrentSession.FlushMode);
				//Assert.IsTrue(blog.CurrentSession.Transaction.IsActive);
				Assert.AreEqual(DefaultFlushType.Classic, ActiveRecordStarter.ConfigurationSource.DefaultFlushType);

				// Flushes automatically
				Assert.AreEqual(1, Blog.FindByProperty("Name", "FooBarBaz").Length);
			}

			using (new SessionScope())
			{
				Blog blog = Blog.Find(blogId);
				blog.Name = "FooBar";

				using (new SessionScope())
				{
					// Not flushed here
					Assert.AreEqual(0, Blog.FindByProperty("Name", "FooBar").Length);
				}
			}
			// Here it is flushed
			Assert.AreEqual(1, Blog.FindByProperty("Name", "FooBar").Length);
		}
		public void SessionScopeFlushModeNever()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			using(new SessionScope(FlushAction.Never))
			{
				Blog blog = new Blog();					
				blog.Author = "hammett";
				blog.Name = "some name";
				
				//This gets flushed automatically because of the identity field
				blog.Save();

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

				//This change won't be saved to the db
				blog.Author = "A New Author";
				blog.Save();

				//The change should not be in the db
				blogs = Blog.FindByProperty("Author", "A New Author");
				Assert.AreEqual(0, blogs.Length);
								
				SessionScope.Current.Flush();

				//The change should now be in the db
				blogs = Blog.FindByProperty("Author", "A New Author");
				Assert.AreEqual(1, blogs.Length);

				//This change will be save to the db because it uses the SaveNow method
				blog.Name = "A New Name";
				blog.SaveAndFlush();

				//The change should now be in the db
				blogs = Blog.FindByProperty("Name", "A New Name");
				Assert.AreEqual(1, blogs.Length);

				//This deletion should not get to the db
				blog.Delete();

				blogs = Blog.FindAll();
				Assert.AreEqual(1, blogs.Length);
				
				SessionScope.Current.Flush();

				//The deletion should now be in the db
				blogs = Blog.FindAll();
				Assert.AreEqual(0, blogs.Length);
			}
		}
		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 );
		}
Ejemplo n.º 17
0
		public void ExistsByCriterion()
		{
			ActiveRecordStarter.Initialize(GetConfigSource());
			ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
			Recreate();

			Blog[] blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);

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

			Assert.IsTrue(blog.Id > 0);
			Assert.IsTrue(Blog.Exists(
			              	Expression.Eq("Name", blog.Name),
			              	Expression.Eq("Author", blog.Author)));

			blog = new Blog();
			blog.Name = "chad's blog";
			blog.Author = "chad humphries";
			blog.Save();

			Assert.IsTrue(Blog.Exists(
			              	Expression.Eq("Name", blog.Name),
			              	Expression.Eq("Author", blog.Author)));

			Assert.IsFalse(Blog.Exists(
			               	Expression.Eq("Name", "/\ndrew's Blog"),
			               	Expression.Eq("Author", "Andrew Peters")));
		}
		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);
		}
Ejemplo n.º 19
0
		public void ExistsTest()
		{
			ActiveRecordStarter.Initialize(GetConfigSource());
			ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
			Recreate();

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

			Assert.IsTrue(blog.Id > 0);
			Assert.IsTrue(Blog.Exists(blog.Id));

			blog = new Blog();
			blog.Name = "chad's blog";
			blog.Author = "chad humphries";
			blog.Save();

			Assert.IsTrue(Blog.Exists(blog.Id));

			Assert.IsFalse(Blog.Exists(1000));
		}
Ejemplo n.º 20
0
		public void FetchCount()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			Assert.AreEqual(0, Post.FetchCount());
			Assert.AreEqual(0, Blog.FetchCount());

			Blog[] blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);
			Assert.IsFalse(Blog.Exists());

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

			Assert.AreEqual(1, Blog.FetchCount());
			Assert.IsTrue(Blog.Exists());

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

			Blog blog2 = new Blog();
			blog2.Name = "joe's blog";
			blog2.Author = "joe doe";
			blog2.Save();

			Assert.AreEqual(2, Blog.FetchCount());
			Assert.AreEqual(1, Blog.FetchCount("name=?", "hammett's blog"));
			Assert.IsTrue(Blog.Exists("name=?", "hammett's blog"));

			Blog retrieved = blogs[0];
			Assert.IsNotNull(retrieved);

			Assert.AreEqual(blog.Name, retrieved.Name);
			Assert.AreEqual(blog.Author, retrieved.Author);

			Assert.AreEqual(1, Blog.FetchCount(
			                   	Expression.Eq("Name", blog.Name),
			                   	Expression.Eq("Author", blog.Author)));

			Assert.AreEqual(0, Blog.FetchCount(
			                   	Expression.Eq("Name", "/\ndrew's Blog"),
			                   	Expression.Eq("Author", "Andrew Peters")));
		}
Ejemplo n.º 21
0
		public void LifecycleMethods()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			Blog blog = new Blog();

			Assert.IsTrue(blog.OnDeleteCalled == blog.OnLoadCalled == blog.OnSaveCalled == blog.OnUpdateCalled);

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

			Assert.IsTrue(blog.OnSaveCalled);
			Assert.IsFalse(blog.OnDeleteCalled);
			Assert.IsFalse(blog.OnLoadCalled);
			Assert.IsFalse(blog.OnUpdateCalled);

			blog.Name = "hammett's blog x";
			blog.Author = "hamilton verissimo x";
			blog.Save();
			Assert.IsTrue(blog.OnUpdateCalled);

			blog = Blog.Find(blog.Id);
			Assert.IsTrue(blog.OnLoadCalled);

			blog.Delete();
			Assert.IsTrue(blog.OnDeleteCalled);
		}
Ejemplo n.º 22
0
		public void DeleteAll()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			Blog[] blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);

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

			Blog blog2 = new Blog();
			blog2.Name = "richard's blog";
			blog2.Author = "g. richard bellamy";
			blog2.Save();

			blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(2, blogs.Length);

			Blog.DeleteAll("Author = 'g. richard bellamy'");

			blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(1, blogs.Length);
			Assert.AreEqual("hamilton verissimo", blogs[0].Author);

			blog1.Delete();

			blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);
		}
Ejemplo n.º 23
0
		public void ExecuteAndCallback()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();

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

			Blog[] blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.Length);

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

			blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(1, blogs.Length);

			blog.CustomAction();

			blogs = Blog.FindAll();

			Assert.IsNotNull(blogs);
			Assert.AreEqual(0, blogs.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);
		}
		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);
		}
Ejemplo n.º 26
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 NestedTransactionScopesHaveCorrectTransactionContexts()
		{
			ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
			Recreate();
			Post.DeleteAll();
			Blog.DeleteAll();

			using (TransactionScope t1 = new TransactionScope())
			{
				Blog blog1 = new Blog();
				Blog.FindAll();

				ISession s1 = blog1.CurrentSession;
				ITransaction tx1 = s1.Transaction;
				Assert.IsNotNull(tx1);

				using (TransactionScope t2 = new TransactionScope())
				{
					Blog blog2 = new Blog();
					Blog.FindAll();
					ISession s2 = blog2.CurrentSession;
					ITransaction tx2 = s2.Transaction;

					Assert.IsNotNull(tx2);
					Assert.AreNotSame(tx1, tx2);
					
					// TransactionScope uses a new session!
					// Assert.AreSame(s1, s2);
				}

				using (TransactionScope t3 = new TransactionScope(TransactionMode.Inherits))
				{
					Blog blog3 = new Blog();
					Blog.FindAll();
					ITransaction tx3 = blog3.CurrentSession.Transaction;

					Assert.IsNotNull(tx3);
					Assert.AreSame(tx1, tx3);
				}

				Assert.IsTrue(tx1.IsActive);
			}

			using (new SessionScope())
			{
				Blog blog4 = new Blog();
				Blog.FindAll();

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

					Assert.AreSame(blog4.CurrentSession.Transaction, blog5.CurrentSession.Transaction);
				}
			}

			using (new SessionScope())
			{
				Blog blog6 = new Blog();
				ISession session = blog6.CurrentSession;

				Assert.IsNotNull(session.Transaction);
				ITransaction tx4 = session.Transaction;
				using (ITransaction tx5 = session.BeginTransaction())
				{
					Assert.AreSame(tx4, tx5);
					Blog.FindAll();

					using (ITransaction tx6 = session.BeginTransaction())
					{
						Assert.AreSame(tx5, tx6);
					}
				}
			}
		}
Ejemplo n.º 28
0
		public void TestName()
		{
			ActiveRecordStarter.Initialize(GetConfigSource());
			ActiveRecordStarter.RegisterTypes(typeof(Blog), typeof(Post));
			Recreate();

			Blog blog = new Blog();
			blog.Name = null;
			blog.Author = "hamilton verissimo";
			blog.Save();

			Blog[] blogs = Blog.FindByProperty("Name", null);

			Assert.IsTrue(blogs.Length == 1);

			using(new SessionScope())
			{
				blog.Name = "Hammetts blog";
				blog.Save();
			}

			blogs = Blog.FindByProperty("Name", null);

			Assert.IsTrue(blogs.Length == 0);

			blogs = Blog.FindByProperty("Name", "Hammetts blog");

			Assert.IsTrue(blogs.Length == 1);
		}
Ejemplo n.º 29
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);
		}
Ejemplo n.º 30
0
        public void Query_by_example()
        {
            ActiveRecordStarter.Initialize(GetConfigSource(), typeof(Post), typeof(Blog));
            Recreate();

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

            Blog[] blogs = Blog.FindAll();

            Assert.IsNotNull(blogs);
            Assert.AreEqual(0, blogs.Length);

            var blog = new Blog { Name = "hammett's blog", Author = "hamilton verissimo" };
            blog.Create();

            var count = Blog.FetchCount(Example.Create(new Blog { Name = "hammett" })
                                 				.EnableLike(MatchMode.Start)
                                 				.ExcludeNulls()
                                 				.ExcludeZeroes());
            Assert.AreEqual(1, count);
        }