From 6e7746605c911ed4a4221421c29162b529fa84e3 Mon Sep 17 00:00:00 2001 From: Anatoy Nosov Date: Sun, 30 May 2021 18:07:22 +0700 Subject: [PATCH] Page#Update tests --- block.go | 24 +++++------ block_test.go | 4 +- object.go | 19 --------- page.go | 10 +++-- page_test.go | 58 ++++++++++++++++++++++++++ property.go | 36 ++++++++-------- testdata/page_update.json | 86 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 183 insertions(+), 54 deletions(-) create mode 100644 testdata/page_update.json diff --git a/block.go b/block.go index b89c1d4..356d680 100644 --- a/block.go +++ b/block.go @@ -106,8 +106,8 @@ type Heading1Block struct { 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"` + 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"` @@ -138,8 +138,8 @@ type Heading3Block struct { 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"` + 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"` @@ -154,8 +154,8 @@ type BulletedListItemBlock struct { 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"` + 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"` @@ -171,8 +171,8 @@ type NumberedListItemBlock struct { 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"` + 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"` @@ -188,8 +188,8 @@ type ToDoBlock struct { 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"` + CreatedTime *time.Time `json:"created_time,omitempty"` + LastEditedTime *time.Time `json:"last_edited_time,omitempty"` HasChildren bool `json:"has_children"` ToDo struct { Text Paragraph `json:"text"` @@ -206,8 +206,8 @@ type ToggleBlock struct { 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"` + 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"` diff --git a/block_test.go b/block_test.go index c2297af..b6bc548 100644 --- a/block_test.go +++ b/block_test.go @@ -24,7 +24,7 @@ func TestBlockClient(t *testing.T) { }{ { name: "returns blocks by id of parent block", - id: "d1c2fdf4-9f12-46cc-b168-1ed1bcb732d8", + id: "some_id", filePath: "testdata/block_get_children.json", len: 2, }, @@ -60,7 +60,7 @@ func TestBlockClient(t *testing.T) { }{ { name: "returns blocks by id of parent block", - id: "d1c2fdf4-9f12-46cc-b168-1ed1bcb732d8", + id: "some_id", filePath: "testdata/block_append_children.json", request: ¬ionapi.AppendBlockChildrenRequest{ Children: []notionapi.Block{ diff --git a/object.go b/object.go index c3229f0..7d878f8 100644 --- a/object.go +++ b/object.go @@ -16,25 +16,6 @@ type Object interface { GetObject() ObjectType } -//TODO: dont need anymore -type BasicObject struct { - ID ObjectID `json:"id"` - Type ObjectType `json:"type"` - Title *Text `json:"title,omitempty"` - Text *Text `json:"text,omitempty"` - RichText *RichText `json:"rich_text,omitempty"` - Checkbox *struct{} `json:"checkbox,omitempty"` - Formula *FormulaObject `json:"formula,omitempty"` - Date *struct{} `json:"date,omitempty"` - Relation *RelationObject `json:"relation,omitempty"` - Rollup *RollupObject `json:"rollup,omitempty"` - MultiSelect *MultiSelectObject `json:"multi_select,omitempty"` - People *struct{} `json:"people,omitempty"` - Files *struct{} `json:"files,omitempty"` - Paragraph *Paragraph `json:"paragraph,omitempty"` - Toggle *Toggle `json:"toggle,omitempty"` -} - type Color string func (c Color) String() string { diff --git a/page.go b/page.go index 6150066..2b51c99 100644 --- a/page.go +++ b/page.go @@ -17,7 +17,7 @@ func (pID PageID) String() string { type PageService interface { Get(context.Context, PageID) (*Page, error) Create(context.Context, *PageCreateRequest) (*Page, error) - Update(context.Context, PageID, map[string]BasicObject) (*Page, error) + Update(context.Context, PageID, *PageUpdateRequest) (*Page, error) } type PageClient struct { @@ -44,9 +44,13 @@ func (pc *PageClient) Create(ctx context.Context, requestBody *PageCreateRequest return handlePageResponse(res) } +type PageUpdateRequest struct { + Properties Properties `json:"properties"` +} + // Update https://developers.notion.com/reference/patch-page -func (pc *PageClient) Update(ctx context.Context, id PageID, properties map[string]BasicObject) (*Page, error) { - res, err := pc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("pages/%s", id.String()), nil, properties) +func (pc *PageClient) Update(ctx context.Context, id PageID, request *PageUpdateRequest) (*Page, error) { + res, err := pc.apiClient.request(ctx, http.MethodPatch, fmt.Sprintf("pages/%s", id.String()), nil, request) if err != nil { return nil, err } diff --git a/page_test.go b/page_test.go index ffdeac1..47e6d98 100644 --- a/page_test.go +++ b/page_test.go @@ -117,4 +117,62 @@ func TestPageClient(t *testing.T) { }) } }) + + t.Run("Update", func(t *testing.T) { + tests := []struct { + name string + filePath string + id notionapi.PageID + request *notionapi.PageUpdateRequest + want *notionapi.Page + wantErr bool + err error + }{ + { + name: "change requested properties and return the result", + id: "some_id", + filePath: "testdata/page_update.json", + request: ¬ionapi.PageUpdateRequest{ + Properties: notionapi.Properties{ + "SomeColumn": notionapi.RichTextProperty{ + Type: notionapi.PropertyTypeRichText, + RichText: notionapi.Paragraph{ + { + Type: notionapi.ObjectTypeText, + Text: notionapi.Text{Content: "patch"}, + }, + }, + }, + }, + }, + want: ¬ionapi.Page{ + Object: notionapi.ObjectTypePage, + ID: "some_id", + CreatedTime: timestamp, + LastEditedTime: timestamp, + Parent: notionapi.Parent{ + Type: notionapi.ParentTypeDatabaseID, + DatabaseID: "some_id", + }, + Archived: false, + }, + }, + } + 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.Page.Update(context.Background(), tt.id, tt.request) + if (err != nil) != tt.wantErr { + t.Errorf("Update() 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("Update() got = %v, want %v", got, tt.want) + } + }) + } + }) } diff --git a/property.go b/property.go index 1daddec..2c1a4c1 100644 --- a/property.go +++ b/property.go @@ -19,7 +19,7 @@ func (pID PropertyID) String() string { } type TextProperty struct { - ID PropertyID `json:"id"` + ID PropertyID `json:"id,omitempty"` Type PropertyType `json:"type"` Title []RichText `json:"title"` } @@ -29,7 +29,7 @@ func (p TextProperty) GetType() PropertyType { } type EmptyRichTextProperty struct { - ID PropertyID `json:"id"` + ID PropertyID `json:"id,omitempty"` Type PropertyType `json:"type"` RichText struct{} `json:"rich_text"` } @@ -39,7 +39,7 @@ func (p EmptyRichTextProperty) GetType() PropertyType { } type RichTextProperty struct { - ID PropertyID `json:"id"` + ID PropertyID `json:"id,omitempty"` Type PropertyType `json:"type"` RichText []RichText `json:"rich_text"` } @@ -49,7 +49,7 @@ func (p RichTextProperty) GetType() PropertyType { } type DatabaseTitleProperty struct { - ID PropertyID `json:"id"` + ID PropertyID `json:"id,omitempty"` Type PropertyType `json:"type"` Title RichText `json:"title"` } @@ -75,7 +75,7 @@ func (ft FormatType) String() string { } type NumberProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` Format FormatType `json:"format"` } @@ -85,7 +85,7 @@ func (p NumberProperty) GetType() PropertyType { } type SelectProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` Select Select `json:"select"` } @@ -95,7 +95,7 @@ type Select struct { } type MultiSelectProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` MultiSelect Select `json:"multi_select"` } @@ -105,7 +105,7 @@ func (p MultiSelectProperty) GetType() PropertyType { } type MultiSelectOptionsProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` MultiSelect []Option `json:"multi_select"` } @@ -125,7 +125,7 @@ func (p SelectProperty) GetType() PropertyType { } type DateProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` Date interface{} `json:"date"` } @@ -135,7 +135,7 @@ func (p DateProperty) GetType() PropertyType { } type PeopleProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` People interface{} `json:"people"` } @@ -145,7 +145,7 @@ func (p PeopleProperty) GetType() PropertyType { } type FileProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` File interface{} `json:"file"` } @@ -155,7 +155,7 @@ func (p FileProperty) GetType() PropertyType { } type CheckboxProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` Checkbox interface{} `json:"checkbox"` } @@ -165,7 +165,7 @@ func (p CheckboxProperty) GetType() PropertyType { } type URLProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` URL interface{} `json:"url"` } @@ -175,7 +175,7 @@ func (p URLProperty) GetType() PropertyType { } type EmailProperty struct { - ID PropertyID `json:"id"` + ID PropertyID `json:"id,omitempty"` Type PropertyType `json:"type"` Email interface{} `json:"email"` } @@ -185,7 +185,7 @@ func (p EmailProperty) GetType() PropertyType { } type PhoneNumberProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` PhoneNumber interface{} `json:"phone_number"` } @@ -195,7 +195,7 @@ func (p PhoneNumberProperty) GetType() PropertyType { } type FormulaProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` Expression string `json:"expression"` } @@ -220,7 +220,7 @@ func (p RelationProperty) GetType() PropertyType { } type RollupProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` Rollup Rollup `json:"rollup"` } @@ -238,7 +238,7 @@ func (p RollupProperty) GetType() PropertyType { } type CreatedTimeProperty struct { - ID ObjectID `json:"id"` + ID ObjectID `json:"id,omitempty"` Type PropertyType `json:"type"` CreatedTime interface{} `json:"created_time"` } diff --git a/testdata/page_update.json b/testdata/page_update.json new file mode 100644 index 0000000..569a4cf --- /dev/null +++ b/testdata/page_update.json @@ -0,0 +1,86 @@ +{ + "object": "page", + "id": "some_id", + "created_time": "2021-05-24T05:06:34.827Z", + "last_edited_time": "2021-05-24T05:06:34.827Z", + "parent": { + "type": "database_id", + "database_id": "some_id" + }, + "archived": false, + "properties": { + "Tags": { + "id": ";s|V", + "type": "multi_select", + "multi_select": [ + { + "id": "some_id", + "name": "tag", + "color": "blue" + } + ] + }, + "Some another columg": { + "id": "rJt\\", + "type": "people", + "people": [ + { + "object": "user", + "id": "some_id", + "name": "John Doe", + "avatar_url": "some.url", + "type": "person", + "person": { + "email": "some@email.com" + } + } + ] + }, + "SomeColumn": { + "id": "~j_@", + "type": "rich_text", + "rich_text": [ + { + "type": "text", + "text": { + "content": "patch", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "patch", + "href": null + } + ] + }, + "Name": { + "id": "title", + "type": "title", + "title": [ + { + "type": "text", + "text": { + "content": "Hello", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Hello", + "href": null + } + ] + } + } +}