Skip to content

Commit

Permalink
Page#Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jomei committed May 30, 2021
1 parent b66bd2e commit 6e77466
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 54 deletions.
24 changes: 12 additions & 12 deletions block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand All @@ -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"`
Expand Down
4 changes: 2 additions & 2 deletions block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down Expand Up @@ -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: &notionapi.AppendBlockChildrenRequest{
Children: []notionapi.Block{
Expand Down
19 changes: 0 additions & 19 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
58 changes: 58 additions & 0 deletions page_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: &notionapi.PageUpdateRequest{
Properties: notionapi.Properties{
"SomeColumn": notionapi.RichTextProperty{
Type: notionapi.PropertyTypeRichText,
RichText: notionapi.Paragraph{
{
Type: notionapi.ObjectTypeText,
Text: notionapi.Text{Content: "patch"},
},
},
},
},
},
want: &notionapi.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)
}
})
}
})
}
36 changes: 18 additions & 18 deletions property.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand All @@ -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"`
}
Expand Down
Loading

0 comments on commit 6e77466

Please sign in to comment.