Skip to content

Commit e758209

Browse files
committed
added test for database query
1 parent 96342d4 commit e758209

File tree

4 files changed

+356
-1
lines changed

4 files changed

+356
-1
lines changed

src/Endpoints/Database.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public function query(): PageCollection
4848
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
4949
$postData
5050
)
51-
5251
->json();
5352

5453
return new PageCollection($response);

tests/EndpointDatabaseTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Tests;
4+
5+
use \FiveamCode\LaravelNotionApi\Endpoints\Database;
6+
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
7+
use FiveamCode\LaravelNotionApi\Entities\Page;
8+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
9+
use FiveamCode\LaravelNotionApi\Query\Filter;
10+
use FiveamCode\LaravelNotionApi\Query\Sorting;
11+
use Illuminate\Support\Collection;
12+
use Illuminate\Support\Facades\Http;
13+
14+
/**
15+
* Class EndpointDatabaseTest
16+
*
17+
* Due to the complexity of the query request, there are also tests
18+
* for building the correct request body required, alongside tests
19+
* for processing the API response.
20+
*
21+
* We are using our own Notion database for better traceability and
22+
* understanding. Nevertheless, the responses are stored in the stubs
23+
* folder so you don't have to worry about access when running
24+
* your own tests.
25+
*
26+
* @see https://www.notion.so/8284f3ff77e24d4a939d19459e4d6bdc?v=bc3a9ce8cdb84d3faefc9ae490136ac2
27+
* @see https://developers.notion.com/reference/post-database-query
28+
*
29+
* @package FiveamCode\LaravelNotionApi\Tests
30+
*/
31+
class EndpointDatabaseTest extends NotionApiTest
32+
{
33+
34+
/** @test */
35+
public function it_returns_a_database_endpoint_instance()
36+
{
37+
$endpoint = \Notion::database("897e5a76ae524b489fdfe71f5945d1af");
38+
39+
$this->assertInstanceOf(Database::class, $endpoint);
40+
}
41+
42+
/**
43+
* @dataProvider limitProvider
44+
*/
45+
public function limitProvider(): array
46+
{
47+
return [
48+
[1],
49+
[2]
50+
];
51+
}
52+
53+
/** @test
54+
* @dataProvider limitProvider
55+
*/
56+
public function it_queries_a_database_with_filter_and_sorting_and_processes_result($limit)
57+
{
58+
// success /v1/databases/DATABASE_DOES_EXIST/query
59+
Http::fake([
60+
'https://api.notion.com/v1/databases/8284f3ff77e24d4a939d19459e4d6bdc/query*'
61+
=> Http::response(
62+
json_decode(file_get_contents("tests/stubs/endpoints/databases/response_query_limit{$limit}_200.json"), true),
63+
200,
64+
['Headers']
65+
)
66+
]);
67+
68+
// Let's search for women developing the UNIVAC I computer
69+
// and sort them by birth year descending
70+
$sortings = new Collection();
71+
$filters = new Collection();
72+
73+
$sortings->add(
74+
Sorting::propertySort("Birth year", "descending")
75+
);
76+
77+
$filters
78+
->add(
79+
Filter::rawFilter(
80+
"Known for",
81+
[
82+
"multi_select" =>
83+
["contains" => "UNIVAC"]
84+
]
85+
)
86+
);
87+
88+
$result = \Notion::database("8284f3ff77e24d4a939d19459e4d6bdc")
89+
->filterBy($filters)
90+
->sortBy($sortings)
91+
->limit($limit)
92+
->query();
93+
94+
$this->assertInstanceOf(PageCollection::class, $result);
95+
96+
$resultCollection = $result->asCollection();
97+
98+
$this->assertIsIterable($resultCollection);
99+
$this->assertCount($limit, $resultCollection);
100+
$this->assertContainsOnly(Page::class, $resultCollection);
101+
102+
// check page object
103+
$page = $resultCollection->first();
104+
$this->assertEquals("Betty Holberton", $page->getTitle());
105+
}
106+
107+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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": "text",
18+
"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+
"next_cursor": null,
86+
"has_more": false
87+
}
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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": "text",
18+
"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": "text",
98+
"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+
}

0 commit comments

Comments
 (0)