public void Get_With_String_Selector_Should_Return_Item_If_Item_Exists(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User { Username = "******", Age = 21 };
            repository.Add(item);

            var result = repository.Get(item.Username, item.Age, c => c.Username);
            result.ShouldEqual("Test User");
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Composite_Specification(ICompoundKeyRepository<User, string, int> repository)
        {
            for (var i = 1; i <= 3; i++)
            {
                var contact = new User { Username = "******" + i, Age = i };
                repository.Add(contact);
            }

            var result = repository.FindAll(new Specification<User>(p => p.Username == "Test User 1").OrElse(new Specification<User>(p => p.Username == "Test User 2")));
            result.Count().ShouldEqual(2);
        }
        public void Delete_Should_Remove_Item(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User {Username = "******", Age = 11, FullName = "Test User - 11"};
            repository.Add(item);

            //var result = repository.Get(contact.ContactId);
            //result.ShouldNotBeNull();

            repository.Delete(item);
            //result = repository.Get(contact.ContactId);
            //result.ShouldBeNull();
        }
        public void Delete_Should_Remove_Item_By_Key(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User { Username = "******", Age = 11, FullName = "Test User - 11" };
            repository.Add(item);

            var result = repository.Get(item.Username, item.Age);
            result.ShouldNotBeNull();

            repository.Delete(item.Username, item.Age);

            result = repository.Get(item.Username, item.Age);
            result.ShouldBeNull();
        }
        public void Update_Should_Save_Modified_Business_Name(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User { Username = "******", Age = 22, FullName = "Test User - 22" };
            repository.Add(item);

            var item2 = new User { Username = "******", Age = 22, FullName = "Test User 2 - 22" };
            repository.Add(item2);

            item.FullName = "Test User - 22 - Updated";
            repository.Update(item);

            var updated = repository.Get(item.Username, item.Age);
            var notUpdated = repository.Get(item2.Username, item2.Age);

            updated.FullName.ShouldEqual("Test User - 22 - Updated");
            notUpdated.FullName.ShouldEqual("Test User 2 - 22");
        }
        public void Delete_Should_Wait_To_Remove_Item_If_Item_Exists_In_BatchMode(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User { Username = "******", Age = 11, FullName = "Test User - 11" };
            repository.Add(item);

            var result = repository.Get(item.Username, item.Age);
            result.ShouldNotBeNull();

            using (var batch = repository.BeginBatch())
            {
                batch.Delete(item);// not really delete until call Save, because in BatchMode

                result = repository.Get(item.Username, item.Age);
                result.ShouldNotBeNull();

                batch.Commit(); // actually do the delete
            }

            result = repository.Get(item.Username, item.Age);
            result.ShouldBeNull();
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Composite_Specification_With_Paging_And_Sort_Descending(ICompoundKeyRepository<User, string, int> repository)
        {
            const int resultingPage = 2;
            const int pageSize = 2;
            var queryOptions = new PagingOptions<User>(resultingPage, pageSize, "Username", true);

            for (var i = 1; i <= 10; i++)
            {
                var contact = new User { Username = "******" + i, Age = i };
                repository.Add(contact);
            }

            IEnumerable<User> result = repository
                .FindAll(new Specification<User>(p => p.Username == "Test User 1")
                                .OrElse(new Specification<User>(p => p.Username == "Test User 5")
                                        .OrElse(new Specification<User>(p => p.Username == "Test User 8"))),
                            queryOptions);

            result.Count().ShouldEqual(1);
            queryOptions.TotalItems.ShouldEqual(3);
            result.First().Username.ShouldEqual("Test User 1");
        }
        public void TryGet_Should_Return_True_If_Item_Exists(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User { Username = "******", Age = 21 };
            repository.Add(item);

            repository.Exists(item.Username, item.Age).ShouldBeTrue();
        }
        public void TryGet_Should_Return_True_And_Item_If_Item_Exists(ICompoundKeyRepository<User, string, int> repository)
        {
            var item = new User { Username = "******", Age = 21 };
            repository.Add(item);

            User result;
            repository.TryGet(item.Username, item.Age, out result).ShouldBeTrue();
            result.Username.ShouldEqual(item.Username);
            result.Age.ShouldEqual(item.Age);
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Specification_With_Paging_MagicString(ICompoundKeyRepository<User, string, int> repository)
        {
            const int resultingPage = 2;
            const int pageSize = 2;
            const int totalItems = 10;

            var queryOptions = new PagingOptions<User>(resultingPage, pageSize, "Username");

            for (var i = 1; i <= totalItems; i++)
            {
                var contact = new User { Username = "******" + i, Age = i };
                repository.Add(contact);
            }

            // this fails for RavenDb because the ContactId is an int but is being used as the key, so the check on ContactId <= 5 is doing a string comparison and including ContactId = 10 as well
            //  need to look into why this happens and how to get around it
            var result = repository.FindAll(new Specification<User>(p => p.Age <= totalItems / 2), queryOptions);
            result.Count().ShouldEqual(pageSize);
            queryOptions.TotalItems.ShouldEqual(totalItems / 2);
            result.First().Username.ShouldEqual("Test User 3");
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Specification_With_Paging(ICompoundKeyRepository<User, string, int> repository)
        {
            const int resultingPage = 2;
            const int pageSize = 2;
            const int totalItems = 10;

            var queryOptions = new PagingOptions<User, string>(resultingPage, pageSize, m => m.Username);

            for (var i = 1; i <= totalItems; i++)
            {
                var contact = new User { Username = "******" + i, Age = i };
                repository.Add(contact);
            }

            var result = repository.FindAll(new Specification<User>(p => p.Age <= totalItems / 2), queryOptions);
            result.Count().ShouldEqual(pageSize);
            queryOptions.TotalItems.ShouldEqual(totalItems / 2);
            result.First().Username.ShouldEqual("Test User 3");
        }
        public void FindAll_Should_Return_All_Items_Which_Satisfy_Predicate(ICompoundKeyRepository<User, string, int> repository)
        {
            for (var i = 1; i <= 3; i++)
            {
                var contact = new User { Username = "******" + i, Age = i };
                repository.Add(contact);
            }

            var result = repository.FindAll(p => p.Username == "Test User 1"); // Note: Raven doesn't like p.Name.Equals("...");
            result.Count().ShouldEqual(1);
        }