Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro committed Dec 4, 2024
1 parent ce37aba commit ff5e7de
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 59 deletions.
8 changes: 8 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/typegate/src/engine/planner/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,18 @@ export class Planner {
) {
throw this.unexpectedFieldError(node, name);
}
const fieldType = this.tg.type(fieldIdx);
const scope: Scope | undefined = (fieldType.type === Type.FUNCTION)
? {
runtime: this.tg
.runtimeReferences[
this.tg.materializer(fieldType.materializer).runtime
],
fnIdx: fieldIdx,
path: [],
}
: node.scope && { ...node.scope, path: [...node.scope.path, name] };
const fieldType = fieldIdx == null ? null : this.tg.type(fieldIdx);
const scope: Scope | undefined =
(fieldType && fieldType.type === Type.FUNCTION)
? {
runtime: this.tg
.runtimeReferences[
this.tg.materializer(fieldType.materializer).runtime
],
fnIdx: fieldIdx,
path: [],
}
: node.scope && { ...node.scope, path: [...node.scope.path, name] };

return {
parent: node,
Expand Down
69 changes: 61 additions & 8 deletions tests/injection/random_injection.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ def random_injection(g: Graph):
}
)

user_out = t.struct(
{
"id": t.uuid(),
"ean": t.ean(),
"name": t.string(),
"age": t.integer(),
"married": t.boolean(),
"birthday": t.datetime(),
"friends": t.list(t.string()),
"phone": t.string(),
"gender": t.string(),
"firstname": t.string(),
"lastname": t.string(),
"occupation": t.string(),
"street": t.string(),
"city": t.string(),
"postcode": t.string(),
"country": t.string(),
"uri": t.uri(),
"hostname": t.string(),
}
)

# test int, str, float enum
test_enum_str = t.struct(
{
Expand All @@ -45,24 +68,44 @@ def random_injection(g: Graph):
).from_random(),
}
)
test_enum_str_out = t.struct(
{
"educationLevel": t.string(),
}
)

test_enum_int = t.struct(
{
"bits": t.integer(enum=[0, 1]).from_random(),
}
)
test_enum_int_out = t.struct(
{
"bits": t.integer(),
}
)

test_enum_float = t.struct(
{
"cents": t.float(enum=[0.25, 0.5, 1.0]).from_random(),
}
)
test_enum_float_out = t.struct(
{
"cents": t.float(),
}
)

# test either
rubix_cube = t.struct({"name": t.string(), "size": t.integer()}, name="Rubix")
toygun = t.struct({"color": t.string()}, name="Toygun")
toy = t.either([rubix_cube, toygun], name="Toy").from_random()
toy = t.either([rubix_cube, toygun], name="Toy")
toy_struct = t.struct(
{
"toy": toy.from_random(),
}
)
toy_struct_out = t.struct(
{
"toy": toy,
}
Expand All @@ -76,21 +119,31 @@ def random_injection(g: Graph):
"field": t.union([rgb, vec], name="UnionStruct").from_random(),
}
)
union_struct_out = t.struct(
{
"field": t.union([rgb, vec]),
}
)

random_list = t.struct(
{
"names": t.list(t.string(config={"gen": "name"})).from_random(),
}
)
random_list_out = t.struct(
{
"names": t.list(t.string()),
},
)
# Configure random injection seed value or the default will be used
g.configure_random_injection(seed=1)
g.expose(
pub,
randomUser=deno.identity(user),
randomList=deno.identity(random_list),
testEnumStr=deno.identity(test_enum_str),
testEnumInt=deno.identity(test_enum_int),
testEnumFloat=deno.identity(test_enum_float),
testEither=deno.identity(toy_struct),
testUnion=deno.identity(union_struct),
randomUser=deno.func(user, user_out, code="(x) => x"),
randomList=deno.func(random_list, random_list_out, code="(x) => x"),
testEnumStr=deno.func(test_enum_str, test_enum_str_out, code="(x) => x"),
testEnumInt=deno.func(test_enum_int, test_enum_int_out, code="(x) => x"),
testEnumFloat=deno.func(test_enum_float, test_enum_float_out, code="(x) => x"),
testEither=deno.func(toy_struct, toy_struct_out, code="(x) => x"),
testUnion=deno.func(union_struct, union_struct_out, code="(x) => x"),
)
25 changes: 24 additions & 1 deletion tests/injection/random_injection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,28 @@ const user = t.struct({
hostname: t.string({ format: "hostname" }).fromRandom(),
});

const userOut = t.struct({
id: t.uuid(),
ean: t.ean(),
name: t.string(),
age: t.integer(),
married: t.boolean(),
email: t.string({ format: "email" }),
birthday: t.datetime(),
friends: t.list(t.string()),
phone: t.string(),
gender: t.string(),
firstname: t.string(),
lastname: t.string(),
occupation: t.string(),
street: t.string(),
city: t.string(),
postcode: t.string(),
country: t.string(),
uri: t.string({ format: "uri" }),
hostname: t.string({ format: "hostname" }),
});

