public void AddsEntityTo_database_ReturnsKey()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            Person p = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
            int id = personRepository.Insert(p);
            Assert.AreEqual(p.Id, id);
        }
        public void UsingKey_DeletesFrom_database()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            Person p1 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
            int id = personRepository.Insert(p1);
            bool deleted = personRepository.Delete(p1);
            Assert.IsTrue(deleted);
            Assert.IsNull(personRepository.Get(id));
        }
        public void UsingKey_ReturnsEntity()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            Person p1 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
            int id = personRepository.Insert(p1);

            Person p2 = personRepository.Get(id);
            Assert.AreEqual(id, p2.Id);
            Assert.AreEqual("Foo", p2.FirstName);
            Assert.AreEqual("Bar", p2.LastName);
        }
        public void Delete_All()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            Person p1 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
            personRepository.Insert(p1);

            bool deleted = personRepository.Delete();
            int count = personRepository.Count();

            Assert.IsTrue(deleted);
            Assert.AreEqual(count, 0);
        }
        public void IdentityKey_UsingEntity()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            DateTime start = DateTime.Now;
            for (int i = 0; i < Count; i++)
            {
                Person p2 = new Person { FirstName = "FirstName" + i, LastName = "LastName" + i, DateCreated = DateTime.Now, Active = true };
                personRepository.Insert(p2);
            }

            double total = DateTime.Now.Subtract(start).TotalMilliseconds;
            Console.WriteLine("Total Time:" + total);
            Console.WriteLine("Average Time:" + total / Count);
        }
        public void UsingKey_UpdatesEntity()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            Person p1 = new Person { Active = true, FirstName = "Foo", LastName = "Bar", DateCreated = DateTime.UtcNow };
            int id = personRepository.Insert(p1);
            var p2 = personRepository.Get(id);
            p2.FirstName = "Baz";
            p2.Active = false;

            personRepository.Update(p2);

            var p3 = personRepository.Get(id);
            Assert.AreEqual("Baz", p3.FirstName);
            Assert.AreEqual("Bar", p3.LastName);
            Assert.AreEqual(false, p3.Active);
        }
        public void UsingQuery_List_Contains()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();
            var person = new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow };
            int id = personRepository.Insert(person);

            var people = new List<int> { id };

            int personCount = personRepository
                .Query(e => people.Contains(e.Id))
                .Count();

            Assert.AreEqual(1, personCount);
        }
        public void UsingQuery_With_Expression_Ordered_By_Name_Constant()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            personRepository.Delete();

            var person = new Person
            {
                Active = false,
                FirstName = "b",
                LastName = "b1",
                DateCreated = DateTime.UtcNow
            };
            personRepository.Insert(person);

            person = new Person
            {
                Active = false,
                FirstName = "b",
                LastName = "b2",
                DateCreated = DateTime.UtcNow
            };
            personRepository.Insert(person);

            const string firstName = "b";
            Expression<Func<Person, bool>> expression = PredicateBuilder.True<Person>();
            expression = expression.And(e => e.FirstName == firstName);

            int count = personRepository
                .Query(expression)
                .OrderBy(e => e.FirstName)
                .Count();

            Assert.AreEqual(2, count);
        }
        public void UsingQuery_With_Expression_Value_Contains()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();

            personRepository.Delete();

            var person = new Person { Active = false, FirstName = "ba", LastName = "b1", DateCreated = DateTime.UtcNow, ProfileId = 14 };
            personRepository.Insert(person);

            Expression<Func<Person, bool>> expression = PredicateBuilder.True<Person>();
            expression = expression.And(e => e.FirstName.Contains("b"));

            int count = personRepository
                .Query(expression)
                .Count();

            Assert.AreEqual(1, count);
        }
        public void UsingQuery_Enumerable_Contains_Nullable()
        {
            var personRepository = Container.Resolve<IRepository<Person>>();
            var person = new Person { Active = false, FirstName = "b", LastName = "b1", DateCreated = DateTime.UtcNow, ProfileId = 13 };
            personRepository.Insert(person);

            var profileIds = new List<int> { 13 };

            int personCount = personRepository
                .Query(e => profileIds.Contains(e.ProfileId.Value))
                .Count();

            Assert.AreEqual(1, personCount);
        }