Exemplo n.º 1
0
 public void ef_batch_delete_test()
 {
     var db    = new TrackerContext();
     int count = db.Tasks
                 .Where(t => t.Id > 10)
                 .Delete();
 }
Exemplo n.º 2
0
        public void ef_future_count_reverse_test()
        {
            var db = new TrackerContext();

            // build up queries

            string emailDomain = "@battlestar.com";
            var    q1          = db.Users
                                 .Where(p => p.EmailAddress.EndsWith(emailDomain))
                                 .Future();

            string search = "Summary";
            var    q2     = db.Tasks
                            .Where(t => t.Summary.Contains(search))
                            .FutureCount();

            // should be 2 queries
            //Assert.Equal(2, db.FutureQueries.Count);

            // access q2 first to trigger loading, testing loading from FutureCount
            // this triggers the loading of all the future queries
            var count = q2.Value;

            Assert.NotEqual(count, 0);

            // should be cleared at this point
            //Assert.Equal(0, db.FutureQueries.Count);

            // this should already be loaded
            Assert.True(((IFutureQuery)q1).IsLoaded);

            var users = q1.ToList();

            Assert.NotNull(users);
        }
Exemplo n.º 3
0
        public void ef_aduit_log_with_nullable_relations_test2()
        {
            var db    = new TrackerContext();
            var tran  = db.Database.BeginTransaction();
            var audit = db.BeginAudit();

            var task = new Task()
            {
                AssignedId   = 1,
                StatusId     = 1,
                Priority     = null,
                Summary      = "Summary: " + DateTime.Now.Ticks,
                CreatedId    = 1,
                CreatedDate  = DateTime.Now,
                ModifiedDate = DateTime.Now,
            };

            db.Tasks.Add(task);

            var entries = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added);

            //var relation = entries.First().RelationshipManager.GetRelatedReference<Priority>("Tracker.SqlServer.CodeFirst.Task_Priority", "Task_Priority_Target");
            //relation.Load();

            db.SaveChanges();

            foreach (var property in audit.LastLog.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }

            //undo work
            tran.Rollback();
        }
Exemplo n.º 4
0
        public void ef_aduit_create_log_loaded_test()
        {
            var db    = new TrackerContext();
            var audit = db.BeginAudit();

            var user = db.Users.Find(1);

            user.Comment = "Testing: " + DateTime.Now.Ticks;

            var newTask = new Task()
            {
                AssignedId = 1,
                CreatedId  = 1,
                StatusId   = 1,
                PriorityId = 2,
                Summary    = "Summary: " + DateTime.Now.Ticks
            };

            db.Tasks.Add(newTask);

            var p = db.Priorities.Find(2);

            var updateTask = db.Tasks.Find(2);

            updateTask.Priority = p;
            updateTask.StatusId = 2;
            updateTask.Summary  = "Summary: " + DateTime.Now.Ticks;

            var log = audit.CreateLog();

            Assert.NotNull(log);
        }
Exemplo n.º 5
0
        public void ef_batch_delete_nodata_test()
        {
            var db    = new TrackerContext();
            var today = DateTime.Now;

            db.Tasks.Where(t => t.CreatedDate > today).Delete();
        }
Exemplo n.º 6
0
        public void ef_aduit_create_log_test3()
        {
            var db    = new TrackerContext();
            var audit = db.BeginAudit();

            var user = new User();

            user.EmailAddress     = string.Format("email.{0}@test.com", DateTime.Now.Ticks);
            user.CreatedDate      = DateTime.Now;
            user.ModifiedDate     = DateTime.Now;
            user.PasswordHash     = DateTime.Now.Ticks.ToString();
            user.PasswordSalt     = "abcde";
            user.IsApproved       = false;
            user.LastActivityDate = DateTime.Now;

            db.Users.Add(user);

            var log = audit.CreateLog();

            Assert.NotNull(log);
            foreach (var property in log.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }
            db.SaveChanges();
            log.Refresh();
        }
