|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using EntityFramework.Utilities; |
| 4 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 5 | +using Tests.FakeDomain; |
| 6 | +using Tests.FakeDomain.Models; |
| 7 | + |
| 8 | +namespace Tests |
| 9 | +{ |
| 10 | + [TestClass] |
| 11 | + public class UpdateBulkTests |
| 12 | + { |
| 13 | + [TestMethod] |
| 14 | + public void UpdateBulk_UpdatesAll() |
| 15 | + { |
| 16 | + Setup(); |
| 17 | + |
| 18 | + using (var db = Context.Sql()) |
| 19 | + { |
| 20 | + var posts = db.BlogPosts.ToList(); |
| 21 | + |
| 22 | + Assert.AreEqual(3, db.BlogPosts.Count()); |
| 23 | + } |
| 24 | + |
| 25 | + using (var db = Context.Sql()) |
| 26 | + { |
| 27 | + Assert.AreEqual(3, db.BlogPosts.Count()); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + private static void Setup() |
| 32 | + { |
| 33 | + using (var db = Context.Sql()) |
| 34 | + { |
| 35 | + if (db.Database.Exists()) |
| 36 | + { |
| 37 | + db.Database.Delete(); |
| 38 | + } |
| 39 | + db.Database.Create(); |
| 40 | + |
| 41 | + var list = new List<BlogPost>(){ |
| 42 | + BlogPost.Create("T1"), |
| 43 | + BlogPost.Create("T2"), |
| 44 | + BlogPost.Create("T3") |
| 45 | + }; |
| 46 | + |
| 47 | + EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [TestMethod] |
| 52 | + public void InsertAll_WrongColumnOrder_InsertsItems() |
| 53 | + { |
| 54 | + using (var db = new ReorderedContext()) |
| 55 | + { |
| 56 | + if (db.Database.Exists()) |
| 57 | + { |
| 58 | + db.Database.Delete(); |
| 59 | + } |
| 60 | + db.Database.Create(); |
| 61 | + } |
| 62 | + |
| 63 | + using (var db = Context.Sql()) |
| 64 | + { |
| 65 | + |
| 66 | + var list = new List<BlogPost>(){ |
| 67 | + BlogPost.Create("T1"), |
| 68 | + BlogPost.Create("T2"), |
| 69 | + BlogPost.Create("T3") |
| 70 | + }; |
| 71 | + |
| 72 | + EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); |
| 73 | + } |
| 74 | + |
| 75 | + using (var db = Context.Sql()) |
| 76 | + { |
| 77 | + Assert.AreEqual(3, db.BlogPosts.Count()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + [TestMethod] |
| 82 | + public void InsertAll_WrongColumnOrderAndRenamedColumn_InsertsItems() |
| 83 | + { |
| 84 | + using (var db = new RenamedAndReorderedContext()) |
| 85 | + { |
| 86 | + if (db.Database.Exists()) |
| 87 | + { |
| 88 | + db.Database.Delete(); |
| 89 | + } |
| 90 | + db.Database.Create(); |
| 91 | + db.Database.ExecuteSqlCommand("drop table dbo.RenamedAndReorderedBlogPosts;"); |
| 92 | + db.Database.ExecuteSqlCommand(RenamedAndReorderedBlogPost.CreateTableSql()); |
| 93 | + } |
| 94 | + |
| 95 | + using (var db = new RenamedAndReorderedContext()) |
| 96 | + { |
| 97 | + |
| 98 | + var list = new List<RenamedAndReorderedBlogPost>(){ |
| 99 | + RenamedAndReorderedBlogPost.Create("T1"), |
| 100 | + RenamedAndReorderedBlogPost.Create("T2"), |
| 101 | + RenamedAndReorderedBlogPost.Create("T3") |
| 102 | + }; |
| 103 | + |
| 104 | + EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); |
| 105 | + } |
| 106 | + |
| 107 | + using (var db = new RenamedAndReorderedContext()) |
| 108 | + { |
| 109 | + Assert.AreEqual(3, db.BlogPosts.Count()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + [TestMethod] |
| 114 | + public void InsertAll_NoProvider_UsesDefaultInsert() |
| 115 | + { |
| 116 | + string fallbackText = null; |
| 117 | + |
| 118 | + Configuration.Log = str => fallbackText = str; |
| 119 | + |
| 120 | + using (var db = Context.SqlCe()) |
| 121 | + { |
| 122 | + if (db.Database.Exists()) |
| 123 | + { |
| 124 | + db.Database.Delete(); |
| 125 | + } |
| 126 | + db.Database.Create(); |
| 127 | + } |
| 128 | + |
| 129 | + var list = new List<BlogPost>(){ |
| 130 | + BlogPost.Create("T1"), |
| 131 | + BlogPost.Create("T2"), |
| 132 | + BlogPost.Create("T3") |
| 133 | + }; |
| 134 | + |
| 135 | + using (var db = Context.SqlCe()) |
| 136 | + { |
| 137 | + EFBatchOperation.For(db, db.BlogPosts).InsertAll(list); |
| 138 | + } |
| 139 | + |
| 140 | + using (var db = Context.SqlCe()) |
| 141 | + { |
| 142 | + Assert.AreEqual(3, db.BlogPosts.Count()); |
| 143 | + } |
| 144 | + |
| 145 | + Assert.IsNotNull(fallbackText); |
| 146 | + } |
| 147 | + |
| 148 | + |
| 149 | + [TestMethod] |
| 150 | + public void InsertAll_WithForeignKey() |
| 151 | + { |
| 152 | + int postId = -1; |
| 153 | + using (var db = Context.Sql()) |
| 154 | + { |
| 155 | + if (db.Database.Exists()) |
| 156 | + { |
| 157 | + db.Database.Delete(); |
| 158 | + } |
| 159 | + db.Database.Create(); |
| 160 | + |
| 161 | + var bp = BlogPost.Create("B1"); |
| 162 | + db.BlogPosts.Add(bp); |
| 163 | + db.SaveChanges(); |
| 164 | + postId = bp.ID; |
| 165 | + |
| 166 | + var comments = new List<Comment>(){ |
| 167 | + new Comment{Text = "C1", PostId = bp.ID }, |
| 168 | + new Comment{Text = "C2", PostId = bp.ID }, |
| 169 | + }; |
| 170 | + |
| 171 | + EFBatchOperation.For(db, db.Comments).InsertAll(comments); |
| 172 | + } |
| 173 | + |
| 174 | + using (var db = Context.Sql()) |
| 175 | + { |
| 176 | + Assert.AreEqual(2, db.Comments.Count()); |
| 177 | + Assert.AreEqual(2, db.Comments.Count(c => c.PostId == postId)); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments