public void CallCollectionClearSetsParentReferenceToNull()
 {
     Post p = new Post("Flaming");
     Comment c;
     p.Comments.Add(c = new Comment("Ashes"));
     p.Comments.Clear();
     Assert.AreEqual(0, p.Comments.Count);
     Assert.IsNull(c.Post);
 }
        public void AddCommentToPostSetsPostOnComment()
        {
            Post p = new Post("Flaming");
            p.Comments.Add(new Comment("Ashes"));

            Assert.AreEqual(p, p.Comments[0].Post);

            Comment c = new Comment("backwards");
            c.Post = p = new Post("yeah");
            Assert.AreEqual(1, p.Comments.Count);
            Assert.AreEqual(p, p.Comments[0].Post);
        }
        public void UseCascades()
        {
            Blog blog = new Blog("Ayende @ Blog");
            for (int i = 0; i < 59; i++)
            {
                Post post = new Post(i.ToString());
                blog.Posts.Add(post);
            }

            session.Save(blog);

            session.Dispose();

            session = factory.OpenSession();

            Blog fromDb = (Blog)session.Load(typeof(Blog), blog.BlogID);
            Assert.AreEqual(59, fromDb.Posts.Count);
        }
        public void AddBlogToPostAndSave()
        {
            Blog blog = new Blog("My Blog");
            //Need to save it first, so it would exist in the DB
            //when saving the post
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            Post postFromDB = (Post)session.Load(typeof(Post), post.PostId);

            Assert.AreEqual(blogFromDB, postFromDB.Blog);
            Assert.IsTrue(blogFromDB.Posts.Contains(postFromDB));
        }
        public void Setup()
        {
            blog = new Blog();
            post1 = new Post();
            post2 = new Post();
            post1.Blog = blog;
            post2.Blog = blog;

            session = factory.OpenSession();
            session.Save(blog);
            session.Save(post1);
            session.Save(post2);
            session.Dispose();

            session = factory.OpenSession();
        }
        public void LoadCollectionAndThenAddingToItAddsToInMemoryCollection()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            posts.Load();//Load the lazy collection.
            Assert.IsTrue(posts.IsInitialized);
            Post newPost = new Post("Second Post");
            blogFromDB.Posts.Add(newPost);
            Assert.IsTrue(blogFromDB.Posts.Contains(newPost));
            Assert.IsTrue(posts.IsInitialized);
        }
 public void LazyLoadingWhenAddingAPost()
 {
     Blog blog = new Blog("My Blog");
     //Need to save it first, so it would exist in the DB
     //when saving the post
     session.Save(blog);
     session.Flush();
     Post post = new Post("First Post");
     blog.Posts.Add(post);
     session.Save(post);
     session.Dispose();
     session = factory.OpenSession();
     Blog fromDb = (Blog)session.Load(typeof(Blog), blog.BlogID);
     AssertPostCollectionIsLazy(fromDb);
     Post newPost = new Post("Second Post");
     fromDb.Posts.Add(newPost);
     session.Save(newPost);
     AssertPostCollectionIsLazy(fromDb);
 }
        public void CantGetAddedItemsFromLazyLoadCollectionIfNotSavedToDB()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            Post newPost = new Post("Second Post");
            blogFromDB.Posts.Add(newPost);
            Assert.IsFalse(posts.IsInitialized);
            Assert.IsFalse(blogFromDB.Posts.Contains(newPost));
            Assert.IsTrue(posts.IsInitialized);
        }
        public void AssignBlogToPostPersistWhilePostsIsNotLoadedDoesNotLoad()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            Assert.IsFalse(posts.IsInitialized);
            Post newPost = new Post("Second Post");
            newPost.Blog = blogFromDB;
            Assert.IsFalse(posts.IsInitialized);
        }
        public void AddToPostsDoesNotLoadsThem()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Blog blogFromDB = (Blog)session.Load(typeof(Blog), blog.BlogID);
            EntitySet<Post> posts = (EntitySet<Post>)blogFromDB.Posts;
            Assert.IsFalse(posts.IsInitialized);
            Post newPost = new Post("Second Post");
            blogFromDB.Posts.Add(newPost);
            Assert.IsFalse(posts.IsInitialized);

            //Need to save before changes will appear
            session.Save(newPost);
            session.Flush();
            Assert.IsTrue(blogFromDB.Posts.Contains(newPost));
        }
        public void AddCommentToPost_SaveAndLoad()
        {
            Blog blog = new Blog("My Blog");
            session.Save(blog);
            session.Flush();
            Post post = new Post("First Post");
            blog.Posts.Add(post);
            post.Comments.Add(new Comment("First Comment"));
            post.Comments.Add(new Comment("Second Comment"));
            session.Save(post);

            session.Flush();
            session.Dispose();

            //Needs to create a new session so I would get the same instance
            session = factory.OpenSession();

            Post fromDb = (Post)session.Load(typeof(Post), post.PostId);
            Assert.AreEqual(2, fromDb.Comments.Count);

            Assert.AreEqual("First Comment", fromDb.Comments[0].Text);
            Assert.AreEqual("Second Comment", fromDb.Comments[1].Text);
        }
        public void CallingRemoveAtWillCallDelegate()
        {
            Post p = new Post("Flaming");
            Comment c = new Comment("Ashes");
            p.Comments.Add(c);

            p.Comments.RemoveAt(0);

            Assert.IsNull(c.Post);
        }
        public void ModifyCollectionsUsingInsertAtPositionAllowsDoubling()
        {
            Post p = new Post("q");
            Comment c = new Comment("a");

            p.Comments.Add(c);
            p.Comments.Insert(0,c);
            p.Comments.Add(c);

            Assert.AreEqual(2, p.Comments.Count);
        }
        public void DoubleAddOfSameInstanceDoesNothing()
        {
            /* I have modified EntityList.DoAdd with this to prevent adding the same item twice
             *

            if (_list.Contains(item))
                return false;
            else
            {
                _list.Add(item);
                return true;
            }
             */
            Post p = new Post("1");
            Comment c;

            p.Comments.Add(new Comment("c1"));
            Assert.AreEqual(1, p.Comments.Count);

            p.Comments.Add(c = new Comment("c2"));
            Assert.AreEqual(2, p.Comments.Count);

            p.Comments.Add(c);
            Assert.AreEqual(2, p.Comments.Count, "Double test add failed");
        }
 public int CreateAPost()
 {
     CloseDisposeOpen();
     Post p = new Post("test");
     p.Comments.Add(new Comment("dah"));
     session.Save(p);
     CloseDisposeOpen();
     return p.PostId;
 }