Skip to content

Commit e62e2cc

Browse files
committed
Move database query tests to data source query tests
1 parent d9ace69 commit e62e2cc

File tree

3 files changed

+213
-189
lines changed

3 files changed

+213
-189
lines changed

data_source_test.go

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
package notionapi_test
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"reflect"
7+
"testing"
8+
"time"
9+
10+
"github.com/jomei/notionapi"
11+
)
12+
13+
func TestDataSourceClient(t *testing.T) {
14+
timestamp, err := time.Parse(time.RFC3339, "2021-05-24T05:06:34.827Z")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
19+
var user = notionapi.User{
20+
Object: "user",
21+
ID: "some_id",
22+
}
23+
24+
t.Run("Query", func(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
filePath string
28+
statusCode int
29+
id notionapi.DataSourceID
30+
request *notionapi.DataSourceQueryRequest
31+
want *notionapi.DataSourceQueryResponse
32+
wantErr bool
33+
err error
34+
}{
35+
{
36+
name: "returns query results",
37+
id: "some_id",
38+
filePath: "testdata/data_source_query.json",
39+
statusCode: http.StatusOK,
40+
request: &notionapi.DataSourceQueryRequest{
41+
Filter: &notionapi.PropertyFilter{
42+
Property: "Name",
43+
RichText: &notionapi.TextFilterCondition{
44+
Contains: "Hel",
45+
},
46+
},
47+
},
48+
want: &notionapi.DataSourceQueryResponse{
49+
Object: notionapi.ObjectTypeList,
50+
Results: []notionapi.Page{
51+
{
52+
Object: notionapi.ObjectTypePage,
53+
ID: "some_id",
54+
CreatedTime: timestamp,
55+
LastEditedTime: timestamp,
56+
CreatedBy: user,
57+
LastEditedBy: user,
58+
Parent: notionapi.Parent{
59+
Type: notionapi.ParentTypeDataSourceID,
60+
DatabaseID: "some_id",
61+
},
62+
Archived: false,
63+
URL: "some_url",
64+
},
65+
},
66+
HasMore: false,
67+
NextCursor: "",
68+
},
69+
},
70+
}
71+
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
c := newMockedClient(t, tt.filePath, tt.statusCode)
75+
client := notionapi.NewClient("some_token", notionapi.WithHTTPClient(c))
76+
got, err := client.DataSource.Query(context.Background(), tt.id, tt.request)
77+
78+
if (err != nil) != tt.wantErr {
79+
t.Errorf("Query() error = %v, wantErr %v", err, tt.wantErr)
80+
return
81+
}
82+
got.Results[0].Properties = nil
83+
if !reflect.DeepEqual(got, tt.want) {
84+
t.Errorf("Query() got = %v, want %v", got, tt.want)
85+
}
86+
})
87+
}
88+
})
89+
90+
}
91+
92+
func TestDataSourceQueryRequest_MarshalJSON(t *testing.T) {
93+
timeObj, err := time.Parse(time.RFC3339, "2021-05-10T02:43:42Z")
94+
if err != nil {
95+
t.Error(err)
96+
return
97+
}
98+
dateObj := notionapi.Date(timeObj)
99+
tests := []struct {
100+
name string
101+
req *notionapi.DataSourceQueryRequest
102+
want []byte
103+
wantErr bool
104+
}{
105+
{
106+
name: "timestamp created",
107+
req: &notionapi.DataSourceQueryRequest{
108+
Filter: &notionapi.TimestampFilter{
109+
Timestamp: notionapi.TimestampCreated,
110+
CreatedTime: &notionapi.DateFilterCondition{
111+
NextWeek: &struct{}{},
112+
},
113+
},
114+
},
115+
want: []byte(`{"filter":{"timestamp":"created_time","created_time":{"next_week":{}}}}`),
116+
},
117+
{
118+
name: "timestamp last edited",
119+
req: &notionapi.DataSourceQueryRequest{
120+
Filter: &notionapi.TimestampFilter{
121+
Timestamp: notionapi.TimestampLastEdited,
122+
LastEditedTime: &notionapi.DateFilterCondition{
123+
Before: &dateObj,
124+
},
125+
},
126+
},
127+
want: []byte(`{"filter":{"timestamp":"last_edited_time","last_edited_time":{"before":"2021-05-10T02:43:42Z"}}}`),
128+
},
129+
{
130+
name: "or compound filter one level",
131+
req: &notionapi.DataSourceQueryRequest{
132+
Filter: notionapi.OrCompoundFilter{
133+
notionapi.PropertyFilter{
134+
Property: "Status",
135+
Select: &notionapi.SelectFilterCondition{
136+
Equals: "Reading",
137+
},
138+
},
139+
notionapi.PropertyFilter{
140+
Property: "Publisher",
141+
Select: &notionapi.SelectFilterCondition{
142+
Equals: "NYT",
143+
},
144+
},
145+
},
146+
},
147+
want: []byte(`{"filter":{"or":[{"property":"Status","select":{"equals":"Reading"}},{"property":"Publisher","select":{"equals":"NYT"}}]}}`),
148+
},
149+
{
150+
name: "and compound filter one level",
151+
req: &notionapi.DataSourceQueryRequest{
152+
Filter: notionapi.AndCompoundFilter{
153+
notionapi.PropertyFilter{
154+
Property: "Status",
155+
Select: &notionapi.SelectFilterCondition{
156+
Equals: "Reading",
157+
},
158+
},
159+
notionapi.PropertyFilter{
160+
Property: "Publisher",
161+
Select: &notionapi.SelectFilterCondition{
162+
Equals: "NYT",
163+
},
164+
},
165+
},
166+
},
167+
want: []byte(`{"filter":{"and":[{"property":"Status","select":{"equals":"Reading"}},{"property":"Publisher","select":{"equals":"NYT"}}]}}`),
168+
},
169+
{
170+
name: "compound filter two levels",
171+
req: &notionapi.DataSourceQueryRequest{
172+
Filter: notionapi.OrCompoundFilter{
173+
notionapi.PropertyFilter{
174+
Property: "Description",
175+
RichText: &notionapi.TextFilterCondition{
176+
Contains: "fish",
177+
},
178+
},
179+
notionapi.AndCompoundFilter{
180+
notionapi.PropertyFilter{
181+
Property: "Food group",
182+
Select: &notionapi.SelectFilterCondition{
183+
Equals: "🥦Vegetable",
184+
},
185+
},
186+
notionapi.PropertyFilter{
187+
Property: "Is protein rich?",
188+
Checkbox: &notionapi.CheckboxFilterCondition{
189+
Equals: true,
190+
},
191+
},
192+
},
193+
},
194+
},
195+
want: []byte(`{"filter":{"or":[{"property":"Description","rich_text":{"contains":"fish"}},{"and":[{"property":"Food group","select":{"equals":"🥦Vegetable"}},{"property":"Is protein rich?","checkbox":{"equals":true}}]}]}}`),
196+
},
197+
}
198+
for _, tt := range tests {
199+
t.Run(tt.name, func(t *testing.T) {
200+
got, err := tt.req.MarshalJSON()
201+
if (err != nil) != tt.wantErr {
202+
t.Errorf("MarshalJSON() error = %v, wantErr %v", err, tt.wantErr)
203+
return
204+
}
205+
if !reflect.DeepEqual(got, tt.want) {
206+
t.Errorf("MarshalJSON() got = %s, want %s", got, tt.want)
207+
}
208+
})
209+
}
210+
}

0 commit comments

Comments
 (0)