88
99namespace Notion . IntegrationTests ;
1010
11- public class IPageClientTests : IntegrationTestBase
11+ public class PageClientTests : IntegrationTestBase , IDisposable
1212{
13+ private readonly Page _page ;
14+ private readonly Database _database ;
15+
16+ public PageClientTests ( )
17+ {
18+ // Create a page
19+ _page = Client . Pages . CreateAsync (
20+ PagesCreateParametersBuilder . Create (
21+ new ParentPageInput { PageId = ParentPageId }
22+ ) . Build ( )
23+ ) . Result ;
24+
25+ // Create a database
26+ var createDbRequest = new DatabasesCreateParameters
27+ {
28+ Title = new List < RichTextBaseInput >
29+ {
30+ new RichTextTextInput
31+ {
32+ Text = new Text
33+ {
34+ Content = "Test List" ,
35+ Link = null
36+ }
37+ }
38+ } ,
39+ Properties = new Dictionary < string , IPropertySchema >
40+ {
41+ { "Name" , new TitlePropertySchema { Title = new Dictionary < string , object > ( ) } } ,
42+ {
43+ "TestSelect" ,
44+ new SelectPropertySchema
45+ {
46+ Select = new OptionWrapper < SelectOptionSchema >
47+ {
48+ Options = new List < SelectOptionSchema >
49+ {
50+ new ( ) { Name = "Red" } ,
51+ new ( ) { Name = "Blue" }
52+ }
53+ }
54+ }
55+ } ,
56+ { "Number" , new NumberPropertySchema { Number = new Number { Format = "number" } } }
57+ } ,
58+ Parent = new ParentPageInput { PageId = _page . Id }
59+ } ;
60+
61+ _database = Client . Databases . CreateAsync ( createDbRequest ) . GetAwaiter ( ) . GetResult ( ) ;
62+ }
63+
64+ public void Dispose ( )
65+ {
66+ Client . Pages . UpdateAsync ( _page . Id , new PagesUpdateParameters { Archived = true } ) . GetAwaiter ( ) . GetResult ( ) ;
67+ }
68+
1369 [ Fact ]
1470 public async Task CreateAsync_CreatesANewPage ( )
1571 {
1672 var pagesCreateParameters = PagesCreateParametersBuilder
17- . Create ( new DatabaseParentInput { DatabaseId = ParentDatabaseId } )
73+ . Create ( new DatabaseParentInput { DatabaseId = _database . Id } )
1874 . AddProperty ( "Name" ,
1975 new TitlePropertyValue
2076 {
@@ -30,10 +86,10 @@ public async Task CreateAsync_CreatesANewPage()
3086 page . Should ( ) . NotBeNull ( ) ;
3187
3288 page . Parent . Should ( ) . BeOfType < DatabaseParent > ( ) . Which
33- . DatabaseId . Should ( ) . Be ( ParentDatabaseId ) ;
89+ . DatabaseId . Should ( ) . Be ( _database . Id ) ;
3490
3591 page . Properties . Should ( ) . ContainKey ( "Name" ) ;
36- var pageProperty = page . Properties [ "Name" ] . Should ( ) . BeOfType < PropertyValue > ( ) . Subject ;
92+ var pageProperty = page . Properties [ "Name" ] . Should ( ) . BeOfType < TitlePropertyValue > ( ) . Subject ;
3793
3894 var titleProperty
3995 = ( ListPropertyItem ) await Client . Pages . RetrievePagePropertyItemAsync (
@@ -44,15 +100,14 @@ var titleProperty
44100 } ) ;
45101
46102 titleProperty . Results . First ( ) . As < TitlePropertyItem > ( ) . Title . PlainText . Should ( ) . Be ( "Test Page Title" ) ;
47-
48- await Client . Pages . UpdateAsync ( page . Id , new PagesUpdateParameters { Archived = true } ) ;
49103 }
50104
51105 [ Fact ]
52106 public async Task Bug_unable_to_create_page_with_select_property ( )
53107 {
108+ // Arrange
54109 var pagesCreateParameters = PagesCreateParametersBuilder
55- . Create ( new DatabaseParentInput { DatabaseId = ParentDatabaseId } )
110+ . Create ( new DatabaseParentInput { DatabaseId = _database . Id } )
56111 . AddProperty ( "Name" ,
57112 new TitlePropertyValue
58113 {
@@ -62,37 +117,33 @@ public async Task Bug_unable_to_create_page_with_select_property()
62117 }
63118 } )
64119 . AddProperty ( "TestSelect" ,
65- new SelectPropertyValue { Select = new SelectOption { Id = "dfbfbe65-6f67-4876-9f75-699124334d06 " } } )
120+ new SelectPropertyValue { Select = new SelectOption { Name = "Blue " } } )
66121 . Build ( ) ;
67122
123+ // Act
68124 var page = await Client . Pages . CreateAsync ( pagesCreateParameters ) ;
69125
126+ // Asserts
70127 page . Should ( ) . NotBeNull ( ) ;
71128
72129 page . Parent . Should ( ) . BeOfType < DatabaseParent > ( ) . Which
73- . DatabaseId . Should ( ) . Be ( ParentDatabaseId ) ;
130+ . DatabaseId . Should ( ) . Be ( _database . Id ) ;
74131
75132 page . Properties . Should ( ) . ContainKey ( "Name" ) ;
76- var pageProperty = page . Properties [ "Name" ] . Should ( ) . BeOfType < PropertyValue > ( ) . Subject ;
77-
78- var titleProperty
79- = ( ListPropertyItem ) await Client . Pages . RetrievePagePropertyItemAsync (
80- new RetrievePropertyItemParameters
81- {
82- PageId = page . Id ,
83- PropertyId = pageProperty . Id
84- } ) ;
85-
86- titleProperty . Results . First ( ) . As < TitlePropertyItem > ( ) . Title . PlainText . Should ( ) . Be ( "Test Page Title" ) ;
133+ var titlePropertyValue = page . Properties [ "Name" ] . Should ( ) . BeOfType < TitlePropertyValue > ( ) . Subject ;
134+ titlePropertyValue . Title . First ( ) . PlainText . Should ( ) . Be ( "Test Page Title" ) ;
87135
88- await Client . Pages . UpdateAsync ( page . Id , new PagesUpdateParameters { Archived = true } ) ;
136+ page . Properties . Should ( ) . ContainKey ( "TestSelect" ) ;
137+ var selectPropertyValue = page . Properties [ "TestSelect" ] . Should ( ) . BeOfType < SelectPropertyValue > ( ) . Subject ;
138+ selectPropertyValue . Select . Name . Should ( ) . Be ( "Blue" ) ;
89139 }
90140
91141 [ Fact ]
92142 public async Task Test_RetrievePagePropertyItemAsync ( )
93143 {
144+ // Arrange
94145 var pagesCreateParameters = PagesCreateParametersBuilder
95- . Create ( new DatabaseParentInput { DatabaseId = ParentDatabaseId } )
146+ . Create ( new DatabaseParentInput { DatabaseId = _database . Id } )
96147 . AddProperty ( "Name" ,
97148 new TitlePropertyValue
98149 {
@@ -105,12 +156,14 @@ public async Task Test_RetrievePagePropertyItemAsync()
105156
106157 var page = await Client . Pages . CreateAsync ( pagesCreateParameters ) ;
107158
159+ // Act
108160 var property = await Client . Pages . RetrievePagePropertyItemAsync ( new RetrievePropertyItemParameters
109161 {
110162 PageId = page . Id ,
111163 PropertyId = "title"
112164 } ) ;
113165
166+ // Assert
114167 property . Should ( ) . NotBeNull ( ) ;
115168 property . Should ( ) . BeOfType < ListPropertyItem > ( ) ;
116169
@@ -125,16 +178,14 @@ public async Task Test_RetrievePagePropertyItemAsync()
125178
126179 titleProperty . Title . PlainText . Should ( ) . Be ( "Test Page Title" ) ;
127180 } ) ;
128-
129- // cleanup
130- await Client . Pages . UpdateAsync ( page . Id , new PagesUpdateParameters { Archived = true } ) ;
131181 }
132182
133183 [ Fact ]
134184 public async Task Test_UpdatePageProperty_with_date_as_null ( )
135185 {
136- // setup - add property to db and create a page with the property having a date
186+ // Arrange
137187
188+ // Add property Date property to database
138189 const string DatePropertyName = "Test Date Property" ;
139190
140191 var updateDatabaseParameters = new DatabasesUpdateParameters
@@ -149,8 +200,9 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
149200 }
150201 } ;
151202
203+ // Create a page with the property having a date
152204 var pagesCreateParameters = PagesCreateParametersBuilder
153- . Create ( new DatabaseParentInput { DatabaseId = ParentDatabaseId } )
205+ . Create ( new DatabaseParentInput { DatabaseId = _database . Id } )
154206 . AddProperty ( "Name" ,
155207 new TitlePropertyValue
156208 {
@@ -170,10 +222,11 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
170222 } )
171223 . Build ( ) ;
172224
173- await Client . Databases . UpdateAsync ( ParentDatabaseId , updateDatabaseParameters ) ;
225+ await Client . Databases . UpdateAsync ( _database . Id , updateDatabaseParameters ) ;
174226
175227 var page = await Client . Pages . CreateAsync ( pagesCreateParameters ) ;
176228
229+ // Act
177230 var setDate = ( DatePropertyItem ) await Client . Pages . RetrievePagePropertyItemAsync (
178231 new RetrievePropertyItemParameters
179232 {
@@ -182,50 +235,53 @@ public async Task Test_UpdatePageProperty_with_date_as_null()
182235 }
183236 ) ;
184237
238+ // Assert
185239 setDate ? . Date ? . Start . Should ( ) . Be ( Convert . ToDateTime ( "2020-12-08T12:00:00Z" ) ) ;
186240
187- // verify
188- IDictionary < string , PropertyValue > testProps = new Dictionary < string , PropertyValue > ( ) ;
189-
190- testProps . Add ( DatePropertyName , new DatePropertyValue { Date = null } ) ;
241+ var pageUpdateParameters = new PagesUpdateParameters
242+ {
243+ Properties = new Dictionary < string , PropertyValue >
244+ {
245+ { DatePropertyName , new DatePropertyValue { Date = null } }
246+ }
247+ } ;
191248
192- var updatedPage =
193- await Client . Pages . UpdateAsync ( page . Id , new PagesUpdateParameters { Properties = testProps } ) ;
249+ var updatedPage = await Client . Pages . UpdateAsync ( page . Id , pageUpdateParameters ) ;
194250
195251 var verifyDate = ( DatePropertyItem ) await Client . Pages . RetrievePagePropertyItemAsync (
196252 new RetrievePropertyItemParameters
197253 {
198254 PageId = page . Id ,
199255 PropertyId = updatedPage . Properties [ DatePropertyName ] . Id
200- } ) ;
256+ }
257+ ) ;
201258
202259 verifyDate ? . Date . Should ( ) . BeNull ( ) ;
203-
204- //cleanup
205- await Client . Blocks . DeleteAsync ( page . Id ) ;
206260 }
207261
208262 [ Fact ]
209263 public async Task Bug_Unable_To_Parse_NumberPropertyItem ( )
210264 {
211265 // Arrange
212266 var pagesCreateParameters = PagesCreateParametersBuilder
213- . Create ( new DatabaseParentInput { DatabaseId = ParentDatabaseId } ) . AddProperty ( "Name" ,
267+ . Create ( new DatabaseParentInput { DatabaseId = _database . Id } )
268+ . AddProperty ( "Name" ,
214269 new TitlePropertyValue
215270 {
216271 Title = new List < RichTextBase >
217272 {
218273 new RichTextText { Text = new Text { Content = "Test Page Title" } }
219274 }
220- } ) . AddProperty ( "Number" , new NumberPropertyValue { Number = 200.00 } ) . Build ( ) ;
275+ } )
276+ . AddProperty ( "Number" , new NumberPropertyValue { Number = 200.00 } ) . Build ( ) ;
221277
222278 // Act
223279 var page = await Client . Pages . CreateAsync ( pagesCreateParameters ) ;
224280
225281 // Assert
226282 Assert . NotNull ( page ) ;
227283 var pageParent = Assert . IsType < DatabaseParent > ( page . Parent ) ;
228- Assert . Equal ( ParentDatabaseId , pageParent . DatabaseId ) ;
284+ Assert . Equal ( _database . Id , pageParent . DatabaseId ) ;
229285
230286 var titleProperty = ( ListPropertyItem ) await Client . Pages . RetrievePagePropertyItemAsync (
231287 new RetrievePropertyItemParameters
@@ -244,8 +300,6 @@ public async Task Bug_Unable_To_Parse_NumberPropertyItem()
244300 } ) ;
245301
246302 Assert . Equal ( 200.00 , numberProperty . Number ) ;
247-
248- await Client . Pages . UpdateAsync ( page . Id , new PagesUpdateParameters { Archived = true } ) ;
249303 }
250304
251305 [ Fact ]
@@ -255,19 +309,11 @@ public async Task Bug_exception_when_attempting_to_set_select_property_to_nothin
255309 var databaseCreateRequest = new DatabasesCreateParameters
256310 {
257311 Title =
258- new List < RichTextBaseInput >
259- {
260- new RichTextTextInput ( ) { Text = new Text { Content = "Test Database" } }
261- } ,
262- Parent = new ParentPageInput ( ) { PageId = ParentPageId } ,
312+ new List < RichTextBaseInput > { new RichTextTextInput { Text = new Text { Content = "Test Database" } } } ,
313+ Parent = new ParentPageInput ( ) { PageId = _page . Id } ,
263314 Properties = new Dictionary < string , IPropertySchema >
264315 {
265- {
266- "title" , new TitlePropertySchema
267- {
268- Title = new Dictionary < string , object > ( )
269- }
270- } ,
316+ { "title" , new TitlePropertySchema { Title = new Dictionary < string , object > ( ) } } ,
271317 {
272318 "Colors1" ,
273319 new SelectPropertySchema
@@ -337,8 +383,5 @@ public async Task Bug_exception_when_attempting_to_set_select_property_to_nothin
337383
338384 updatedPage . Properties [ "Colors1" ] . As < SelectPropertyValue > ( ) . Select . Name . Should ( ) . Be ( "Blue" ) ;
339385 updatedPage . Properties [ "Colors2" ] . As < SelectPropertyValue > ( ) . Select . Should ( ) . BeNull ( ) ;
340-
341- await Client . Pages . UpdateAsync ( page . Id , new PagesUpdateParameters { Archived = true } ) ;
342- await Client . Databases . UpdateAsync ( database . Id , new DatabasesUpdateParameters { Archived = true } ) ;
343386 }
344387}
0 commit comments