public async void TestMetadataQueries()
        {
            var configManager = new ServiceDbConfigManager("TestService");
            var dbAccess      = await CreateDbAccess(configManager);

            var dbAccessProvider = new TestDocumentDbAccessProvider(dbAccess);

            var fruitStore = new FruitStore(dbAccessProvider);

            var bartlett = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Red"
            };

            var comice = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Green"
            };

            for (int i = 0; i < 50; i++)
            {
                // Mutate the record.
                bartlett.Colour = i % 2 == 0 ? "Red" : "DarkRed";

                await fruitStore.UpsertPear(bartlett);

                await fruitStore.UpsertPear(comice);
            }

            await fruitStore.DeletePearById(bartlett.Id);

            var r1 = await fruitStore.GetPearByQuery("[x].Colour = @colour", new[] { new DbParameter("colour", "Red") });

            Assert.True(r1.All(x => x.Colour == "Red"));

            var r2 = await fruitStore.GetPearVersions(bartlett.Id.ToString());

            Assert.NotNull(r2);
            Assert.Equal(51, r2.Metadata.Count);
            Assert.Equal(1, r2.Metadata[0].Version);
            Assert.False(r2.Metadata[0].IsDeleted);

            var lastVersion = r2.Metadata.Last();

            Assert.Equal(51, lastVersion.Version);
            Assert.True(lastVersion.IsDeleted);
            Assert.True(lastVersion.CreatedTime < lastVersion.ModifiedTime);
            Assert.Equal(r2.Metadata[0].CreatedTime, lastVersion.CreatedTime);
        }
        public async void TestCustomDocumentMetadata()
        {
            var configManager    = new ServiceDbConfigManager("TestService");
            var dbAccess         = CreateDbAccess(configManager);
            var dbAccessProvider = new TestDocumentDbAccessProvider(dbAccess);
            var metadataSource   = new TestDocumentMetadataSource("User1");

            var fruitStore = new FruitStore(dbAccessProvider, metadataSource);

            await dbAccess.Open(new[] { fruitStore });

            var bartlett = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Red"
            };

            await fruitStore.UpsertPear(bartlett);

            metadataSource.ActorId = "User2";
            await fruitStore.UpsertPear(bartlett);

            metadataSource.ActorId = "User3";
            await fruitStore.DeletePearById(bartlett.Id);

            var r1 = await fruitStore.GetPearById(bartlett.Id, 1);

            Assert.Equal("User1", r1.Metadata.ActorId);

            var r2 = await fruitStore.GetPearById(bartlett.Id, 2);

            Assert.Equal("User2", r2.Metadata.ActorId);

            var r3 = await fruitStore.GetPearById(bartlett.Id, 3);

            Assert.True(r3.Metadata.IsDeleted);
            Assert.Equal("User3", r3.Metadata.ActorId);
        }
        public async void TestDocumentPurge()
        {
            var configManager    = new ServiceDbConfigManager("TestService");
            var dbAccess         = CreateDbAccess(configManager);
            var dbAccessProvider = new TestDocumentDbAccessProvider(dbAccess);

            var fruitStore = new FruitStore(dbAccessProvider);

            await dbAccess.Open(new[] { fruitStore });

            var gala = new Apple {
                Id = Guid.NewGuid(), Type = "Gala"
            };
            var opal = new Apple {
                Id = Guid.NewGuid(), Type = "Opal"
            };

            var redBartlett = new Pear {
                Id = Guid.NewGuid(), Colour = "Red"
            };
            var darkRedBartlett = new Pear {
                Id = Guid.NewGuid(), Colour = "DarkRed"
            };

            await fruitStore.UpsertApple(gala);

            await fruitStore.UpsertApple(gala);

            await fruitStore.UpsertApple(gala);

            await fruitStore.UpsertApple(opal);

            await fruitStore.UpsertPear(redBartlett);

            await fruitStore.UpsertPear(darkRedBartlett);

            await fruitStore.UpsertPear(darkRedBartlett);

            await fruitStore.DeletePearById(darkRedBartlett.Id);

            var galaResult = await fruitStore.GetAppleVersions(gala.Id);

            var opalResult = await fruitStore.GetAppleVersions(opal.Id);

            var redBartlettResult = await fruitStore.GetPearVersions(redBartlett.Id);

            var darkRedBartlettResult = await fruitStore.GetPearVersions(redBartlett.Id);

            Assert.NotNull(galaResult);
            Assert.NotNull(opalResult);
            Assert.NotNull(redBartlettResult);
            Assert.NotNull(darkRedBartlettResult);

            await fruitStore.PurgeAllPears();

            galaResult = await fruitStore.GetAppleVersions(gala.Id);

            opalResult = await fruitStore.GetAppleVersions(opal.Id);

            redBartlettResult = await fruitStore.GetPearVersions(redBartlett.Id);

            darkRedBartlettResult = await fruitStore.GetPearVersions(redBartlett.Id);

            Assert.NotNull(galaResult);
            Assert.NotNull(opalResult);
            Assert.Null(redBartlettResult);
            Assert.Null(darkRedBartlettResult);

            await fruitStore.PurgeApple(gala.Id);

            galaResult = await fruitStore.GetAppleVersions(gala.Id);

            opalResult = await fruitStore.GetAppleVersions(opal.Id);

            Assert.Null(galaResult);
            Assert.NotNull(opalResult);

            await fruitStore.UpsertApple(gala);

            var versionedGalaResult = await fruitStore.GetVersionedAppleById(gala.Id);

            Assert.NotNull(versionedGalaResult);
            Assert.Equal(1, versionedGalaResult.Metadata.Version);
        }
        public async void TestVersionedQueries()
        {
            var configManager    = new ServiceDbConfigManager("TestService");
            var dbAccess         = CreateDbAccess(configManager);
            var dbAccessProvider = new TestDocumentDbAccessProvider(dbAccess);

            var fruitStore = new FruitStore(dbAccessProvider);

            await dbAccess.Open(new[] { fruitStore });

            var bartlett = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Red"
            };

            var comice = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Green"
            };

            for (int i = 0; i < 50; i++)
            {
                // Mutate the record.
                bartlett.Colour = i % 2 == 0 ? "Red" : "DarkRed";

                await fruitStore.UpsertPear(bartlett);

                await fruitStore.UpsertPear(comice);
            }

            await fruitStore.DeletePearById(bartlett.Id);

            const int lastVersion = 51;

            var r1 = await fruitStore.GetPearById(bartlett.Id, lastVersion);

            Assert.Equal(lastVersion, r1.Metadata.Version);
            Assert.True(r1.Metadata.IsDeleted);
            Assert.Equal("DarkRed", r1.Document.Colour);

            var r2 = await fruitStore.GetPearById(bartlett.Id, lastVersion - 1);

            Assert.Equal(lastVersion - 1, r2.Metadata.Version);
            Assert.False(r2.Metadata.IsDeleted);
            Assert.Equal("DarkRed", r2.Document.Colour);

            var r3 = await fruitStore.GetPearById(bartlett.Id, lastVersion - 2);

            Assert.Equal(lastVersion - 2, r3.Metadata.Version);
            Assert.False(r3.Metadata.IsDeleted);
            Assert.Equal("Red", r3.Document.Colour);

            var r4 = await fruitStore.GetPearById(bartlett.Id, 1);

            Assert.Equal(1, r4.Metadata.Version);
            Assert.False(r4.Metadata.IsDeleted);
            Assert.Equal("Red", r4.Document.Colour);

            var r5 = await fruitStore.GetPearById(comice.Id, 1);

            Assert.Equal(1, r5.Metadata.Version);
            Assert.False(r5.Metadata.IsDeleted);
            Assert.Equal("Green", r5.Document.Colour);
        }
        public async void TestMetadataQueries()
        {
            var configManager    = new ServiceDbConfigManager("TestService");
            var dbAccess         = CreateDbAccess(configManager);
            var dbAccessProvider = new TestDocumentDbAccessProvider(dbAccess);

            var fruitStore = new FruitStore(dbAccessProvider);

            await dbAccess.Open(new[] { fruitStore });

            var bartlett = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Red"
            };

            var comice = new Pear
            {
                Id     = Guid.NewGuid(),
                Colour = "Green"
            };

            for (int i = 0; i < 5; i++)
            {
                // Mutate the record.
                bartlett.Colour = i % 2 == 0 ? "Red" : "DarkRed";

                await fruitStore.UpsertPear(bartlett);

                await fruitStore.UpsertPear(comice);

                Thread.Sleep(1000);
            }

            await fruitStore.DeletePearById(bartlett.Id);

            var r1 = await fruitStore.GetPearByQuery("[x].Colour = @colour", new[] { new DbParameter("colour", "Red") });

            Assert.True(r1.All(x => x.Colour == "Red"));

            var r2 = await fruitStore.GetPearVersions(bartlett.Id);

            Assert.NotNull(r2);
            Assert.Equal(6, r2.Metadata.Count);
            Assert.Equal(1, r2.Metadata[0].Version);
            Assert.False(r2.Metadata[0].IsDeleted);

            var lastVersion = r2.Metadata.Last();

            Assert.Equal(6, lastVersion.Version);
            Assert.True(lastVersion.IsDeleted);
            Assert.True(lastVersion.CreatedTime < lastVersion.ModifiedTime);
            Assert.Equal(r2.Metadata[0].CreatedTime, lastVersion.CreatedTime);

            for (var i = 1; i < r2.Metadata.Count; i++)
            {
                var prev    = r2.Metadata[i - 1];
                var current = r2.Metadata[i];

                Assert.True(current.ModifiedTime > prev.ModifiedTime);
                Assert.True(current.ModifiedTime - prev.ModifiedTime >= TimeSpan.FromSeconds(1));

                Assert.True(current.ModifiedTime > prev.CreatedTime);
                Assert.Equal(current.CreatedTime, prev.CreatedTime);

                Assert.False(prev.IsDeleted);
            }
        }