Exemplo n.º 7
0
        public void ef_future_simple_test()
        {
            var db = new TrackerContext();

            // build up queries

            string emailDomain = "@battlestar.com";
            var    q1          = db.Users
                                 .Where(p => p.EmailAddress.EndsWith(emailDomain))
                                 .Future();

            string search = "Earth";
            var    q2     = db.Tasks
                            .Where(t => t.Summary.Contains(search))
                            .Future();

            // should be 2 queries
            //Assert.Equal(2, db.FutureQueries.Count);

            // this triggers the loading of all the future queries
            var users = q1.ToList();

            Assert.NotNull(users);

            // should be cleared at this point
            //Assert.Equal(0, db.FutureQueries.Count);

            // this should already be loaded
            Assert.True(((IFutureQuery)q2).IsLoaded);

            var tasks = q2.ToList();

            Assert.NotNull(tasks);
        }
Exemplo n.º 8
0
 public async void ef_batch_delete_async_test()
 {
     var db    = new TrackerContext();
     int count = await db.Tasks
                 .Where(t => t.Id > 10)
                 .DeleteAsync();
 }
Exemplo n.º 9
0
 public void ef_batch_update_test()
 {
     var db    = new TrackerContext();
     int count = db.Tasks
                 .Where(t => t.PriorityId == 2)
                 .Update(t => new Task {
         Summary = "Summary XXXXX: " + DateTime.Now.Ticks
     });
 }
Exemplo n.º 10
0
 public async void ef_batch_update_async_test()
 {
     var db    = new TrackerContext();
     int count = await db.Tasks
                 .Where(t => t.PriorityId == 2)
                 .UpdateAsync(t => new Task {
         Summary = "Summary YYYYY: " + DateTime.Now.Ticks
     });
 }
Exemplo n.º 11
0
 public void ef_batch_update_append_test()
 {
     var db    = new TrackerContext();
     int count = db.Tasks
                 .Where(t => t.PriorityId == 2)
                 .Update(t => new Task {
         Summary = t.Summary + " ZZZZZ"
     });
 }
Exemplo n.º 12
0
        public void ef_aduit_log_with_nullable_relations_test()
        {
            var db    = new TrackerContext();
            var tran  = db.Database.BeginTransaction();
            var audit = db.BeginAudit();

            var task = new Task()
            {
                AssignedId   = 1,
                StatusId     = 1,
                PriorityId   = 2,
                Summary      = "Summary: " + DateTime.Now.Ticks,
                CreatedId    = 1,
                CreatedDate  = DateTime.Now,
                ModifiedDate = DateTime.Now,
            };

            db.Tasks.Add(task);
            db.SaveChanges();

            foreach (var property in audit.LastLog.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }

            task.PriorityId = null;
            db.SaveChanges();

            foreach (var property in audit.LastLog.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }

            task.PriorityId = 1;
            db.SaveChanges();

            foreach (var property in audit.LastLog.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }

            task.PriorityId = 2;
            db.SaveChanges();

            foreach (var property in audit.LastLog.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }

            //undo work
            tran.Rollback();
        }
Exemplo n.º 13
0
        public void ef_aduit_create_log_format_test()
        {
            var db    = new TrackerContext();
            var audit = db.BeginAudit();

            var user = db.Users.Find(1);

            user.Comment = "Testing: " + DateTime.Now.Ticks;

            var task = new Task()
            {
                AssignedId = 1,
                CreatedId  = 1,
                StatusId   = 1,
                PriorityId = 2,
                Summary    = "Summary: " + DateTime.Now.Ticks
            };

            db.Tasks.Add(task);

            var task2 = db.Tasks.Where(p => p.Id == 2)
                        .Include(p => p.TaskExtended)
                        .Include(p => p.Audits)
                        .FirstOrDefault();

            task2.PriorityId = 2;
            task2.StatusId   = 2;
            task2.Summary    = "Summary: " + DateTime.Now.Ticks;
            task2.TaskExtended.ModifiedDate = DateTime.Now.AddDays(1);
            var auditList = task2.Audits.ToList();

            auditList.RemoveAt(0);

            auditList.AddRange(new List <AuditData>
            {
                new AuditData
                {
                    Content     = "1111",
                    CreatedDate = DateTime.Now,
                    Date        = DateTime.Now,
                    UserId      = 1,
                    Username    = "******"
                }
            });

            task2.Audits       = auditList;
            task2.TaskExtended = new TaskExtended
            {
                CreatedDate  = DateTime.Now,
                ModifiedDate = DateTime.Now
            };
            db.SaveChanges();
            var log = audit.LastLog;

            Assert.NotNull(log);
        }
