Example #1
0
        public void CreateLogLoaded()
        {
            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);
        }
Example #2
0
 public async void ef_extension_batch_delete_async_test()
 {
     var db    = new TrackerContext();
     int count = await db.Tasks
                 .Where(t => t.Id > 10)
                 .DeleteAsync();
 }
Example #3
0
        public void CreateLog3()
        {
            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();
        }
Example #4
0
        public void LogWithNullableRelationWithoutValueAndAllreadyLoadedRelation()
        {
            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();
        }
Example #5
0
 public void ef_extension_batch_delete_test()
 {
     var db    = new TrackerContext();
     int count = db.Tasks
                 .Where(t => t.Id > 10)
                 .Delete();
 }
Example #6
0
        public void FutureCountTest()
        {
            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);

            // 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);

            int count = q2;

            Assert.NotEqual(count, 0);
        }
Example #7
0
 public void ef_extension_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"
     });
 }
Example #8
0
 public void ef_extension_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
     });
 }
Example #9
0
 public async void ef_extension_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
     });
 }
Example #10
0
        public void ef_extension_mapping_test()
        {
            var db       = new TrackerContext();
            var resolver = new MetadataMappingProvider();

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

            Assert.Equal("[dbo].[Audit]", map.TableFullName);
        }
Example #11
0
        public void LogWithNullableRelations()
        {
            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();
        }
Example #12
0
        public void ef_extension_batch_update_copy_test()
        {
            var db = new TrackerContext();

            int count = db.Tasks
                        .Where(t => t.Id == 6)
                        .Update(t => new Task {
                Details = t.Summary
            });
        }
Example #13
0
        public void ef_extension_batch_update_append_and_null_test()
        {
            var db = new TrackerContext();

            db.Tasks
            .Where(t => t.Id == 5)
            .Update(t => new Task {
                Details = null, StartDate = DateTime.Now
            });
        }
Example #14
0
        public void MaintainAcrossSaves()
        {
            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();
        }
Example #15
0
        public void CreateLogFormatWith()
        {
            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;

            var auditList = task2.Audits.ToList();

            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);
        }
Example #16
0
        public void ef_extension_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
            });
        }
Example #17
0
        public void ef_extension_batch_insert_test()
        {
            var taskList = GetTaskList();
            var watch    = Stopwatch.StartNew();

            using (var context = new TrackerContext())
            {
                context.InsertBatch(taskList);
                context.SaveChanges();
            }
            watch.Stop();
            var msgFormat = "Done, cost {0} milliseconds.";

            Console.WriteLine(string.Format(msgFormat, watch.ElapsedMilliseconds));
        }
Example #18
0
        public void CreateLog2()
        {
            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);
        }
Example #19
0
        public void FutureValueReverseTest()
        {
            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);

            // 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.Value;

            Assert.NotNull(users);

            var tasks = q3.ToList();

            Assert.NotNull(tasks);
        }
Example #20
0
        public void PageTest()
        {
            var db = new TrackerContext();

            // base query
            var q = db.Tasks
                    .Where(p => p.PriorityId == 2)
                    .OrderByDescending(t => t.CreatedDate);

            // 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);
        }
Example #21
0
        public void NoTransactionObjectContext()
        {
            using (var db = new TrackerContext())
            {
                string emailDomain = "@test.com";

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

                count = db.Tasks
                        .Where(u => u.Id == 1)
                        .Update(t => new Task {
                    Summary = "YYY"
                });
            }
        }
Example #22
0
        public void FutureValueWithAggregateFunctions()
        {
            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);
        }
Example #23
0
        public void BeginTransactionObjectContext()
        {
            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, LastActivityDate = DateTime.Now
                    });

                    count = db.Tasks
                            .Where(u => u.Id == 1)
                            .Update(t => new Task {
                        Summary = "XXX"
                    });

                    tx.Commit();
                }
        }
Example #24
0
        public void CompareXml()
        {
            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.Find(2);

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

            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}");
            }
        }