Skip to content

Commit 3bcf9d8

Browse files
committed
make test data more readable, tweak contributing instructions
1 parent 59643b9 commit 3bcf9d8

File tree

1 file changed

+70
-32
lines changed

1 file changed

+70
-32
lines changed

test/integration/data-connect.spec.ts

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ interface GetUserVariables {
8787
id: { id: string; };
8888
}
8989

90+
interface DeleteResponse {
91+
email_deleteMany: number
92+
user_deleteMany: number
93+
}
94+
9095
const connectorConfig: ConnectorConfig = {
9196
location: 'us-west2',
9297
serviceId: 'my-service',
@@ -97,14 +102,12 @@ const fredrickUser = { id: fredUser.id, address: '64 Elm St. North', name: 'Fred
97102

98103
const jeffUser = { id: 'jeff_id', address: '99 Oak St.', name: 'Jeff' }
99104

100-
const expectedUserIds = [fredUser.id, jeffUser.id]
101-
102105
const fredEmail = {
103106
id: 'email_id',
104107
subject: 'free bitcoin inside',
105108
date: '1999-12-31',
106109
text: 'get pranked! LOL!',
107-
fromId: fredUser.id
110+
from: { id: fredUser.id }
108111
}
109112

110113
describe('getDataConnect()', () => {
@@ -116,13 +119,41 @@ describe('getDataConnect()', () => {
116119
});
117120
});
118121

122+
afterEach(async () => {
123+
await cleanupDatabase();
124+
})
125+
126+
beforeEach(async () => {
127+
await initializeDatabase();
128+
});
129+
130+
/** initial state of database after calling initializeDatabase() */
131+
const initialState = { users: [fredUser, jeffUser], emails: [fredEmail] };
132+
133+
/** helper function which sets initial state of the database before each test */
134+
async function initializeDatabase(): Promise<void> {
135+
await getDataConnect(connectorConfig).executeGraphql<UserUpsertResponse, unknown>(
136+
upsertFredUser
137+
);
138+
await getDataConnect(connectorConfig).executeGraphql<UserUpsertResponse, unknown>(
139+
upsertJeffUser
140+
);
141+
await getDataConnect(connectorConfig).executeGraphql<EmailUpsertResponse, unknown>(
142+
upsertFredEmail
143+
);
144+
}
145+
146+
/** helper function which clears state of the database after each test */
147+
async function cleanupDatabase(): Promise<void> {
148+
await getDataConnect(connectorConfig).executeGraphql<DeleteResponse, unknown>(deleteAll);
149+
}
119150
/** @auth(level: PUBLIC) */
120151
const queryListUsers = 'query ListUsers @auth(level: PUBLIC) { users { id, name, address } }';
121152
/** @auth(level: NO_ACCESS) */
122153
const queryListEmails =
123-
'query ListEmails @auth(level: NO_ACCESS) { emails { id subject text date from { name } } }';
154+
'query ListEmails @auth(level: NO_ACCESS) { emails { id subject text date from { id } } }';
124155
/** no @auth specified - default permissions */
125-
const queryGetUserById = 'query GetUser($id: User_Key!) { user(key: $id) { id name } }';
156+
const queryGetUserById = 'query GetUser($id: User_Key!) { user(key: $id) { id name address } }';
126157

127158
/** @auth(level: USER) */
128159
const queryListUsersImpersonation = `
@@ -163,10 +194,16 @@ describe('getDataConnect()', () => {
163194
subject: "${fredEmail.subject}",
164195
date: "${fredEmail.date}",
165196
text: "${fredEmail.text}",
166-
fromId: "${fredEmail.fromId}"
197+
fromId: "${fredEmail.from.id}"
167198
})
168199
}`;
169200

201+
/** hardcoded delete all mutation, for cleanup */
202+
const deleteAll = `mutation delete {
203+
email_deleteMany(all: true)
204+
user_deleteMany(all: true)
205+
}`
206+
170207
describe('executeGraphql()', () => {
171208
it('executeGraphql() successfully executes a GraphQL mutation', async () => {
172209
const fredResponse = await getDataConnect(connectorConfig).executeGraphql<UserUpsertResponse, unknown>(
@@ -188,15 +225,19 @@ describe('getDataConnect()', () => {
188225
);
189226
//{ data: { email_upsert: { id: 'email_id' } } }
190227
expect(emailResponse.data.email_upsert.id).to.be.not.empty;
228+
229+
const deleteResponse = await getDataConnect(connectorConfig).executeGraphql<DeleteResponse, unknown>(deleteAll);
230+
expect(deleteResponse.data.email_deleteMany).to.be.greaterThan(0);
231+
expect(deleteResponse.data.user_deleteMany).to.be.greaterThan(0);
191232
});
192233

193234
it('executeGraphql() successfully executes a GraphQL query', async () => {
194235
const resp = await getDataConnect(connectorConfig)
195236
.executeGraphql<ListUsersResponse, undefined>(queryListUsers);
196237
expect(resp.data.users).to.be.not.empty;
197-
expect(resp.data.users.length).to.greaterThan(1);
238+
expect(resp.data.users.length).to.equal(initialState.users.length);
198239
resp.data.users.forEach((user) => {
199-
expect(expectedUserIds).to.include(user.id);
240+
expect(initialState.users).to.deep.include(user);
200241
});
201242
});
202243

@@ -205,10 +246,8 @@ describe('getDataConnect()', () => {
205246
multipleQueries,
206247
{ operationName: 'ListEmails' }
207248
);
208-
expect(resp.data.emails).to.be.not.empty;
209-
expect(resp.data.emails.length).equals(1);
210-
expect(resp.data.emails[0].id).to.be.not.undefined;
211-
expect(resp.data.emails[0].from?.name).to.equal(fredUser.name);
249+
expect(resp.data.emails).to.not.be.empty;
250+
expect(resp.data.emails).to.deep.equal(initialState.emails);
212251
});
213252

214253
it('executeGraphql() should throw for a query error when no variables are provided', async () => {
@@ -219,11 +258,9 @@ describe('getDataConnect()', () => {
219258
it('executeGraphql() successfully executes a GraphQL query with variables', async () => {
220259
const resp = await getDataConnect(connectorConfig).executeGraphql<GetUserResponse, GetUserVariables>(
221260
queryGetUserById,
222-
{ variables: { id: { id: fredUser.id } } }
261+
{ variables: { id: { id: initialState.users[0].id } } }
223262
);
224-
expect(resp.data.user.id).to.equal(fredUser.id);
225-
expect(resp.data.user.name).to.equal(fredUser.name);
226-
expect(resp.data.user.address).to.be.undefined;
263+
expect(resp.data.user).to.deep.equal(initialState.users[0]);
227264
});
228265
});
229266

@@ -232,9 +269,9 @@ describe('getDataConnect()', () => {
232269
const resp = await getDataConnect(connectorConfig)
233270
.executeGraphqlRead<ListUsersResponse, undefined>(queryListUsers);
234271
expect(resp.data.users).to.be.not.empty;
235-
expect(resp.data.users.length).to.be.greaterThan(1);
272+
expect(resp.data.users.length).to.equal(initialState.users.length);
236273
resp.data.users.forEach((user) => {
237-
expect(expectedUserIds).to.include(user.id);
274+
expect(initialState.users).to.deep.include(user);
238275
});
239276
});
240277

@@ -273,9 +310,7 @@ describe('getDataConnect()', () => {
273310
it('executeGraphqlRead() successfully executes an impersonated query with authenticated claims', async () => {
274311
const resp =
275312
await getDataConnect(connectorConfig).executeGraphqlRead<ListUsersResponse, undefined>(
276-
queryListUsersImpersonation,
277-
optsAuthorizedFredClaims
278-
);
313+
queryListUsersImpersonation, optsAuthorizedFredClaims);
279314
expect(resp.data.users).to.be.not.empty;
280315
expect(resp.data.users.length).equals(1);
281316
expect(resp.data.users[0]).to.deep.equal(fredUser);
@@ -290,16 +325,15 @@ describe('getDataConnect()', () => {
290325
});
291326

292327
it('executeGraphql() successfully executes an impersonated query with authenticated claims', async () => {
293-
const resp = await getDataConnect(connectorConfig).executeGraphqlRead<ListUsersResponse, undefined>(
328+
const resp = await getDataConnect(connectorConfig).executeGraphql<ListUsersResponse, undefined>(
294329
queryListUsersImpersonation, optsAuthorizedFredClaims);
295330
expect(resp.data.users).to.be.not.empty;
296331
expect(resp.data.users.length).equals(1);
297332
expect(resp.data.users[0]).to.deep.equal(fredUser);
298333
});
299334

300335
it('executeGraphql() should throw for impersonated query with unauthenticated claims', async () => {
301-
return getDataConnect(connectorConfig).executeGraphql(
302-
queryListUsersImpersonation, optsUnauthorizedClaims)
336+
return getDataConnect(connectorConfig).executeGraphql(queryListUsersImpersonation, optsUnauthorizedClaims)
303337
.should.eventually.be.rejected.and.has.property('code', 'data-connect/unauthenticated');
304338
});
305339

@@ -314,10 +348,14 @@ describe('getDataConnect()', () => {
314348

315349
it('executeGraphql() successfully executes an impersonated mutation with authenticated claims',
316350
async () => {
317-
const resp = await getDataConnect(connectorConfig).executeGraphql<UserUpdateResponse, undefined>(
351+
const updateResp = await getDataConnect(connectorConfig).executeGraphql<UserUpdateResponse, undefined>(
318352
updateFredrickUserImpersonated, optsAuthorizedFredClaims);
319353
// Fred -> Fredrick
320-
expect(resp.data.user_update.id).equals(fredUser.id);
354+
expect(updateResp.data.user_update.id).equals(fredUser.id);
355+
const queryResp = await getDataConnect(connectorConfig).executeGraphql<GetUserResponse, GetUserVariables>(
356+
queryGetUserById, { variables: { id: { id: fredUser.id } } });
357+
expect(queryResp.data.user).to.not.be.empty;
358+
expect(queryResp.data.user).to.deep.equal(fredrickUser);
321359
});
322360

323361
it('executeGraphql() should throw for impersonated mutation with unauthenticated claims', async () => {
@@ -339,19 +377,19 @@ describe('getDataConnect()', () => {
339377
const resp = await getDataConnect(connectorConfig).executeGraphql<ListUsersResponse, undefined>(
340378
queryListUsers, optsAuthorizedFredClaims);
341379
expect(resp.data.users).to.be.not.empty;
342-
expect(resp.data.users.length).to.be.greaterThan(1);
380+
expect(resp.data.users.length).to.equal(initialState.users.length);
343381
resp.data.users.forEach((user) => {
344-
expect(expectedUserIds).to.include(user.id);
382+
expect(initialState.users).to.deep.include(user);
345383
});
346384
});
347385

348386
it('executeGraphql() successfully executes an impersonated query with unauthenticated claims', async () => {
349387
const resp = await getDataConnect(connectorConfig).executeGraphql<ListUsersResponse, undefined>(
350388
queryListUsers, optsUnauthorizedClaims);
351389
expect(resp.data.users).to.be.not.empty;
352-
expect(resp.data.users.length).to.be.greaterThan(1);
390+
expect(resp.data.users.length).to.equal(initialState.users.length);
353391
resp.data.users.forEach((user) => {
354-
expect(expectedUserIds).to.include(user.id);
392+
expect(initialState.users).to.deep.include(user);
355393
});
356394
});
357395

@@ -360,9 +398,9 @@ describe('getDataConnect()', () => {
360398
const resp = await getDataConnect(connectorConfig).executeGraphql<ListUsersResponse, undefined>(
361399
queryListUsers, optsNonExistingClaims);
362400
expect(resp.data.users).to.be.not.empty;
363-
expect(resp.data.users.length).to.be.greaterThan(1);
401+
expect(resp.data.users.length).to.equal(initialState.users.length);
364402
resp.data.users.forEach((user) => {
365-
expect(expectedUserIds).to.include(user.id);
403+
expect(initialState.users).to.deep.include(user);
366404
});
367405
});
368406
});

0 commit comments

Comments
 (0)