Exemplo n.º 14
0
        public void ef_batch_update_copy_test()
        {
            var db = new TrackerContext();

            int count = db.Tasks
                        .Where(t => t.Id == 6)
                        .Update(t => new Task {
                Details = t.Summary
            });
        }
Exemplo n.º 15
0
        public void ef_aduit_maintain_across_save_test()
        {
            var db    = new TrackerContext();
            var tran  = db.Database.BeginTransaction();
            var audit = db.BeginAudit();

            var user = db.Users.Find(1);

            user.Comment = "Testing: " + DateTime.Now.Ticks;

            var task = new Task()
            {
                AssignedId   = 1,
                StatusId     = 1,
                PriorityId   = 2,
                Summary      = "Summary: " + DateTime.Now.Ticks,
                CreatedId    = 1,
                CreatedDate  = DateTime.Now,
                ModifiedDate = DateTime.Now
            };

            db.Tasks.Add(task);

            db.SaveChanges();

            Assert.NotNull(audit.LastLog);
            Assert.Equal(2, audit.LastLog.Entities.Count);


            var task2 = db.Tasks.Find(2);

            task2.PriorityId = 2;
            task2.StatusId   = 2;
            task2.Summary    = "Summary: " + DateTime.Now.Ticks;

            db.SaveChanges();

            Assert.NotNull(audit.LastLog);
            Assert.Equal(3, audit.LastLog.Entities.Count);

            var log = audit.LastLog;

            Assert.NotNull(log);



            foreach (var property in log.Entities.SelectMany(e => e.Properties))
            {
                Assert.NotEqual(property.Current, "{error}");
                Assert.NotEqual(property.Original, "{error}");
            }

            //undo work
            tran.Rollback();
        }
Exemplo n.º 16
0
        public void ef_batch_update_join_test()
        {
            var    db    = new TrackerContext();
            string space = " ";

            int count = db.Tasks
                        .Where(t => t.Id == 4)
                        .Update(t => new Task {
                Summary = t.Summary + space + t.Details
            });
        }
Exemplo n.º 17
0
 public void ef_batch_delete_havedata_test()
 {
     var db      = new TrackerContext();
     var taskIDs = new List <int>()
     {
         12212, 1212, 12121212
     };
     var today = DateTime.Now;
     int count = db.Tasks
                 .Where(t => t.CreatedDate > today && taskIDs.Contains(t.Id))
                 .Delete();
 }
Exemplo n.º 18
0
        public void ef_future_new_page_test()
        {
            var db = new TrackerContext();
            var q  = db.Tasks
                     .Where(p => p.PriorityId == 2)
                     .OrderByDescending(t => t.CreatedDate);
            var result = q.Paging();

            Assert.NotNull(result);
            Assert.Equal(20, result.Datas.Count());
            Assert.NotNull(result.Datas.First().Id);
        }
Exemplo n.º 19
0
        public void ef_mapping_test()
        {
            var db       = new TrackerContext();
            var resolver = new MetadataMappingProvider();

            var map = resolver.GetEntityMap(typeof(AuditData), db);

            Assert.Equal("[dbo].[AuditData]", map.TableFullName);

            resolver = new MetadataMappingProvider();

            map = resolver.GetEntityMap(typeof(AuditData), db);

            Assert.Equal("[dbo].[AuditData]", map.TableFullName);
        }
Exemplo n.º 20
0
 public void ef_batch_insert_test()
 {
     for (var i = 0; i <= 5; i++)
     {
         var taskList = GetTaskList();
         var watch    = Stopwatch.StartNew();
         using (var context = new TrackerContext())
         {
             context.BulkInsert(taskList);
             context.SaveChanges();
         }
         watch.Stop();
         var msgFormat = "Done, cost {0} milliseconds.";
         Console.WriteLine(string.Format(msgFormat, watch.ElapsedMilliseconds));
     }
 }
Exemplo n.º 21
0
        public void ef_batch_no_transaction_test()
        {
            using (var db = new TrackerContext())
            {
                string emailDomain = "@test1.com";

                int count = db.Users
                            .Where(u => u.EmailAddress.EndsWith(emailDomain))
                            .Update(u => new User {
                    LastActivityDate = DateTime.Now
                });

                count = db.Users
                        .Where(u => u.EmailAddress.EndsWith(emailDomain))
                        .Delete();
            }
        }
