forked from microsoft/cppgraphqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientTests.cpp
More file actions
326 lines (283 loc) · 14.4 KB
/
Copy pathClientTests.cpp
File metadata and controls
326 lines (283 loc) · 14.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <gtest/gtest.h>
#include <chrono>
#include <cstddef>
#include <future>
import GraphQL.Parse;
import GraphQL.Client;
import GraphQL.Service;
import GraphQL.Mutate.MutateClient;
import GraphQL.Query.QueryClient;
import GraphQL.Subscribe.SubscribeClient;
import GraphQL.Today.Mock;
using namespace graphql;
using namespace std::literals;
class ClientCase : public ::testing::Test
{
public:
void SetUp() override
{
_mockService = today::mock_service();
}
void TearDown() override
{
_mockService.reset();
}
protected:
std::shared_ptr<today::TodayMockService> _mockService;
};
TEST_F(ClientCase, QueryEverything)
{
using namespace query::client::query::Query;
auto query = GetRequestObject();
response::Value variables(response::Type::Map);
auto state = std::make_shared<today::RequestState>(1);
auto result = _mockService->service
->resolve({ query, {}, std::move(variables), std::launch::async, state })
.get();
EXPECT_EQ(std::size_t { 1 }, _mockService->getAppointmentsCount)
<< "today service lazy loads the appointments and caches the result";
EXPECT_EQ(std::size_t { 1 }, _mockService->getTasksCount)
<< "today service lazy loads the tasks and caches the result";
EXPECT_EQ(std::size_t { 1 }, _mockService->getUnreadCountsCount)
<< "today service lazy loads the unreadCounts and caches the result";
EXPECT_EQ(std::size_t { 1 }, state->appointmentsRequestId)
<< "today service passed the same RequestState";
EXPECT_EQ(std::size_t { 1 }, state->tasksRequestId)
<< "today service passed the same RequestState";
EXPECT_EQ(std::size_t { 1 }, state->unreadCountsRequestId)
<< "today service passed the same RequestState";
EXPECT_EQ(std::size_t { 1 }, state->loadAppointmentsCount)
<< "today service called the loader once";
EXPECT_EQ(std::size_t { 1 }, state->loadTasksCount) << "today service called the loader once";
EXPECT_EQ(std::size_t { 1 }, state->loadUnreadCountsCount)
<< "today service called the loader once";
try
{
ASSERT_TRUE(result.type() == response::Type::Map);
auto serviceResponse = client::parseServiceResponse(std::move(result));
const auto response = parseResponse(std::move(serviceResponse.data));
EXPECT_EQ(std::size_t { 0 }, serviceResponse.errors.size()) << "no errors expected";
ASSERT_TRUE(response.appointments.edges.has_value()) << "appointments should be set";
ASSERT_EQ(std::size_t { 1 }, response.appointments.edges->size())
<< "appointments should have 1 entry";
ASSERT_TRUE((*response.appointments.edges)[0].has_value()) << "edge should be set";
const auto& appointmentNode = (*response.appointments.edges)[0]->node;
ASSERT_TRUE(appointmentNode.has_value()) << "node should be set";
EXPECT_EQ(today::getFakeAppointmentId(), appointmentNode->id)
<< "id should match in base64 encoding";
ASSERT_TRUE(appointmentNode->subject.has_value()) << "subject should be set";
EXPECT_EQ("Lunch?", *(appointmentNode->subject)) << "subject should match";
ASSERT_TRUE(appointmentNode->when.has_value()) << "when should be set";
EXPECT_EQ("tomorrow", appointmentNode->when->get<std::string>()) << "when should match";
EXPECT_FALSE(appointmentNode->isNow) << "isNow should match";
EXPECT_EQ("Appointment", appointmentNode->_typename) << "__typename should match";
ASSERT_TRUE(response.tasks.edges.has_value()) << "tasks should be set";
ASSERT_EQ(std::size_t { 1 }, response.tasks.edges->size()) << "tasks should have 1 entry";
ASSERT_TRUE((*response.tasks.edges)[0].has_value()) << "edge should be set";
const auto& taskNode = (*response.tasks.edges)[0]->node;
ASSERT_TRUE(taskNode.has_value()) << "node should be set";
EXPECT_EQ(today::getFakeTaskId(), taskNode->id) << "id should match in base64 encoding";
ASSERT_TRUE(taskNode->title.has_value()) << "subject should be set";
EXPECT_EQ("Don't forget", *(taskNode->title)) << "title should match";
EXPECT_TRUE(taskNode->isComplete) << "isComplete should match";
EXPECT_EQ("Task", taskNode->_typename) << "__typename should match";
ASSERT_TRUE(response.unreadCounts.edges.has_value()) << "unreadCounts should be set";
ASSERT_EQ(std::size_t { 1 }, response.unreadCounts.edges->size())
<< "unreadCounts should have 1 entry";
ASSERT_TRUE((*response.unreadCounts.edges)[0].has_value()) << "edge should be set";
const auto& unreadCountNode = (*response.unreadCounts.edges)[0]->node;
ASSERT_TRUE(unreadCountNode.has_value()) << "node should be set";
EXPECT_EQ(today::getFakeFolderId(), unreadCountNode->id)
<< "id should match in base64 encoding";
ASSERT_TRUE(unreadCountNode->name.has_value()) << "name should be set";
EXPECT_EQ("\"Fake\" Inbox", *(unreadCountNode->name)) << "name should match";
EXPECT_EQ(3, unreadCountNode->unreadCount) << "unreadCount should match";
EXPECT_EQ("Folder", unreadCountNode->_typename) << "__typename should match";
EXPECT_EQ(query::client::query::Query::TaskState::Unassigned, response.testTaskState)
<< "testTaskState should match";
ASSERT_EQ(std::size_t { 1 }, response.anyType.size()) << "anyType should have 1 entry";
ASSERT_TRUE(response.anyType[0].has_value()) << "appointment should be set";
const auto& anyType = *response.anyType[0];
EXPECT_EQ("Appointment", anyType._typename) << "__typename should match";
EXPECT_EQ(today::getFakeAppointmentId(), anyType.id)
<< "id should match in base64 encoding";
EXPECT_FALSE(anyType.title.has_value()) << "appointment should not have a title";
EXPECT_FALSE(anyType.isComplete) << "appointment should not set isComplete";
ASSERT_TRUE(anyType.subject.has_value()) << "subject should be set";
EXPECT_EQ("Lunch?", *(anyType.subject)) << "subject should match";
ASSERT_TRUE(anyType.when.has_value()) << "when should be set";
EXPECT_EQ("tomorrow", anyType.when->get<std::string>()) << "when should match";
EXPECT_FALSE(anyType.isNow) << "isNow should match";
}
catch (const std::logic_error& ex)
{
FAIL() << ex.what();
}
}
TEST_F(ClientCase, QueryEverythingWithVisitor)
{
using namespace query::client::query::Query;
auto query = GetRequestObject();
response::Value variables(response::Type::Map);
auto state = std::make_shared<today::RequestState>(1);
auto result =
_mockService->service->visit({ query, {}, std::move(variables), std::launch::async, state })
.get();
EXPECT_EQ(std::size_t { 1 }, _mockService->getAppointmentsCount)
<< "today service lazy loads the appointments and caches the result";
EXPECT_EQ(std::size_t { 1 }, _mockService->getTasksCount)
<< "today service lazy loads the tasks and caches the result";
EXPECT_EQ(std::size_t { 1 }, _mockService->getUnreadCountsCount)
<< "today service lazy loads the unreadCounts and caches the result";
EXPECT_EQ(std::size_t { 1 }, state->appointmentsRequestId)
<< "today service passed the same RequestState";
EXPECT_EQ(std::size_t { 1 }, state->tasksRequestId)
<< "today service passed the same RequestState";
EXPECT_EQ(std::size_t { 1 }, state->unreadCountsRequestId)
<< "today service passed the same RequestState";
EXPECT_EQ(std::size_t { 1 }, state->loadAppointmentsCount)
<< "today service called the loader once";
EXPECT_EQ(std::size_t { 1 }, state->loadTasksCount) << "today service called the loader once";
EXPECT_EQ(std::size_t { 1 }, state->loadUnreadCountsCount)
<< "today service called the loader once";
try
{
auto visitor = std::make_shared<ResponseVisitor>();
auto responseVisitor = std::make_shared<response::ValueVisitor>(visitor);
std::move(result.data).visit(responseVisitor);
const auto response = visitor->response();
EXPECT_EQ(std::size_t { 0 }, result.errors.size()) << "no errors expected";
ASSERT_TRUE(response.appointments.edges.has_value()) << "appointments should be set";
ASSERT_EQ(std::size_t { 1 }, response.appointments.edges->size())
<< "appointments should have 1 entry";
ASSERT_TRUE((*response.appointments.edges)[0].has_value()) << "edge should be set";
const auto& appointmentNode = (*response.appointments.edges)[0]->node;
ASSERT_TRUE(appointmentNode.has_value()) << "node should be set";
EXPECT_EQ(today::getFakeAppointmentId(), appointmentNode->id)
<< "id should match in base64 encoding";
ASSERT_TRUE(appointmentNode->subject.has_value()) << "subject should be set";
EXPECT_EQ("Lunch?", *(appointmentNode->subject)) << "subject should match";
ASSERT_TRUE(appointmentNode->when.has_value()) << "when should be set";
EXPECT_EQ("tomorrow", appointmentNode->when->get<std::string>()) << "when should match";
EXPECT_FALSE(appointmentNode->isNow) << "isNow should match";
EXPECT_EQ("Appointment", appointmentNode->_typename) << "__typename should match";
ASSERT_TRUE(response.tasks.edges.has_value()) << "tasks should be set";
ASSERT_EQ(std::size_t { 1 }, response.tasks.edges->size()) << "tasks should have 1 entry";
ASSERT_TRUE((*response.tasks.edges)[0].has_value()) << "edge should be set";
const auto& taskNode = (*response.tasks.edges)[0]->node;
ASSERT_TRUE(taskNode.has_value()) << "node should be set";
EXPECT_EQ(today::getFakeTaskId(), taskNode->id) << "id should match in base64 encoding";
ASSERT_TRUE(taskNode->title.has_value()) << "subject should be set";
EXPECT_EQ("Don't forget", *(taskNode->title)) << "title should match";
EXPECT_TRUE(taskNode->isComplete) << "isComplete should match";
EXPECT_EQ("Task", taskNode->_typename) << "__typename should match";
ASSERT_TRUE(response.unreadCounts.edges.has_value()) << "unreadCounts should be set";
ASSERT_EQ(std::size_t { 1 }, response.unreadCounts.edges->size())
<< "unreadCounts should have 1 entry";
ASSERT_TRUE((*response.unreadCounts.edges)[0].has_value()) << "edge should be set";
const auto& unreadCountNode = (*response.unreadCounts.edges)[0]->node;
ASSERT_TRUE(unreadCountNode.has_value()) << "node should be set";
EXPECT_EQ(today::getFakeFolderId(), unreadCountNode->id)
<< "id should match in base64 encoding";
ASSERT_TRUE(unreadCountNode->name.has_value()) << "name should be set";
EXPECT_EQ("\"Fake\" Inbox", *(unreadCountNode->name)) << "name should match";
EXPECT_EQ(3, unreadCountNode->unreadCount) << "unreadCount should match";
EXPECT_EQ("Folder", unreadCountNode->_typename) << "__typename should match";
EXPECT_EQ(query::client::query::Query::TaskState::Unassigned, response.testTaskState)
<< "testTaskState should match";
ASSERT_EQ(std::size_t { 1 }, response.anyType.size()) << "anyType should have 1 entry";
ASSERT_TRUE(response.anyType[0].has_value()) << "appointment should be set";
const auto& anyType = *response.anyType[0];
EXPECT_EQ("Appointment", anyType._typename) << "__typename should match";
EXPECT_EQ(today::getFakeAppointmentId(), anyType.id)
<< "id should match in base64 encoding";
EXPECT_FALSE(anyType.title.has_value()) << "appointment should not have a title";
EXPECT_FALSE(anyType.isComplete) << "appointment should not set isComplete";
ASSERT_TRUE(anyType.subject.has_value()) << "subject should be set";
EXPECT_EQ("Lunch?", *(anyType.subject)) << "subject should match";
ASSERT_TRUE(anyType.when.has_value()) << "when should be set";
EXPECT_EQ("tomorrow", anyType.when->get<std::string>()) << "when should match";
EXPECT_FALSE(anyType.isNow) << "isNow should match";
}
catch (const std::logic_error& ex)
{
FAIL() << ex.what();
}
}
TEST_F(ClientCase, MutateCompleteTask)
{
using namespace mutate::client::mutation::CompleteTaskMutation;
auto query = GetRequestObject();
auto variables = serializeVariables(
{ std::make_unique<CompleteTaskInput>(CompleteTaskInput { today::getFakeTaskId(),
std::nullopt,
std::make_optional(true),
std::make_optional("Hi There!"s) }) });
auto state = std::make_shared<today::RequestState>(5);
auto result =
_mockService->service->resolve({ query, {}, std::move(variables), {}, state }).get();
try
{
ASSERT_TRUE(result.type() == response::Type::Map);
auto serviceResponse = client::parseServiceResponse(std::move(result));
const auto response = parseResponse(std::move(serviceResponse.data));
EXPECT_EQ(std::size_t { 0 }, serviceResponse.errors.size()) << "no errors expected";
const auto& completedTask = response.completedTask;
const auto& task = completedTask.completedTask;
ASSERT_TRUE(task.has_value()) << "should get back a task";
EXPECT_EQ(today::getFakeTaskId(), task->completedTaskId)
<< "id should match in base64 encoding";
ASSERT_TRUE(task->title.has_value()) << "title should be set";
EXPECT_EQ("Mutated Task!", *(task->title)) << "title should match";
EXPECT_TRUE(task->isComplete) << "isComplete should match";
const auto& clientMutationId = completedTask.clientMutationId;
ASSERT_TRUE(clientMutationId.has_value()) << "clientMutationId should be set";
EXPECT_EQ("Hi There!", *clientMutationId) << "clientMutationId should match";
}
catch (const std::logic_error& ex)
{
FAIL() << ex.what();
}
}
TEST_F(ClientCase, SubscribeNextAppointmentChangeDefault)
{
using namespace subscribe::client::subscription::TestSubscription;
auto query = GetRequestObject();
response::Value variables(response::Type::Map);
auto state = std::make_shared<today::RequestState>(6);
response::Value result;
auto key = _mockService->service
->subscribe({ [&result](response::Value&& response) {
result = std::move(response);
},
std::move(query),
"TestSubscription"s,
std::move(variables),
{},
state })
.get();
_mockService->service->deliver({ "nextAppointmentChange"sv }).get();
_mockService->service->unsubscribe({ key }).get();
try
{
ASSERT_TRUE(result.type() == response::Type::Map);
auto serviceResponse = client::parseServiceResponse(std::move(result));
const auto response = parseResponse(std::move(serviceResponse.data));
EXPECT_EQ(std::size_t { 0 }, serviceResponse.errors.size()) << "no errors expected";
const auto& appointmentNode = response.nextAppointment;
ASSERT_TRUE(appointmentNode.has_value()) << "should get back a task";
EXPECT_EQ(today::getFakeAppointmentId(), appointmentNode->nextAppointmentId)
<< "id should match in base64 encoding";
ASSERT_TRUE(appointmentNode->subject.has_value()) << "subject should be set";
EXPECT_EQ("Lunch?", *(appointmentNode->subject)) << "subject should match";
ASSERT_TRUE(appointmentNode->when.has_value()) << "when should be set";
EXPECT_EQ("tomorrow", appointmentNode->when->get<std::string>()) << "when should match";
EXPECT_TRUE(appointmentNode->isNow) << "isNow should match";
}
catch (const std::logic_error& ex)
{
FAIL() << ex.what();
}
}