Skip to content

Commit

Permalink
Update last of the API tests (#5729)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed May 17, 2021
1 parent 737b3e6 commit 44b8c1d
Show file tree
Hide file tree
Showing 13 changed files with 480 additions and 770 deletions.
1 change: 0 additions & 1 deletion docs/pages/apis/fields.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ Options:
- `graphql`: Configures certain aspects of the GraphQL API.
- `cacheHint` (default: `undefined`): Allows you to specific the [dynamic cache control hints](https://www.apollographql.com/docs/apollo-server/performance/caching/#in-your-resolvers-dynamic) used for queries to this this list.


```typescript
export default config({
lists: createSchema({
Expand Down
1 change: 0 additions & 1 deletion examples/NEW-EXAMPLES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ There are three types of example project:
- Solution Examples: Each solution example is a complete project which demonstrates a complete solution, such as the e-commerce project.
These projects bring together a collection of features in a cohesive way.


### Feature Example steps

1. Decide which base project to use. This is up to you, and will depend on what feature you're trying to demonstrate and where it makes most contextual sense to add it.
Expand Down
20 changes: 6 additions & 14 deletions tests/api-tests/fields/unique.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,13 @@ multiAdapterRunners().map(({ runner, provider, after }) =>
test(
'Configuring uniqueness on one field does not affect others',
keystoneTestWrapper(async ({ context }) => {
const data = await context.graphql.run({
query: `
mutation($fooData: TestCreateInput, $barData: TestCreateInput) {
foo: createTest(data: $fooData) { id }
bar: createTest(data: $barData) { id }
}
`,
variables: {
fooData: { testField: mod.exampleValue(matrixValue), name: 'jess' },
barData: { testField: mod.exampleValue2(matrixValue), name: 'jess' },
},
const items = await context.lists.Test.createMany({
data: [
{ data: { testField: mod.exampleValue(matrixValue), name: 'jess' } },
{ data: { testField: mod.exampleValue2(matrixValue), name: 'jess' } },
],
});

expect(data).toHaveProperty('foo.id');
expect(data).toHaveProperty('bar.id');
expect(items).toHaveLength(2);
})
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ type IdType = any;
const alphanumGenerator = gen.alphaNumString.notEmpty();

const createInitialData = async (context: KeystoneContext) => {
type T = { createUsers: { id: IdType }[] };
const data = (await context.graphql.run({
query: `
mutation {
createUsers(data: [
{ data: { name: "${sampleOne(alphanumGenerator)}" } },
{ data: { name: "${sampleOne(alphanumGenerator)}" } },
{ data: { name: "${sampleOne(alphanumGenerator)}" } }
]) { id }
}`,
})) as T;
return { users: data.createUsers };
const users = await context.lists.User.createMany({
data: [
{ data: { name: sampleOne(alphanumGenerator) } },
{ data: { name: sampleOne(alphanumGenerator) } },
{ data: { name: sampleOne(alphanumGenerator) } },
],
});

return { users };
};

const createUserAndFriend = async (context: KeystoneContext) => {
Expand Down Expand Up @@ -53,13 +50,11 @@ const getUserAndFriend = async (context: KeystoneContext, userId: IdType, friend

const createReadData = async (context: KeystoneContext) => {
// create locations [A, A, B, B, C, C];
const data = await context.graphql.run({
query: `mutation create($users: [UsersCreateInput]) { createUsers(data: $users) { id name } }`,
variables: {
users: ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E'].map(name => ({ data: { name } })),
},
const users = await context.lists.User.createMany({
data: ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E'].map(name => ({ data: { name } })),
query: 'id name',
});
const { createUsers } = data;

await Promise.all(
[
[0, 1, 2, 3, 4, 5], // -> (A1) -> [A, A, B, B, C, C]
Expand All @@ -72,9 +67,9 @@ const createReadData = async (context: KeystoneContext) => {
[2], // -> (D2) -> [B]
[], // -> (E1) -> []
].map(async (locationIdxs, j) => {
const ids = locationIdxs.map(i => ({ id: createUsers[i].id }));
const ids = locationIdxs.map(i => ({ id: users[i].id }));
await context.lists.User.updateOne({
id: createUsers[j].id,
id: users[j].id,
data: { friends: { connect: ids } },
query: 'id friends { name }',
});
Expand Down Expand Up @@ -333,10 +328,8 @@ multiAdapterRunners().map(({ runner, provider }) =>
const { user, friend } = await createUserAndFriend(context);

// Run the query to disconnect the location from company
const data = await context.graphql.run({
query: `mutation { deleteUser(id: "${user.id}") { id } } `,
});
expect(data.deleteUser.id).toBe(user.id);
const _user = await context.lists.User.deleteOne({ id: user.id });
expect(_user?.id).toBe(user.id);

// Check the link has been broken
const result = await getUserAndFriend(context, user.id, friend.id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ type IdType = any;
const alphanumGenerator = gen.alphaNumString.notEmpty();

const createInitialData = async (context: KeystoneContext) => {
type T = { createUsers: { id: IdType }[] };
const data = (await context.graphql.run({
query: `
mutation {
createUsers(data: [
{ data: { name: "${sampleOne(alphanumGenerator)}" } },
{ data: { name: "${sampleOne(alphanumGenerator)}" } },
{ data: { name: "${sampleOne(alphanumGenerator)}" } }
]) { id }
}`,
})) as T;
return { users: data.createUsers };
const users = await context.lists.User.createMany({
data: [
{ data: { name: sampleOne(alphanumGenerator) } },
{ data: { name: sampleOne(alphanumGenerator) } },
{ data: { name: sampleOne(alphanumGenerator) } },
],
});

return { users };
};

const createUserAndFriend = async (context: KeystoneContext) => {
Expand Down Expand Up @@ -312,10 +309,8 @@ multiAdapterRunners().map(({ runner, provider }) =>
const { friend, user } = await createUserAndFriend(context);

// Run the query to disconnect the location from company
const data = await context.graphql.run({
query: `mutation { deleteUser(id: "${user.id}") { id } } `,
});
expect(data.deleteUser.id).toBe(user.id);
const _user = await context.lists.User.deleteOne({ id: user.id });
expect(_user?.id).toBe(user.id);

// Check the link has been broken
const result = await getUserAndFriend(context, user.id, friend.id);
Expand All @@ -332,10 +327,8 @@ multiAdapterRunners().map(({ runner, provider }) =>

// Delete company {name}
const id = users.find(company => company.name === name)?.id;
const data = await context.graphql.run({
query: `mutation { deleteUser(id: "${id}") { id } }`,
});
expect(data.deleteUser.id).toBe(id);
const _user = await context.lists.User.deleteOne({ id });
expect(_user?.id).toBe(id);

// Check all the companies look how we expect
await (async () => {
Expand Down Expand Up @@ -391,10 +384,8 @@ multiAdapterRunners().map(({ runner, provider }) =>

// Delete friend {name}
const id = users.find(user => user.name === name)?.id;
const data = await context.graphql.run({
query: `mutation { deleteUser(id: "${id}") { id } }`,
});
expect(data.deleteUser.id).toBe(id);
const _user = await context.lists.User.deleteOne({ id });
expect(_user?.id).toBe(id);

// Check all the companies look how we expect
await (async () => {
Expand Down
38 changes: 15 additions & 23 deletions tests/api-tests/relationships/crud-self-ref/one-to-many.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@ type IdType = any;
const alphanumGenerator = gen.alphaNumString.notEmpty();

const createInitialData = async (context: KeystoneContext) => {
type T = { createUsers: { id: IdType }[] };
const data = (await context.graphql.run({
query: `
mutation {
createUsers(data: [
{ data: { name: "${sampleOne(alphanumGenerator)}" } },
{ data: { name: "${sampleOne(alphanumGenerator)}" } },
{ data: { name: "${sampleOne(alphanumGenerator)}" } }
]) { id }
}`,
})) as T;
return { users: data.createUsers };
const users = await context.lists.User.createMany({
data: [
{ data: { name: sampleOne(alphanumGenerator) } },
{ data: { name: sampleOne(alphanumGenerator) } },
{ data: { name: sampleOne(alphanumGenerator) } },
],
});

return { users };
};

const createUserAndFriend = async (context: KeystoneContext) => {
Expand Down Expand Up @@ -60,21 +57,18 @@ const getUserAndFriend = async (context: KeystoneContext, userId: IdType, friend

const createReadData = async (context: KeystoneContext) => {
// create locations [A, A, B, B, C, C];
const data = await context.graphql.run({
query: `mutation create($users: [UsersCreateInput]) { createUsers(data: $users) { id name } }`,
variables: {
users: ['A', 'A', 'B', 'B', 'C', 'C', 'D'].map(name => ({ data: { name } })),
},
const users = await context.lists.User.createMany({
data: ['A', 'A', 'B', 'B', 'C', 'C', 'D'].map(name => ({ data: { name } })),
query: 'id name',
});
const { createUsers } = data;
await Promise.all(
Object.entries({
ABC: [0, 2, 4], // -> [A, B, C]
AB: [1, 3], // -> [A, B]
C: [5], // -> [C]
'': [], // -> []
}).map(async ([name, locationIdxs]) => {
const ids = locationIdxs.map((i: number) => ({ id: createUsers[i].id }));
const ids = locationIdxs.map((i: number) => ({ id: users[i].id }));
await context.lists.User.createOne({
data: { name, friends: { connect: ids } },
query: 'id friends { id friendOf { id } }',
Expand Down Expand Up @@ -455,10 +449,8 @@ multiAdapterRunners().map(({ runner, provider }) =>
const { user, friend } = await createUserAndFriend(context);

// Run the query to disconnect the location from company
const data = await context.graphql.run({
query: `mutation { deleteUser(id: "${user.id}") { id } } `,
});
expect(data.deleteUser.id).toBe(user.id);
const _user = await context.lists.User.deleteOne({ id: user.id });
expect(_user?.id).toBe(user.id);

// Check the link has been broken
const result = await getUserAndFriend(context, user.id, friend.id);
Expand Down
Loading

1 comment on commit 44b8c1d

@vercel
Copy link

@vercel vercel bot commented on 44b8c1d May 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.