Пример #1
0
        public void Bug_1751_Include_with_select()
        {
            var user  = new User();
            var issue = new Issue {
                AssigneeId = user.Id, Title = "Garage Door is busted"
            };

            theSession.Store <object>(user, issue);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var  issue2   = query
                                .Query <Issue>()
                                .Include <User>(x => x.AssigneeId, x => included = x)
                                .Where(x => x.Title == issue.Title)
                                .Select(x => new IssueDTO {
                    Id = x.Id, AssigneeId = x.AssigneeId
                })
                                .Single();

                included.ShouldNotBeNull();
                included.Id.ShouldBe(user.Id);

                issue2.ShouldNotBeNull();
            }
        }
Пример #2
0
        public void multiple_includes()
        {
            var assignee = new User();
            var reporter = new User();

            var issue1 = new Issue {
                AssigneeId = assignee.Id, ReporterId = reporter.Id, Title = "Garage Door is busted"
            };

            theSession.Store(assignee, reporter);
            theSession.Store(issue1);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                query.Logger = new TestOutputMartenLogger(_output);
                User assignee2 = null;
                User reporter2 = null;

                query
                .Query <Issue>()
                .Include <User>(x => x.AssigneeId, x => assignee2 = x)
                .Include <User>(x => x.ReporterId, x => reporter2 = x)
                .Single()
                .ShouldNotBeNull();

                assignee2.Id.ShouldBe(assignee.Id);
                reporter2.Id.ShouldBe(reporter.Id);
            }
        }
Пример #3
0
        public void update_mixed_document_types()
        {
            var user1 = new User();
            var user2 = new User();
            var issue1 = new Issue();
            var issue2 = new Issue();
            var company1 = new Company();
            var company2 = new Company();

            var uow = theContainer.GetInstance<UnitOfWork>();
            uow.Store(user1, user2);
            uow.Store(issue1, issue2);
            uow.Store(company1, company2);

            var batch = theContainer.GetInstance<UpdateBatch>();

            uow.ApplyChanges(batch);

            batch.Connection.Dispose();

            using (var session2 = theContainer.GetInstance<IDocumentStore>().OpenSession())
            {
                session2.Query<User>().ToArray().Select(x => x.Id).ShouldHaveTheSameElementsAs(user1.Id, user2.Id);
                session2.Query<Issue>().ToArray().Select(x => x.Id).ShouldHaveTheSameElementsAs(issue1.Id, issue2.Id);
                session2.Query<Company>().ToArray().Select(x => x.Id).ShouldHaveTheSameElementsAs(company1.Id, company2.Id);
            }


        }
Пример #4
0
        public void include_to_dictionary_using_outer_join()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue {
                AssigneeId = user1.Id, Title = "Garage Door is busted"
            };
            var issue2 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };
            var issue3 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };
            var issue4 = new Issue {
                AssigneeId = null, Title = "Garage Door is busted"
            };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3, issue4);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                var dict = new Dictionary <Guid, User>();

                var issues = query.Query <Issue>().Include(x => x.AssigneeId, dict).ToArray();

                dict.Count.ShouldBe(2);
                dict.ContainsKey(user1.Id).ShouldBeTrue();
                dict.ContainsKey(user2.Id).ShouldBeTrue();

                issues.Length.ShouldBe(4);
            }
        }
Пример #5
0
        public void include_to_list()
        {
            var user1 = new User {
                FirstName = "Travis", LastName = "Kelce"
            };
            var user2 = new User {
                FirstName = "Tyrann", LastName = "Mathieu"
            };

            var issue1 = new Issue {
                AssigneeId = user1.Id, Title = "Garage Door is busted 1"
            };
            var issue2 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted 2"
            };
            var issue3 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted 3"
            };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                var list = new List <User>();

                var issues = query.Query <Issue>().Include <User>(x => x.AssigneeId, list).ToArray();

                list.Count.ShouldBe(2);

                list.Any(x => x.Id == user1.Id).ShouldBeTrue();
                list.Any(x => x.Id == user2.Id).ShouldBeTrue();
            }
        }
