|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Notion.Client; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Notion.IntegrationTests; |
| 9 | + |
| 10 | +public class DatabasesClientTests : IntegrationTestBase, IDisposable |
| 11 | +{ |
| 12 | + private readonly Page _page; |
| 13 | + |
| 14 | + public DatabasesClientTests() |
| 15 | + { |
| 16 | + _page = Client.Pages.CreateAsync( |
| 17 | + PagesCreateParametersBuilder.Create( |
| 18 | + new ParentPageInput { PageId = ParentPageId } |
| 19 | + ).Build() |
| 20 | + ).Result; |
| 21 | + } |
| 22 | + |
| 23 | + public void Dispose() |
| 24 | + { |
| 25 | + Client.Pages.UpdateAsync(_page.Id, new PagesUpdateParameters { Archived = true }).Wait(); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task QueryDatabase() |
| 30 | + { |
| 31 | + // Arrange |
| 32 | + var createdDatabase = await CreateDatabaseWithAPageAsync(); |
| 33 | + |
| 34 | + |
| 35 | + // Act |
| 36 | + var response = await Client.Databases.QueryAsync(createdDatabase.Id, new DatabasesQueryParameters()); |
| 37 | + |
| 38 | + // Assert |
| 39 | + Assert.NotNull(response.Results); |
| 40 | + Assert.Single(response.Results); |
| 41 | + var page = response.Results.Cast<Page>().First(); |
| 42 | + var title = page.Properties["Name"] as TitlePropertyValue; |
| 43 | + Assert.Equal("Test Title", (title!.Title.Cast<RichTextText>().First()).Text.Content); |
| 44 | + } |
| 45 | + |
| 46 | + private async Task<Database> CreateDatabaseWithAPageAsync() |
| 47 | + { |
| 48 | + var createDbRequest = new DatabasesCreateParameters |
| 49 | + { |
| 50 | + Title = new List<RichTextBaseInput> |
| 51 | + { |
| 52 | + new RichTextTextInput |
| 53 | + { |
| 54 | + Text = new Text |
| 55 | + { |
| 56 | + Content = "Test List", |
| 57 | + Link = null |
| 58 | + } |
| 59 | + } |
| 60 | + }, |
| 61 | + Properties = new Dictionary<string, IPropertySchema> |
| 62 | + { |
| 63 | + { "Name", new TitlePropertySchema { Title = new Dictionary<string, object>() } }, |
| 64 | + }, |
| 65 | + Parent = new ParentPageInput { PageId = _page.Id } |
| 66 | + }; |
| 67 | + |
| 68 | + var createdDatabase = await Client.Databases.CreateAsync(createDbRequest); |
| 69 | + |
| 70 | + var pagesCreateParameters = PagesCreateParametersBuilder |
| 71 | + .Create(new DatabaseParentInput { DatabaseId = createdDatabase.Id }) |
| 72 | + .AddProperty("Name", |
| 73 | + new TitlePropertyValue |
| 74 | + { |
| 75 | + Title = new List<RichTextBase> |
| 76 | + { |
| 77 | + new RichTextText { Text = new Text { Content = "Test Title" } } |
| 78 | + } |
| 79 | + }) |
| 80 | + .Build(); |
| 81 | + |
| 82 | + await Client.Pages.CreateAsync(pagesCreateParameters); |
| 83 | + |
| 84 | + return createdDatabase; |
| 85 | + } |
| 86 | +} |
0 commit comments