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 = "Earth";
            var q2 = db.Tasks
                .Where(t => t.Summary.Contains(search))
                .FutureCount();

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

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

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

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

            int count = q2;
            Assert.AreNotEqual(count, 0);
        }
Beispiel #2
0
 public void Delete()
 {
     var db = new TrackerContext();
     string emailDomain = "@test.com";
     int count = db.Users
         .Delete(u => u.EmailAddress.EndsWith(emailDomain));
 }
        public void TestCacheStrategy3()
        {
            using (var db = new TrackerContext())
            {
                var role1 = CreateRole("Joe 1");
                var role2 = CreateRole("Joe 2");
                var role3 = CreateRole("Bob 3");
                db.Roles.Add(role1);
                db.Roles.Add(role2);
                db.Roles.Add(role3);

                db.SaveChanges();

                db.Roles.Update(o => o.Name.StartsWith("J"), o => new Role { Description = "Updated joe" }, CacheStrategy.FullClean);

                db.SaveChanges();

                db.Roles.Local.Where(o => o.Id == role1.Id).Should().BeEmpty();
                db.Roles.Local.Where(o => o.Id == role2.Id).Should().BeEmpty();
                db.Roles.Local.Where(o => o.Id == role3.Id).Should().BeEmpty();

                db.Roles.Where(o => o.Name.StartsWith("J") && o.Description != "Updated joe").Should().BeEmpty();
                db.Roles.Where(o => o.Id == role3.Id && o.Description == role3.Name).Should().NotBeEmpty();
            }
        }
        public void CreateLog2()
        {
            AuditConfiguration.Default.IncludeRelationships = true;
            AuditConfiguration.Default.LoadRelationships = true;

            AuditConfiguration.Default.IsAuditable<Task>();
            AuditConfiguration.Default.IsAuditable<User>();

            var db = new TrackerContext();
            var task2 = new Task()
            {
                Id = 1,
                AssignedId = 1,
                CreatedId = 1,
                StatusId = 1,
                PriorityId = 2,
                Summary = "Summary: " + DateTime.Now.Ticks
            };
            db.Tasks.Add(task2);

            var audit = db.BeginAudit();

            var task = db.Tasks.Find(1);
            Assert.IsNotNull(task);

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

            var log = audit.CreateLog();
            Assert.IsNotNull(log);

            string xml = log.ToXml();
            Assert.IsNotNull(xml);
        }
        public void TestCacheStrategy3()
        {
            using (var db = new TrackerContext())
            {
                var role1 = CreateRole("Joe 1");
                var role2 = CreateRole("Joe 2");
                var role3 = CreateRole("Bob 3");
                db.Roles.Add(role1);
                db.Roles.Add(role2);
                db.Roles.Add(role3);

                db.SaveChanges();

                db.Roles.Delete(o => o.Name.StartsWith("J"), CacheStrategy.FullClean);

                db.SaveChanges();

                db.Roles.Local.Where(o => o.Id == role1.Id).Should().BeEmpty();
                db.Roles.Local.Where(o => o.Id == role2.Id).Should().BeEmpty();
                db.Roles.Local.Where(o => o.Id == role3.Id).Should().BeEmpty();

                db.Roles.Where(o => o.Name.StartsWith("J")).Should().BeEmpty();
                db.Roles.Where(o => o.Id == role3.Id).Should().NotBeEmpty();
            }
        }
 public async void UpdateAsync()
 {
     var db = new TrackerContext();
     string emailDomain = "@test.com";
     int count = await db.Users
         .Where(u => u.EmailAddress.EndsWith(emailDomain))
         .UpdateAsync(u => new User { IsApproved = false, LastActivityDate = DateTime.Now });
 }
Beispiel #7
0
        public void GetEntityMapAuditData()
        {
            var db = new TrackerContext();

            var map = db.Audits.ToObjectQuery().GetEntityMap<AuditData>();

            Assert.AreEqual("[dbo].[Audit]", map.TableName);
        }
        public async void DeleteAsync()
        {
            var db = new TrackerContext();
            string emailDomain = "@test.com";

            int count = await db.Users
                .Where(u => u.EmailAddress.EndsWith(emailDomain))
                .DeleteAsync();
        }
        public void FromCacheTest()
        {
            var db = new TrackerContext();
            var roles = db.Roles.FromCache();
            roles.Should().NotBeEmpty();

            var roles2 = db.Roles.FromCache();
            roles2.Should().NotBeEmpty();
        }
        public void FromCacheFirstOrDefaultTest()
        {
            var db = new TrackerContext();
            var role = db.Roles.FromCacheFirstOrDefault();
            role.Should().NotBeNull();

            var role2 = db.Roles.FromCacheFirstOrDefault();
            role2.Should().NotBeNull();
        }
        public void DeleteWithExpressionContainingNullParameter()
        {
            // This test verifies that the delete is processed correctly when the where expression uses a parameter with a null parameter
            var db = new TrackerContext();
            string emailDomain = "@test.com";
            string optionalComparisonString = null;

            int count = db.Users
                .Delete(u => u.EmailAddress.EndsWith(emailDomain) && (string.IsNullOrEmpty(optionalComparisonString) || u.AvatarType == optionalComparisonString));
        }