Пример #6
0
        public void include_is_running_through_identitymap()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue {
                AssigneeId = user1.Id, Title = "Garage Door is busted"
            };
            var issue2 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };
            var issue3 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3);
            theSession.SaveChanges();

            // This will only work with a non-NulloIdentityMap
            using (var query = theStore.OpenSession())
            {
                var dict = new Dictionary <Guid, User>();

                query.Query <Issue>().Include(x => x.AssigneeId, dict).ToArray();

                query.Load <User>(user1.Id).ShouldBeSameAs(dict[user1.Id]);
                query.Load <User>(user2.Id).ShouldBeSameAs(dict[user2.Id]);
            }
        }
Пример #7
0
        public void include_with_any_containment_where_for_a_single_document_with_snake_casing()
        {
            StoreOptions(_ => _.UseDefaultSerialization(casing: Casing.SnakeCase));

            var user  = new User();
            var issue = new Issue {
                AssigneeId = user.Id, Tags = new[] { "DIY" }, Title = "Garage Door is busted"
            };

            theSession.Store <object>(user, issue);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var  issue2   = query.Query <Issue>()
                                .Include <User>(x => x.AssigneeId, x => included = x)
                                .Where(x => x.Tags.Any(t => t == "DIY"))
                                .Single();

                SpecificationExtensions.ShouldNotBeNull(included);
                included.Id.ShouldBe(user.Id);

                SpecificationExtensions.ShouldNotBeNull(issue2);
            }
        }
Пример #8
0
        public void include_with_any_array_containment_where_for_a_single_document()
        {
            var user   = new User();
            var issue1 = new Issue {
                AssigneeId = user.Id, Tags = new [] { "DIY" }, Title = "Garage Door is busted"
            };
            var issue2 = new Issue {
                AssigneeId = user.Id, Tags = new [] { "TAG" }, Title = "Garage Door is busted"
            };
            var issue3 = new Issue {
                AssigneeId = user.Id, Tags = new string[] { }, Title = "Garage Door is busted"
            };

            var requestedTags = new[] { "DIY", "TAG" };

            theSession.Store(user);
            theSession.Store(issue1, issue2, issue3);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                var users  = new List <User>();
                var issues = query.Query <Issue>()
                             .Include(x => x.AssigneeId, users)
                             .Where(x => x.Tags.Any(t => requestedTags.Contains(t)))
                             .ToList();

                users.Count.ShouldBe(1);
                SpecificationExtensions.ShouldContain(users, x => x.Id == user.Id);

                issues.Count.ShouldBe(2);
                SpecificationExtensions.ShouldContain(issues, x => x.Id == issue1.Id);
                SpecificationExtensions.ShouldContain(issues, x => x.Id == issue2.Id);
            }
        }
Пример #9
0
        public async Task include_to_dictionary_async()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue {
                AssigneeId = user1.Id, Title = "Garage Door is busted"
            };
            var issue2 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };
            var issue3 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3);
            await theSession.SaveChangesAsync().ConfigureAwait(false);

            using (var query = theStore.QuerySession())
            {
                var dict = new Dictionary <Guid, User>();

                await query.Query <Issue>().Include(x => x.AssigneeId, dict).ToListAsync().ConfigureAwait(false);

                dict.Count.ShouldBe(2);
                dict.ContainsKey(user1.Id).ShouldBeTrue();
                dict.ContainsKey(user2.Id).ShouldBeTrue();
            }
        }
