Example #1
0
        public async Task Changes()
        {
            using (var client = new CouchClient("http://localhost:5984"))
            {
                IEnumerable <string> dbs = await client.GetDatabasesNamesAsync().ConfigureAwait(false);

                CouchDatabase <Rebel> rebels = client.GetDatabase <Rebel>();

                if (dbs.Contains(rebels.Database))
                {
                    await client.DeleteDatabaseAsync <Rebel>().ConfigureAwait(false);
                }

                rebels = await client.CreateDatabaseAsync <Rebel>().ConfigureAwait(false);

                Rebel luke = await rebels.CreateAsync(new Rebel { Name = "Luke", Age = 19 }).ConfigureAwait(false);

                Assert.Equal("Luke", luke.Name);

                var options = new ChangesFeedOptions
                {
                    IncludeDocs = true
                };
                var filter        = ChangesFeedFilter.Selector <Rebel>(r => r.Name == "Luke" && r.Age == 19);
                var changesResult = await rebels.GetChangesAsync(options, filter);

                Assert.NotEmpty(changesResult.Results);
                Assert.Equal(changesResult.Results[0].Id, luke.Id);

                await client.DeleteDatabaseAsync <Rebel>().ConfigureAwait(false);
            }
        }
Example #2
0
        public async Task Attachment()
        {
            using (var client = new CouchClient("http://*****:*****@"{runningPath}\Assets\luke.txt", MediaTypeNames.Text.Plain);
                luke = await rebels.CreateAsync(luke).ConfigureAwait(false);

                Assert.Equal("Luke", luke.Name);
                Assert.NotEmpty(luke.Attachments);

                CouchAttachment attachment = luke.Attachments.First();
                Assert.NotNull(attachment);
                Assert.NotNull(attachment.Uri);

                // Download
                var downloadFilePath = await rebels.DownloadAttachment(attachment, $@"{runningPath}\Assets", "luke-downloaded.txt");

                Assert.True(File.Exists(downloadFilePath));
                File.Delete(downloadFilePath);

                // Find
                luke = await rebels.FindAsync(luke.Id).ConfigureAwait(false);

                Assert.Equal(19, luke.Age);
                attachment = luke.Attachments.First();
                Assert.NotNull(attachment);
                Assert.NotNull(attachment.Uri);
                Assert.NotNull(attachment.Digest);
                Assert.NotNull(attachment.Length);

                // Update
                luke.Surname = "Skywalker";
                luke         = await rebels.CreateOrUpdateAsync(luke).ConfigureAwait(false);

                Assert.Equal("Skywalker", luke.Surname);

                await client.DeleteDatabaseAsync <Rebel>().ConfigureAwait(false);
            }
        }
Example #3
0
        public async Task Users()
        {
            using (var client = new CouchClient("http://localhost:5984"))
            {
                IEnumerable <string> dbs = await client.GetDatabasesNamesAsync().ConfigureAwait(false);

                CouchDatabase <CouchUser> users = client.GetUsersDatabase();

                if (!dbs.Contains(users.Database))
                {
                    users = await client.CreateDatabaseAsync <CouchUser>().ConfigureAwait(false);
                }

                CouchUser luke = await users.CreateAsync(new CouchUser(name : "luke", password : "******")).ConfigureAwait(false);

                Assert.Equal("luke", luke.Name);

                luke = await users.FindAsync(luke.Id).ConfigureAwait(false);

                Assert.Equal("luke", luke.Name);

                await users.DeleteAsync(luke).ConfigureAwait(false);

                luke = await users.FindAsync(luke.Id).ConfigureAwait(false);

                Assert.Null(luke);

                await client.DeleteDatabaseAsync <CouchUser>().ConfigureAwait(false);
            }
        }
Example #4
0
        public async Task ContinuousChanges()
        {
            using (var client = new CouchClient("http://localhost:5984"))
            {
                IEnumerable <string> dbs = await client.GetDatabasesNamesAsync().ConfigureAwait(false);

                CouchDatabase <Rebel> rebels = client.GetDatabase <Rebel>();

                if (dbs.Contains(rebels.Database))
                {
                    await client.DeleteDatabaseAsync <Rebel>().ConfigureAwait(false);
                }

                rebels = await client.CreateDatabaseAsync <Rebel>().ConfigureAwait(false);

                Rebel luke = await rebels.CreateAsync(new Rebel { Name = "Luke", Age = 19 }).ConfigureAwait(false);

                Assert.Equal("Luke", luke.Name);


                luke.Surname = "Vader";
                _            = Task.Run(async() =>
                {
                    await Task.Delay(2000);
                    await rebels.CreateOrUpdateAsync(luke);
                });

                var ids    = new[] { luke.Id };
                var option = new ChangesFeedOptions
                {
                    Since = "now"
                };
                var filter = ChangesFeedFilter.DocumentIds(ids);
                using var cancelSource = new CancellationTokenSource();
                await foreach (var _ in rebels.GetContinuousChangesAsync(option, filter, cancelSource.Token))
                {
                    cancelSource.Cancel();
                }

                await client.DeleteDatabaseAsync <Rebel>().ConfigureAwait(false);
            }
        }
Example #5
0
        public async Task Crud_SpecialCharacters()
        {
            var databaseName = "rebel0_$()+/-";

            using (var client = new CouchClient("http://localhost:5984"))
            {
                IEnumerable <string> dbs = await client.GetDatabasesNamesAsync().ConfigureAwait(false);

                CouchDatabase <Rebel> rebels = client.GetDatabase <Rebel>(databaseName);

                if (dbs.Contains(rebels.Database))
                {
                    await client.DeleteDatabaseAsync <Rebel>(databaseName).ConfigureAwait(false);
                }

                rebels = await client.CreateDatabaseAsync <Rebel>(databaseName).ConfigureAwait(false);

                Rebel luke = await rebels.CreateAsync(new Rebel { Name = "Luke", Age = 19 }).ConfigureAwait(false);

                Assert.Equal("Luke", luke.Name);

                luke.Surname = "Skywalker";
                luke         = await rebels.CreateOrUpdateAsync(luke).ConfigureAwait(false);

                Assert.Equal("Skywalker", luke.Surname);

                luke = await rebels.FindAsync(luke.Id).ConfigureAwait(false);

                Assert.Equal(19, luke.Age);

                await rebels.DeleteAsync(luke).ConfigureAwait(false);

                luke = await rebels.FindAsync(luke.Id).ConfigureAwait(false);

                Assert.Null(luke);

                await client.DeleteDatabaseAsync <Rebel>(databaseName).ConfigureAwait(false);
            }
        }
Example #6
0
        public async Task DeleteDatabase_InvalidCharacters_ThrowsArgumentException()
        {
            using var httpTest = new HttpTest();
            // Operation result
            httpTest.RespondWithJson(new { ok = true });
            // Logout
            httpTest.RespondWithJson(new { ok = true });

            await using var client = new CouchClient("http://localhost");
            Func <Task> action = () => client.DeleteDatabaseAsync("rebel.");
            var         ex     = await Assert.ThrowsAsync <ArgumentException>(action);

            Assert.Contains("invalid characters", ex.Message);
        }
Example #7
0
        public async Task DeleteDatabase_CustomCharacterName()
        {
            using var httpTest = new HttpTest();
            // Operation result
            httpTest.RespondWithJson(new { ok = true });
            // Logout
            httpTest.RespondWithJson(new { ok = true });

            await using var client = new CouchClient("http://localhost");
            await client.DeleteDatabaseAsync("rebel0_$()+/-");

            httpTest
            .ShouldHaveCalled("http://localhost/rebel0_%24%28%29%2B%2F-")
            .WithVerb(HttpMethod.Delete);
        }
Example #8
0
        public async Task DeleteDatabase_CustomName()
        {
            using var httpTest = new HttpTest();
            // Operation result
            httpTest.RespondWithJson(new { ok = true });
            // Logout
            httpTest.RespondWithJson(new { ok = true });

            await using var client = new CouchClient("http://localhost");
            await client.DeleteDatabaseAsync("some_rebels");

            httpTest
            .ShouldHaveCalled("http://localhost/some_rebels")
            .WithVerb(HttpMethod.Delete);
        }
Example #9
0
        public async Task DeleteDatabase_Default()
        {
            using (var httpTest = new HttpTest())
            {
                // Operation result
                httpTest.RespondWithJson(new { ok = true });
                // Logout
                httpTest.RespondWithJson(new { ok = true });

                using (var client = new CouchClient("http://localhost"))
                {
                    await client.DeleteDatabaseAsync <Rebel>();

                    httpTest
                    .ShouldHaveCalled("http://localhost/rebels")
                    .WithVerb(HttpMethod.Delete);
                }
            }
        }
Example #10
0
        public async Task NotFoundException()
        {
            using (var httpTest = new HttpTest())
            {
                httpTest.RespondWith(status: (int)HttpStatusCode.NotFound);

                using (var client = new CouchClient("http://localhost"))
                {
                    var couchException = await Assert.ThrowsAsync <CouchNotFoundException>(() => client.DeleteDatabaseAsync <Rebel>());

                    Assert.IsType <Flurl.Http.FlurlHttpException>(couchException.InnerException);
                }
            }
        }