Skip to content

Commit

Permalink
fix: API error while updating page file property (#220)
Browse files Browse the repository at this point in the history
Closes #219
  • Loading branch information
mariosimao committed May 6, 2023
1 parent 8d32008 commit d4fdb47
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 7 deletions.
16 changes: 13 additions & 3 deletions src/Common/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
/**
* @psalm-type FileJson = array{
* type: "external"|"file",
* name: string,
* file?: array{ url: string, expiry_time: string },
* external?: array{ url: string },
* }
Expand All @@ -19,19 +20,20 @@ private function __construct(
public readonly FileType $type,
public readonly string $url,
public readonly DateTimeImmutable|null $expiryTime,
public readonly string $name,
) {
}

public static function createExternal(string $url): self
{
return new self(FileType::External, $url, null);
return new self(FileType::External, $url, null, "File");
}

public static function createInternal(
string $url,
DateTimeImmutable|null $expiryTime = null
): self {
return new self(FileType::Internal, $url, $expiryTime);
return new self(FileType::Internal, $url, $expiryTime, "File");
}

/**
Expand All @@ -49,6 +51,7 @@ public static function fromArray(array $array): self
FileType::from($type),
$file["url"] ?? "",
isset($file["expiry_time"]) ? new DateTimeImmutable($file["expiry_time"]) : null,
$array["name"] ?? "File",
);
}

Expand All @@ -60,6 +63,7 @@ public function toArray(): array
if ($type === FileType::Internal) {
$array = [
"type" => "file",
"name" => $this->name,
"file" => [
"url" => $this->url,
"expiry_time" => $this->expiryTime?->format(Date::FORMAT),
Expand All @@ -70,6 +74,7 @@ public function toArray(): array
if ($type === FileType::External) {
$array = [
"type" => "external",
"name" => $this->name,
"external" => [ "url" => $this->url ],
];
}
Expand All @@ -89,6 +94,11 @@ public function isInternal(): bool

public function changeUrl(string $url): self
{
return new self($this->type, $url, $this->expiryTime);
return new self($this->type, $url, $this->expiryTime, $this->name);
}

public function changeName(string $name): self
{
return new self($this->type, $this->url, $this->expiryTime, $name);
}
}
5 changes: 3 additions & 2 deletions tests/Integration/DatabasesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ public function test_query_all_pages_from_database(): void
$this->assertCount(5, $pages);
}

/** @group bigdb */
public function test_query_big_database(): void
{
$client = Helper::client();
$result = $client->search()->search(SearchQuery::title("Big database"));
$result = $client->search()->search(SearchQuery::title("Big dataset")->filterByDatabases());

if (
count($result->results) > 0 &&
Expand Down Expand Up @@ -239,7 +240,7 @@ private static function bigDatabase(): Database
$client = Helper::client();

$database = Database::create(DatabaseParent::page(Helper::testPageId()))
->changeTitle("Big database");
->changeTitle("Big dataset");

$database = $client->databases()->create($database);

Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/FileBlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "file",
"file" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/file.doc"
],
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "image",
"image" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/image.png"
],
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "pdf",
"pdf" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/document.pdf"
],
Expand Down
3 changes: 1 addition & 2 deletions tests/Unit/Blocks/ToggleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Notion\Test\Unit\Blocks;

use Notion\Blocks\BlockFactory;
use Notion\Blocks\ToDo;
use Notion\Exceptions\BlockException;
use Notion\Blocks\Toggle;
use Notion\Common\Color;
Expand Down Expand Up @@ -190,7 +189,7 @@ public function test_add_child(): void

