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 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 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 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); }
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 ); }
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 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); }
public void SimpleCase() { var blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"}; using (new SessionScope()) { blog.Save(); } using (var conn = CreateSqlConnection2()) { conn.Open(); using(new DifferentDatabaseScope(conn)) { blog.Create(); } } using (new SessionScope()) { var blogs = Blog.FindAll().ToArray(); Assert.IsNotNull( blogs ); Assert.AreEqual( 1, Blog.Count()); var blogs2 = OtherDbBlog.FindAll().ToArray(); Assert.IsNotNull( blogs2 ); Assert.AreEqual( 1, OtherDbBlog.Count()); } }
private static void CreateBlog() { var blog = new Blog {Author = "Henry", Name = "Senseless"}; blog.Save(); }
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 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 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); }
public void UsingSessionAndTransactionScope() { ISession session1, session2; using (var conn = CreateSqlConnection2()) { conn.Open(); using (new SessionScope()) { using (new TransactionScope()) { var blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"}; blog.Save(); session1 = blog.GetCurrentSession(); Assert.IsNotNull(session1); Assert.IsNotNull(session1.Transaction); Assert.IsFalse(session1.Transaction.WasCommitted); Assert.IsFalse(session1.Transaction.WasRolledBack); using (new DifferentDatabaseScope(conn)) { blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"}; blog.Save(); session2 = blog.GetCurrentSession(); Assert.IsNotNull(session2); Assert.IsFalse(Object.ReferenceEquals(session1, session2)); Assert.IsNotNull(session2.Transaction); Assert.IsFalse(session2.Transaction.WasCommitted); Assert.IsFalse(session2.Transaction.WasRolledBack); } using (new DifferentDatabaseScope(conn)) { blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"}; blog.Save(); session2 = blog.GetCurrentSession(); Assert.IsNotNull(session2); Assert.IsFalse(Object.ReferenceEquals(session1, session2)); Assert.IsNotNull(session2.Transaction); Assert.IsFalse(session2.Transaction.WasCommitted); Assert.IsFalse(session2.Transaction.WasRolledBack); } } } } Assert.IsFalse(session1.IsOpen); Assert.IsFalse(session2.IsOpen); Assert.IsFalse(session1.Transaction.IsActive); Assert.IsFalse(session2.Transaction.IsActive); using (new SessionScope()) { var blogs = Blog.FindAll().ToArray(); Assert.IsNotNull(blogs); Assert.AreEqual(1, Blog.Count()); var blogs2 = OtherDbBlog.FindAll().ToArray(); Assert.IsNotNull(blogs2); Assert.AreEqual(2, OtherDbBlog.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); }
public void SequenceOfTransactions() { SqlConnection conn = CreateSqlConnection(); ISession session1, session2; ITransaction trans1, trans2; using(new SessionScope()) { using(TransactionScope scope = new TransactionScope()) { Blog blog = new Blog(); blog.Name = "hammett's blog"; blog.Author = "hamilton verissimo"; blog.Save(); session1 = blog.CurrentSession; Assert.IsNotNull(session1); trans1 = session1.Transaction; Assert.IsNotNull(trans1); Assert.IsFalse(trans1.WasCommitted); Assert.IsFalse(trans1.WasRolledBack); conn.Open(); using(new DifferentDatabaseScope(conn)) { blog = new Blog(); blog.Name = "hammett's blog"; blog.Author = "hamilton verissimo"; blog.Save(); session2 = blog.CurrentSession; Assert.IsNotNull(session2); trans2 = session2.Transaction; Assert.IsFalse( Object.ReferenceEquals(session1, session2) ); Assert.IsNotNull(session2.Transaction); Assert.IsFalse(session2.Transaction.WasCommitted); Assert.IsFalse(session2.Transaction.WasRolledBack); } using(new DifferentDatabaseScope(conn)) { blog = new Blog(); blog.Name = "hammett's blog"; blog.Author = "hamilton verissimo"; blog.Save(); session2 = blog.CurrentSession; Assert.IsNotNull(session2); Assert.IsFalse( Object.ReferenceEquals(session1, session2) ); Assert.IsNotNull(trans2); Assert.IsFalse(trans2.WasCommitted); Assert.IsFalse(trans2.WasRolledBack); } } conn.Close(); using(TransactionScope scope = new TransactionScope()) { Blog blog = new Blog(); blog.Name = "another blog"; blog.Author = "erico verissimo"; blog.Save(); } } Assert.IsFalse(session1.IsOpen); Assert.IsFalse(session2.IsOpen); Assert.IsTrue(trans1.WasCommitted); Assert.IsFalse(trans1.WasRolledBack); Assert.IsTrue(trans2.WasCommitted); Assert.IsFalse(trans2.WasRolledBack); Blog[] blogs = Blog.FindAll(); Assert.IsNotNull( blogs ); Assert.AreEqual( 2, blogs.Length ); OtherDbBlog[] blogs2 = OtherDbBlog.FindAll(); Assert.IsNotNull( blogs2 ); Assert.AreEqual( 2, blogs2.Length ); }
public void UsingSessionScope() { ISession session1, session2; using (var conn = CreateSqlConnection2()) { conn.Open(); using (new SessionScope()) { Blog blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"}; blog.Save(); session1 = blog.GetCurrentSession(); Assert.IsNotNull(session1); blog.Evict(); using (new DifferentDatabaseScope(conn)) { blog.Create(); session2 = blog.GetCurrentSession(); Assert.IsNotNull(session2); Assert.IsFalse(Object.ReferenceEquals(session1, session2)); } } } using (new SessionScope()) { var blogs = Blog.FindAll().ToArray(); Assert.IsNotNull( blogs ); Assert.AreEqual(1, Blog.Count()); var blogs2 = OtherDbBlog.FindAll().ToArray(); Assert.IsNotNull( blogs2 ); Assert.AreEqual(1, OtherDbBlog.Count()); } }
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"))); }
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); }
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 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)); }
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); }
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 ); }
public void SaveUpdate() { 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.Name = "Something else"; blog.Author = "changed too"; blog.Save(); 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 UsingTransactionScopeWithRollback() { SqlConnection conn = CreateSqlConnection2(); ISession session1, session2; ITransaction trans1, trans2; using(TransactionScope scope = new TransactionScope()) { Blog blog = new Blog {Name = "hammett's blog", Author = "hamilton verissimo"}; blog.Save(); session1 = blog.GetCurrentSession(); trans1 = blog.GetCurrentSession().Transaction; Assert.IsNotNull(session1); Assert.IsNotNull(session1.Transaction); Assert.IsFalse(session1.Transaction.WasCommitted); Assert.IsFalse(session1.Transaction.WasRolledBack); blog.Evict(); conn.Open(); using(new DifferentDatabaseScope(conn)) { blog.Create(); session2 = blog.GetCurrentSession(); trans2 = blog.GetCurrentSession().Transaction; Assert.IsNotNull(session2); Assert.IsFalse( Object.ReferenceEquals(session1, session2) ); Assert.IsNotNull(session2.Transaction); Assert.IsFalse(session2.Transaction.WasCommitted); Assert.IsFalse(session2.Transaction.WasRolledBack); scope.VoteRollBack(); } } Assert.IsFalse(session1.IsOpen); Assert.IsFalse(session2.IsOpen); Assert.IsFalse(trans1.WasCommitted); Assert.IsTrue(trans1.WasRolledBack); Assert.IsFalse(trans2.WasCommitted); Assert.IsTrue(trans2.WasRolledBack); using (new SessionScope()) { var blogs = Blog.FindAll().ToArray(); Assert.IsNotNull( blogs ); Assert.AreEqual(0, Blog.Count()); var blogs2 = OtherDbBlog.FindAll().ToArray(); Assert.IsNotNull( blogs2 ); Assert.AreEqual(0, OtherDbBlog.Count()); } }