Skip to content

Commit

Permalink
Block#AppendChildren tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jomei committed May 30, 2021
1 parent d11c77b commit b66bd2e
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 65 deletions.
128 changes: 74 additions & 54 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (bID BlockID) String() string {

type BlockService interface {
GetChildren(context.Context, BlockID, *Pagination) (*GetChildrenResponse, error)
AppendChildren(context.Context, BlockID, AppendBlockChildrenRequest) (Block, error)
AppendChildren(context.Context, BlockID, *AppendBlockChildrenRequest) (Block, error)
}

type BlockClient struct {
Expand Down Expand Up @@ -58,8 +58,8 @@ type GetChildrenResponse struct {
}

// AppendChildren https://developers.notion.com/reference/patch-block-children
func (bc *BlockClient) AppendChildren(ctx context.Context, id BlockID, requestBody AppendBlockChildrenRequest) (Block, error) {
res, err := bc.apiClient.request(ctx, http.MethodPost, fmt.Sprintf("blocks/%s", id.String()), nil, requestBody)
func (bc *BlockClient) AppendChildren(ctx context.Context, id BlockID, requestBody *AppendBlockChildrenRequest) (Block, error) {
res, err := bc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("blocks/%s/children", id.String()), nil, requestBody)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -88,12 +88,14 @@ type Block interface {

type ParagraphBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Paragraph struct {
Text Paragraph `json:"text"`
} `json:"paragraph"`
}

func (b *ParagraphBlock) GetType() BlockType {
Expand All @@ -102,12 +104,14 @@ func (b *ParagraphBlock) GetType() BlockType {

type Heading1Block struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
CreatedTime time.Time `json:"created_time,omitempty"`
LastEditedTime time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading1 struct {
Text Paragraph `json:"text"`
} `json:"heading_1"`
}

func (b *Heading1Block) GetType() BlockType {
Expand All @@ -116,12 +120,14 @@ func (b *Heading1Block) GetType() BlockType {

type Heading2Block struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading2 struct {
Text Paragraph `json:"text"`
} `json:"heading_2"`
}

func (b *Heading2Block) GetType() BlockType {
Expand All @@ -130,42 +136,48 @@ func (b *Heading2Block) GetType() BlockType {

type Heading3Block struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
CreatedTime time.Time `json:"created_time,omitempty"`
LastEditedTime time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Heading3 struct {
Text Paragraph `json:"text"`
} `json:"heading_3"`
}

func (b *Heading3Block) GetType() BlockType {
return b.Type
}

type BulletedListItemBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
Children []Block `json:"children"`
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time,omitempty"`
LastEditedTime time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
BulletedListItem struct {
Text Paragraph `json:"text"`
Children []Block `json:"children"`
} `json:"bulleted_list_item"`
}

func (b *BulletedListItemBlock) GetType() BlockType {
return b.Type
}

type NumberedListItemBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
Children []Block `json:"children"`
Object ObjectType `json:"object"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time,omitempty"`
LastEditedTime time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
NumberedListItem struct {
Text Paragraph `json:"text"`
Children []Block `json:"children"`
} `json:"numbered_list_item"`
}

func (b *NumberedListItemBlock) GetType() BlockType {
Expand All @@ -174,14 +186,16 @@ func (b *NumberedListItemBlock) GetType() BlockType {

type ToDoBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
CreatedTime time.Time `json:"created_time,omitempty"`
LastEditedTime time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children"`
Text Paragraph `json:"text"`
Children []Block `json:"children"`
Checked bool `json:"checked"`
ToDo struct {
Text Paragraph `json:"text"`
Children []Block `json:"children"`
Checked bool `json:"checked"`
} `json:"to_do"`
}

func (b *ToDoBlock) GetType() BlockType {
Expand All @@ -190,13 +204,17 @@ func (b *ToDoBlock) GetType() BlockType {

type ToggleBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
CreatedTime time.Time `json:"created_time,omitempty"`
LastEditedTime time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
Text Paragraph `json:"text"`
Children []Block `json:"children"`
Toggle struct {
Text Paragraph `json:"text"`
Children []Block `json:"children"`
} `json:"toggle"`
}

func (b *ToggleBlock) GetType() BlockType {
Expand All @@ -205,12 +223,14 @@ func (b *ToggleBlock) GetType() BlockType {

type ChildPageBlock struct {
Object ObjectType `json:"object"`
ID BlockID `json:"id"`
ID BlockID `json:"id,omitempty"`
Type BlockType `json:"type"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
HasChildren bool `json:"has_children"`
Title Text `json:"title"`
CreatedTime *time.Time `json:"created_time,omitempty"`
LastEditedTime *time.Time `json:"last_edited_time,omitempty"`
HasChildren bool `json:"has_children,omitempty"`
ChildPage struct {
Title string `json:"title"`
} `json:"child_page"`
}

func (b *ChildPageBlock) GetType() BlockType {
Expand Down
71 changes: 70 additions & 1 deletion block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ package notionapi_test
import (
"context"
"github.com/jomei/notionapi"
"reflect"
"testing"
"time"
)

func TestBlockClient(t *testing.T) {
timestamp, err := time.Parse(time.RFC3339, "2021-05-24T05:06:34.827Z")
if err != nil {
t.Fatal(err)
}
t.Run("GetChildren", func(t *testing.T) {
tests := []struct {
name string
Expand All @@ -31,7 +37,7 @@ func TestBlockClient(t *testing.T) {
got, err := client.Block.GetChildren(context.Background(), tt.id, nil)

if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("GetChildren() error = %v, wantErr %v", err, tt.wantErr)
return
}

Expand All @@ -41,4 +47,67 @@ func TestBlockClient(t *testing.T) {
})
}
})

t.Run("AppendChildren", func(t *testing.T) {
tests := []struct {
name string
filePath string
id notionapi.BlockID
request *notionapi.AppendBlockChildrenRequest
want *notionapi.ChildPageBlock
wantErr bool
err error
}{
{
name: "returns blocks by id of parent block",
id: "d1c2fdf4-9f12-46cc-b168-1ed1bcb732d8",
filePath: "testdata/block_append_children.json",
request: &notionapi.AppendBlockChildrenRequest{
Children: []notionapi.Block{
&notionapi.Heading2Block{
Object: notionapi.ObjectTypeBlock,
Type: notionapi.BlockTypeHeading2,
Heading2: struct {
Text notionapi.Paragraph `json:"text"`
}{notionapi.Paragraph{
{
Type: notionapi.ObjectTypeText,
Text: notionapi.Text{Content: "Hello"},
},
}},
},
},
},
want: &notionapi.ChildPageBlock{
Object: notionapi.ObjectTypeBlock,
ID: "some_id",
Type: notionapi.BlockTypeChildPage,
CreatedTime: &timestamp,
LastEditedTime: &timestamp,
HasChildren: true,
ChildPage: struct {
Title string `json:"title"`
}{
Title: "Hello",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newMockedClient(t, tt.filePath)
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
got, err := client.Block.AppendChildren(context.Background(), tt.id, tt.request)

if (err != nil) != tt.wantErr {
t.Errorf("AppendChidlren() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("AppendChidlren() got = %v, want %v", got, tt.want)
}
})
}
})
}
1 change: 0 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ func (c *Client) request(ctx context.Context, method string, urlStr string, quer
if err != nil {
return nil, err
}
fmt.Println(req)

req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.Token.String()))
req.Header.Add("Notion-Version", c.notionVersion)
Expand Down
8 changes: 4 additions & 4 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ func TestDatabaseClient(t *testing.T) {
got, err := client.Database.List(context.Background(), nil)

if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
return
}
got.Results[0].Properties = nil
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
t.Errorf("List() got = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -195,12 +195,12 @@ func TestDatabaseClient(t *testing.T) {
got, err := client.Database.Query(context.Background(), tt.id, tt.request)

if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Query() error = %v, wantErr %v", err, tt.wantErr)
return
}
got.Results[0].Properties = nil
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
t.Errorf("Query() got = %v, want %v", got, tt.want)
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ func TestPageClient(t *testing.T) {
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
got, err := client.Page.Create(context.Background(), tt.request)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Create() error = %v, wantErr %v", err, tt.wantErr)
return
}
// TODO: remove properties from comparing for a while. Have to compare with interface somehow
got.Properties = nil
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
t.Errorf("Create() got = %v, want %v", got, tt.want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestSearchClient(t *testing.T) {
got, err := client.Search.Do(context.Background(), tt.request)

if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("Do() error = %v, wantErr %v", err, tt.wantErr)
return
}

Expand Down
11 changes: 11 additions & 0 deletions testdata/block_append_children.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"object": "block",
"id": "some_id",
"created_time": "2021-05-24T05:06:34.827Z",
"last_edited_time": "2021-05-24T05:06:34.827Z",
"has_children": true,
"type": "child_page",
"child_page": {
"title": "Hello"
}
}
4 changes: 2 additions & 2 deletions user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ func TestUserClient(t *testing.T) {
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
got, err := client.User.List(context.Background(), nil)
if (err != nil) != tt.wantErr {
t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf("List() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Get() got = %v, want %v", got, tt.want)
t.Errorf("List() got = %v, want %v", got, tt.want)
}
})
}
Expand Down

0 comments on commit b66bd2e

Please sign in to comment.