public function test_change_color(): void
{
$block = ToDo::fromString("Hello World!")->changeColor(Color::Red);
$block = Toggle::fromString("Hello World!")->changeColor(Color::Red);

$this->assertSame(Color::Red, $block->color);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Blocks/VideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public function test_transform_in_array(): void
"type" => "video",
"video" => [
"type" => "external",
"name" => "File",
"external" => [
"url" => "https://my-site.com/video.mp4"
],
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Common/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function test_intenral_array_conversion(): void
{
$array = [
"type" => "file",
"name" => "Test file",
"file" => [
"url" => "https://notion.so/image.png",
"expiry_time" => "2020-12-08T12:00:00.000000Z",
Expand All @@ -48,6 +49,7 @@ public function test_external_array_conversion(): void
{
$array = [
"type" => "external",
"name" => "Test file",
"external" => [ "url" => "https://my-site.com/image.png" ],
];
$file = File::fromArray($array);
Expand All @@ -61,4 +63,11 @@ public function test_change_url(): void

$this->assertEquals("https://my-site.com/image.png", $file->url);
}

public function test_change_name(): void
{
$file = File::createExternal("")->changeName("My file name");

$this->assertSame("My file name", $file->name);
}
}
1 change: 1 addition & 0 deletions tests/Unit/Common/IconTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function test_icon_from_file_array_conversion(): void

$expected = [
"type" => "external",
"name" => "File",
"external" => ["url" => "http://example.com/icon.png"],
];

Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Databases/DatabaseParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function test_create_parent_workspace(): void
$this->assertEquals("workspace", $parent->type->value);
}

public function test_create_parent_block(): void
{
$parent = DatabaseParent::block("0181c3aa-1112-489f-b34a-515b4e3583ed");

$this->assertTrue($parent->isBlock());
$this->assertSame("0181c3aa-1112-489f-b34a-515b4e3583ed", $parent->id);
}

public function test_page_array_conversion(): void
{
$array = [
Expand All @@ -45,6 +53,17 @@ public function test_workspace_array_conversion(): void
$this->assertEquals($array["workspace"], $parent->toArray()["workspace"]);
}

public function test_block_array_conversion(): void
{
$array = [
"type" => "block_id",
"block_id" => "7a774b5d-ca74-4679-9f18-689b5a98f138",
];
$parent = DatabaseParent::fromArray($array);

$this->assertEquals($array["block_id"], $parent->toArray()["block_id"]);
}

public function test_invalid_type_array(): void
{
$this->expectException(\ValueError::class);
Expand Down
19 changes: 19 additions & 0 deletions tests/Unit/Pages/PageParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public function test_create_parent_page(): void
$this->assertEquals("058d158b-09de-4d69-be07-901c20a7ca5c", $parent->id);
}

public function test_create_parent_block(): void
{
$parent = PageParent::block("0181c3aa-1112-489f-b34a-515b4e3583ed");

$this->assertTrue($parent->isBlock());
$this->assertSame("0181c3aa-1112-489f-b34a-515b4e3583ed", $parent->id);
}

public function test_create_parent_workspace(): void
{
$parent = PageParent::workspace();
Expand Down Expand Up @@ -65,6 +73,17 @@ public function test_workspace_array_conversion(): void
$this->assertEquals($array["workspace"], $parent->toArray()["workspace"]);
}

public function test_block_array_conversion(): void
{
$array = [
"type" => "block_id",
"block_id" => "7a774b5d-ca74-4679-9f18-689b5a98f138",
];
$parent = PageParent::fromArray($array);

$this->assertEquals($array["block_id"], $parent->toArray()["block_id"]);
}

public function test_invalid_type_array(): void
{
$this->expectException(\ValueError::class);
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Pages/Properties/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ public function test_is_empty(): void
$date = Date::fromArray($array);

$this->assertTrue($date->isEmpty());
$this->assertFalse($date->isRange());
}
}
1 change: 1 addition & 0 deletions tests/Unit/Pages/Properties/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function test_array_conversion(): void
"files" => [
[
"type" => "external",
"name" => "Test file",
"external" => [
"url" => "https://example.com/image.png",
],
Expand Down

0 comments on commit d4fdb47

Please sign in to comment.