export const tg = await typegraph("random_injection", (g: any) => {
const pub = Policy.public();
const deno = new DenoRuntime();
Expand All @@ -36,6 +58,7 @@ export const tg = await typegraph("random_injection", (g: any) => {
g.configureRandomInjection({ seed: 1 });

g.expose({
randomUser: deno.identity(user).withPolicy(pub),
// randomUser: deno.identity(user).withPolicy(pub),
randomUser: deno.func(user, userOut, { code: "x => x" }).withPolicy(pub),
});
});
1 change: 0 additions & 1 deletion tests/planner/__snapshots__/planner_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ snapshot[`planner 2`] = `
{
one: {
funcType: {
injections: {},
input: 2,
materializer: 0,
output: 3,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
provider = "postgresql"
4 changes: 0 additions & 4 deletions tests/query_parsers/__snapshots__/query_parsers_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ snapshot[`GraphQL parser 1`] = `
type: "object",
},
{
injections: {},
input: 3,
materializer: 1,
output: 5,
Expand Down Expand Up @@ -79,7 +78,6 @@ snapshot[`GraphQL parser 1`] = `
type: "string",
},
{
injections: {},
input: 5,
materializer: 2,
output: 5,
Expand All @@ -104,7 +102,6 @@ snapshot[`GraphQL parser 1`] = `
type: "object",
},
{
injections: {},
input: 3,
materializer: 3,
output: 10,
Expand Down Expand Up @@ -137,7 +134,6 @@ snapshot[`GraphQL parser 1`] = `
type: "string",
},
{
injections: {},
input: 10,
materializer: 4,
output: 10,
Expand Down
5 changes: 0 additions & 5 deletions tests/runtimes/graphql/__snapshots__/graphql_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ snapshot[`Typegraph generation with GraphQL runtime 1`] = `
],
"input": 2,
"output": 4,
"injections": {},
"runtimeConfig": null,
"materializer": 0,
"rate_weight": null,
Expand Down Expand Up @@ -75,7 +74,6 @@ snapshot[`Typegraph generation with GraphQL runtime 1`] = `
],
"input": 6,
"output": 7,
"injections": {},
"runtimeConfig": null,
"materializer": 2,
"rate_weight": null,
Expand Down Expand Up @@ -113,7 +111,6 @@ snapshot[`Typegraph generation with GraphQL runtime 1`] = `
],
"input": 10,
"output": 4,
"injections": {},
"runtimeConfig": null,
"materializer": 3,
"rate_weight": null,
Expand Down Expand Up @@ -149,7 +146,6 @@ snapshot[`Typegraph generation with GraphQL runtime 1`] = `
],
"input": 17,
"output": 20,
"injections": {},
"runtimeConfig": null,
"materializer": 4,
"rate_weight": null,
Expand Down Expand Up @@ -255,7 +251,6 @@ snapshot[`Typegraph generation with GraphQL runtime 1`] = `
],
"input": 22,
"output": 76,
"injections": {},
"runtimeConfig": null,
"materializer": 6,
"rate_weight": null,
Expand Down
2 changes: 0 additions & 2 deletions tests/runtimes/grpc/__snapshots__/grpc_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ snapshot[`Typegraph using grpc 1`] = `
],
"input": 2,
"output": 5,
"injections": {},
"runtimeConfig": null,
"materializer": 0,
"rate_weight": null,
Expand Down Expand Up @@ -165,7 +164,6 @@ snapshot[`Typegraph using grpc 2`] = `
],
"input": 2,
"output": 5,
"injections": {},
"runtimeConfig": null,
"materializer": 0,
"rate_weight": null,
Expand Down
10 changes: 0 additions & 10 deletions tests/runtimes/kv/__snapshots__/kv_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ snapshot[`Typegraph using kv 1`] = `
],
"input": 2,
"output": 4,
"injections": {},
"runtimeConfig": null,
"materializer": 0,
"rate_weight": null,
Expand Down Expand Up @@ -68,7 +67,6 @@ snapshot[`Typegraph using kv 1`] = `
],
"input": 6,
"output": 3,
"injections": {},
"runtimeConfig": null,
"materializer": 2,
"rate_weight": null,
Expand All @@ -93,7 +91,6 @@ snapshot[`Typegraph using kv 1`] = `
],
"input": 2,
"output": 8,
"injections": {},
"runtimeConfig": null,
"materializer": 3,
"rate_weight": null,
Expand All @@ -112,7 +109,6 @@ snapshot[`Typegraph using kv 1`] = `
],
"input": 10,
"output": 12,
"injections": {},
"runtimeConfig": null,
"materializer": 4,
"rate_weight": null,
Expand Down Expand Up @@ -149,7 +145,6 @@ snapshot[`Typegraph using kv 1`] = `
],
"input": 14,
"output": 16,
"injections": {},
"runtimeConfig": null,
"materializer": 5,
"rate_weight": null,
Expand Down Expand Up @@ -315,7 +310,6 @@ snapshot[`Typegraph using kv 2`] = `
],
"input": 2,
"output": 4,
"injections": {},
"runtimeConfig": null,
"materializer": 0,
"rate_weight": null,
Expand Down Expand Up @@ -351,7 +345,6 @@ snapshot[`Typegraph using kv 2`] = `
],
"input": 6,
"output": 3,
"injections": {},
"runtimeConfig": null,
"materializer": 2,
"rate_weight": null,
Expand All @@ -376,7 +369,6 @@ snapshot[`Typegraph using kv 2`] = `
],
"input": 2,
"output": 8,
"injections": {},
"runtimeConfig": null,
"materializer": 3,
"rate_weight": null,
Expand All @@ -395,7 +387,6 @@ snapshot[`Typegraph using kv 2`] = `
],
"input": 10,
"output": 12,
"injections": {},
"runtimeConfig": null,
"materializer": 4,
"rate_weight": null,
Expand Down Expand Up @@ -432,7 +423,6 @@ snapshot[`Typegraph using kv 2`] = `
],
"input": 14,
"output": 16,
"injections": {},
"runtimeConfig": null,
"materializer": 5,
"rate_weight": null,
Expand Down
Loading

0 comments on commit ff5e7de

Please sign in to comment.