Beispiel #1
0
        public async Task doc_metadata_is_mapped_for_bulk_inserted_conjoined_tenant()
        {
            StoreOptions(c =>
            {
                c.Schema.For <DocWithMeta>()
                .MultiTenanted()
                .Metadata(m =>
                {
                    m.TenantId.MapTo(x => x.TenantId);
                    m.LastModified.MapTo(x => x.LastModified);
                });
            });

            var doc    = new DocWithMeta();
            var tenant = "TENANT_A";

            await theStore.BulkInsertAsync(tenant, new DocWithMeta[] { doc });

            using var session = theStore.OpenSession(tenant);
            session.Query <DocWithMeta>().Count(d => d.TenantId == tenant).ShouldBe(1);

            var loaded = await session.Query <DocWithMeta>().Where(d => d.Id == doc.Id).FirstOrDefaultAsync();

            loaded.TenantId.ShouldBe(tenant);
            (DateTime.UtcNow - loaded.LastModified.ToUniversalTime()).ShouldBeLessThan(TimeSpan.FromMinutes(1));
        }
Beispiel #2
0
        public async Task doc_metadata_is_mapped_for_conjoined_tenant()
        {
            StoreOptions(c =>
            {
                c.Schema.For <DocWithMeta>()
                .MultiTenanted()
                .Metadata(m =>
                {
                    m.TenantId.MapTo(x => x.TenantId);
                    m.LastModified.MapTo(x => x.LastModified);
                });
            });

            var doc    = new DocWithMeta();
            var tenant = "TENANT_A";

            using (var session = theStore.OpenSession(tenant))
            {
                doc.LastModified = DateTime.UtcNow.AddYears(-1);
                session.Store(doc);
                (await session.MetadataForAsync(doc)).ShouldBeNull();
                await session.SaveChangesAsync();
            }

            using (var session = theStore.OpenSession(tenant))
            {
                session.Query <DocWithMeta>().Count(d => d.TenantId == tenant).ShouldBe(1);

                var loaded = await session.Query <DocWithMeta>().Where(d => d.Id == doc.Id).FirstOrDefaultAsync();

                loaded.TenantId.ShouldBe(tenant);
                loaded.LastModified.ShouldNotBe(DateTimeOffset.MinValue); // it's pretty well impossible to compare timestamps
            }
        }
Beispiel #3
0
        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";
                userQuery.LastModified.ShouldNotBe(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();
                userQuery.LastModified.ShouldBeGreaterThanOrEqualTo(lastMod);
            }
        }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        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);
                redDocs.First().DocType.ShouldBe("red_doc_with_meta");
                session.Delete(redDocs.First());

                session.SaveChanges();
            }
        }
Beispiel #6
0
        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);
                loaded.LastModified.ShouldNotBe(DateTimeOffset.MinValue);
            }
        }