示例#1
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);
            }
        }
示例#2
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);
            }
        }
示例#3
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);
            }
        }
示例#4
0
        public async Task CreateDatabase_402_ThrowsException()
        {
            using var httpTest = new HttpTest();
            // Operation result
            httpTest.RespondWith((string)null, 412);
            // Logout
            httpTest.RespondWithJson(new { ok = true });

            await using var client = new CouchClient("http://localhost");
            Func <Task> action = () => client.CreateDatabaseAsync <Rebel>();
            await Assert.ThrowsAsync <CouchException>(action);
        }
示例#5
0
        public async Task CreateDatabaseAsync_Default()
        {
            using var httpTest = new HttpTest();
            // Logout
            httpTest.RespondWithJson(new { ok = true });

            await using var client = new CouchClient("http://localhost");
            httpTest.RespondWithJson(new { ok = true });
            var rebels = await client.CreateDatabaseAsync <Rebel>();

            httpTest
            .ShouldHaveCalled("http://localhost/rebels")
            .WithVerb(HttpMethod.Put);
            Assert.Equal("rebels", rebels.Database);
        }
示例#6
0
        public async Task CreateDatabase_InvalidCharacters_ThrowsArgumentException()
        {
            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"))
                {
                    Func <Task> action = () => client.CreateDatabaseAsync <Rebel>("rebel.");
                    var         ex     = await Assert.ThrowsAsync <ArgumentException>(action);

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

            await using var client = new CouchClient("http://localhost");
            httpTest.RespondWithJson(new { ok = true });
            var rebels = await client.CreateDatabaseAsync <Rebel>(9, 2, true);

            httpTest
            .ShouldHaveCalled("http://localhost/rebels*")
            .WithQueryParam("q", 9)
            .WithQueryParam("n", 2)
            .WithQueryParam("partitioned", "true")
            .WithVerb(HttpMethod.Put);
            Assert.Equal("rebels", rebels.Database);
        }
示例#8
0
        public async Task CreateDatabase_CustomName()
        {
            using (var httpTest = new HttpTest())
            {
                // Logout
                httpTest.RespondWithJson(new { ok = true });

                using (var client = new CouchClient("http://localhost"))
                {
                    httpTest.RespondWithJson(new { ok = true });
                    var rebels = await client.CreateDatabaseAsync <Rebel>("some_rebels");

                    httpTest
                    .ShouldHaveCalled("http://localhost/some_rebels")
                    .WithVerb(HttpMethod.Put);
                    Assert.Equal("some_rebels", rebels.Database);
                }
            }
        }
示例#9
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);
            }
        }
示例#10
0
        public async Task CreateDatabase_CustomCharacterName()
        {
            var databaseName = "rebel0_$()+/-";

            using (var httpTest = new HttpTest())
            {
                // Logout
                httpTest.RespondWithJson(new { ok = true });

                using (var client = new CouchClient("http://localhost"))
                {
                    httpTest.RespondWithJson(new { ok = true });
                    var rebels = await client.CreateDatabaseAsync <Rebel>(databaseName);

                    httpTest
                    .ShouldHaveCalled("http://localhost/rebel0_%24%28%29%2B%2F-")
                    .WithVerb(HttpMethod.Put);
                    Assert.Equal(databaseName, rebels.Database);
                }
            }
        }
示例#11
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);
            }
        }
示例#12
0
        public async Task ConflictException()
        {
            using (var httpTest = new HttpTest())
            {
                httpTest.RespondWith(status: (int)HttpStatusCode.Conflict);

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

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