Exemplo n.º 22
0
        public void ef_aduit_create_log_test2()
        {
            var db    = new TrackerContext();
            var audit = db.BeginAudit();

            var task = db.Tasks.Find(2);

            Assert.NotNull(task);

            task.PriorityId = 2;
            task.StatusId   = 2;
            task.Summary    = "Summary: " + DateTime.Now.Ticks;

            var log = audit.CreateLog();

            Assert.NotNull(log);
        }
Exemplo n.º 23
0
        public void ef_batch_begin_transaction_test()
        {
            using (var db = new TrackerContext())
                using (var tx = db.Database.BeginTransaction())
                {
                    string emailDomain = "@test.com";

                    var query = db.Users.Where(u => u.EmailAddress.Contains(emailDomain));
                    int count = query.Update(u => new User {
                        IsApproved = false
                    });

                    count = db.Users
                            .Where(u => u.IsApproved == false)
                            .Delete();

                    tx.Commit();
                }
        }
Exemplo n.º 24
0
        public void ef_future_value_test()
        {
            var db = new TrackerContext();

            // build up queries
            string emailDomain = "@battlestar.com";
            var    q1          = db.Users
                                 .Where(p => p.EmailAddress.EndsWith(emailDomain))
                                 .FutureFirstOrDefault();

            string search = "Summary";
            var    q2     = db.Tasks
                            .Where(t => t.Summary.Contains(search))
                            .FutureCount();

            // duplicate query except count
            var q3 = db.Tasks
                     .Where(t => t.Summary.Contains(search))
                     .Future();

            // should be 3 queries
            //Assert.Equal(3, db.FutureQueries.Count);

            // this triggers the loading of all the future queries
            User user = q1;

            Assert.NotNull(user);

            // should be cleared at this point
            //Assert.Equal(0, db.FutureQueries.Count);

            // this should already be loaded
            Assert.True(((IFutureQuery)q2).IsLoaded);

            var count = q2.Value;

            Assert.NotEqual(count, 0);

            var tasks = q3.ToList();

            Assert.NotNull(tasks);
        }
Exemplo n.º 25
0
        public void ef_future_value_with_aggregate_test()
        {
            var db = new TrackerContext();

            var q1 = db.Users.Where(x => x.EmailAddress.EndsWith("@battlestar.com")).FutureValue(x => x.Count());
            var q2 = db.Users.Where(x => x.EmailAddress.EndsWith("@battlestar.com")).FutureValue(x => x.Min(t => t.LastName));
            var q3 = db.Tasks.FutureValue(x => x.Sum(t => t.Priority.Order));

            Assert.False(((IFutureQuery)q1).IsLoaded);
            Assert.False(((IFutureQuery)q2).IsLoaded);
            Assert.False(((IFutureQuery)q3).IsLoaded);

            var r1 = q1.Value;
            var r2 = q2.Value;
            var r3 = q3.Value;

            Assert.True(((IFutureQuery)q1).IsLoaded);
            Assert.True(((IFutureQuery)q2).IsLoaded);
            Assert.True(((IFutureQuery)q3).IsLoaded);
        }
Exemplo n.º 26
0
        public void ef_batch_transaction_scope_test()
        {
            using (var tx = new TransactionScope())
                using (var db = new TrackerContext())
                {
                    string emailDomain = "@test2.com";

                    int count = db.Users
                                .Where(u => u.EmailAddress.EndsWith(emailDomain))
                                .Update(u => new User {
                        IsApproved = false, LastActivityDate = DateTime.Now
                    });

                    count = db.Users
                            .Where(u => u.EmailAddress.EndsWith(emailDomain) && u.IsApproved == false)
                            .Delete();

                    tx.Complete();
                }
        }
Exemplo n.º 27
0
        public void ef_future_page_test()
        {
            var db = new TrackerContext();

            // base query
            var q = db.Tasks
                    .Where(p => p.PriorityId == 2)
                    .OrderByDescending(t => t.CreatedDate);
            int totalCount = 0;
            var result     = PagingTest(q, out totalCount);
            var aa         = result.ToList();

            // get total count
            var q1 = q.FutureCount();
            // get first page
            var q2 = q.Skip(0).Take(10).Future();
            // triggers sql execute as a batch
            var tasks = q2.ToList();
            int total = q1.Value;

            Assert.NotNull(tasks);
        }