Пример #1
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);
            }


        }
Пример #2
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);
            }
        }
Пример #3
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);
            }
        }
Пример #4
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();
            }
        }
Пример #5
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);
            }
        }
Пример #6
0
        //[Fact] -- hiccups on CI because of having the assembly version in place
        public void origin_is_added_to_tables()
        {
            var user1 = new User { FirstName = "Jeremy" };
            var user2 = new User { FirstName = "Max" };
            var user3 = new User { FirstName = "Declan" };

            using (var store = DocumentStore.For(ConnectionSource.ConnectionString))
            {                
                store.Advanced.Clean.CompletelyRemoveAll();

                store.BulkInsert(new User[] { user1, user2, user3 });
            }

            using (var store = DocumentStore.For(ConnectionSource.ConnectionString))
            using (var session = store.QuerySession())
            using (var cmd = session.Connection.CreateCommand())
            {
                var mapping = store.Schema.MappingFor(typeof(User));

                cmd.CommandText = "SELECT description from pg_description " +
                                  "join pg_class on pg_description.objoid = pg_class.oid where relname = :name";
                cmd.AddParameter("name", mapping.Table.Name);

                var result = (string)cmd.ExecuteScalar();  
                Assert.NotNull(result);              
                Assert.Contains(typeof(IDocumentStore).AssemblyQualifiedName, result);
            }
        }
Пример #7
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();
            }
        }
Пример #8
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]);
            }
        }
        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);
            }
        }
Пример #10
0
        public void apply_updates_via_the_actual_document()
        {
            var stringDoc1 = new StringDoc {Id = "Foo"};
            var stringDoc2 = new StringDoc {Id = "Bar"};
            var user1 = new User();
            var user2 = new User();
            var int1 = new IntDoc {Id = 1};
            var int2 = new IntDoc {Id = 2};
            var long1 = new LongDoc {Id = 3};
            var long2 = new LongDoc {Id = 4};

            var uow1 = theContainer.GetInstance<UnitOfWork>();
            uow1.Store(user1, user2);
            uow1.Store(stringDoc1, stringDoc2);
            uow1.Store(int1, int2);
            uow1.Store(long1, long2);
            var batch1 = theContainer.GetInstance<UpdateBatch>();
            uow1.ApplyChanges(batch1);


            var uow2 = theContainer.GetInstance<UnitOfWork>();
            uow2.Delete(stringDoc2);
            uow2.Delete(user2);
            uow2.Delete(int2);
            uow2.Delete(long2);
            var batch2 = theContainer.GetInstance<UpdateBatch>();
            uow2.ApplyChanges(batch2);

            theSession.Query<StringDoc>().Single().Id.ShouldBe(stringDoc1.Id);
            theSession.Query<User>().Single().Id.ShouldBe(user1.Id);
            theSession.Query<IntDoc>().Single().Id.ShouldBe(int1.Id);
            theSession.Query<LongDoc>().Single().Id.ShouldBe(long1.Id);
        }
Пример #11
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);
            }
        }
Пример #12
0
        public void cannot_add_fields_if_mode_is_create_only()
        {
            var user1 = new User { FirstName = "Jeremy" };
            var user2 = new User { FirstName = "Max" };
            var user3 = new User { FirstName = "Declan" };

            using (var store = DocumentStore.For(ConnectionSource.ConnectionString))
            {
                store.Advanced.Clean.CompletelyRemoveAll();

                store.BulkInsert(new User[] { user1, user2, user3 });
            }

            using (var store2 = DocumentStore.For(_ =>
            {
                _.AutoCreateSchemaObjects = AutoCreate.CreateOnly;
                _.Connection(ConnectionSource.ConnectionString);
                _.Schema.For<User>().Searchable(x => x.FirstName);
            }))
            {
                var ex = Exception<InvalidOperationException>.ShouldBeThrownBy(() =>
                {
                    store2.Schema.EnsureStorageExists(typeof(User));
                });

                ex.Message.ShouldBe($"The table for document type {typeof(User).FullName} is different than the current schema table, but AutoCreateSchemaObjects = '{nameof(AutoCreate.CreateOnly)}'");
            }
        }
        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");
            }
        }
Пример #14
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);
            }
        }
Пример #15
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);
            }
        }
Пример #16
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();
            }
        }
Пример #17
0
        public void assign_a_given_id()
        {
            var user = new User();
            var id = Guid.NewGuid();

            theAssigner.Assign(user, id);

            user.Id.ShouldBe(id);
        }
        public void should_auto_assign()
        {
            var user = new User();
            user.Id = Guid.Empty;

            theSession.Store(user);

            user.Id.ShouldNotBe(Guid.Empty);
        }