Пример #10
0
        public void include_to_list_using_outer_join()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue { AssigneeId = user1.Id, Title = "Garage Door is busted" };
            var issue2 = new Issue { AssigneeId = user2.Id, Title = "Garage Door is busted" };
            var issue3 = new Issue { AssigneeId = user2.Id, Title = "Garage Door is busted" };
            var issue4 = new Issue { AssigneeId = null, Title = "Garage Door is busted" };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3, issue4);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                var list = new List<User>();

                var issues = query.Query<Issue>().Include<User>(x => x.AssigneeId, list).ToArray();

                list.Count.ShouldBe(2);

                list.Any(x => x.Id == user1.Id).ShouldBeTrue();
                list.Any(x => x.Id == user2.Id).ShouldBeTrue();
                list.Any(x => x == null).ShouldBeFalse();

                issues.Length.ShouldBe(4);
            }
        }
        public void multiple_documents()
        {
            var user1 = new User {FirstName = "Jeremy", LastName = "Miller"};
            var issue1 = new Issue {Title = "TV won't turn on"}; // unfortunately true as I write this...
            var company1 = new Company{Name = "Widgets, inc."};
            var company2 = new Company{Name = "BigCo"};
            var company3 = new Company{Name = "SmallCo"};

            theSession.Store(user1);
            theSession.Store(issue1);
            theSession.Store(company1);
            theSession.Store(company2);
            theSession.Store(company3);
            theSession.SaveChanges();

            using (var session = theContainer.GetInstance<IDocumentSession>())
            {
                var user = session.Load<User>(user1.Id);
                user.FirstName = "Max";

                session.Store(user);

                session.Delete(company2);

                session.SaveChanges();
            }

            using (var session = theContainer.GetInstance<IDocumentSession>())
            {
                session.Load<User>(user1.Id).FirstName.ShouldBe("Max");
                session.Load<Company>(company1.Id).Name.ShouldBe("Widgets, inc.");
                session.Load<Company>(company2.Id).ShouldBeNull();
                session.Load<Company>(company3.Id).Name.ShouldBe("SmallCo");
            }
        }
        public void include_to_list()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue {
                AssigneeId = user1.Id, Title = "Garage Door is busted"
            };
            var issue2 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };
            var issue3 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                var list = new List <User>();

                query.Query <Issue>().Include <User>(x => x.AssigneeId, list).ToArray();

                list.Count.ShouldBe(2);

                list.Any(x => x.Id == user1.Id);
                list.Any(x => x.Id == user2.Id);
            }
        }
Пример #13
0
        public async Task include_to_list_with_orderby_ascending_async()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue {
                AssigneeId = user1.Id, Title = "Garage Door is busted"
            };
            var issue2 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };
            var issue3 = new Issue {
                AssigneeId = user2.Id, Title = "Garage Door is busted"
            };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3);
            await theSession.SaveChangesAsync().ConfigureAwait(false);

            using (var query = theStore.QuerySession())
            {
                var list = new List <User>();

                await query.Query <Issue>().Include(x => x.AssigneeId, list)
                .OrderBy(x => x.Id)
                .ToListAsync().ConfigureAwait(false);

                list.Count.ShouldBe(2);

                list.Any(x => x.Id == user1.Id);
                list.Any(x => x.Id == user2.Id);
            }
        }
Пример #14
0
        public async Task include_within_batch_query()
        {
            var user1 = new User();
            var user2 = new User();

            var issue1 = new Issue { AssigneeId = user1.Id, Title = "Garage Door is busted #1" };
            var issue2 = new Issue { AssigneeId = user2.Id, Title = "Garage Door is busted #2" };
            var issue3 = new Issue { AssigneeId = user2.Id, Title = "Garage Door is busted #3" };

            theSession.Store(user1, user2);
            theSession.Store(issue1, issue2, issue3);
            await theSession.SaveChangesAsync();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var list = new List<User>();
                var dict = new Dictionary<Guid, User>();

                #region sample_batch_include
                var batch = query.CreateBatchQuery();

                var found = batch.Query<Issue>()
                    .Include<User>(x => x.AssigneeId, x => included = x)
                    .Where(x => x.Title == issue1.Title)
                    .Single();
                #endregion sample_batch_include

                var toList = batch.Query<Issue>()
                    .Include<User>(x => x.AssigneeId, list).ToList();

                var toDict = batch.Query<Issue>()
                    .Include(x => x.AssigneeId, dict).ToList();

                await batch.Execute();


                (await found).Id.ShouldBe(issue1.Id);

                included.ShouldNotBeNull();
                included.Id.ShouldBe(user1.Id);

                (await toList).Count.ShouldBe(3);

                list.Count.ShouldBe(2); // Only 2 users


                (await toDict).Count.ShouldBe(3);

                dict.Count.ShouldBe(2);

                dict.ContainsKey(user1.Id).ShouldBeTrue();
                dict.ContainsKey(user2.Id).ShouldBeTrue();

            }
        }
Пример #15
0
        public void include_with_generic_type()
        {
            var user = new UserWithInterface { Id = Guid.NewGuid(), UserName = "******" };
            var issue = new Issue { AssigneeId = user.Id, Tags = new[] { "DIY" }, Title = "Garage Door is busted" };

            theSession.Store<object>(user, issue);
            theSession.SaveChanges();

            IncludeGeneric<UserWithInterface>(user);
        }
Пример #16
0
        public void can_resolve_and_callback_from_the_reader()
        {
            var reader = Substitute.For<DbDataReader>();
            var issue = new Issue();
            var user = new User();

            var map = new NulloIdentityMap(null);

            theResolver.Resolve(3, reader, map).Returns(user);
            inner.Resolve(reader, map).Returns(issue);

            theSelector.Resolve(reader, map).ShouldBe(issue);

            theCallback.Received().Invoke(user);
        }
