public void doc_metadata_is_updated_for_user_supplied_query()
        {
            StoreOptions(c =>
            {
                c.Schema.For <DocWithMeta>().Metadata(m => m.LastModified.MapTo(x => x.LastModified));
            });

            var            doc     = new DocWithMeta();
            DateTimeOffset lastMod = DateTime.UtcNow;

            using (var session = theStore.OpenSession())
            {
                session.Store(doc);
                session.MetadataFor(doc).ShouldBeNull();
                session.SaveChanges();
            }

            using (var session = theStore.OpenSession())
            {
                var userQuery = session.Query <DocWithMeta>($"where data ->> 'Id' = '{doc.Id.ToString()}'").Single();
                userQuery.Description = "updated via a user SQL query";
                ShouldBeTestExtensions.ShouldNotBe(userQuery.LastModified, DateTimeOffset.MinValue);
                lastMod = userQuery.LastModified;
                session.Store(userQuery);
                session.SaveChanges();
            }

            using (var session = theStore.OpenSession())
            {
                var userQuery = session.Query <DocWithMeta>($"where data ->> 'Id' = '{doc.Id.ToString()}'").Single();
                ShouldBeTestExtensions.ShouldBeGreaterThanOrEqualTo(userQuery.LastModified, lastMod);
            }
        }
        public void doc_metadata_is_mapped_for_query_includes()
        {
            StoreOptions(c =>
            {
                c.Schema.For <DocWithMeta>().Metadata(m => m.LastModified.MapTo(x => x.LastModified));
            });

            var include = new IncludedDocWithMeta();
            var doc     = new DocWithMeta();

            using (var session = theStore.OpenSession())
            {
                session.Store(include);
                doc.IncludedDocId = include.Id;
                session.Store(doc);
                session.MetadataFor(include).ShouldBeNull();
                session.MetadataFor(doc).ShouldBeNull();
                session.SaveChanges();
            }

            using (var session = theStore.OpenSession())
            {
                IncludedDocWithMeta loadedInclude = null;
                var loaded = session.Query <DocWithMeta>().Include <IncludedDocWithMeta>(d => d.IncludedDocId, i => loadedInclude = i).Single(d => d.Id == doc.Id);

                loaded.ShouldNotBeNull();

                loadedInclude.ShouldNotBeNull();
                loadedInclude.Version.ShouldNotBe(Guid.Empty);
            }
        }
        public void doc_metadata_is_mapped_for_doc_hierarchies()
        {
            StoreOptions(c =>
            {
                c.Schema.For <DocWithMeta>()
                .AddSubClassHierarchy(typeof(RedDocWithMeta), typeof(BlueDocWithMeta), typeof(GreenDocWithMeta),
                                      typeof(EmeraldGreenDocWithMeta))
                .SoftDeleted()
                .Metadata(m =>
                {
                    m.IsSoftDeleted.MapTo(x => x.Deleted);
                    m.SoftDeletedAt.MapTo(x => x.DeletedAt);
                    m.DocumentType.MapTo(x => x.DocType);
                });
            });

            using (var session = theStore.OpenSession())
            {
                var doc = new DocWithMeta {
                    Description = "transparent"
                };
                var red = new RedDocWithMeta {
                    Description = "red doc"
                };
                var green = new GreenDocWithMeta {
                    Description = "green doc"
                };
                var blue = new BlueDocWithMeta {
                    Description = "blue doc"
                };
                var emerald = new EmeraldGreenDocWithMeta {
                    Description = "emerald doc"
                };

                session.Store(doc, red, green, blue, emerald);
                session.SaveChanges();
            }

            using (var session = theStore.OpenSession())
            {
                session.Query <DocWithMeta>().Count(d => d.DocType == "BASE").ShouldBe(1);
                session.Query <DocWithMeta>().Count(d => d.DocType == "blue_doc_with_meta").ShouldBe(1);
                session.Query <DocWithMeta>().Count(d => d.DocType == "red_doc_with_meta").ShouldBe(1);
                session.Query <DocWithMeta>().Count(d => d.DocType == "green_doc_with_meta").ShouldBe(1);
                session.Query <DocWithMeta>().Count(d => d.DocType == "emerald_green_doc_with_meta").ShouldBe(1);


                var redDocs = session.Query <RedDocWithMeta>().ToList();
                redDocs.Count.ShouldBe(1);
                ShouldBeStringTestExtensions.ShouldBe(redDocs.First().DocType, "red_doc_with_meta");
                session.Delete(redDocs.First());

                session.SaveChanges();
            }
        }
        public void doc_has_projected_data_after_storage()
        {
            StoreOptions(c =>
            {
                c.Schema.For <DocWithMeta>()
                .Metadata(m => m.LastModified.MapTo(x => x.LastModified));
            });

            var doc = new DocWithMeta();

            using (var session = theStore.OpenSession())
            {
                session.Store(doc);
                session.MetadataFor(doc).ShouldBeNull();
                session.SaveChanges();
            }

            using (var session = theStore.OpenSession())
            {
                var loaded = session.Load <DocWithMeta>(doc.Id);
                ShouldBeTestExtensions.ShouldNotBe(loaded.LastModified, DateTimeOffset.MinValue);
            }
        }