Skip to content

Commit 09cc0ff

Browse files
committed
✅ Adds filter clasees serialization tests
One test case for each filter type
1 parent 2a15e3a commit 09cc0ff

File tree

1 file changed

+224
-0
lines changed

1 file changed

+224
-0
lines changed
Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
using Newtonsoft.Json;
2+
using Notion.Client;
3+
using System;
4+
using System.Collections.Generic;
5+
using Xunit;
6+
7+
namespace Notion.UnitTests
8+
{
9+
public class SerializerSettingsSource : RestClient
10+
{
11+
public SerializerSettingsSource(ClientOptions options) : base(options)
12+
{
13+
14+
}
15+
16+
public JsonSerializerSettings GetSerializerSettings()
17+
{
18+
return defaultSerializerSettings;
19+
}
20+
21+
}
22+
public class FilterTests
23+
{
24+
private SerializerSettingsSource _settingsSource = new SerializerSettingsSource(new ClientOptions());
25+
26+
private string SerializeFilter(Filter filter)
27+
{
28+
return JsonConvert.SerializeObject(filter, _settingsSource.GetSerializerSettings());
29+
}
30+
31+
[Fact]
32+
public void CompoundFilterTest()
33+
{
34+
var selectFilter = new SelectFilter
35+
{
36+
Property = "A select",
37+
Select = new SelectFilterCondition
38+
{
39+
Equal = "Option"
40+
}
41+
};
42+
var relationFilter = new RelationFilter
43+
{
44+
Property = "Link",
45+
Relation = new RelationFilterCondition
46+
{
47+
Contains = "subtask#1"
48+
}
49+
};
50+
var dateFilter = new DateFilter
51+
{
52+
Property = "Due",
53+
Date = new DateFilterCondition
54+
{
55+
PastMonth = new Dictionary<string, object>()
56+
}
57+
};
58+
59+
var complexFiler = new CompoundFilter
60+
{
61+
And = new List<Filter> {
62+
dateFilter,
63+
new CompoundFilter {
64+
Or = new List<Filter> {relationFilter, selectFilter}
65+
}
66+
}
67+
};
68+
69+
Assert.Equal(
70+
"{\"and\":[{\"date\":{\"past_month\":{}},\"property\":\"Due\"},"
71+
+ "{\"or\":[{\"relation\":{\"contains\":\"subtask#1\"},\"property\":\"Link\"}," +
72+
"{\"select\":{\"equals\":\"Option\"},\"property\":\"A select\"}]}]}",
73+
SerializeFilter(complexFiler)
74+
);
75+
}
76+
77+
[Fact]
78+
public void CheckboxFilterTest()
79+
{
80+
var filter = new CheckboxFilter
81+
{
82+
Property = "Property name",
83+
Checkbox = new CheckboxFilterCondition
84+
{
85+
Equal = false
86+
},
87+
};
88+
89+
Assert.Equal(
90+
"{\"checkbox\":{\"equals\":false},\"property\":\"Property name\"}",
91+
SerializeFilter(filter)
92+
);
93+
}
94+
95+
[Fact]
96+
public void DateFilterTest()
97+
{
98+
var filter = new DateFilter
99+
{
100+
Property = "When",
101+
Date = new DateFilterCondition
102+
{
103+
OnOrAfter = new DateTime(2042, 11, 29)
104+
},
105+
};
106+
107+
Assert.Equal(
108+
"{\"date\":{\"on_or_after\":\"2042-11-29T00:00:00\"},\"property\":\"When\"}",
109+
SerializeFilter(filter)
110+
);
111+
}
112+
113+
[Fact]
114+
public void FilesFilterTest()
115+
{
116+
var filter = new FilesFilter
117+
{
118+
Property = "Attachments",
119+
Files = new FilesFilterCondition
120+
{
121+
IsNotEmpty = false
122+
},
123+
};
124+
125+
Assert.Equal(
126+
"{\"files\":{\"is_not_empty\":false},\"property\":\"Attachments\"}",
127+
SerializeFilter(filter)
128+
);
129+
}
130+
131+
[Fact]
132+
public void FormulaFilterTest()
133+
{
134+
var filter = new FormulaFilter
135+
{
136+
Property = "Some",
137+
Formula = new FormulaFilterCondition
138+
{
139+
Number = new NumberFilterCondition
140+
{
141+
IsEmpty = true
142+
}
143+
},
144+
};
145+
146+
Assert.Equal(
147+
"{\"formula\":{\"number\":{\"is_empty\":true}},\"property\":\"Some\"}",
148+
SerializeFilter(filter)
149+
);
150+
}
151+
152+
[Fact]
153+
public void MultiSelectFilterTest()
154+
{
155+
var filter = new MultiSelectFilter
156+
{
157+
Property = "category 1",
158+
MultiSelect = new MultiSelectFilterCondition
159+
{
160+
DoesNotContain = "tag"
161+
},
162+
};
163+
164+
Assert.Equal(
165+
"{\"multi_select\":{\"does_not_contain\":\"tag\"},\"property\":\"category 1\"}",
166+
SerializeFilter(filter)
167+
);
168+
}
169+
170+
[Fact(Skip = "Not sure if integer should be serialized as a number with decimals")]
171+
public void NumberFilterTest()
172+
{
173+
var filter = new NumberFilter
174+
{
175+
Property = "sum",
176+
Number = new NumberFilterCondition
177+
{
178+
GreaterThanOrEqualTo = -54
179+
},
180+
};
181+
182+
Assert.Equal(
183+
"{\"number\":{\"greater_than_or_equal_to\":-54.0},\"property\":\"sum\"}",
184+
SerializeFilter(filter)
185+
);
186+
}
187+
188+
[Fact]
189+
public void PeopleFilter()
190+
{
191+
var filter = new PeopleFilter
192+
{
193+
Property = "assignee PM",
194+
People = new PeopleFilterCondition
195+
{
196+
DoesNotContain = "some-uuid"
197+
},
198+
};
199+
200+
Assert.Equal(
201+
"{\"people\":{\"does_not_contain\":\"some-uuid\"},\"property\":\"assignee PM\"}",
202+
SerializeFilter(filter)
203+
);
204+
}
205+
206+
[Fact]
207+
public void TextFilterTest()
208+
{
209+
var filter = new TextFilter
210+
{
211+
Property = "Some property",
212+
Text = new TextFilterCondition
213+
{
214+
DoesNotContain = "Example text"
215+
},
216+
};
217+
218+
Assert.Equal(
219+
"{\"text\":{\"does_not_contain\":\"Example text\"},\"property\":\"Some property\"}",
220+
SerializeFilter(filter)
221+
);
222+
}
223+
}
224+
}

0 commit comments

Comments
 (0)