Пример #17
0
        public void Bug_1752_simple_include_for_a_single_document()
        {
            var user = new User();
            var issue = new Issue {AssigneeId = user.Id, Title = "Garage Door is busted"};

            theSession.Store<object>(user, issue);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var issue2 = query
                    .Query<Issue>()
                    .Include<User>(x => x.AssigneeId, x => included = x)
                    .SingleOrDefault(x => x.Title == "Garage Door is not busted");

                included.ShouldBeNull();
                issue2.ShouldBeNull();
            }
        }
Пример #18
0
        public void simple_include_for_a_single_document_using_outer_join()
        {
            var issue = new Issue { AssigneeId = null, Title = "Garage Door is busted" };

            theSession.Store(issue);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var issue2 = query.Query<Issue>()
                    .Include<User>(x => x.AssigneeId, x => included = x)
                    .Where(x => x.Title == issue.Title)
                    .Single();

                SpecificationExtensions.ShouldBeNull(included);

                SpecificationExtensions.ShouldNotBeNull(issue2);
            }
        }
Пример #19
0
        public void include_with_containment_where_for_a_single_document()
        {
            var user = new User();
            var issue = new Issue {AssigneeId = user.Id, Tags = new []{"DIY"}, Title = "Garage Door is busted"};

            theSession.Store<object>(user, issue);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var issue2 = query
                    .Query<Issue>()
                    .Include<User>(x => x.AssigneeId, x => included = x)
                    .Single(x => Enumerable.Contains(x.Tags, "DIY"));

                included.ShouldNotBeNull();
                included.Id.ShouldBe(user.Id);

                issue2.ShouldNotBeNull();
            }
        }
Пример #20
0
        public void simple_include_for_a_single_document()
        {
            var user = new User();
            var issue = new Issue {AssigneeId = user.Id, Title = "Garage Door is busted"};

            theSession.Store<object>(user, issue);
            theSession.SaveChanges();

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var issue2 = query
                    .Query<Issue>()
                    .Include<User>(x => x.AssigneeId, x => included = x)
                    .Single(x => x.Title == issue.Title);

                SpecificationExtensions.ShouldNotBeNull(included);
                included.Id.ShouldBe(user.Id);

                SpecificationExtensions.ShouldNotBeNull(issue2);
            }
        }
Пример #21
0
        public async Task Bug_1715_simple_include_for_a_single_document_async()
        {
            theSession.Logger = new TestOutputMartenLogger(_output);

            var user = new User();
            var bug = new Bug();

            theSession.Store(user);
            theSession.Store(bug);

            var issue = new Issue
            {
                AssigneeId = user.Id, Title = "Garage Door is busted", BugId = bug.Id
            };

            theSession.Store(issue);

            await theSession.SaveChangesAsync();

            using (var query = theStore.QuerySession())
            {
                User includedUser = null;
                Bug includedBug = null;
                var issue2 = await query.Query<Issue>()
                    .Include<User>(x => x.AssigneeId, x => includedUser = x)
                    .Include<Bug>(x => x.BugId, x => includedBug = x)
                    .Where(x => x.Title == issue.Title)
                    .SingleAsync();

                includedUser.ShouldNotBeNull();
                includedBug.ShouldNotBeNull();
                includedUser.Id.ShouldBe(user.Id);
                includedBug.Id.ShouldBe(bug.Id);

                issue2.ShouldNotBeNull();
            }
        }
Пример #22
0
        public async Task simple_include_for_a_single_document_async()
        {
            var user  = new User();
            var issue = new Issue {
                AssigneeId = user.Id, Title = "Garage Door is busted"
            };

            theSession.Store <object>(user, issue);
            await theSession.SaveChangesAsync().ConfigureAwait(false);

            using (var query = theStore.QuerySession())
            {
                User included = null;
                var  issue2   = await query.Query <Issue>()
                                .Include <User>(x => x.AssigneeId, x => included = x)
                                .Where(x => x.Title == issue.Title)
                                .SingleAsync().ConfigureAwait(false);

                included.ShouldNotBeNull();
                included.Id.ShouldBe(user.Id);

                issue2.ShouldNotBeNull();
            }
        }