Пример #19
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();

            }
        }
Пример #20
0
        public void include_many_to_list()
        {
            var user1 = new User {
            };
            var user2 = new User {
            };
            var user3 = new User {
            };
            var user4 = new User {
            };
            var user5 = new User {
            };
            var user6 = new User {
            };
            var user7 = new User {
            };

            theStore.BulkInsert(new User[] { user1, user2, user3, user4, user5, user6, user7 });

            var group1 = new Group
            {
                Name  = "Odds",
                Users = new [] { user1.Id, user3.Id, user5.Id, user7.Id }
            };

            var group2 = new Group {
                Name = "Evens", Users = new[] { user2.Id, user4.Id, user6.Id }
            };

            using (var session = theStore.OpenSession())
            {
                session.Store(group1, group2);
                session.SaveChanges();
            }

            using (var query = theStore.QuerySession())
            {
                query.Logger = new TestOutputMartenLogger(_output);

                var list = new List <User>();

                query.Query <Group>()
                .Include(x => x.Users, list)
                .Where(x => x.Name == "Odds")
                .ToList()
                .Single()
                .Name.ShouldBe("Odds");

                list.Count.ShouldBe(4);
                list.Any(x => x.Id == user1.Id).ShouldBeTrue();
                list.Any(x => x.Id == user3.Id).ShouldBeTrue();
                list.Any(x => x.Id == user5.Id).ShouldBeTrue();
                list.Any(x => x.Id == user7.Id).ShouldBeTrue();
            }
        }
Пример #21
0
        public void resolves_through_identity_map()
        {
            var map = Substitute.For<IIdentityMap>();
            var reader = Substitute.For<DbDataReader>();

            var user = new User();

            theResolver.Resolve(0, reader, map).Returns(user);

            theSelector.Resolve(reader, map, null).ShouldBe(user);
        }
Пример #22
0
        public void needs_to_assign_new_value()
        {
            bool assigned = false;

            var user = new User {Id = Guid.Empty};

            theAssigner.Assign(user, out assigned);

            assigned.ShouldBeTrue();
            user.Id.ShouldNotBe(Guid.Empty);
        }
Пример #23
0
        public void initial_state_of_deleted_columns()
        {
            using (var session = theStore.OpenSession())
            {

                var user = new User();
                session.Store(user);
                session.SaveChanges();

                userIsNotMarkedAsDeleted(session, user.Id);
            }
        }
Пример #24
0
        public void no_need_to_assign()
        {
            bool assigned = true;

            var originalId = Guid.NewGuid();
            var user = new User {Id = originalId};

            theAssigner.Assign(user, out assigned);

            user.Id.ShouldBe(originalId);
            assigned.ShouldBeFalse();
        }
Пример #25
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);
        }
