From bb028c507bf962452470e289b34fe408f3bdf9f7 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Tue, 1 Oct 2024 12:15:17 +0530 Subject: [PATCH] update tests --- src/core/jit/fixtures/dedupe.graphql | 35 ++++++++++++ src/core/jit/model.rs | 46 +++++++--------- .../fixtures/configs/dedupe.graphql | 54 ------------------- 3 files changed, 53 insertions(+), 82 deletions(-) create mode 100644 src/core/jit/fixtures/dedupe.graphql delete mode 100644 tailcall-fixtures/fixtures/configs/dedupe.graphql diff --git a/src/core/jit/fixtures/dedupe.graphql b/src/core/jit/fixtures/dedupe.graphql new file mode 100644 index 0000000000..7b06a3773a --- /dev/null +++ b/src/core/jit/fixtures/dedupe.graphql @@ -0,0 +1,35 @@ +schema + @server(port: 8000, hostname: "0.0.0.0") + @upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: 42) { + query: Query +} + +type Query { + posts: [Post] @http(path: "/posts") + users: [User] @http(path: "/users", dedupe: true) +} + +type User { + id: ID! + name: String! + username: String! + email: String! + phone: String + comments: [Comment] @http(path: "/users/{{.value.id}}/comments", dedupe: true) +} + +type Post { + id: ID! + userId: ID! + title: String! + body: String! + user: User @http(path: "/users/{{.value.userId}}", dedupe: true) +} + +type Comment { + id: ID! + name: String! + email: String! + body: String! + postId: ID! +} diff --git a/src/core/jit/model.rs b/src/core/jit/model.rs index 6b49266127..3842d683d5 100644 --- a/src/core/jit/model.rs +++ b/src/core/jit/model.rs @@ -695,12 +695,22 @@ mod test { use async_graphql::Request; use async_graphql_value::ConstValue; - use super::Directive; + use super::{Directive, OperationPlan}; use crate::core::blueprint::Blueprint; use crate::core::config::ConfigModule; use crate::core::jit; use crate::include_config; + fn plan(query: &str) -> OperationPlan { + let config = include_config!("./fixtures/dedupe.graphql").unwrap(); + let module = ConfigModule::from(config); + let bp = Blueprint::try_from(&module).unwrap(); + + let request = Request::new(query); + let jit_request = jit::Request::from(request); + jit_request.create_plan(&bp).unwrap() + } + #[test] fn test_from_custom_directive() { let custom_directive = Directive { @@ -711,45 +721,25 @@ mod test { let async_directive: ConstDirective = (&custom_directive).into(); insta::assert_debug_snapshot!(async_directive); } + #[test] fn test_operation_plan_dedupe() { - let config = - include_config!("../../../tailcall-fixtures/fixtures/configs/dedupe.graphql").unwrap(); - let module = ConfigModule::from(config); - let bp = Blueprint::try_from(&module).unwrap(); + let actual = plan(r#"{ posts { id } }"#); - let request = Request::new(r#"{ posts { id } }"#); - let jit_request = jit::Request::from(request); - let plan = jit_request.create_plan(&bp).unwrap(); - - assert!(plan.dedupe); + assert!(!actual.dedupe); } #[test] fn test_operation_plan_dedupe_nested() { - let config = - include_config!("../../../tailcall-fixtures/fixtures/configs/dedupe.graphql").unwrap(); - let module = ConfigModule::from(config); - let bp = Blueprint::try_from(&module).unwrap(); - - let request = Request::new(r#"{ posts { id users { id } } }"#); - let jit_request = jit::Request::from(request); - let plan = jit_request.create_plan(&bp).unwrap(); + let actual = plan(r#"{ posts { id users { id } } }"#); - assert!(!plan.dedupe); + assert!(!actual.dedupe); } #[test] fn test_operation_plan_dedupe_false() { - let config = - include_config!("../../../tailcall-fixtures/fixtures/configs/dedupe.graphql").unwrap(); - let module = ConfigModule::from(config); - let bp = Blueprint::try_from(&module).unwrap(); - - let request = Request::new(r#"{ users { id } }"#); - let jit_request = jit::Request::from(request); - let plan = jit_request.create_plan(&bp).unwrap(); + let actual = plan(r#"{ users { id comments {body} } }"#); - assert!(!plan.dedupe); + assert!(actual.dedupe); } } diff --git a/tailcall-fixtures/fixtures/configs/dedupe.graphql b/tailcall-fixtures/fixtures/configs/dedupe.graphql deleted file mode 100644 index f657d4048b..0000000000 --- a/tailcall-fixtures/fixtures/configs/dedupe.graphql +++ /dev/null @@ -1,54 +0,0 @@ -schema - @server(port: 8000, hostname: "0.0.0.0") - @upstream(baseURL: "http://jsonplaceholder.typicode.com", httpCache: 42) { - query: Query -} - -type Query { - posts: [Post] @http(path: "/posts?_limit=11", dedupe: true) - users: [User] @http(path: "/users") - user(id: Int!): User @http(path: "/users/{{.args.id}}") - post(id: Int! = 1): Post @http(path: "/posts/{{.args.id}}") -} - -type User { - id: Int! - name: String! - username: String! - email: String! - phone: String - blog: String @expr(body: "https://test.blog/users/website/{{.value.username}}") - albums: [Album] @http(path: "/users/{{.value.id}}/albums?_limit=2") -} - -type Post { - id: Int! - userId: Int! - title: String! - body: String! - user: User @http(path: "/users/{{.value.userId}}") - users: [User] @http(path: "/users") - comments: [Comment] @http(path: "/posts/{{.value.id}}/comments") -} - -type Comment { - id: Int! - name: String! - email: String! - body: String! - title: String! @expr(body: "{{.value.email}}: {{.value.name}}") -} - -type Photo { - albumId: Int! - id: Int! - title: String! - combinedId: String! @expr(body: "Album: {{.value.albumId}}, photo: {{.value.id}}") -} - -type Album { - userId: Int! - id: Int! - title: Int - photos: [Photo] @http(path: "/albums/{{.value.id}}/photos?_limit=3") -}