-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: split huge main test into individual units
- Loading branch information
1 parent
0a697a5
commit fea85fb
Showing
24 changed files
with
1,465 additions
and
1,262 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* IMPORTANT | ||
* This snapshot file is auto-generated, but designed for humans. | ||
* It should be checked into source control and tracked carefully. | ||
* Re-generate by setting TAP_SNAPSHOT=1 and running tests. | ||
* Make sure to inspect the output below. Do not ignore changes! | ||
*/ | ||
'use strict' | ||
exports[`tests/nested-pagination.ts TAP nested pagination > double nested pagination 1`] = ` | ||
Object { | ||
"user": Object { | ||
"id": 1, | ||
"name": "Alice", | ||
"posts": Object { | ||
"cursor": "[\\"2\\"]", | ||
"nodes": Array [ | ||
Object { | ||
"author": Object { | ||
"name": "Alice", | ||
}, | ||
"id": 1, | ||
"section": Object { | ||
"name": "News", | ||
"posts": Object { | ||
"cursor": "[\\"4\\"]", | ||
"nodes": Array [ | ||
Object { | ||
"author": Object { | ||
"name": "Alice", | ||
}, | ||
"section": Object { | ||
"name": "News", | ||
}, | ||
"text": "Oil price rising.", | ||
}, | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
}, | ||
"section": Object { | ||
"name": "News", | ||
}, | ||
"text": "Good news from China.", | ||
}, | ||
], | ||
}, | ||
}, | ||
"text": "Oil price rising.", | ||
}, | ||
Object { | ||
"author": Object { | ||
"name": "Alice", | ||
}, | ||
"id": 2, | ||
"section": Object { | ||
"name": "Editorial", | ||
"posts": Object { | ||
"cursor": null, | ||
"nodes": Array [ | ||
Object { | ||
"author": Object { | ||
"name": "Alice", | ||
}, | ||
"section": Object { | ||
"name": "Editorial", | ||
}, | ||
"text": "Is communism dead yet?", | ||
}, | ||
], | ||
}, | ||
}, | ||
"text": "Is communism dead yet?", | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
` | ||
|
||
exports[`tests/nested-pagination.ts TAP nested pagination > nested pagination 1`] = ` | ||
Object { | ||
"user": Object { | ||
"name": "Alice", | ||
"posts": Object { | ||
"cursor": "[\\"2\\"]", | ||
"nodes": Array [ | ||
Object { | ||
"id": 1, | ||
"section": Object { | ||
"name": "News", | ||
}, | ||
"text": "Oil price rising.", | ||
}, | ||
Object { | ||
"id": 2, | ||
"section": Object { | ||
"name": "Editorial", | ||
}, | ||
"text": "Is communism dead yet?", | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
` | ||
|
||
exports[`tests/nested-pagination.ts TAP nested pagination > triple nested pagination 1`] = ` | ||
Object { | ||
"user": Object { | ||
"name": "Bob", | ||
"posts": Object { | ||
"cursor": "[\\"4\\"]", | ||
"nodes": Array [ | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
"posts": Object { | ||
"nodes": Array [ | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
}, | ||
"text": "Latest COVID figures.", | ||
}, | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
}, | ||
"text": "Good news from China.", | ||
}, | ||
], | ||
}, | ||
}, | ||
"text": "Latest COVID figures.", | ||
}, | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
"posts": Object { | ||
"nodes": Array [ | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
}, | ||
"text": "Latest COVID figures.", | ||
}, | ||
Object { | ||
"author": Object { | ||
"name": "Bob", | ||
}, | ||
"text": "Good news from China.", | ||
}, | ||
], | ||
}, | ||
}, | ||
"text": "Good news from China.", | ||
}, | ||
], | ||
}, | ||
}, | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import gql from "graphql-tag" | ||
import { Model, QueryBuilder, ref } from "objection" | ||
import { | ||
GraphResolver, | ||
ModelResolver, | ||
RelationResolver, | ||
} from "objection-graphql-resolver" | ||
import tap from "tap" | ||
|
||
import { Resolvers, setup } from "./setup" | ||
|
||
class UserModel extends Model { | ||
static tableName = "user" | ||
|
||
id?: number | ||
name?: string | ||
favorite_tag?: string | ||
} | ||
|
||
export class PostModel extends Model { | ||
static tableName = "post" | ||
|
||
static modifiers = { | ||
favorite_for_user(query: QueryBuilder<PostModel>, user_id: number) { | ||
query.runBefore(async function () { | ||
const { favorite_tag } = await UserModel.query( | ||
this.context().transaction | ||
) | ||
.findById(user_id) | ||
.throwIfNotFound() | ||
.castTo<PostModel & { favorite_tag: string }>() | ||
this.where("tag", favorite_tag) | ||
}) | ||
}, | ||
} | ||
|
||
id?: number | ||
text?: string | ||
tag?: string | ||
} | ||
|
||
const schema = gql` | ||
scalar Filter | ||
type Post { | ||
id: Int! | ||
text: String! | ||
} | ||
type Query { | ||
posts(filter: Filter): [Post!]! | ||
} | ||
` | ||
|
||
const resolve_graph = GraphResolver({ | ||
Post: ModelResolver(PostModel), | ||
}) | ||
|
||
const resolvers: Resolvers = { | ||
Query: { | ||
posts(_parent, _args, ctx, info) { | ||
return resolve_graph(ctx, info, PostModel.query(), { | ||
filter: true, | ||
}) | ||
}, | ||
}, | ||
} | ||
|
||
tap.test("filter with async modifier", async (tap) => { | ||
const { client, knex } = await setup(tap, { typeDefs: schema, resolvers }) | ||
|
||
await knex.schema.createTable("user", (user) => { | ||
user.increments("id").notNullable().primary() | ||
user.string("name").notNullable() | ||
user.string("favorite_tag").notNullable() | ||
}) | ||
|
||
await knex.schema.createTable("post", (post) => { | ||
post.increments("id").notNullable().primary() | ||
post.string("text").notNullable() | ||
post.string("tag").notNullable() | ||
}) | ||
|
||
await UserModel.query().insertGraph([ | ||
{ name: "Alice", favorite_tag: "politics" }, | ||
{ name: "Bob", favorite_tag: "celebrities" }, | ||
]) | ||
|
||
await PostModel.query().insertGraph([ | ||
{ text: "Oil price rising.", tag: "politics" }, | ||
{ text: "Is communism dead yet?", tag: "politics" }, | ||
{ text: "Elon Musk marries again.", tag: "celebrities" }, | ||
]) | ||
|
||
tap.strictSame( | ||
await client.request( | ||
gql` | ||
{ | ||
posts(filter: { favorite_for_user: 2 }) { | ||
text | ||
} | ||
} | ||
` | ||
), | ||
{ | ||
posts: [{ text: "Elon Musk marries again." }], | ||
} | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import gql from "graphql-tag" | ||
import { Model } from "objection" | ||
import { | ||
FieldResolver, | ||
GraphResolver, | ||
ModelResolver, | ||
} from "objection-graphql-resolver" | ||
import tap from "tap" | ||
|
||
import { Resolvers, setup } from "./setup" | ||
|
||
class UserModel extends Model { | ||
static tableName = "user" | ||
|
||
id?: number | ||
name?: string | ||
password?: string | ||
} | ||
|
||
const schema = gql` | ||
type User { | ||
id: Int! | ||
name: String! | ||
# empty if not allowed | ||
password: String | ||
} | ||
type Query { | ||
user(id: Int!): User | ||
} | ||
` | ||
|
||
const resolve_graph = GraphResolver({ | ||
User: ModelResolver(UserModel, { | ||
fields: { | ||
id: true, | ||
name: true, | ||
password: FieldResolver({ | ||
clean(password, user, context) { | ||
if (context.user_id && context.user_id === user.id) { | ||
return password | ||
} else { | ||
return undefined | ||
} | ||
}, | ||
}), | ||
}, | ||
}), | ||
}) | ||
|
||
const resolvers: Resolvers = { | ||
Query: { | ||
user(_parent, { id }, ctx, info) { | ||
return resolve_graph(ctx, info, UserModel.query().findById(id)) | ||
}, | ||
}, | ||
} | ||
|
||
tap.test("field cleaner", async (tap) => { | ||
const { client, knex } = await setup(tap, { typeDefs: schema, resolvers }) | ||
|
||
await knex.schema.createTable("user", function (table) { | ||
table.increments("id").notNullable().primary() | ||
table.string("name").notNullable() | ||
table.string("password").notNullable() | ||
}) | ||
|
||
await UserModel.query().insert({ name: "Alice", password: "secret" }) | ||
|
||
tap.strictSame( | ||
await client.request( | ||
gql` | ||
{ | ||
user(id: 1) { | ||
id | ||
name | ||
password | ||
} | ||
} | ||
` | ||
), | ||
{ | ||
user: { id: 1, name: "Alice", password: null }, | ||
}, | ||
"reject password to public" | ||
) | ||
|
||
tap.strictSame( | ||
await client.request( | ||
gql` | ||
{ | ||
user(id: 1) { | ||
id | ||
name | ||
password | ||
} | ||
} | ||
`, | ||
undefined, | ||
{ user_id: "2" } | ||
), | ||
{ | ||
user: { id: 1, name: "Alice", password: null }, | ||
}, | ||
"reject password to other users" | ||
) | ||
|
||
tap.strictSame( | ||
await client.request( | ||
gql` | ||
{ | ||
user(id: 1) { | ||
id | ||
name | ||
password | ||
} | ||
} | ||
`, | ||
undefined, | ||
{ user_id: "1" } | ||
), | ||
{ | ||
user: { id: 1, name: "Alice", password: "secret" }, | ||
}, | ||
"return own password to user" | ||
) | ||
}) |
Oops, something went wrong.