Пример #26
0
        public void soft_delete_a_document_row_state()
        {
            using (var session = theStore.OpenSession())
            {

                var user = new User();
                session.Store(user);
                session.SaveChanges();

                session.Delete(user);
                session.SaveChanges();

                userIsMarkedAsDeleted(session, user.Id);
            }
        }
        // SAMPLE: query_with_only_the_where_clause
        public void query_for_single_document()
        {
            using (var container = Container.For<DevelopmentModeRegistry>())
            {
                using (var session = container.GetInstance<IDocumentStore>().OpenSession())
                {
                    var u = new User { FirstName = "Jeremy", LastName = "Miller" };
                    session.Store(u);
                    session.SaveChanges();

                    var user = session.Query<User>("where data ->> 'FirstName' = 'Jeremy'").Single();
                    user.LastName.ShouldBe("Miller");
                    user.Id.ShouldBe(u.Id);
                }
            }
        }
        public void do_not_lose_data_if_only_change_is_searchable_field_for_AutoCreateMode_is_CreateOrUpdate()
        {
            var user1 = new User { FirstName = "Jeremy" };
            var user2 = new User { FirstName = "Max" };
            var user3 = new User { FirstName = "Declan" };

            using (var store = DocumentStore.For(ConnectionSource.ConnectionString))
            {
                store.Advanced.Clean.CompletelyRemoveAll();

                store.BulkInsert(new User[] { user1, user2, user3 });
            }

            using (var store2 = DocumentStore.For(_ =>
            {
                _.AutoCreateSchemaObjects = AutoCreate.CreateOrUpdate;
                _.Connection(ConnectionSource.ConnectionString);
                _.Schema.For<User>().Searchable(x => x.FirstName);
            }))
            {
                using (var session = store2.QuerySession())
                {
                    session.Query<User>().Count().ShouldBe(3);

                    var list = new List<string>();

                    using (
                        var reader =
                            session.Connection.CreateCommand()
                                .WithText("select first_name from mt_doc_user")
                                .ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            list.Add(reader.GetString(0));
                        }


                    }

                    list.OrderBy(x => x).ShouldHaveTheSameElementsAs("Declan", "Jeremy", "Max");

                    session.Query<User>().Where(x => x.FirstName == "Jeremy").Single().ShouldNotBeNull();

                }
            }
        }
        public void persist_and_delete_a_document_by_id()
        {
            var user = new User { FirstName = "Mychal", LastName = "Thompson" };
            theSession.Store(user);
            theSession.SaveChanges();

            using (var session = theContainer.GetInstance<IDocumentSession>())
            {
                session.Delete<User>(user.Id);
                session.SaveChanges();
            }

            using (var session = theContainer.GetInstance<IDocumentSession>())
            {
                session.Load<User>(user.Id).ShouldBeNull();
            }
        }
        public void query_with_select_in_query()
        {
            using (var container = Container.For<DevelopmentModeRegistry>())
            {
                using (var session = container.GetInstance<IDocumentStore>().OpenSession())
                {
                    var u = new User { FirstName = "Jeremy", LastName = "Miller" };
                    session.Store(u);
                    session.SaveChanges();

                    // SAMPLE: use_all_your_own_sql
                    var user = session.Query<User>("select data from mt_doc_user where data ->> 'FirstName' = 'Jeremy'").Single();
                    // ENDSAMPLE
                    user.LastName.ShouldBe("Miller");
                    user.Id.ShouldBe(u.Id);
                }
            }
        }
Пример #31
0
        public void transform_all_documents()
        {
            var user1 = new User {FirstName = "Jeremy", LastName = "Miller"};
            var user2 = new User {FirstName = "Corey", LastName = "Kaylor"};
            var user3 = new User {FirstName = "Tim", LastName = "Cools"};

            theStore.BulkInsert(new User[] {user1, user2, user3});

            theStore.Transform.All<User>("default_username");


            using (var session = theStore.QuerySession())
            {
                session.Load<User>(user1.Id).UserName.ShouldBe("jeremy.miller");
                session.Load<User>(user2.Id).UserName.ShouldBe("corey.kaylor");
                session.Load<User>(user3.Id).UserName.ShouldBe("tim.cools");
            }
        }
        public void persist_and_reload_a_document()
        {
            var user = new User { FirstName = "James", LastName = "Worthy" };

            theSession.Store(user);
            theSession.SaveChanges();

            using (var session2 = theContainer.GetInstance<IDocumentSession>())
            {
                session2.ShouldNotBeSameAs(theSession);

                var user2 = session2.Load<User>(user.Id);

                user.ShouldNotBeSameAs(user2);
                user2.FirstName.ShouldBe(user.FirstName);
                user2.LastName.ShouldBe(user.LastName);
            }
        }
Пример #33
0
        public void include_many_to_list_with_empty_parent_collection()
        {
            var user1 = new User {
            };
            var user2 = new User {
            };
            var user3 = new User {
            };

            theStore.BulkInsert(new User[] { user1, user2, user3 });

            var group1 = new Group {
                Name = "Users", Users = new[] { user1.Id, user2.Id, user3.Id }
            };
            var group2 = new Group {
                Name = "Empty", Users = new Guid[0]
            };

            using (var session = theStore.OpenSession())
            {
                session.Store(group1, group2);
                session.SaveChanges();
            }

            using (var query = theStore.QuerySession())
            {
                query.Logger = new TestOutputMartenLogger(_output);

                var list = new List <User>();

                var groups = query.Query <Group>()
                             .Include(x => x.Users, list)
                             .Where(x => x.Name == "Users" || x.Name == "Empty")
                             .ToList();

                groups.Count.ShouldBe(2);

                list.Count.ShouldBe(3);
                list.Any(x => x.Id == user1.Id).ShouldBeTrue();
                list.Any(x => x.Id == user2.Id).ShouldBeTrue();
                list.Any(x => x.Id == user3.Id).ShouldBeTrue();
            }
        }
        public void pending_changes_by_type()
        {
            using (var session = theStore.LightweightSession())
            {
                var user1 = new User();
                var user2 = new User();

                session.Store(user1, user2);

                var target1 = new Target();
                var target2 = new Target();

                session.Store(target1, target2);


                session.PendingChanges.UpdatesFor<User>().ShouldHaveTheSameElementsAs(user1, user2);
                session.PendingChanges.UpdatesFor<Target>().ShouldHaveTheSameElementsAs(target1, target2);
            }
        }