Beispiel #12
0
        public void UpdateJoin()
        {
            var db = new TrackerContext();
            string emailDomain = "@test.com";
            string space = " ";

            int count = db.Users.Update(
                u => u.EmailAddress.EndsWith(emailDomain),
                u => new User { LastName = u.FirstName + space + u.LastName });
        }
        public void UpdateAppend()
        {
            var db = new TrackerContext();

            string emailDomain = "@test.com";
            string newComment = " New Comment";

            int count = db.Users
                .Where(u => u.EmailAddress.EndsWith(emailDomain))
                .Update(u => new User { LastName = u.LastName + newComment });
        }
Beispiel #14
0
        public void DeleteWhere()
        {
            var db = new TrackerContext();
            string emailDomain = "@test.com";

            //var user = db.Users.Select(u => new User { FirstName = u.FirstName, LastName = u.LastName });

            int count = db.Users
                .Where(u => u.EmailAddress.EndsWith(emailDomain))
                .Delete();
        }
        public void UpdateWithExpressionContainingNullParameter()
        {
            // This test verifies that the update is interpreted correctly when the where expression uses a parameter with a null parameter
            var db = new TrackerContext();
            string emailDomain = "@test.com";
            string optionalComparisonString = null;

            int count = db.Users.Update(
                u => u.EmailAddress.EndsWith(emailDomain) && (string.IsNullOrEmpty(optionalComparisonString) || u.AvatarType == optionalComparisonString),
                u => new User { IsApproved = false, LastActivityDate = DateTime.Now });
        }
        public void GetInheritedEntityMapAuditData()
        {
            var db = new TrackerContext();
            var resolver = new MetadataMappingProvider();

            var map = resolver.GetEntityMap(typeof(CodeFirst.Entities.Task), db);

            //var map = db.Audits.ToObjectQuery().GetEntityMap<AuditData>();

            Assert.Equal("[dbo].[Task]", map.TableName);
        }
        public void GetEntityMapAuditData()
        {
            var db = new TrackerContext();
            var resolver = new MetadataMappingProvider();

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

            //var map = db.Audits.ToObjectQuery().GetEntityMap<AuditData>();

            Assert.AreEqual("[dbo].[Audit]", map.TableName);
        }
        public void CreateLog()
        {
            var auditConfiguration = AuditConfiguration.Default;

            auditConfiguration.IncludeRelationships = true;
            auditConfiguration.LoadRelationships = true;
            auditConfiguration.DefaultAuditable = true;

            // customize the audit for Task entity
            auditConfiguration.IsAuditable<Task>()
              .NotAudited(t => t.TaskExtended)
              .FormatWith(t => t.Status, v => FormatStatus(v));

            // set name as the display member when status is a foreign key
            auditConfiguration.IsAuditable<Status>()
              .DisplayMember(t => t.Name);

            var db = new TrackerContext();

            var user1 = new User()
            {
                Id = 1
            };
            db.Users.Add(user1);

            var audit = db.BeginAudit();

            var user = db.Users.Find(1);
            user.Comment = "Testing: " + DateTime.Now.Ticks;

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

            var task2 = db.Tasks.Find(1);
            task2.PriorityId = 2;
            task2.StatusId = 2;
            task2.Summary = "Summary: " + DateTime.Now.Ticks;

            var log = audit.CreateLog();
            Assert.IsNotNull(log);

            string xml = log.ToXml();
            Assert.IsNotNull(xml);
        }
        public void CreateLogLoaded()
        {
            var auditConfiguration = AuditConfiguration.Default;

            auditConfiguration.IncludeRelationships = true;
            auditConfiguration.LoadRelationships = true;
            auditConfiguration.DefaultAuditable = true;

            // customize the audit for Task entity
            //auditConfiguration.IsAuditable<Task>()
            //  .NotAudited(t => t.TaskExtended)
            //  .FormatWith(t => t.Status, v => FormatStatus(v));

            // set name as the display member when status is a foreign key
            auditConfiguration.IsAuditable<Status>()
              .DisplayMember(t => t.Name);

            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(1);
            updateTask.Priority = p;
            updateTask.StatusId = 2;
            updateTask.Summary = "Summary: " + DateTime.Now.Ticks;

            var log = audit.CreateLog();
            Assert.IsNotNull(log);

            string xml = log.ToXml();
            Assert.IsNotNull(xml);

            foreach (var property in log.Entities.SelectMany(e => e.Properties))
            {
                Assert.AreNotEqual(property.Current, "{error}");
                Assert.AreNotEqual(property.Original, "{error}");
            }
        }
        public void SelectByKey()
        {
            var db = new TrackerContext();
            var contextAdapter = db as IObjectContextAdapter;
            var objectContext = contextAdapter.ObjectContext;

            var sql = "SELECT VALUE U.EmailAddress FROM TrackerContext.Users AS U WHERE U.Id = 1";

            var q = objectContext.CreateQuery<object>(sql);
            var list = q.FirstOrDefault();

            list.Should().NotBeNull();


        }
