Skip to content

Commit e1b3aca

Browse files
committed
add tests for improved pagination handling
- test without offset in query and with next_cursor in response - test with offset in query (from previous response) and without next_cursor in response
1 parent dafd6de commit e1b3aca

File tree

3 files changed

+385
-0
lines changed

3 files changed

+385
-0
lines changed

tests/EndpointDatabaseTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
99
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
1010
use FiveamCode\LaravelNotionApi\Query\Sorting;
11+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
1112
use Illuminate\Support\Collection;
1213
use Illuminate\Support\Facades\Http;
1314
use Notion;
@@ -157,4 +158,62 @@ public function it_throws_a_notion_exception_bad_request()
157158

158159
Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')->query();
159160
}
161+
162+
/** @test */
163+
public function it_queries_a_database_with_and_without_offset_and_processes_result()
164+
{
165+
// success /v1/databases/DATABASE_DOES_EXIST/query
166+
Http::fake([
167+
'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*' =>
168+
Http::sequence()
169+
->push(
170+
json_decode(file_get_contents("tests/stubs/endpoints/databases/response_query_offset_start_200.json"), true),
171+
200,
172+
['Headers']
173+
)
174+
->push(
175+
json_decode(file_get_contents("tests/stubs/endpoints/databases/response_query_offset_end_200.json"), true),
176+
200,
177+
['Headers']
178+
)
179+
]);
180+
181+
$result = Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')
182+
->query();
183+
184+
//check instance and offset
185+
$this->assertInstanceOf(PageCollection::class, $result);
186+
$this->assertEquals(true, $result->hasMoreEntries());
187+
$this->assertInstanceOf(StartCursor::class, $result->nextCursor());
188+
$this->assertEquals('1500b7c7-329f-4854-8912-4c6972a8743e', $result->nextCursor());
189+
$this->assertEquals('1500b7c7-329f-4854-8912-4c6972a8743e', $result->getRawNextCursor());
190+
191+
$resultCollection = $result->asCollection();
192+
193+
$this->assertIsIterable($resultCollection);
194+
$this->assertContainsOnly(Page::class, $resultCollection);
195+
196+
// check page object
197+
$page = $resultCollection->first();
198+
$this->assertEquals('Betty Holberton', $page->getTitle());
199+
200+
$resultWithOffset = Notion::database('8284f3ff77e24d4a939d19459e4d6bdc')
201+
->offsetByResponse($result)
202+
->query();
203+
204+
// check instance and offset
205+
$this->assertInstanceOf(PageCollection::class, $resultWithOffset);
206+
$this->assertEquals(false, $resultWithOffset->hasMoreEntries());
207+
$this->assertEquals(null, $resultWithOffset->nextCursor());
208+
$this->assertEquals(null, $resultWithOffset->getRawNextCursor());
209+
210+
$resultWithOffsetCollection = $resultWithOffset->asCollection();
211+
212+
$this->assertIsIterable($resultWithOffsetCollection);
213+
$this->assertContainsOnly(Page::class, $resultWithOffsetCollection);
214+
215+
// check page object
216+
$page = $resultWithOffsetCollection->first();
217+
$this->assertEquals('Betty Holberton', $page->getTitle());
218+
}
160219
}
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
{
2+
"object": "list",
3+
"results": [
4+
{
5+
"object": "page",
6+
"id": "1500b7c7-329f-4854-8912-4c6972a8743e",
7+
"created_time": "2021-05-24T11:03:00.000Z",
8+
"last_edited_time": "2021-05-24T11:13:00.000Z",
9+
"parent": {
10+
"type": "database_id",
11+
"database_id": "8284f3ff-77e2-4d4a-939d-19459e4d6bdc"
12+
},
13+
"archived": false,
14+
"properties": {
15+
"Birth year": {
16+
"id": "_f<<",
17+
"type": "rich_text",
18+
"rich_text": [
19+
{
20+
"type": "text",
21+
"text": {
22+
"content": "1917",
23+
"link": null
24+
},
25+
"annotations": {
26+
"bold": false,
27+
"italic": false,
28+
"strikethrough": false,
29+
"underline": false,
30+
"code": false,
31+
"color": "default"
32+
},
33+
"plain_text": "1917",
34+
"href": null
35+
}
36+
]
37+
},
38+
"Known for": {
39+
"id": "aUmo",
40+
"type": "multi_select",
41+
"multi_select": [
42+
{
43+
"id": "f55ee1a3-e67f-4793-ba3f-5dac02938a5f",
44+
"name": "ENIAC",
45+
"color": "purple"
46+
},
47+
{
48+
"id": "2016de6e-5325-4549-8e1a-60ee7570382a",
49+
"name": "UNIVAC",
50+
"color": "default"
51+
},
52+
{
53+
"id": "55c46053-f87e-40e9-8070-6c398939fed6",
54+
"name": "Breakpoints",
55+
"color": "red"
56+
}
57+
]
58+
},
59+
"Name": {
60+
"id": "title",
61+
"type": "title",
62+
"title": [
63+
{
64+
"type": "text",
65+
"text": {
66+
"content": "Betty Holberton",
67+
"link": null
68+
},
69+
"annotations": {
70+
"bold": false,
71+
"italic": false,
72+
"strikethrough": false,
73+
"underline": false,
74+
"code": false,
75+
"color": "default"
76+
},
77+
"plain_text": "Betty Holberton",
78+
"href": null
79+
}
80+
]
81+
}
82+
}
83+
},
84+
{
85+
"object": "page",
86+
"id": "ab2a7a85-08a1-4dfc-be89-0e30aeffc0f6",
87+
"created_time": "2021-05-24T11:03:02.464Z",
88+
"last_edited_time": "2021-05-24T11:12:00.000Z",
89+
"parent": {
90+
"type": "database_id",
91+
"database_id": "8284f3ff-77e2-4d4a-939d-19459e4d6bdc"
92+
},
93+
"archived": false,
94+
"properties": {
95+
"Birth year": {
96+
"id": "_f<<",
97+
"type": "rich_text",
98+
"rich_text": [
99+
{
100+
"type": "text",
101+
"text": {
102+
"content": "1906",
103+
"link": null
104+
},
105+
"annotations": {
106+
"bold": false,
107+
"italic": false,
108+
"strikethrough": false,
109+
"underline": false,
110+
"code": false,
111+
"color": "default"
112+
},
113+
"plain_text": "1906",
114+
"href": null
115+
}
116+
]
117+
},
118+
"Known for": {
119+
"id": "aUmo",
120+
"type": "multi_select",
121+
"multi_select": [
122+
{
123+
"id": "1ada9715-0139-4c1c-b1af-9d8d2fe80ea2",
124+
"name": "COBOL",
125+
"color": "orange"
126+
},
127+
{
128+
"id": "2016de6e-5325-4549-8e1a-60ee7570382a",
129+
"name": "UNIVAC",
130+
"color": "default"
131+
}
132+
]
133+
},
134+
"Name": {
135+
"id": "title",
136+
"type": "title",
137+
"title": [
138+
{
139+
"type": "text",
140+
"text": {
141+
"content": "Grace Hopper",
142+
"link": null
143+
},
144+
"annotations": {
145+
"bold": false,
146+
"italic": false,
147+
"strikethrough": false,
148+
"underline": false,
149+
"code": false,
150+
"color": "default"
151+
},
152+
"plain_text": "Grace Hopper",
153+
"href": null
154+
}
155+
]
156+
}
157+
}
158+
}
159+
],
160+
"next_cursor": null,
161+
"has_more": false
162+
}
163+

0 commit comments

Comments
 (0)