Пример #35
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();
            }
        }
Пример #36
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);
            }
        }
        public void persist_a_single_document()
        {
            var user = new User {FirstName = "Magic", LastName = "Johnson"};

            theSession.Store(user);

            theSession.SaveChanges();

            var runner = new CommandRunner(new ConnectionSource());

            var json = runner.QueryScalar<string>("select data from mt_doc_user where id = '{0}'".ToFormat(user.Id));

            json.ShouldNotBeNull();

            var loadedUser = new JsonNetSerializer().FromJson<User>(json);

            user.ShouldNotBeSameAs(loadedUser);
            loadedUser.FirstName.ShouldBe(user.FirstName);
            loadedUser.LastName.ShouldBe(user.LastName);
        }
        public void end_to_end()
        {
            using (var container = Container.For<DevelopmentModeRegistry>())
            {
                container.GetInstance<DocumentCleaner>().CompletelyRemove(typeof(User));

                var schema = container.GetInstance<IDocumentSchema>();
                schema.MappingFor(typeof (User)).DuplicateField("FirstName");

                var user1 = new User {FirstName = "Byron", LastName = "Scott"};
                using (var session = container.GetInstance<IDocumentSession>())
                {
                    session.Store(user1);
                    session.SaveChanges();
                }

                var runner = container.GetInstance<CommandRunner>();
                runner.QueryScalar<string>($"select first_name from mt_doc_user where id = '{user1.Id.ToString()}'")
                    .ShouldBe("Byron");
            }
        }
        public void load_by_id_array()
        {
            var user1 = new User { FirstName = "Magic", LastName = "Johnson" };
            var user2 = new User { FirstName = "James", LastName = "Worthy" };
            var user3 = new User { FirstName = "Michael", LastName = "Cooper" };
            var user4 = new User { FirstName = "Mychal", LastName = "Thompson" };
            var user5 = new User { FirstName = "Kurt", LastName = "Rambis" };

            theSession.Store(user1);
            theSession.Store(user2);
            theSession.Store(user3);
            theSession.Store(user4);
            theSession.Store(user5);
            theSession.SaveChanges();

            using (var session = theContainer.GetInstance<IDocumentSession>())
            {
                var users = session.Load<User>().ById(user2.Id, user3.Id, user4.Id);
                users.Count().ShouldBe(3);
            }
        }
Пример #40
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();
            }
        }
Пример #41
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);
            }
        }
        public void pending_changes_from_store()
        {
            using (var session = theStore.LightweightSession())
            {
                var user1 = new User();
                var user2 = new User();

                session.Store(user1, user2);

                var target1 = new Target();
                var target2 = new Target();

                session.Store(target1, target2);


                session.PendingChanges.Updates().Count().ShouldBe(4);
                session.PendingChanges.Updates().ShouldContain(user1);
                session.PendingChanges.Updates().ShouldContain(user2);
                session.PendingChanges.Updates().ShouldContain(target1);
                session.PendingChanges.Updates().ShouldContain(target2);
            }
        }
        public void pending_deletions()
        {
            using (var session = theStore.LightweightSession())
            {
                var user1 = new User();
                var user2 = new User();

                session.Delete(user1);
                session.Delete<User>(user2.Id);

                var target1 = new Target();

                session.Delete(target1);

                session.PendingChanges.Deletions().Count().ShouldBe(3);
                session.PendingChanges.DeletionsFor(typeof(Target)).Single().Id.ShouldBe(target1.Id);
                session.PendingChanges.DeletionsFor(typeof(Target)).Single().Document.ShouldBe(target1);

                session.PendingChanges.DeletionsFor<User>().Count().ShouldBe(2);
                session.PendingChanges.DeletionsFor<User>().Any(x => x.Document == user1).ShouldBeTrue();
                session.PendingChanges.DeletionsFor<User>().Any(x => x.Id.As<Guid>() == user2.Id).ShouldBeTrue();
            }
        }
        public void pending_with_dirty_checks()
        {
            var user1 = new User();
            var user2 = new User();

            using (var session1 = theStore.LightweightSession())
            {
                session1.Store(user1, user2);
                session1.SaveChanges();
            }

            using (var session2 = theStore.DirtyTrackedSession())
            {
                var user12 = session2.Load<User>(user1.Id);
                var user22 = session2.Load<User>(user2.Id);
                user12.FirstName = "Hank";

                session2.PendingChanges.UpdatesFor<User>().Single()
                    .ShouldBe(user12);

                session2.PendingChanges.UpdatesFor<User>().ShouldNotContain(user22);
            }
        }
        public async Task query_with_select_in_query_async()
        {
            using (var container = Container.For<DevelopmentModeRegistry>())
            {
                var store = container.GetInstance<IDocumentStore>();

                // SAMPLE: using-queryasync
                using (var session = store.OpenSession())
                {
                    var u = new User { FirstName = "Jeremy", LastName = "Miller" };
                    session.Store(u);
                    session.SaveChanges();

                    var users = await session.QueryAsync<User>("select data from mt_doc_user where data ->> 'FirstName' = 'Jeremy'");
                    var user = users.Single();

                    user.LastName.ShouldBe("Miller");
                    user.Id.ShouldBe(u.Id);
                }
                // ENDSAMPLE

            }
        }
