public void NonGenericPrimaryKeyGetterWorks() { var map = new Map<Post>(); map.Columns.Add("PostId", new Column<int> { IsPrimaryKey = true, Map = map, Name = "PostId" }); map.PrimaryKey = map.Columns["PostId"]; var post = new Post { PostId = 123 }; Assert.Equal(post.PostId, map.GetPrimaryKeyValue((object)post)); }
public void SingleDeleteWorks() { var deleteWriter = new DeleteWriter(new SqlServerDialect(), MakeConfig()); var post = new Post { PostId = 1 }; var result = deleteWriter.GenerateSql(new[] { post }); Debug.Write(result.Sql); Assert.Equal("delete from [Posts] where [PostId] in (@p_1)", result.Sql); }
public void SingleFetchWithNullWorks() { var func = GenerateSingleMapper(); var post = new Post { PostId = 1 }; Blog blog = null; var result = ((Func<object[], Post>)func)(new object[] { post, blog }); Assert.Null(post.Blog); }
public void EnableTrackingCalledLast() { var func = GenerateSingleMapper(); var post1 = new Post { PostId = 1 }; var blog1 = new Blog { BlogId = 2 }; var post = ((Func<object[], Post>)func)(new object[] { post1, blog1 }); Assert.False(((ITrackedEntity)post).GetDirtyProperties().Any()); }
public void SetPrimaryKeyValueWorks() { var map = new Map<Post>(); map.Columns.Add("PostId", new Column<int> { IsPrimaryKey = true, Map = map, Name = "PostId" }); map.PrimaryKey = map.Columns["PostId"]; var post = new Post { PostId = 123 }; map.SetPrimaryKeyValue(post, 256); Assert.Equal(256, post.PostId); }
public void FetchedEntitiesHaveTrackingEnabled() { var func = GenerateSingleMapper(); var post = new Post { PostId = 1 }; var blog = new Blog { BlogId = 2 }; var result = ((Func<object[], Post>)func)(new object[] { post, blog }); Assert.True(((ITrackedEntity)result).IsTrackingEnabled()); Assert.True(((ITrackedEntity)result.Blog).IsTrackingEnabled()); }
public void MultiFetchNoCollectionWorks() { var func = GenerateMultipleNoCollectionMapper(); var comment = new Comment(); var post = new Post { PostId = 1 }; var author = new User { UserId = 3 }; var resultComment = ((Func<object[], Comment>)func)(new object[] { comment, post, author }); Assert.Equal(3, resultComment.Post.Author.UserId); }
public void SingleFetchWorks() { var func = GenerateSingleMapper(); var post1 = new Post { PostId = 1 }; var blog1 = new Blog { BlogId = 2 }; var post = ((Func<object[], Post>)func)(new object[] { post1, blog1 }); Assert.Equal(2, post.Blog.BlogId); }
public void SimpleInsertWorks() { var insertWriter = new InsertWriter(new SqlServerDialect(), MakeConfig()); var post = new Post { PostId = 1, Title = "Boo", Rating = 11 }; var result = insertWriter.GenerateSql(post); Debug.Write(result.Sql); Assert.Equal( "insert into [Posts] ([AuthorId], [BlogId], [Content], [DoNotMap], [Rating], [Title]) values (@p_1, @p_2, @p_3, @p_4, @p_5, @p_6)", result.Sql); }
public void MultiFetchNoCollectionHasTrackingEnabled() { var func = GenerateMultipleNoCollectionMapper(); var comment = new Comment(); var post = new Post { PostId = 1 }; var author = new User { UserId = 3 }; var resultComment = ((Func<object[], Comment>)func)(new object[] { comment, post, author }); Assert.True(((ITrackedEntity)resultComment).IsTrackingEnabled()); Assert.True(((ITrackedEntity)resultComment.Post).IsTrackingEnabled()); Assert.True(((ITrackedEntity)resultComment.Post.Author).IsTrackingEnabled()); }
public void UpdateSinglePropertyWorks() { var post = new Post(); post.PostId = 1; post.Title = "Boo"; ((ITrackedEntity)post).EnableTracking(); post.Title = "New Boo"; var updateWriter = new UpdateWriter(new SqlServerDialect(), MakeConfig()); var result = updateWriter.GenerateSql(new[] { post }); Debug.Write(result.Sql); Assert.Equal("update [Posts] set [Title] = @p_1 where [PostId] = @p_2;", result.Sql); }
public void SingleRowCollectionTestForJames() { var funcFac = GenerateSingleMapperWithFetch(); var post1 = new Post { PostId = 1 }; var comment1 = new Comment { CommentId = 1 }; var blog1 = new Blog { BlogId = 1 }; Post currentRoot = null; IList<Post> results = new List<Post>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { post1, blog1, comment1 }); Assert.Equal(1, results[0].Comments.First().CommentId); Assert.Equal(1, results[0].Blog.BlogId); }
public void UpdateTwoPropertiesWorks() { var target = MakeTarget(); var post = new Post(); post.PostId = 1; ((ITrackedEntity)post).EnableTracking(); post.Title = "New Boo"; post.Content = "New Content"; // act var result = target.GenerateSql(new[] { post }); // assert Assert.Equal("update [Posts] set [Title] = @p_1, [Content] = @p_2 where [PostId] = @p_3;", result.Sql); }
public void SingleCollectionWorks() { var funcFac = GenerateSingleMapper(); var post1 = new Post { PostId = 1 }; var post2 = new Post { PostId = 2 }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; Post currentRoot = null; IList<Post> results = new List<Post>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { post1, comment1 }); func(new object[] { post1, comment2 }); func(new object[] { post2, comment3 }); Assert.Equal(1, results[0].Comments.First().CommentId); Assert.Equal(2, results[0].Comments.Last().CommentId); Assert.Equal(3, results[1].Comments.First().CommentId); }
public void SingleCollectionHasTrackingEnabledLast() { var funcFac = GenerateSingleMapper(); var post1 = new Post { PostId = 1 }; var post2 = new Post { PostId = 2 }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; Post currentRoot = null; IList<Post> results = new List<Post>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { post1, comment1 }); func(new object[] { post1, comment2 }); func(new object[] { post2, comment3 }); Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Comments.First()).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Comments.ElementAt(1)).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[1]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[1].Comments.First()).GetDirtyProperties().Any()); }
public void UpdateManyToOneProperty() { // assemble var post = new Post(); post.PostId = 1; post.Blog = new Blog { BlogId = 1 }; ((ITrackedEntity)post).EnableTracking(); post.Blog = new Blog { BlogId = 2 }; var updateWriter = new UpdateWriter(new SqlServerDialect(), MakeConfig()); // act var result = updateWriter.GenerateSql(new[] { post }); // assert Debug.Write(result.Sql); Assert.Equal("update [Posts] set [BlogId] = @p_1 where [PostId] = @p_2;", result.Sql); // Is this the correct result? var param1 = result.Parameters.GetValueOfParameter("@p_1"); var param2 = result.Parameters.GetValueOfParameter("@p_2"); Assert.IsType(typeof(int), param1); Assert.IsType(typeof(int), param2); }
public void ThenFetchWorksTrackingEnabledLast() { var funcFac = GenerateThenFetchMapper(); var post1 = new Post { PostId = 1 }; var post2 = new Post { PostId = 2 }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; Post currentRoot = null; IList<Post> results = new List<Post>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { post1, comment1, user1 }); func(new object[] { post1, comment2, user2 }); func(new object[] { post2, comment3, user1 }); Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Comments.First()).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Comments.First().User).GetDirtyProperties().Any()); }
public void FetchManyNonRootWorks() { var config = new CustomConfig(); var selectQuery = new SelectQuery<PostTag>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as SelectQuery<PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper<PostTag>(result.FetchTree).Item1; // setup the scenario var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; var user3 = new User { UserId = 3 }; var user4 = new User { UserId = 4 }; var user5 = new User { UserId = 5 }; PostTag currentRoot = null; IList<PostTag> results = new List<PostTag>(); var func = (Func<object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1, user1 }); func(new object[] { tag1, post1, comment2, user1 }); func(new object[] { tag1, post1, comment3, user2 }); func(new object[] { tag2, anotherPost1, comment1, user1 }); func(new object[] { tag2, anotherPost1, comment2, user1 }); func(new object[] { tag2, anotherPost1, comment3, user2 }); func(new object[] { tag3, post2, comment4, user3 }); func(new object[] { tag3, post2, comment5, user4 }); func(new object[] { tag3, post2, comment6, user5 }); Assert.Equal(3, results.Count); Assert.Equal(3, results.First().Post.Comments.Count); Assert.Equal(1, results.First().Post.PostId); Assert.Equal(1, results.First().Post.Comments.First().User.UserId); Assert.Equal(1, results.First().Post.Comments.ElementAt(1).User.UserId); Assert.Equal(2, results.First().Post.Comments.ElementAt(2).User.UserId); Assert.Equal(3, results.ElementAt(1).Post.Comments.Count); Assert.Equal(1, results.ElementAt(1).Post.PostId); Assert.Equal(1, results.ElementAt(1).Post.Comments.First().User.UserId); Assert.Equal(1, results.ElementAt(1).Post.Comments.ElementAt(1).User.UserId); Assert.Equal(2, results.ElementAt(1).Post.Comments.ElementAt(2).User.UserId); Assert.Equal(3, results.ElementAt(2).Post.Comments.Count); Assert.Equal(2, results.ElementAt(2).Post.PostId); Assert.Equal(4, results.ElementAt(2).Post.Comments.First().CommentId); Assert.Equal(5, results.ElementAt(2).Post.Comments.ElementAt(1).CommentId); Assert.Equal(6, results.ElementAt(2).Post.Comments.ElementAt(2).CommentId); }
public void FetchManyNonRootTrackingEnabledLast() { var config = new CustomConfig(); var selectQuery = new SelectQuery<PostTag>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Post.Comments).ThenFetch(c => c.User) as SelectQuery<PostTag>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new SingleCollectionMapperGenerator(config); var funcFac = mapper.GenerateCollectionMapper<PostTag>(result.FetchTree).Item1; // setup the scenario var tag1 = new PostTag { PostTagId = 1 }; var tag2 = new PostTag { PostTagId = 2 }; var tag3 = new PostTag { PostTagId = 3 }; var post1 = new Post { PostId = 1, Title = "Foo" }; var anotherPost1 = new Post { PostId = 1, Title = "Foo" }; var post2 = new Post { PostId = 2, Title = "Foo" }; var post3 = new Post { PostId = 3, Title = "Foo" }; var post4 = new Post { PostId = 4, Title = "Foo" }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var comment4 = new Comment { CommentId = 4 }; var comment5 = new Comment { CommentId = 5 }; var comment6 = new Comment { CommentId = 6 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; var user3 = new User { UserId = 3 }; var user4 = new User { UserId = 4 }; var user5 = new User { UserId = 5 }; PostTag currentRoot = null; IList<PostTag> results = new List<PostTag>(); var func = (Func<object[], PostTag>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { tag1, post1, comment1, user1 }); func(new object[] { tag1, post1, comment2, user1 }); func(new object[] { tag1, post1, comment3, user2 }); func(new object[] { tag2, anotherPost1, comment1, user1 }); func(new object[] { tag2, anotherPost1, comment2, user1 }); func(new object[] { tag2, anotherPost1, comment3, user2 }); func(new object[] { tag3, post2, comment4, user3 }); func(new object[] { tag3, post2, comment5, user4 }); func(new object[] { tag3, post2, comment6, user5 }); Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post.Comments[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Post.Comments[0].User).GetDirtyProperties().Any()); }
public void MultipleManyToManyFetchingWorks() { // setup the factory var config = new CustomConfig(); var selectQuery = new SelectQuery<Post>(new Mock<ISelectQueryExecutor>().Object).FetchMany(p => p.Tags) .ThenFetch(p => p.ElTag) .FetchMany(p => p.DeletedTags) .ThenFetch(t => t.ElTag) as SelectQuery<Post>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new MultiCollectionMapperGenerator(config); var funcFac = mapper.GenerateMultiCollectionMapper<Post>(result.FetchTree).Item1; // setup the scenario var post1 = new Post { PostId = 1 }; var tag1 = new Tag { TagId = 1 }; var tag2 = new Tag { TagId = 2 }; var tag3 = new Tag { TagId = 3 }; var postTag1 = new PostTag { PostTagId = 1 }; var postTag2 = new PostTag { PostTagId = 2 }; var postTag3 = new PostTag { PostTagId = 3 }; // act Post currentRoot = null; IList<Post> results = new List<Post>(); var dict0 = new Dictionary<int, PostTag>(); var hashsetPair0 = new HashSet<Tuple<int, int>>(); var dict1 = new Dictionary<int, PostTag>(); var hashsetPair1 = new HashSet<Tuple<int, int>>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results, dict0, hashsetPair0, dict1, hashsetPair1); func(new object[] { post1, postTag2, tag2, postTag1, tag1 }); func(new object[] { post1, postTag3, tag3, postTag1, tag1 }); Assert.Equal(1, results.Count); Assert.Equal(1, results[0].Tags.Count); Assert.Equal(1, results[0].Tags[0].PostTagId); Assert.Equal(2, results[0].DeletedTags.Count); Assert.Equal(2, results[0].DeletedTags[0].PostTagId); Assert.Equal(3, results[0].DeletedTags[1].PostTagId); }
public void NestedMultipleOneToManyFetchingWorksTrackingEnabledLast() { // setup the factory var config = new CustomConfig(); var selectQuery = new SelectQuery<Blog>(new Mock<ISelectQueryExecutor>().Object).FetchMany(b => b.Posts) .ThenFetchMany(p => p.Tags) .ThenFetch(t => t.ElTag) .FetchMany(b => b.Posts) .ThenFetchMany(p => p.DeletedTags) .ThenFetch(t => t.ElTag) .FetchMany(p => p.Posts) .ThenFetch(p => p.Author) as SelectQuery<Blog>; var writer = new SelectWriter(new SqlServer2012Dialect(), config); var result = writer.GenerateSql(selectQuery); var mapper = new MultiCollectionMapperGenerator(config); var funcFac = mapper.GenerateMultiCollectionMapper<Blog>(result.FetchTree).Item1; // setup the scenario var blog1 = new Blog { BlogId = 1 }; var blog2 = new Blog { BlogId = 2 }; var post1 = new Post { PostId = 1 }; var post2 = new Post { PostId = 2 }; var post3 = new Post { PostId = 3 }; var posttag1 = new PostTag { PostTagId = 1 }; var posttag2 = new PostTag { PostTagId = 2 }; var posttag3 = new PostTag { PostTagId = 3 }; var tag1 = new Tag { TagId = 1 }; var tag2 = new Tag { TagId = 2 }; var tag3 = new Tag { TagId = 3 }; var tag4 = new Tag { TagId = 4 }; var delPostTag1 = new PostTag { PostTagId = 3 }; var delPostTag2 = new PostTag { PostTagId = 4 }; var delPostTag3 = new PostTag { PostTagId = 5 }; var author1 = new User { UserId = 1 }; var author2 = new User { UserId = 2 }; // act Blog currentRoot = null; IList<Blog> results = new List<Blog>(); var dict0 = new Dictionary<int, Post>(); var hashsetPair0 = new HashSet<Tuple<int, int>>(); var dict1 = new Dictionary<int, PostTag>(); var hashsetPair1 = new HashSet<Tuple<int, int>>(); var dict2 = new Dictionary<int, PostTag>(); var hashsetPair2 = new HashSet<Tuple<int, int>>(); var func = (Func<object[], Blog>)funcFac.DynamicInvoke(currentRoot, results, dict0, hashsetPair0, dict1, hashsetPair1, dict2, hashsetPair2); func(new object[] { blog1, post1, author1, null, null, posttag1, tag1 }); func(new object[] { blog1, post1, author1, null, null, posttag2, tag2 }); func(new object[] { blog1, post2, author2, delPostTag1, tag3, null, null }); func(new object[] { blog1, post2, author2, delPostTag2, tag4, null, null }); func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag1, tag1 }); func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag1, tag1 }); func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag1, tag1 }); func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag2, tag2 }); func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag2, tag2 }); func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag2, tag2 }); func(new object[] { blog2, post3, author1, delPostTag1, tag3, posttag3, tag3 }); func(new object[] { blog2, post3, author1, delPostTag2, tag4, posttag3, tag3 }); func(new object[] { blog2, post3, author1, delPostTag3, tag4, posttag3, tag3 }); Assert.False(((ITrackedEntity)results[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Posts[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Posts[0].Author).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Posts[0].Tags[0]).GetDirtyProperties().Any()); Assert.False(((ITrackedEntity)results[0].Posts[0].Tags[0].ElTag).GetDirtyProperties().Any()); }
public void UsePrimaryKeySetterWithoutPrimaryKeyColumnThrow() { var map = new Map<Post>(); map.Columns.Add("PostId", new Column<int> { IsPrimaryKey = true, Map = map, Name = "PostId" }); var post = new Post { PostId = 123 }; Assert.Throws<Exception>(() => map.SetPrimaryKeyValue(post, 123)); }
public void UpdateDetachedEntityWorks() { // assemble var post = new Post(); post.PostId = 1; post.Title = "Boo"; var updateWriter = MakeTarget(); // act var result = updateWriter.GenerateSql(new[] { post }); // assert Debug.Write(result.Sql); Assert.Equal( "update [Posts] set [Title] = @p_1, [Content] = @p_2, [Rating] = @p_3, [AuthorId] = @p_4, [BlogId] = @p_5 where [PostId] = @p_6;", result.Sql); }
public void ThenFetchWorks() { var funcFac = GenerateThenFetchMapper(); var post1 = new Post { PostId = 1 }; var post2 = new Post { PostId = 2 }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var user1 = new User { UserId = 1 }; var user2 = new User { UserId = 2 }; Post currentRoot = null; IList<Post> results = new List<Post>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results); func(new object[] { post1, comment1, user1 }); func(new object[] { post1, comment2, user2 }); func(new object[] { post2, comment3, user1 }); Assert.Equal(1, results[0].Comments.First().User.UserId); Assert.Equal(2, results[0].Comments.Last().User.UserId); Assert.Equal(1, results[1].Comments.First().User.UserId); }
public void MultiCollectionWorks() { var funcFac = GenerateMultiMapper(); var post1 = new Post { PostId = 1 }; var post2 = new Post { PostId = 2 }; var comment1 = new Comment { CommentId = 1 }; var comment2 = new Comment { CommentId = 2 }; var comment3 = new Comment { CommentId = 3 }; var postTag1 = new PostTag { PostTagId = 1 }; var postTag2 = new PostTag { PostTagId = 2 }; var postTag3 = new PostTag { PostTagId = 3 }; Post currentRoot = null; IList<Post> results = new List<Post>(); IDictionary<int, Comment> dict0 = new Dictionary<int, Comment>(); var hashsetPair0 = new HashSet<Tuple<int, int>>(); IDictionary<int, PostTag> dict1 = new Dictionary<int, PostTag>(); var hashsetPair1 = new HashSet<Tuple<int, int>>(); var func = (Func<object[], Post>)funcFac.DynamicInvoke(currentRoot, results, dict0, hashsetPair0, dict1, hashsetPair1); func(new object[] { post1, comment1, postTag1 }); func(new object[] { post1, comment2, postTag1 }); func(new object[] { post2, comment3, postTag2 }); func(new object[] { post2, comment3, postTag3 }); Assert.Equal(1, results[0].Comments.First().CommentId); Assert.Equal(2, results[0].Comments.Last().CommentId); Assert.Equal(2, results[0].Comments.Count); Assert.Equal(3, results[1].Comments.First().CommentId); Assert.Equal(1, results[1].Comments.Count); Assert.Equal(1, results[0].Tags.First().PostTagId); Assert.Equal(1, results[0].Tags.Count); Assert.Equal(2, results[1].Tags.First().PostTagId); Assert.Equal(3, results[1].Tags.Last().PostTagId); Assert.Equal(2, results[1].Tags.Count); }