Beispiel #21
0
        public void UpdateAppendAndNull()
        {
            var db = new TrackerContext();

            string emailDomain = "@test.com";
            string newComment = " New Comment";

            int count = db.Users.Update(
                u => u.EmailAddress.EndsWith(emailDomain),
                u => new User
                {
                    FirstName = "Test",
                    LastName = u.LastName + newComment,
                    Comment = null
                });
        }
        public void CreateLogFormatWith()
        {
            var auditConfiguration = AuditConfiguration.Default;

            auditConfiguration.IncludeRelationships = true;
            auditConfiguration.LoadRelationships = true;
            auditConfiguration.DefaultAuditable = true;

            // customize the audit for Task entity
            auditConfiguration.IsAuditable<Task>()
              .NotAudited(t => t.TaskExtended)
              .FormatWith(t => t.Status, v => FormatStatus(v));

            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(1);
            task2.PriorityId = 2;
            task2.StatusId = 2;
            task2.Summary = "Summary: " + DateTime.Now.Ticks;

            var log = audit.CreateLog();
            Assert.NotNull(log);

            string xml = log.ToXml();
            Assert.NotNull(xml);

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

            //query result is now cached 300 seconds
            var tasks = db.Tasks
                .Select(t => new TaskLookup
                {
                    Id = t.Id,
                    Summary = t.Summary
                })
                .FromCache()
                .ToList();



        }
        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.IsNotNull(tasks);
        }
        public void TaskFromCacheTest()
        {
            var db = new TrackerContext();

            int myUserId = 0;

            //query result is now cached 300 seconds
            var tasks = db.Tasks
                .Where(t => t.AssignedId == myUserId && t.CompleteDate == null)
                .FromCache(CachePolicy.WithDurationExpiration(TimeSpan.FromSeconds(300)));

            // cache assigned tasks
            var tagTasks = db.Tasks
                .Where(t => t.AssignedId == myUserId && t.CompleteDate == null)
                .FromCache(tags: new[] { "Task", "Assigned-Task-" + myUserId });

            // some update happened, expire task tag
            CacheManager.Current.Expire("Task");


        }
        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.IsFalse(((IFutureQuery)q1).IsLoaded);
            Assert.IsFalse(((IFutureQuery)q2).IsLoaded);
            Assert.IsFalse(((IFutureQuery)q3).IsLoaded);

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

            Assert.IsTrue(((IFutureQuery)q1).IsLoaded);
            Assert.IsTrue(((IFutureQuery)q2).IsLoaded);
            Assert.IsTrue(((IFutureQuery)q3).IsLoaded);
        }
        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 = "Earth";
            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.AreEqual(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.AreNotEqual(count, 0);

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

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

            var users = q1.Value;
            Assert.IsNotNull(users);

            var tasks = q3.ToList();
            Assert.IsNotNull(tasks);

        }
        public void LoggingInterception()
        {
            //log to a string builder
            var sb = new StringBuilder();

            var db = new TrackerContext();
            db.Database.Log = s => sb.AppendLine(s);

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

            //materialize it
            var user = q1.Value;

            //did we log anything?
            var logged = sb.ToString();

            Assert.Contains("[EmailAddress] LIKE N'*****@*****.**'", logged);
        }
        public void CreateLog3()
        {
            AuditConfiguration.Default.IncludeRelationships = true;
            AuditConfiguration.Default.LoadRelationships = true;

            AuditConfiguration.Default.IsAuditable<Task>();
            AuditConfiguration.Default.IsAuditable<User>();

            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.IsNotNull(log);

            string beforeXml = log.ToXml();
            Assert.IsNotNull(beforeXml);

            db.SaveChanges();

            log.Refresh();

            string afterXml = log.ToXml();
            Assert.IsNotNull(afterXml);
        }
        public void MaintainAcrossSaves()
        {
            var auditConfiguration = AuditConfiguration.Default;

            auditConfiguration.IncludeRelationships = true;
            auditConfiguration.LoadRelationships = true;
            auditConfiguration.DefaultAuditable = true;
            auditConfiguration.MaintainAcrossSaves = true;

            // customize the audit for Task entity
            //auditConfiguration.IsAuditable<Task>()
            //  .NotAudited(t => t.TaskExtended)
            //  .FormatWith(t => t.Status, v => FormatStatus(v));

            // set name as the display member when status is a foreign key
            auditConfiguration.IsAuditable<Status>()
              .DisplayMember(t => t.Name);

            var db = new TrackerContext();
            var tran = db.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.IsNotNull(audit.LastLog);
            Assert.AreEqual(2, audit.LastLog.Entities.Count);


            var task2 = db.Tasks.Find(1);
            task2.PriorityId = 2;
            task2.StatusId = 2;
            task2.Summary = "Summary: " + DateTime.Now.Ticks;

            db.SaveChanges();

            Assert.IsNotNull(audit.LastLog);
            Assert.AreEqual(3, audit.LastLog.Entities.Count);

            var log = audit.LastLog;
            Assert.IsNotNull(log);

            string xml = log.ToXml();
            Assert.IsNotNull(xml);

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

            //undo work
            tran.Rollback();
        }