Пример #46
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();
            }
        }
Пример #47
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();
            }
        }
        public void call_listener_events_on_document_query_and_dirty_tracking_session()
        {
            var stub1 = new StubDocumentSessionListener();
            var stub2 = new StubDocumentSessionListener();

            using (var store = DocumentStore.For(_ =>
            {
                _.Connection(ConnectionSource.ConnectionString);
                _.AutoCreateSchemaObjects = AutoCreate.All;

                _.Listeners.Add(stub1);
                _.Listeners.Add(stub2);
            }))
            {
                store.Advanced.Clean.CompletelyRemoveAll();

                var user1 = new User { Id = Guid.NewGuid() };
                var user2 = new User { Id = Guid.NewGuid() };

                using (var session = store.OpenSession())
                {
                    session.StoreObjects(new[] { user1, user2 });
                    session.SaveChanges();
                }

                using (var session = store.DirtyTrackedSession())
                {
                    var users = session.Query<User>().ToList();

                    stub1.LoadedDocuments.ShouldContainKeyAndValue(user1.Id, users.FirstOrDefault(where => where.Id == user1.Id));
                    stub1.LoadedDocuments.ShouldContainKeyAndValue(user2.Id, users.FirstOrDefault(where => where.Id == user2.Id));

                    stub2.LoadedDocuments.ShouldContainKeyAndValue(user1.Id, users.FirstOrDefault(where => where.Id == user1.Id));
                    stub2.LoadedDocuments.ShouldContainKeyAndValue(user2.Id, users.FirstOrDefault(where => where.Id == user2.Id));
                }
            }
        }
        public void call_listener_events_on_document_store()
        {
            var stub1 = new StubDocumentSessionListener();
            var stub2 = new StubDocumentSessionListener();

            using (var store = DocumentStore.For(_ =>
            {
                _.Connection(ConnectionSource.ConnectionString);
                _.AutoCreateSchemaObjects = AutoCreate.All;

                _.Listeners.Add(stub1);
                _.Listeners.Add(stub2);
            }))
            {
                store.Advanced.Clean.CompletelyRemoveAll();

                using (var session = store.OpenSession())
                {
                    var user1 = new User { Id = Guid.NewGuid() };
                    var user2 = new User { Id = Guid.NewGuid() };

                    session.Store(user1, user2);

                    stub1.StoredDocuments.ShouldContainKeyAndValue(user1.Id, user1);
                    stub1.StoredDocuments.ShouldContainKeyAndValue(user2.Id, user2);

                    stub2.StoredDocuments.ShouldContainKeyAndValue(user1.Id, user1);
                    stub2.StoredDocuments.ShouldContainKeyAndValue(user2.Id, user2);
                }
            }
        }
Пример #50
0
        public void end_to_end_test_using_the_transform()
        {
            using (var store = TestingDocumentStore.Basic())
            {
                var user = new User {FirstName = "Jeremy", LastName = "Miller"};
                var json = new TestsSerializer().ToCleanJson(user);

                var func = TransformFunction.ForFile(new StoreOptions(), _getFullnameJs);

                using (var conn = store.Advanced.OpenConnection())
                {
                    conn.Execute(cmd => cmd.Sql(func.GenerateFunction()).ExecuteNonQuery());

                    var actual = conn.Execute(cmd =>
                    {
                        return cmd.Sql("select mt_transform_get_fullname(:json)")
                            .WithJsonParameter("json", json).ExecuteScalar().As<string>();
                    });

                    actual.ShouldBe("{\"fullname\": \"Jeremy Miller\"}");
                }

                
            }
        }